/*將數(shù)據(jù)寫入AT24C02的0地址處,再讀出,并用數(shù)碼管顯示*/ #include"reg51.h"
sbit SDA=P2^0; sbit SCL=P2^1; sbit duan=P2^6; sbit wei=P2^7;
void delay() {;; }
void init() {SCL=1; SDA=1; delay(); }
void start() {SDA=1; delay(); SCL=1; delay(); SDA=0; delay(); }
void stop() {SDA=0; delay(); SCL=1; delay(); SDA=1; delay(); }
void ack() {unsigned char i; SCL=1; while((SDA==1)&&(i<255))i++; SCL=0; delay(); }
void write(unsigned char date) {unsigned char i,temp; temp=date; for(i=0;i<8;i++) {SCL=0; delay(); temp<<=1; SDA=CY; delay(); SCL=1; delay(); } SCL=0; delay(); SDA=1; delay(); }
unsigned char read() {unsigned char i,j; SCL=0; delay(); SDA=1; delay(); for(i=0;i<8;i++) {SCL=1; delay(); j=(j<<1)|SDA; SCL=0; delay(); } return j; }
write_addr(unsigned char date,unsigned char addr) {start(); write(0xa0); ack(); write(addr); ack(); write(date); ack(); stop(); }
unsigned char read_addr(unsigned char addr) {unsigned char i; start(); write(0xa0); ack(); write(addr); ack(); start(); write(0xa1); ack(); i=read(); stop(); return i; }
void shumaguan(unsigned char dat,unsigned char addr) {unsigned char ziku[18]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x00}; unsigned char wzk[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; duan=0; P0=wzk[addr]; wei=1; wei=0; P0=ziku[dat]; duan=1; delay(); P0=0; duan=0; }
main() {unsigned char i; init(); write_addr(0x11,0); i=read_addr(0); while(1) {shumaguan(i%10,0); shumaguan(i/10%10,1); shumaguan(i/100,2); } }
|