I2C总线读写AT24c02的程序
时间:11-27
来源:互联网
点击:
#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);}}
I2C总线读写AT24c0 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)