các bạn có thể chỉ dùm mình tại sao cái code giao tiếp đồn hồ thời gian thực DS1307 với PIC16f877 của mình nó lại chạy chậm hơn thời gian máy tính vài giây và chạy sạu 1 thời gian thì thời gian lại bị sai kiểu như chạy chậm hơn máy tính ý. cảm ơn mấy bạn nhiều
file đính kèm: I2C DS1307
Code:
/*============================================================== Program: I2C DS1307 MCU: PIC16F877 Complier CCS Crytal: 20.000.000 Hz ===============================================================*/ #include <16F877A.h> #FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT #use i2c(master,sda=PIN_C4,scl=PIN_C3) #use delay(clock=200000000) #use fast_io(D) #use fast_io(C) #byte PORTD=0x08 #byte PORTC=0x07 unsigned char ma_led_7seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; unsigned char sec,min,hour;//,day,date,month,year; unsigned char i,j,sec_dv,sec_c,min_dv,min_c,hour_dv,hour_c; unsigned long l,m,n,h; //--------------------------------------------------- void write_ds1307(byte address,byte data) { i2c_start(); /* Start i2c bus */ i2c_write(0xd0); /* Connect to DS1307 */ i2c_write(address); /* Request RAM address on DS1307 */ i2c_write(data); /* Connect to DS1307 for Read */ i2c_stop(); } BYTE read_DS1307(byte address) { BYTE data;//,tam; i2c_start(); /* Start i2c bus */ i2c_write(0xd0); /* Connect to DS1307 */ i2c_write(address); /* Request RAM address on DS1307 */ i2c_stop(); i2c_start(); /* Start i2c bus */ i2c_write(0xd1); /* Connect to DS1307 for Read (0x68<<1)|1*/ data=i2c_read(0); /* Receive data /AK */ i2c_stop(); return(data); } void giai_ma() { sec_dv=(sec&0x0f); sec_c=((sec&0xf0)>>4); min_dv=(min&0x0f); min_c=((min&0xf0)>>4); hour_dv=(hour&0x0f); hour_c=((hour&0xf0)>>4); } void ghi_data(unsigned char ma) { m=h; l=0x8000; n=0x0000; ma=ma_led_7seg[ma]; n|=ma; n|=m; for(j=0;j<16;j++) { if((n&l)==l) output_high(PIN_D1); //DS else output_low(PIN_D1); output_high(PIN_D0); //SH_CP output_low(PIN_D0); l>>=1; } output_high(PIN_D2); //ST_CP output_low(PIN_D2); } void main(void) { set_tris_D(0); set_tris_C(1); delay_ms(50); PORTD=0; h=0x8000; sec=read_ds1307(0); write_ds1307(0,sec&0x7F); // enable oscillator(bit 7 =0) while(true) { sec=read_ds1307(0); // read second min=read_ds1307(1); // read minute hour=read_ds1307(2); // read hour giai_ma(); for(i=0;i<50;i++) { ghi_data(sec_dv); h>>=1; ghi_data(sec_c); h>>=1; ghi_data(min_dv); h>>=1; ghi_data(min_c); h>>=1; ghi_data(hour_dv); h>>=1; ghi_data(hour_c); h=0x8000; } } }
Comment