最后一个形参默认填Disable,这个参数是配置这个寄存器的b8位(副功能许可)的。
接下来,贴出我的初始化代码:
uint8_t SMBus_Init(uint8_t SMBusNum)
{
stc_i2c_init_t stcI2cInit;
stc_clk_freq_t stcClkFreq;
stc_i2c_smbus_init_t stcSmBusInit;
/* Initialize I2C port*/
//根据要启用的具体某一组SMBus,配置其引脚为I2C3_SCL和I2C3_SDA功能,并把另外一组引脚配置为普通的GPIO
if(SMBusNum == SMBus_Num1)
{
PORT_SetFunc(SMBUS_SCL_PORT, SMBUS_2_SCL_PIN, Func_Gpio, Disable);
PORT_SetFunc(SMBUS_SDA_PORT, SMBUS_2_SDA_PIN, Func_Gpio, Disable);
PORT_SetFunc(SMBUS_SCL_PORT, SMBUS_1_SCL_PIN, Func_I2c3_Scl, Disable);
PORT_SetFunc(SMBUS_SDA_PORT, SMBUS_1_SDA_PIN, Func_I2c3_Sda, Disable);
}
else
{
PORT_SetFunc(SMBUS_SCL_PORT, SMBUS_1_SCL_PIN, Func_Gpio, Disable);
PORT_SetFunc(SMBUS_SDA_PORT, SMBUS_1_SDA_PIN, Func_Gpio, Disable);
PORT_SetFunc(SMBUS_SCL_PORT, SMBUS_2_SCL_PIN, Func_I2c3_Scl, Disable);
PORT_SetFunc(SMBUS_SDA_PORT, SMBUS_2_SDA_PIN, Func_I2c3_Sda, Disable);
}
/* Enable I2C Peripheral*/
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_I2C3, Enable);
I2C_DeInit(I2C_CH); //初始化之前先复位I2C
/* Get system clock frequency */
CLK_GetClockFreq(&stcClkFreq);
MEM_ZERO_STRUCT(stcI2cInit);
stcI2cInit.u32Baudrate = I2C_BAUDRATE; //宏定义,80
stcI2cInit.u32Pclk3 = stcClkFreq.pclk3Freq;
stcI2cInit.enI2cMode = I2cMaster; //主设备
stcI2cInit.u32SclTime = 0ul;
I2C_Init(I2C_CH, &stcI2cInit); //I2C初始化
MEM_ZERO_STRUCT(stcSmBusInit);
stcSmBusInit.enHostAdrMatchFunc = Disable;
stcSmBusInit.enDefaultAdrMatchFunc = Disable;
stcSmBusInit.enAlarmAdrMatchFunc = Disable;
I2C_SmbusConfig(I2C_CH, &stcSmBusInit);
使能I2C功能
I2C_Cmd(I2C_CH, Enable);
//使能SMBus功能
I2C_SmBusCmd(I2C_CH,Enable);
return I2C_RET_OK;
}
|