STM32F407作为主机写的代码如下所示:
以下代码经验证可以正常写数据到MCP4017.
static uint32_t GetI2C3TimeLimit(void)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
return RCC_Clocks.SYSCLK_Frequency/10000;
}
int32_t IIC3_WriteOneByte(uint8_t devAdder, uint8_t value)
{
uint32_t TimeLimit = GetI2C3TimeLimit();
uint32_t Timetmp = TimeLimit;
while((--Timetmp)&&(I2C_GetFlagStatus(IIC, I2C_FLAG_BUSY)));
if(Timetmp == 0)
return -1;//i2c busy
I2C_GenerateSTART(IIC, ENABLE);
Timetmp = TimeLimit;
while((--Timetmp)&&(!I2C_CheckEvent(IIC,I2C_EVENT_MASTER_MODE_SELECT)));//EV5
//while((--Timetmp)&&(I2C_GetFlagStatus(IIC, I2C_FLAG_SB) == RESET));//EV5
if(Timetmp == 0)
return -2;//i2c send start fail
I2C_Send7bitAddress(IIC, devAdder, I2C_Direction_Transmitter);//
Timetmp = TimeLimit;
//while((--Timetmp)&&(I2C_GetFlagStatus(IIC, I2C_FLAG_ADDR) == RESET));//EV6
while((--Timetmp)&&(!I2C_CheckEvent(IIC,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)));//EV6
if(Timetmp == 0)
return -3;//i2c send address failed
I2C_SendData(IIC, value);
Timetmp = TimeLimit;
while((--Timetmp)&&(!I2C_CheckEvent(IIC,I2C_EVENT_MASTER_BYTE_TRANSMITTED)));//
if(Timetmp == 0)
return -6;//i2c send data failed
//--SEND STOP
I2C_GenerateSTOP(IIC, ENABLE);
Timetmp = TimeLimit;
while((--Timetmp)&&(I2C_GetFlagStatus(IIC, I2C_FLAG_BUSY)));
return 1;
}
|