v特仑苏v v特仑苏v
关注数: 82 粉丝数: 368 发帖数: 30,235 关注贴吧数: 16
请教下吧主iic总线读取存储24c02的问题 一直调不出来 请大神帮忙看看 #include <stc12c5a60s2.h> #include <intrins.h> #include <DS18B20.h> #define uchar unsigned char #define uint unsigned int sbit SDA=P2^0; //数据线 sbit SCL=P2^1; //时钟线 bit ack; void delay_1()//4.339us延时 48*1/11.0592 { _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); } void delay_2()//1.0848us延时 { _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); } void iic_init() //?? { SCL=1; SDA=1; delay_1(); } void Start_I2c() { SCL=1; SDA=0; delay_2(); SDA=1; delay_1(); delay_2(); SDA=0; delay_1(); SCL=0; //时钟信号拉低为通信做准备 delay_2(); } void Stop_I2c() { SDA=0; SCL=1; delay_1(); SDA=1; delay_1(); delay_2(); SDA=0; } void SendByte(uchar c) { uchar BitCnt; uchar temp; uchar i; temp=c; for(BitCnt=0;BitCnt<8;BitCnt++) { temp=temp<<1; SDA=CY; delay_1(); SCL=1; delay_1();//数据稳定 SCL=0; delay_2(); } SDA=1; delay_2(); while((SDA==1)&&(i<250))i++; if(SDA==1) { SDA=0; } SCL=1; delay_1(); SCL=0; delay_2(); } uchar RcvByte() { uchar retc; uchar BitCnt; retc=0; SDA=1; delay_2(); for(BitCnt=0;BitCnt<8;BitCnt++) { SCL=1; delay_1(); retc=(retc<<1)|SDA; SCL=0; delay_1(); } return(retc); } void Ack_I2c(bit a) { if(a==0)SDA=0; else SDA=1; delay_2(); SCL=1; delay_1(); SCL=0; delay_2(); SDA=0; delay_2(); } void write_add(uchar address,uchar *s,uchar no) { uchar i; Start_I2c(); SendByte(0xa0); SendByte(address); for(i=0;i<no;i++) { SendByte(*s); s++; } Stop_I2c(); } void read_add(uchar address,uchar *s,uchar no) { uchar i; Start_I2c(); SendByte(0xa0); SendByte(address); Start_I2c(); SendByte(0xa1); for(i=0;i<no-1;i++) { *s=RcvByte(); Ack_I2c(0); //应答 s++; } *s=RcvByte(); Ack_I2c(1); //非应答 Stop_I2c(); }
1 下一页