giải thích giùm lỗi giao tiếp DS1307 và eeprom
<script type="text/javascript">
<!-- alert("#define RTC_SDA PIN_C4 #define RTC_SCL PIN_C3 #use i2c(master, sda=RTC_SDA, scl=RTC_SCL) BYTE bin2bcd(BYTE binary_value); BYTE bcd2bin(BYTE bcd_value); void ds1307_init(void) { BYTE seconds = 0; i2c_start(); i2c_write(0xD0); // WR to RTC i2c_write(0x00); // REG 0 i2c_start(); i2c_write(0xD1); // RD from RTC seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307 i2c_stop(); seconds &= 0x7F; delay_us(3); i2c_start(); i2c_write(0xD0); // WR to RTC i2c_write(0x00); // REG 0 i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value i2c_start(); i2c_write(0xD0); // WR to RTC i2c_write(0x07); // Control Register i2c_write(0x80); // Disable squarewave output pin i2c_stop(); } void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec) { sec &= 0x7F; hr &= 0x3F; i2c_start(); i2c_write(0xD0); // I2C write address i2c_write(0x00); // Start at REG 0 - Seconds i2c_write(bin2bcd(sec)); // REG 0 i2c_write(bin2bcd(min)); // REG 1 i2c_write(bin2bcd(hr)); // REG 2 i2c_write(bin2bcd(dow)); // REG 3 i2c_write(bin2bcd(day)); // REG 4 i2c_write(bin2bcd(mth)); // REG 5 i2c_write(bin2bcd(year)); // REG 6 i2c_write(0x80); // REG 7 - Disable squarewave output pin i2c_stop(); } void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow) { i2c_start(); i2c_write(0xD0); i2c_write(0x03); // Start at REG 3 - Day of week i2c_start(); i2c_write(0xD1); dow = bcd2bin(i2c_read() & 0x7f); // REG 3 day = bcd2bin(i2c_read() & 0x3f); // REG 4 mth = bcd2bin(i2c_read() & 0x1f); // REG 5 year = bcd2bin(i2c_read(0)); // REG 6 i2c_stop(); } void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec) { i2c_start(); i2c_write(0xD0); i2c_write(0x00); // Start at REG 0 - Seconds i2c_start(); i2c_write(0xD1); sec = bcd2bin(i2c_read() & 0x7f); min = bcd2bin(i2c_read() & 0x7f); hr = bcd2bin(i2c_read(0) & 0x3f); i2c_stop(); } BYTE bin2bcd(BYTE binary_value) { BYTE temp; BYTE retval; temp = binary_value; retval = 0; while(1) { // Get the tens digit by doing multiple subtraction // of 10 from the binary value. if(temp >= 10) { temp -= 10; retval += 0x10; } else // Get the ones digit by adding the remainder. { retval += temp; break; } } return(retval); } // Input range - 00 to 99. BYTE bcd2bin(BYTE bcd_value) { BYTE temp; temp = bcd_value; // Shifting upper digit right by 1 is same as multiplying by 8. temp >>= 1; // Isolate the bits for the upper digit. temp &= 0x78; // Now return: (Tens * 8) + (Tens * 2) + Ones return(temp + (temp >> 2) + (bcd_value & 0x0f)); }"); //--> </script> |
bên trên là code ds1307
đây là code eeprom. #ifndef EEPROM_SDA #define EEPROM_SDA PIN_C4 #define EEPROM_SCL PIN_C3 #endif #use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL) #define EEPROM_ADDRESS long int #define EEPROM_SIZE 32768 void init_ext_eeprom() { output_float(EEPROM_SCL); output_float(EEPROM_SDA); } void write_ext_eeprom(long int address, BYTE data) { short int status; i2c_start(); i2c_write(0xa0); i2c_write(address>>8); i2c_write(address); i2c_write(data); i2c_stop(); i2c_start(); status=i2c_write(0xa0); while(status==1) { i2c_start(); status=i2c_write(0xa0); } i2c_stop(); } BYTE read_ext_eeprom(long int address) { BYTE data; i2c_start(); i2c_write(0xa0); i2c_write(address>>8); i2c_write(address); i2c_start(); i2c_write(0xa1); data=i2c_read(0); i2c_stop(); return(data); } cho mình hỏi tại sao khi sử dụng chương trình đọc ghi eeprom thì thời gian thực bị sai hoàn toàn. 2 code trên khi sử dụng trong 1 chương trình thì gặp lỗi gì ạ |
khác nhau ở đoạn này
Code:
BYTE bin2bcd(BYTE binary_value); ví dụ số 52 bạn đọc đc ở DS1307 thì ở dạng nhị phân đọc đc là (0101.0010) cái đọc được này mà đưa ra hiển thị luôn thì sẽ là số 82 (vì vậy phải convert giá trị đọc đc trước khi hiển thị) |
Múi giờ GMT. Hiện tại là 02:57 AM. |
Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam