您的位置 首页 芯闻

I2C总线读写AT24c02的程序

includeincludeAstyle=COLOR:rgb(45,100,179);TEXT-DECORATION:noneclass=inner-linkdecor-nonehref=http:z

#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里读出来的数据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;                  // 读取应对SCL = 0;return ack_bit;                 // 回来AT24Cxx应对位}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;                                //最高位数码管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)的数据delayms(250);write_byte(0x02, 0xAA);                 //向0x02地址写入0xAA(170)的数据delayms(250);delayms(250);eepromdata= read_random(0x02);  // 读取其间一个地址内的数据来验证delayms(250);while(1){display(eepromdata);}}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/256879.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部