可以和iic设备通信吗?
rt 想和bmp180气压温度传感器通信该如何是好呢? 可以使用模拟iic/*****************************************//* 初始化IIC */
/*****************************************/
void INIT_IIC(void)
{
SCL_dir = 1;
SCL_dir = 1;
SCL = 1;
SDA = 1;
}
/*****************************************/
/* 启动IIC */
/*****************************************/
void I2Cstart(void)
{
SCL_dir = 1;
SDA_dir = 1;
SDA = 1;
somenop();
SCL = 1;
somenop();
somenop();
SDA = 0;
somenop();
somenop();
SCL = 0;
}
/*****************************************/
/* IIC发送数据 */
/*****************************************/
void I2Csend(byte data)
{
unsigned char i=8;
SCL_dir = 1;
SDA_dir = 1;
while(i)
{
SCL = 0;
somenop();
SDA=(data&0x80)>>7;
data<<=1;
somenop();
SCL = 1;
somenop();
somenop();
i--;
}
SCL = 0;
SDA_dir = 0;
}
/*****************************************/
/* IIC接收数据 */
/*****************************************/
unsigned char I2Creceive(void)
{
unsigned char i=8;
unsigned char data=0;
SCL_dir = 1;
SDA_dir = 0;
while(i)
{
SCL = 0;
somenop();
somenop();
SCL = 1;
somenop();
data<<=1;
data|=SDA;
somenop();
i--;
}
SCL = 0;
return(data);
}
/*****************************************/
/* IIC读取应答位 */
/*****************************************/
void I2Creadack(void)
{
SCL_dir = 1;
SDA_dir = 0;
SCL = 0;
somenop();
somenop();
SCL = 1;
somenop();
if(SDA == 1)
{
for(;;) ;
}
somenop();
SCL = 0;
somenop();
somenop();
}
/*****************************************/
/* IIC发送应答位 */
/*****************************************/
void I2Csendack(void)
{
SCL_dir = 1;
SDA_dir = 1;
SCL = 0;
somenop();
SDA = 0;
somenop();
SCL = 1;
somenop();
somenop();
SCL = 0;
somenop();
somenop();
}
/*****************************************/
/* 停止IIC */
/*****************************************/
void I2Cstop(void)
{
SCL = 0;
SDA = 0;
SCL_dir = 1;
SDA_dir = 1;
SCL = 0;
SDA = 0;
somenop();
somenop();
SCL = 1;
somenop();
SDA = 1;
somenop();
somenop();
SCL_dir = 0;
SDA_dir = 0;
}
/*****************************************/
/* 主函数 */
/*****************************************/
void main(void) {
DisableInterrupts;
INIT_PLL();
INIT_IIC();
LEDCPU_dir = 1;
LED_dir=0xff;
LEDCPU = 0;
EnableInterrupts;
for(;;)
{
delay();
I2Cstart();
I2Csend(0b01000001);
I2Creadack();
receivedata = I2Creceive(); //读取数据
I2Cstop();
LED=receivedata; //显示读取到的数据
}
}
页:
[1]