09-05-2009, 11:51 AM | #5 |
Đệ tử 6 túi
Tham gia ngày: Jul 2007
Bài gửi: 154
: |
ds1037
cái này minh chưa làm mạch thật nhưng chạy mô phỏng tốt. bạn xem tạm nhé
#include<16F877a.h> #fuses NOLVP,NOWDT,PUT,hs,NOPROTECT,NOBROWNOUT #device 16F877a*=16 ADC=10 #include <math.h> #use delay(clock=20000000) //#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #use fast_io(a) #byte porta=0x05 #use fast_io(b) #byte portb=0x06 #use fast_io(c) #byte portc=0x07 #use fast_io(d) #byte portd=0x08 #use fast_io(e) #byte porte=0x09 #define RTC_SCL PIN_B0 #define RTC_SDA PIN_B1 #bit RS = portA.0 #bit RW = portA.1 #bit E = portA.2 //#define RTC_RST PIN_B2 #use i2c(master, fast, sda=RTC_SDA, scl=RTC_SCL) //================================================ int sec,min,hour,date,month,year,a,b; unsigned char so[] = "0123456789"; // unsigned int led[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80}; void write_RTC(int add, BYTE data) { short int status; i2c_start(); // bao hieu bat dau giao tiep i2c i2c_write(0xd0); // dia chi thiet bi nhan i2c_write(add); // dia chi thanh ghi i2c_write(data); //gui data vao dia chi thanh ghi cua thiet bi nhan i2c_stop(); // bao hieu ngung ghi i2c_start(); // bao hieu bat dau giao tiep status=i2c_write(0xd0); // kiem tra trang thai cua thiet bi nhan while(status==1) // lap lai cho den khi thiet bi da nhan xong (No Ack) { i2c_start(); status=i2c_write(0xd0); } delay_us(10); } BYTE read_RTC(int add) { BYTE data; i2c_start(); i2c_write(0xd0); i2c_write(add); i2c_start(); i2c_write(0xd1); data=i2c_read(0); // No Ack i2c_stop(); delay_ms(10); return(data); } void init_RTC() // ham khoi tao eeprom { i2c_start(); // bao hieu bat dau giao tiep i2c i2c_write(0xd0); // dia chi thiet bi nhan i2c_write(0x00); // enable RTC i2c_write(0x01);// sec // dia chi thanh ghi i2c_write(0x00);// min //gui data vao dia chi thanh ghi cua thiet bi nhan i2c_write(0x08);// hur i2c_write(0x04);//day i2c_write(0x11);// date i2c_write(0x02);// month i2c_write(0x09);// year i2c_stop(); // bao hieu ngung ghi i2c_start(); // bao hieu bat dau giao tiep } int rm_bcd(BYTE data) // CHUYEN BCD SANG SO NGUYEN { int i; i=data; data=(i>>4)*10; data=data+(i<<4>>4); return data; } //////////////////////////////////////////////////////////// /*Ham yeu cau goi lenh dieu khien LCD*/ void command() { RS = 0; RW = 0; E = 1; E = 0; delay_ms(1); } /*Ham yeu cau goi du lieu hien thi len LCD*/ void display() { RS = 1; RW = 0; E = 1; E = 0; delay_ms(1); } void main() { set_tris_b(0); set_tris_a(0); set_tris_c(0); set_tris_e(0); set_tris_d(0); init_RTC(); while(true) {//write_RTC(addsec,0); // sec = read_RTC(addsec); // doc thoi gian tu RTC // min = read_RTC(addmin); // hour = read_RTC(addhour); portd = 0x38; // Hai hang, ma tran dot 5*7, 8 bit interface command(); portd = 0x0C; // Bat hien thi, tat con tro command(); ////////////////////////////////////////////////// hour = rm_bcd(read_RTC(0x02)); min = rm_bcd(read_RTC(0x01)); sec = rm_bcd(read_RTC(0x00)); date = rm_bcd(read_RTC(0x04)); month = rm_bcd(read_RTC(0x05)); year = rm_bcd(read_RTC(0x06)); ////////////////////////////////////////////////////////////////// a = sec/10; b = sec-a*10; portd = 0x8b; command(); portd = so[a]; display(); portd = 0x8c; command(); portd = so[b]; display(); portd = 0x8a; command(); portd = ':'; display(); ///// hien thi min len LCD //////// a = min/10; b = min-a*10; portd = 0x88; command(); portd = so[a]; display(); portd = 0x89; command(); portd = so[b]; display(); portd = 0x87; command(); portd = ':'; display(); ///// hien thi sec len LCD //////// a = hour/10; b = hour-a*10; portd = 0x85; command(); portd = so[a]; display(); portd = 0x86; command(); portd = so[b]; display(); /////////////////////////////////// a = year/10; b = year-a*10; portd = 0xcb; command(); portd = so[a]; display(); portd = 0xcc; command(); portd = so[b]; display(); portd = 0xca; command(); portd = '-'; display(); ///// hien thi min len LCD //////// a = month/10; b = month-a*10; portd = 0xc8; command(); portd = so[a]; display(); portd = 0xc9; command(); portd = so[b]; display(); portd = 0xc7; command(); portd = '-'; display(); ///// hien thi sec len LCD //////// a = date/10; b = date-a*10; portd = 0xc5; command(); portd = so[a]; display(); portd = 0xc6; command(); portd = so[b]; display(); } } |
|
|