Code:
#include <regx51.h> #define uchar unsigned char #define pLed0 P3_2 #define pLed1 P3_3 #define pLed2 P3_4 #define pLed3 P3_5 #define DataLed P2 #define SCL P1_0 #define SDA P1_1 #define DSW 0xd0 #define DSR 0xd1 //giao thuc i2c da san sang //================================================ //cac bien toan cuc uchar uLed;//chu y la co 4 led thoi uchar gio,phut,giay,tg;//se duoc cap nhat tai moi khi ham ngat duoc goi uchar maled[10]={0x21,0x7d,0x13,0x15,0x4d,0x85,0x81,0x3d,0x1,0x5}; //================================================ void init(void); void T0_ISR(void); void delay(void); void i2cstart(void); void i2cstop(void); uchar i2cwrite(uchar c); uchar i2cread(void); uchar i2creadlast(void); void scl1(void); void capnhatgio(void); void khoitaods(void);//kiem tra xem con ds da duoc chay chua //neu chua chay thi cho no chay //================================================== void main(void) { init(); while(1) { } } void init(void) { //khoi tao timer 0 che do tu dong nap lai //cho phep ngat luon //uchar c; SDA=SCL=1; //bat dau kiem tra con ds khoitaods(); TMOD=1; //che do 1 cua bo dinh thoi 0 //cho phep ngat thoi gian EA=1;//cho phep ngat ET0=1;//cho phep ngat thoi gian 0 //khoi tao cho t0 TH0=0xd8; TL0=0xf0;//10.000 us uLed=0; TR0=1; //them phan khoi tao cho 1307 } void T0_ISR(void) interrupt 1 { //dau tien la dung bo dinh thoi cai da TR0=0; //thuc hien quet led cai da //kiem tra xem led nao can phuc vu //cap nhat lai gio, phut, giay //????????????????????????????????????????? //????????????????????????????????????????? capnhatgio(); pLed0=pLed1=pLed2=pLed3=0; switch(uLed) { case 0: pLed0=1; //tach chu so tg=gio>>4; //tg>>=4; DataLed=maled[tg]; break; case 1: pLed1=1; tg=gio&0xf; DataLed=maled[tg]; break; case 2: pLed2=1; tg=phut>>4; DataLed=maled[tg]; break; case 3: pLed3=1; tg=phut&0xf; DataLed=maled[tg]; break; } ++uLed; if(uLed==4) uLed=0; TH0=0xd8; TL0=0xf0;//10.000 us TR0=1; } void scl1(void) { SCL=1; while(!SCL); } void i2cstop(void) { SDA=0; scl1(); SDA=1; delay(); } uchar i2cwrite(char c) { uchar i,ret; for(i=0;i<8;++i) { //lan luot gui tung bit 1 ra SDA=0; if(c&0x80) SDA=1; c<<=1; scl1(); delay(); SCL=0; delay(); } ret=0; //tao xung ack SDA=1; scl1(); delay(); if(!SDA) ret=1; SCL=0; delay(); return ret; } uchar i2cread(void) { uchar ret,i; for(i=0;i<8;++i) { ret<<=1; scl1(); delay(); ret|=SDA; SCL=0; delay(); } //gui tra lai ack SDA=0; scl1(); delay(); SCL=0; delay(); return ret; } uchar i2creadlast(void) { uchar ret,i; for(i=0;i<8;++i) { ret<<=1; scl1(); delay(); ret|=SDA; SCL=0; delay(); } //gui tra lai not ack SDA=1; scl1(); delay(); SCL=0; delay(); return ret; } void i2cstart(void) { SDA=1; scl1(); SDA=0; delay(); SCL=0; delay(); } void delay(void) { uchar i; for(i=0;i<5;++i); } void khoitaods(void) { uchar c; i2cstart(); i2cwrite(DSW); i2cwrite(0);//ghi vao dia chi 0 i2cstart(); i2cwrite(DSR); c=i2creadlast(); if(c&0x80) { i2cstart(); i2cwrite(DSW); i2cwrite(0); i2cwrite(0); } i2cstop(); } void capnhatgio(void) { //cap nhat lai gio phut giay i2cstart(); i2cwrite(DSW); i2cwrite(0); i2cstart(); i2cwrite(DSR); giay=i2cread(); phut=i2cread(); gio=i2creadlast(); i2cstop(); }
Comment