欢迎来到天天文库
浏览记录
ID:61487908
大小:24.50 KB
页数:7页
时间:2021-02-05
《I2C总线由单片机控制.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、I2C总线由单片机控制,挂在I2C总线上的设备可以很多个,每个有独立的地址,A0,A1,A2。。。但单片机发送地址后,该地址的设备就会被激活,向单片机反馈一个应答信号,然后进行该设备的操作任务,比如这个设备是AT24C02,它是用来存储数据的,具体的操作有发送地址,发送指令,接受数据以及发送数据等等,你还是仔细研究下I2C协议吧!发个例子给你看看吧!#ifndef_ROM24C02_H_2008_11_30_#define_ROM24C02_H_2008_11_30_#defineucharunsignedchar#defineuintun
2、signedintsbitrom24c02_sda=P2^1;//24c02串行数据sbitrom24c02_scl=P2^0;//24c02串行时钟voidrom_delay1ms(unsignedintcount)//延时1ms{unsignedcharj;for(;count>0;count--)for(j=0;j<120;j++);}voiddelay1c(){;}voidrom24c02_start(void){rom24c02_sda=1;rom24c02_scl=1;delay1c();rom24c02_sda=0;delay
3、1c();}voidrom24c02_stop(void){rom24c02_sda=0;rom24c02_scl=1;delay1c();rom24c02_sda=1;delay1c();}voidrom24c02_ack(void){rom24c02_sda=0;delay1c();rom24c02_scl=1;delay1c();rom24c02_scl=0;}voidrom24c02_noack(void){rom24c02_sda=1;delay1c();rom24c02_scl=1;delay1c();rom24c02_scl=
4、0;}voidrom24c02_write8bit(uchardat){uchari=8;do{rom24c02_scl=0;delay1c();if((dat&0x80)==0x80)rom24c02_sda=1;elserom24c02_sda=0;rom24c02_scl=1;delay1c();rom24c02_scl=0;dat=dat<<1;}while(--i);rom24c02_scl=0;}ucharrom24c02_read8bit(void){uchartemp=0;uchari=8;rom24c02_sda=1;do
5、{rom24c02_scl=0;delay1c();rom24c02_scl=1;delay1c();if(rom24c02_sda)temp=temp
6、0x01;elsetemp=temp&0xfe;if(i-1){temp=temp<<1;}i--;}while(i);return(temp);}voidrom24c02_writeone(ucharpdat,ucharadd){rom24c02_start();rom24c02_write8bit(0xa0);rom24c02_ack();rom24c02_write8bit(add)
7、;rom24c02_ack();rom24c02_write8bit(pdat);rom24c02_ack();rom24c02_stop();rom_delay1ms(10);}ucharrom24c02_readone(ucharadd){uchartemp;rom24c02_start();rom24c02_write8bit(0xa0);rom24c02_ack();rom24c02_write8bit(add);rom24c02_ack();rom24c02_start();rom24c02_write8bit(0xa1);rom
8、24c02_ack();temp=rom24c02_read8bit();rom24c02_scl=0;rom24c02_noack();rom24c02_stop();return(temp);}#endif/*********************************at24c04*******************************//*************************I2C*START**************************************/voidStart(){SDA=1;SCL
9、=1;SDA=0;SCL=0;}/*******************************I2C*STOP*******************************/v
此文档下载收益归作者所有