STM32F3硬件I2C与LSM303DLHC通信
扫描二维码
随时随地手机看文章
I2C(Inter-Integrated Circuit)总线是一种两线式串行总线,用于连接微控制器及其外围设备。和SPI一样,也是一种常用的串行通信方式。
STM32微控制器提供硬件I2C,对它进行相应配置就可以用来进行多个设备之间的通信。
使用步骤:
1. Enable peripheral clock
2. Enable SDA, SCL and SMBA (when used) GPIO clocks
3. Peripherals alternate function:? Call GPIO_Init() function.
4. Program the Mode, Timing , Own address, Ack and Acknowledged Address using the I2C_Init() function.
5. Enable the NVIC and the corresponding interrupt if you need to use interrupt mode.
7. When using the DMA mode Configure the DMA
8. Enable the I2C using the I2C_Cmd() function.
9. Enable the DMA when using DMA mode in the transfers.
基于STM32F3discovery开发板
MCU:STM32F303VC作为主机
从机”LSM303DLHC加速度计/磁强计
使用I2C1,引脚:PB6—I2C1_SCL,PB7—I2C1_SDA
代码:
void I2C_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(I2C_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(LSM303DLHC_I2C_CLK, ENABLE);
GPIO_PinAFConfig(I2C_GPIO_PORT, I2C_SCK_SOURCE, I2C_SCK_AF);
GPIO_PinAFConfig(I2C_GPIO_PORT, I2C_SDA_SOURCE, I2C_SDA_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin =I2C_SCK_PIN;
GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =I2C_SDA_PIN;
GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter= I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter= 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_OwnAddress1=0x00;
I2C_InitStructure.I2C_Timing = 0x00902025;
I2C_Init(LSM303DLHC_I2C,&I2C_InitStructure);
I2C_Cmd(LSM303DLHC_I2C, ENABLE);
}
uint16_t LSM303DLHC_myRead(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
{
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_BUSY)!=RESET);
I2C_TransferHandling(LSM303DLHC_I2C,DeviceAddr,1,I2C_SoftEnd_Mode,I2C_Generate_Start_Write);
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_TXIS)==RESET);
if(NumByteToRead>1)
RegAddr |= 0x80; //读多字节指令
I2C_SendData(LSM303DLHC_I2C,(u8)RegAddr);
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_TC));
I2C_TransferHandling(LSM303DLHC_I2C,DeviceAddr,NumByteToRead,I2C_AutoEnd_Mode,I2C_Generate_Start_Read);
while(NumByteToRead>0)
{
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_RXNE)==RESET);
*pBuffer = I2C_ReceiveData(LSM303DLHC_I2C);
pBuffer++;
NumByteToRead--;
}
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_STOPF)==RESET);
I2C_ClearFlag(LSM303DLHC_I2C,I2C_FLAG_STOPF);
return 1;
}
uint16_t LSM303DLHC_myWrite(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer)
{
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_BUSY)!=0);
I2C_TransferHandling(LSM303DLHC_I2C,DeviceAddr,1,I2C_SoftEnd_Mode,I2C_Generate_Start_Write);
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_TXIS)==0);
I2C_SendData(LSM303DLHC_I2C,RegAddr);
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_TC)==0);
I2C_TransferHandling(LSM303DLHC_I2C,DeviceAddr,1,I2C_AutoEnd_Mode,I2C_Generate_Start_Write);
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_TXIS)==0);
I2C_SendData(LSM303DLHC_I2C,*pBuffer);
while(I2C_GetFlagStatus(LSM303DLHC_I2C,I2C_FLAG_STOPF)==0);
I2C_ClearFlag(LSM303DLHC_I2C,I2C_FLAG_STOPF);
return 1;
}
然后根据需要设置LSM303DLHC初始化参数,就可以用上面的读数据函数不断读取传感器内数据寄存器的值,进行后续数据处理。