我的IIC写AT24C64的程序,怎么写不进去,但是能读出来255,我是按照datasheet上的时序写的,第一次做这东西,是不是有问题啊,请各位大侠指点侠,谢谢了
void delay() { ;; } void start() //开始信号 { sda=1; delay(); scl=1; delay(); sda=0; delay(); }
void stop() //停止 { sda=0; delay(); scl=1; delay(); sda=1; delay(); }
void ack() //应答 { unsigned char i; scl=1; delay(); while((sda==1)&&(i<250))i++; scl=0; delay(); }
void init() { sda=1; delay(); scl=1; delay(); }
void write_byte(unsigned char date) { unsigned char i,temp; temp=date;
for(i=0;i<8;i++) { temp=temp<<1; scl=0; delay(); sda=CY; delay(); scl=1; delay(); } scl=0; delay(); sda=1; delay(); }
unsigned char read_byte() { unsigned char i,k; scl=0; delay(); sda=1; delay(); for(i=0;i<8;i++) { scl=1; delay(); k=(k<<1)|sda; scl=0; delay(); } return k; }
void write_add(unsigned int address,unsigned char date)// { start(); write_byte(0xa0); ack(); write_byte(address/256); ack(); write_byte(address%256); ack(); write_byte(date); ack(); stop(); }
unsigned char read_add(unsigned int address) { unsigned char date; start(); write_byte(0xa0); ack(); write_byte(address); ack(); start(); write_byte(0xa1); ack(); date=read_byte(); stop(); return date; }
|