您好,欢迎访问三七文档
关于IIC总线关于IIC的介绍这里不废话,我们主要讨论一下使用IIC总线时需要注意的几点。硬件设计方面:1、SDA线务必加上拉电阻,通信速度越快上拉电阻阻值越小,推荐2.2K。2、确认I2C器件的设备地址是否需要我们设定。软件设计方面:1、除主机发送起始信号和终止信号外,当SCK为高时SDA不允许变化。2、器件的设备地址由7Bit(地址)+1Bit(W/R)组成,0写1读。3、根据第一条原则,任何一个涉及到操作SCK的函数在退出函数时保证SCK为低。起始信号:起始信号就是在SCK为高时SDA由高变低。终止信号:和起始信号相反,终止信号就是在SCK为高时由低变高。数据传输:在数据传输阶段根据第一条原则,无论主机发送数据还是从机发送数据都是在SCK为低时把要发送的数据位准备好等到SCK为高时就不允许数据变动了。也就是说只有在SCK为低时SDA才可以变动。为了传输更稳定一些,软件上可以把SCK的波形做的窄一些。应答信号:在接受方接收完一字节数据且待发送方释放总线后,接收方拉低总线一个时钟周期表示接受数据完成。说到这里就有必要提一下双方如何判断对方是不是释放总线了。其实很简单,当接收方接收完一个字节后根据第一条原则,如果SCK为高SDA不允许变动那么自然发送方也不敢释放总线(SDA是上拉的,释放总线SDA就会变成高),因此发送方释放总线的标志就是发送完一个字节的最后一个Bit后SCK由高变低。我们编写的主机程序在等待从机应答时务必保证SCK为低,当检测到应答后再把SCK拉高然后再拉低让从机完成应答释放总线。下面附上读AT24C02的时序图(AT24C02的设备地址设置为0xaf,读取的地址为0x01)。图1I2C起始信号+设备地址(写)+从机应答图2内存地址+从机应答图3起始信号+设备地址(读)+从机应答图4内存数据(从机发主机收)+主机应答+终止信号图5主机应答特写附录基于Stm32的IIC驱动代码I2C.c#includeI2C.h#defineDelay_Time5staticvoiddelay_nus(unsignedchartime){unsignedchari=0;while(time--){i=5;while(i--);}}staticvoidI2C_SDA_Write(unsignedcharWrite_Data){I2C_SDA(Write_Data);}staticunsignedcharI2C_SDA_Read(){I2C_SDA_Write(HIGH);if(HAL_GPIO_ReadPin(I2C_SDA_GPIO,I2C_SDA_PIN)){return1;}else{return0;}}voidI2C_Star(void){I2C_SDA_Write(HIGH);delay_nus(Delay_Time);I2C_SCK(HIGH);delay_nus(Delay_Time);I2C_SDA_Write(LOW);delay_nus(Delay_Time);I2C_SCK(LOW);}voidI2C_Stop(void){I2C_SDA_Write(LOW);I2C_SCK(HIGH);delay_nus(Delay_Time);I2C_SDA_Write(HIGH);delay_nus(Delay_Time);I2C_SCK(LOW);}unsignedcharReceive_Response(void){unsignedinti;I2C_SCK(LOW);while(I2C_SDA_Read()){i++;if(i10000){return0;}}I2C_SCK(HIGH);delay_nus(Delay_Time);I2C_SCK(LOW);return1;}voidI2C_WriteOneByte(unsignedcharWrite_Data){unsignedchari;unsignedcharData;Data=Write_Data;for(i=0;i8;i++){I2C_SCK(LOW);delay_nus(Delay_Time);I2C_SDA_Write(Data&0x80);delay_nus(Delay_Time);I2C_SCK(HIGH);delay_nus(Delay_Time);Data=1;}I2C_SCK(LOW);}unsignedcharI2C_ReadOneByte(void){unsignedchari;unsignedcharData;for(i=0;i8;i++){Data=1;I2C_SCK(LOW);delay_nus(Delay_Time);I2C_SCK(HIGH);delay_nus(Delay_Time);Data|=I2C_SDA_Read();}/**************SendACK**************/I2C_SCK(LOW);I2C_SDA_Write(LOW);returnData;}I2C.h#ifndef__I2C_H#define__I2C_H#includestm32f1xx_hal.h#defineLOW0#defineHIGH1#defineI2C_SCK_GPIOGPIOA#defineI2C_SCK_PINGPIO_PIN_15#defineI2C_SDA_GPIOGPIOC#defineI2C_SDA_PINGPIO_PIN_11#defineI2C_SCK(a)if(a)\HAL_GPIO_WritePin(I2C_SCK_GPIO,I2C_SCK_PIN,GPIO_PIN_SET);\else\HAL_GPIO_WritePin(I2C_SCK_GPIO,I2C_SCK_PIN,GPIO_PIN_RESET);#defineI2C_SDA(a)if(a)\HAL_GPIO_WritePin(I2C_SDA_GPIO,I2C_SDA_PIN,GPIO_PIN_SET);\else\HAL_GPIO_WritePin(I2C_SDA_GPIO,I2C_SDA_PIN,GPIO_PIN_RESET);voidI2C_Star(void);voidI2C_Stop(void);unsignedcharReceive_Response(void);voidI2C_WriteOneByte(unsignedcharWrite_Data);unsignedcharI2C_ReadOneByte(void);#endifAT24C02.c#includeAT24C02.h#includeI2C.hunsignedcharAT24C02_Write(unsignedcharadd,unsignedcharData){I2C_Star();I2C_WriteOneByte(0xae);if(!Receive_Response()){return0;}I2C_WriteOneByte(add);if(!Receive_Response()){return0;}I2C_WriteOneByte(Data);if(!Receive_Response()){return0;}I2C_Stop();return1;}unsignedcharAT24C02_Read(unsignedcharadd){unsignedcharData;I2C_Star();I2C_WriteOneByte(0xae);if(!Receive_Response()){return1;}I2C_WriteOneByte(add);if(!Receive_Response()){return2;}I2C_Star();I2C_WriteOneByte(0xaf);if(!Receive_Response()){return3;}Data=I2C_ReadOneByte();I2C_Stop();returnData;}AT24C02.h#ifndef__AT24C02_H#define__AT24C02_H#includestm32f1xx_hal.hunsignedcharAT24C02_Write(unsignedcharadd,unsignedcharData);unsignedcharAT24C02_Read(unsignedcharadd);#endif
本文标题:关于IIC总线
链接地址:https://www.777doc.com/doc-2665519 .html