mong ace góp ý
đi jump tùm lum quá
đang hoàn thiện code
sẽ post sớm
đi jump tùm lum quá
đang hoàn thiện code
sẽ post sớm
#include <regx51.h> #define uchar unsigned char #define SCL P2_0 #define SDA P2_1 #define DSW 0xd0 #define DSR 0xd1 void delay(void); void i2cstart(void); void i2cstop(void); uchar i2cwrite(uchar c); uchar i2cread(void); uchar i2creadlast(void); uchar maled[10]={0x81,0xb7,0xc2,0x92,0xb4,0x98,0x88,0xb3,0x80,0x9 0}; //uchar maled[10]={0x21,0x7d,0x13,0x15,0x4d,0x85,0x81,0x3d,0x1,0x5} ; uchar uLed; void scl1(void); void main(void) { //dau tien khoi tao i2c free uchar c,phut,gio; SDA=1; SCL=1; uLed=0; //kiem tra xem boc tem chua i2cstart(); i2cwrite(DSW); i2cwrite(0);//ghi vao dia chi 0 i2cstart(); i2cwrite(DSR); c=i2creadlast(); //phut=i2creadlast(); //P2=0; P2_7=P2_6=P2_4=P2_3=0; if(c&0x80) { i2cstart(); i2cwrite(DSW); i2cwrite(0); i2cwrite(0); } i2cstop(); while(1) { i2cstart(); i2cwrite(DSW); i2cwrite(0); i2cstart(); i2cwrite(DSR); c=i2creadlast(); //da doc giay thanh cong //tiep tuc doc thu phut i2cstart(); i2cwrite(DSW); i2cwrite(1); i2cstart(); i2cwrite(DSR); phut=i2creadlast(); /* i2cstart(); i2cwrite(DSW); i2cwrite(2); i2cstart(); i2cwrite(DSR); gio=i2creadlast(); */ i2cstop(); P2_7=P2_6=P2_4=P2_3=0; switch(uLed) { case 0: P2_4=1; c>>=4; P0=maled[c]; break; case 1: P2_3=1; c&=0xf; P0=maled[c]; break; case 2: P2_7=1; phut>>=4; P0=maled[phut]; break; case 3: P2_6=1; phut&=0xf; P0=maled[phut]; break; } //c&=0xf; //P2_6=1; //P0=maled[c]; /* i2cstart(); i2cwrite(DSW); i2cwrite(1); i2cstart(); i2cwrite(DSR); phut=i2creadlast(); i2cstart(); i2cwrite(DSW); i2cwrite(2); i2cstart(); i2cwrite(DSR); gio=i2creadlast(); i2cstop(); //xuat du lieu ra led 7 seg //c=8; //P3=0xff;//cap nguon cho led P2_7=P2_6=P2_4=P2_3=0; switch(uLed) { case 0: P2_7=1; phut>>=4; P0=maled[gio]; break; case 1: P2_6=1; phut&=0xf; P0=maled[gio]; break; case 2: P2_4=1; c>>=4; P0=maled[phut]; break; case 3: P2_3=1; c&=0xf; P0=maled[phut]; break; /* case 4: P3_5=1; c>>=4; P2=maled[c]; break; case 5: P3_6=1; c&=0xf; P2=maled[c]; break; } */ ++uLed; if(uLed==4) uLed=0; } } void delay(void) { uchar i; for(i=0;i<5;++i); } void i2cstart(void) { SDA=1; scl1(); SDA=0; delay(); SCL=0; delay(); } 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; }
Comment