|
Tài trợ cho PIC Vietnam |
Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC |
|
Ðiều Chỉnh | Xếp Bài |
27-03-2013, 01:09 PM | #1 |
Đệ tử 1 túi
Tham gia ngày: Jan 2013
Bài gửi: 21
: |
pic 18
các anh ơi giúp em bài này với ạ.
em cảm ơn nhiều #include"p18f26k20.h" #include"i2c.h" #include"xlcd.h" #include"delays.h" //#pragma config OSC = HS, FCMEN=OFF, IESO=OFF #pragma config PBADEN = OFF //#pragma config WDT = OFF,WDTPS=512 #pragma config LVP = OFF #pragma config PWRT = OFF , BOREN=OFF #pragma config MCLRE = ON,CCP2MX = PORTC #define SLAVE_ADD 0X68 #define control_LCD TRISB #define WRITE 0 #define READ 1 char time[7]; char c[14]; int i=0; /************************************************** ************/ //prototype void clock_write(void); void clock_read(void); void LCD_IN_INTER(unsigned int d); void Display(char b[] ); void DelayFor18TCY(void); void DelayPORXLCD(void); void DelayXLCD(void); void port_init(void); void main(void) { char ch1[]= "chu khanh"; OpenI2C(MASTER, SLEW_OFF); OpenXLCD(FOUR_BIT & LINES_5X7); WriteCmdXLCD(BLINK_OFF & CURSOR_OFF);//no blink cursor and no display cursor port_init(); clock_read(); Delay1KTCYx(10); while(1) { while(!BusyXLCD){}; putsXLCD(ch1); clock_read(); Display(time); Delay1KTCYx(10); }; } /************************************************** ************/ void DelayFor18TCY(void) { Nop();Nop();Nop();Nop(); Nop();Nop();Nop();Nop(); Nop();Nop();Nop();Nop(); } void DelayPORXLCD(void) { Delay10KTCYx(6); // timedelay =15ms //cycles = (timedelay * 4MHz)/4 //cycles =60000 return; } void DelayXLCD(void) { Delay10KTCYx(1); // timedelay =5ms //cycles = (timedelay * 4MHz)/4 //cycles =10000 return; } void port_init(void) { ANSEL = 0x00; ANSELH = 0x00; control_LCD=0; } void clock_write(void)//write { unsigned int sec, min, hour, day, date, mouth, year; sec = 0x00; min = 0x00; hour = 0x15; day = 0x02; date = 0x03; mouth = 0x05; year = 0x03; StartI2C(); IdleI2C(); //wait WriteI2C(0xD0);//WriteI2C(0xD0);//gui 7 bit dia chi cua slave cung voi 1 bit ghi du lieu AckI2C(); IdleI2C(); //wait WriteI2C(0x00);//resister first AckI2C(); WriteI2C(sec); WriteI2C(min);//xuat noi dung cua thang ghi chua giay0x00 WriteI2C(hour);//xuat noi dung cua thanh ghi phut 0x01 WriteI2C(day);//gio 0x02 WriteI2C(date);//thu 0x03 WriteI2C(mouth);//ngay 0x04 WriteI2C(year);//thang 0x05 StopI2C(); } /************************************************** **********/ void clock_read(void)//read { StartI2C();//gui tin hieu start IdleI2C(); //wait WriteI2C(0xD0);//WriteI2C(0xD0);//gui 7 bit dia chi cua slave cung voi 1 bit ghi du lieu AckI2C(); IdleI2C();//wait WriteI2C(0x00);//gui dia chi thanh ghi ma master muon su dung cua DS1307; thanh ghi sec AckI2C(); IdleI2C();//wait RestartI2C(); WriteI2C(0xD1);//WriteI2C(0xD1);////gui 7 bit dia chi cua slave cung voi 1 bit doc du lieu for(i=0; i<6;i++) { time[i]=ReadI2C(); AckI2C(); } time[6]=ReadI2C(); NotAckI2C(); StopI2C(); } void LCD_IN_INTER(unsigned int d) { unsigned i,j,k,l; l=d/1000;//nghin k=(d%1000)/100;//tram j=((d%1000)%100)/10;//chuc i=((d%1000)%100)%10;//donvi if(l>=1) { WriteDataXLCD(0x30+l); } if(k>=1) { WriteDataXLCD(0x30+k); } WriteDataXLCD(0x30+j); WriteDataXLCD(0x30+i); } void Display(char b[]) { char ch1[] = "THU"; char ch2[] =":"; char ch3[]="/"; int i,j=0; //unsigned char c[]; // Luu y: ket qua doc tu RTC DS1307 la so BCD // Vi du: 24giay --> Ket qua doc ve la: 0x24 // Nen phai tach chu so "2" va so "4" rieng ra de hien thi // tach bcd tu mang b, luu vao mang c -------------------------// for(i=0;i<7;i++) { // mang c chua cac gia tri ascii, 14 bytes // tach BCD, lay nibble cao c[j]=b[i]/16+0x30; // lay nibble thap c[j+1]=(b[i]-(c[j]-0x30)*16)+0x30; j+=2; } SetDDRamAddr(0); putsXLCD(ch1); LCD_IN_INTER(c[7]); putsXLCD(ch3); LCD_IN_INTER(c[8]); LCD_IN_INTER(c[9]); putsXLCD(ch3); LCD_IN_INTER(c[10]); LCD_IN_INTER(c[11]); putsXLCD(ch3); LCD_IN_INTER(c[12]); LCD_IN_INTER(c[13]); SetDDRamAddr(64); LCD_IN_INTER(c[4]); LCD_IN_INTER(c[5]); putsXLCD(ch2); LCD_IN_INTER(c[2]); LCD_IN_INTER(c[3]); putsXLCD(ch2); LCD_IN_INTER(c[1]); LCD_IN_INTER(c[0]); } |
|
|