Cách gửi xung, đọc và ghi dữ liệu của con 24c64 và 24c00 có giống nhau ko các bác?
Thanks!
Thanks!
#include <reg2051.h> //#include <reg52.h> //#include <intrins.h> //#include "E:\KeilC_uVision4\common.h" sbit SCL = P3^0; sbit SDA = P3^1; //extern delay_nop(); void delay_nop() { _nop_(); _nop_(); _nop_(); _nop_(); } void i2c_start(void) { SDA = 1; delay_nop(); SCL = 1; delay_nop(); SDA = 0; delay_nop(); SCL = 0; } void i2c_stop(void) { SCL = 0; SDA = 0; delay_nop(); SCL = 1; delay_nop(); SDA = 1; } unsigned char i2c_read(bit ack) { unsigned char data i, result; result = 0; SDA = 1; //select input direction delay_nop(); SCL = 0; for(i = 0; i < 8; i++) { delay_nop(); SCL = 1; delay_nop(); result <<= 1; if(SDA == 1) result++; SCL = 0; } delay_nop(); SDA = (ack == 1)? 0:1; delay_nop(); SCL = 1; delay_nop(); SCL = 0; return result; } bit i2c_write(unsigned char value) { unsigned char data i, temp; bit bdata ack; temp = value; SCL = 0; SCL = 0; for(i = 0; i < 8; i++) { delay_nop(); SDA = (temp & 0x80)? 1:0; delay_nop(); SCL = 1; SCL = 1; delay_nop(); SCL = 0; SCL = 0; temp <<= 1; } SDA = 1; // read ACK delay_nop(); SCL = 1; SCL = 1; ack = SDA; delay_nop(); SCL = 0; SCL = 0; return ack; }
#include <reg2051.h> //#include <reg52.h> #include <intrins.h> #include "E:\KeilC_uVision4\common.h" #define RTC_SLAVE_ADDRESS 0xD0 #define RTC_READ_COMMAND RTC_SLAVE_ADDRESS | 0x01 #define RTC_WRITE_COMMAND RTC_SLAVE_ADDRESS /* struct Time { char second; char minute; char hour; //char day; //char date; //char month; //char year; }; */ extern i2c_start(); extern i2c_stop(); extern bit i2c_write(unsigned char value); extern unsigned char i2c_read(bit ack); extern delay_us(unsigned int us); /* void delay_us(unsigned int us) { while(us--); } */ extern delay_nop(); void DS1307_init() { unsigned char x; i2c_start(); i2c_write(RTC_WRITE_COMMAND); //Write slave address i2c_write(0x00); i2c_start(); i2c_write(RTC_READ_COMMAND); x = i2c_read(0); // Read second with NACK i2c_stop(); delay_us(10); i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(0x00); // Go to Second Reggister i2c_write(x & 0x7F); // start DS1307 i2c_stop(); i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(0x07); // Go to Control Reggister i2c_write(0x10); // configure RTC i2c_stop(); } struct Time DS1307_get_time() { struct Time t; i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(0x00); // Go to top of memory i2c_start(); i2c_write(RTC_READ_COMMAND); t.second = i2c_read(1); // Read with ACK t.minute = i2c_read(1); t.hour = i2c_read(0); i2c_stop(); return t; } /* struct Time DS1307_get_date_time() { struct Time t; i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(0x00); // Go to top of memory i2c_start(); i2c_write(RTC_READ_COMMAND); t.second = i2c_read(1); // Read with ACK t.minute = i2c_read(1); t.hour = i2c_read(1); t.day = i2c_read(1); t.date = i2c_read(1); t.month = i2c_read(1); t.year = i2c_read(0); i2c_stop(); return t; } * void DS1307_set_time(struct Time t) { i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(0x00); i2c_write(t.second); i2c_write(t.minute); i2c_write(t.hour); i2c_stop(); } /* void DS1307_set_date_time(struct Time t) { i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(0x00); i2c_write(t.second); i2c_write(t.minute); i2c_write(t.hour); i2c_write(t.day); i2c_write(t.date); i2c_write(t.month); i2c_write(t.year); i2c_stop(); } */ /* void DS1307_write(char addr, char value) { i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(addr); i2c_write(value); i2c_stop(); } unsigned char DS1307_read(char addr) { unsigned char value; i2c_start(); i2c_write(RTC_WRITE_COMMAND); i2c_write(addr); i2c_start(); i2c_write(RTC_READ_COMMAND); value = i2c_read(0); i2c_stop(); return value; } */
Comment