#include#include #define OP_READ 0xa1 // 器件地址以及讀取操作#define OP_WRITE 0xa0 // 器件地址以及寫入操作unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};int eepromdata; //從EEPROM里讀出來(lái)的數(shù)據(jù)sbit SDA = P3^4;sbit SCL = P3^3;void delay()ms(unsigned char ms) {unsigned char i;while(ms--){for(i = 0; i < 120; i++);}}void delay()(void){int k;for(k=0;k<400;k++);}void start(){SDA = 1;SCL = 1;_nop_();_nop_();SDA = 0;_nop_();_nop_();_nop_();_nop_();SCL = 0;}void stop(){SDA = 0;_nop_();_nop_();SCL = 1;_nop_();_nop_();_nop_();_nop_();SDA = 1;}unsigned char shin(){unsigned char i,read_data;for(i = 0; i < 8; i++){SCL = 1;read_data <<= 1;read_data |= (unsigned char)SDA;SCL = 0;}return(read_data);}bit shout(unsigned char write_data){unsigned char i;bit ack_bit;for(i = 0; i < 8; i++) {SDA = (bit)(write_data & 0x80);_nop_();SCL = 1;_nop_();_nop_();SCL = 0;write_data <<= 1;}SDA = 1; _nop_();_nop_();SCL = 1;_nop_();_nop_();_nop_();_nop_();ack_bit = SDA; // 讀取應(yīng)答SCL = 0;return ack_bit; // 返回AT24Cxx應(yīng)答位}void write_byte(unsigned char addr, unsigned char write_data){start();shout(OP_WRITE);shout(addr);shout(write_data);stop();delay()ms(10); }unsigned char read_current(){unsigned char read_data;start();shout(OP_READ);read_data = shin();stop();return read_data;}unsigned char read_random(unsigned char random_addr){start();shout(OP_WRITE);shout(random_addr);return(read_current());}void display(int k){P2=0xfe; //最高位數(shù)碼管1234P0=tab[k/1000];delay();P2=0xfd;P0=tab[k00/100];delay();P2=0xfb;P0=tab[k0/10];delay();P2=0xf7;P0=tab[k];delay();P2=0xff;}void main(void){SDA = 1;SCL = 1;eepromdata=0;write_byte(0x01, 0x55); //向0x01地址寫入0x55(85)的數(shù)據(jù)delayms(250);write_byte(0x02, 0xAA); //向0x02地址寫入0xAA(170)的數(shù)據(jù)delayms(250);delayms(250);eepromdata= read_random(0x02); // 讀取其中一個(gè)地址內(nèi)的數(shù)據(jù)來(lái)驗(yàn)證delayms(250);while(1){display(eepromdata);}}
評(píng)論