感觉AVR的IO模拟I2C和51模拟还是有区别的,下面是AVR和51的模拟I2C的例子。小弟现在有点迷惑,那位达人能解释一下? /***********************AVR unsigned char I2C_Read(unsigned char bEnd) { unsigned char i,mb; Set_SDA; //release sda line mb=0; for(i=0;i<8;i++) { mb=mb<<1; I2C_Delay(); Set_SCL; I2C_Delay(); if( SDA_High ) mb++; I2C_Delay(); Cls_SCL; if(i!=7) I2C_Delay(); } if( bEnd ) Set_SDA; //end read else Cls_SDA; //ack I2C_Delay(); Set_SCL; I2C_Delay(); Cls_SCL; I2C_Delay(); Set_SDA; return mb;
} ////////////////////////51 unsigned char I2C_Read() { unsigned char dat; unsigned char t = 8; I2C_SDA = 1; //在读取数据之前,要把SDA拉高,使之处于输入状态 do { I2C_SCL = 1; I2C_Delay(); dat <<= 1; if ( I2C_SDA ) dat++; I2C_SCL = 0; I2C_Delay(); } while ( --t != 0 ); return dat; }
|