STM32F3硬件I2C與LSM303DLHC通信
I2C(Inter-Integrated Circuit)總線是一種兩線式串行總線,用于連接微控制器及其外圍設(shè)備。和SPI一樣,也是一種常用的串行通信方式。
STM32微控制器提供硬件I2C,對(duì)它進(jìn)行相應(yīng)配置就可以用來進(jìn)行多個(gè)設(shè)備之間的通信。
使用步驟:
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開發(fā)板
MCU:STM32F303VC作為主機(jī)
從機(jī)”LSM303DLHC加速度計(jì)/磁強(qiáng)計(jì)
使用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; //讀多字節(jié)指令
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;
}
然后根據(jù)需要設(shè)置LSM303DLHC初始化參數(shù),就可以用上面的讀數(shù)據(jù)函數(shù)不斷讀取傳感器內(nèi)數(shù)據(jù)寄存器的值,進(jìn)行后續(xù)數(shù)據(jù)處理。