我使用的是STM32的固件庫。
硬件模塊使用之前必須配置其參數(shù),I2C的配置如下:
void IIC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_ClocksTypeDef rcc_clocks;
/* GPIO Peripheral clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1|RCC_APB1Periph_I2C2|RCC_APB1Periph_I2C3, ENABLE);
/* Reset I2Cx IP */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1|RCC_APB1Periph_I2C2|RCC_APB1Periph_I2C3, ENABLE);
/* Release reset signal of I2Cx IP */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1|RCC_APB1Periph_I2C2|RCC_APB1Periph_I2C3, DISABLE);
/*I2C1 configuration*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); //注意,此處不能合并寫成GPIO_PinSource6|GPIO_PinSource7
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
//PB6: I2C1_SCL PB7: I2C1_SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* I2C Struct Initialize */
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
/* I2C Initialize */
I2C_Cmd(I2C1, ENABLE);
/*I2C2 configuration*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2); //注意,此處不能合并寫成GPIO_PinSource6|GPIO_PinSource7
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);
//PB10: I2C2_SCL PB11: I2C2_SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* I2C Struct Initialize */
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C2, &I2C_InitStructure);
/* I2C Initialize */
I2C_Cmd(I2C2, ENABLE);
/*I2C3 configuration*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_I2C3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_I2C3);
//PA8: I2C3_SCL
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PC9: I2C3_SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* I2C Struct Initialize */
I2C_DeInit(I2C3);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C3, &I2C_InitStructure);
/* I2C Initialize */
I2C_Cmd(I2C3, ENABLE);
/*超時設置*/
RCC_GetClocksFreq(&rcc_clocks);
ulTimeOut_Time = (rcc_clocks.SYSCLK_Frequency /10000);
}
注意,I2C一定要加上超時的設置,否則當IIC總線出錯時,沒有超時檢測可能造成MCU卡死在這里。
STM32F407的硬件I2C讀一個字節(jié)與STM32F103類似,代碼如下:
unsigned char I2C_Err=0;
uint8_t I2C_ReadOneByte(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t Reg_addr)
{
uint8_t readout;
u32 tmr;
tmr = ulTimeOut_Time;
while((--tmr)&&I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
if(tmr==0) I2C_Err = 1;
I2C_GenerateSTART(I2Cx, ENABLE);
//發(fā)送I2C的START信號,接口自動從從設備編程主設備
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)));
if(tmr==0) I2C_Err = 1;
I2C_Send7bitAddress(I2Cx,I2C_Addr,I2C_Direction_Transmitter);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)));
if(tmr==0) I2C_Err = 1;
I2C_SendData(I2Cx, Reg_addr);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
if(tmr==0) I2C_Err = 1;
I2C_GenerateSTART(I2Cx, ENABLE);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)));
if(tmr==0) I2C_Err = 1;
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Receiver);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)));
if(tmr==0) I2C_Err = 1;
I2C_AcknowledgeConfig(I2Cx, DISABLE);
I2C_GenerateSTOP(I2Cx, ENABLE);
tmr = ulTimeOut_Time;
while((--tmr)&&(!(I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)))); /* EV7 */
if(tmr==0) I2C_Err = 1;
readout = I2C_ReceiveData(I2Cx);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
return readout;
}
I2C_ReadOneByte函數(shù)輸入?yún)?shù)有3個,分別是:I2Cx,表示使用的I2C編號,I2C_addr,表示從設備的I2C地址,reg_addr,表示要讀取的從設備寄存器地址。I2C_ReadOneByte的返回值就是I2C總線上讀取到的數(shù)據(jù)。讀I2C數(shù)據(jù)后需要檢查I2C_Err是否為0,若為0,表示讀取I2C數(shù)據(jù)時出錯(超時),讀到的數(shù)據(jù)可能不正確。
向從設備寫入一個字節(jié)的數(shù)據(jù),代碼如下:
unsigned char I2C_Err=0;
void I2C_WriteOneByte(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t Reg_addr,uint8_t value)
{
u32 tmr;
tmr = ulTimeOut_Time;
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
while((--tmr)&&I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
if(tmr==0) I2C_Err = 1;
I2C_GenerateSTART(I2Cx, ENABLE);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)));
if(tmr==0) I2C_Err = 1;
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)));
if(tmr==0) I2C_Err = 1;
I2C_SendData(I2Cx, Reg_addr);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
if(tmr==0) I2C_Err = 1;
I2C_SendData(I2Cx, value);
tmr = ulTimeOut_Time;
while((--tmr)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
if(tmr==0) I2C_Err = 1;
I2C_GenerateSTOP(I2Cx, ENABLE);
//I2C_AcknowledgeConfig(I2Cx, DISABLE);
}
寫I2C函數(shù)比讀僅多了一個輸入?yún)?shù),即要寫入的數(shù)據(jù)。同理,在函數(shù)執(zhí)行完成后,需要檢查I2C_Err是否為0以判斷I2C是否出錯。
有了讀1個字節(jié)和寫入1個字節(jié)的函數(shù),當需要一次性讀或寫多個數(shù)據(jù)時,可以在上述函數(shù)的基礎上擴展多字節(jié)操作。