程序用的书上的程序,板子是stm32f030c8板子。数据读取是空白的。不知道是那边出错了。
void I2C_Conf()
{
//I2C所使用I/O的配置
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//I/O口映射到I2C2外设
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);
//I/O口映射到I2C2外设
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_1);
//I2C的初始化
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_Timing = 0x00210507;
//使能I2C
I2C_Cmd(I2C2, ENABLE);
I2C_Init(I2C2, &I2C_InitStructure);
}
//******************************************************************************
//函数名 : viod I2C_Write(uchar data, uchar WriteAddr)
//功能描述 : I2C的指定位置写入数据
//输入 : MemoryAddress:要写入数据的地址;DeviceAddress:物理地址;Data:需要写入的数据
//输出 : 无
//返回 : 无
//******************************************************************************
void I2C2_Write(uint8_t DeviceAddress, uint8_t MemoryAddress, uint8_t Data)
{
while(I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));
I2C_TransferHandling(I2C2,DeviceAddress,1,I2C_Reload_Mode,I2C_Generate_Start_Write); // I2C_SoftEnd_Mode
while(I2C_GetFlagStatus(I2C2,I2C_ISR_TXIS) == RESET);
I2C_SendData(I2C2,MemoryAddress);
while(I2C_GetFlagStatus(I2C2,I2C_ISR_TCR) == RESET);
I2C_TransferHandling(I2C2,DeviceAddress,1,I2C_AutoEnd_Mode,I2C_No_StartStop); // I2C_Generate_Start_Write
while(I2C_GetFlagStatus(I2C2,I2C_ISR_TXIS) == RESET);
I2C_SendData(I2C2, Data);
while(I2C_GetFlagStatus(I2C2,I2C_ISR_STOPF) == RESET);
for(uint32_t i=0;i<20000;i++);
I2C_ClearFlag(I2C2, I2C_ICR_STOPCF);
}
//******************************************************************************
//函数名 : unsigned char I2C2_Read(uchar ReadAddr)
//功能描述 : 读指定位置的数据;
//输入 : DeviceAddress:物理地址; MemoryAddress:读取数据的地址;
//输出 : 无
//返回 : 读取的数据
//******************************************************************************
uint8_t I2C2_Read(uint8_t DeviceAddress, uint8_t MemoryAddress)
{
uint8_t Data;
I2C_TransferHandling(I2C2,DeviceAddress,1,I2C_SoftEnd_Mode,I2C_Generate_Start_Write); // I2C_SoftEnd_Mode
while(I2C_GetFlagStatus(I2C2,I2C_ISR_TXIS) == RESET);
I2C_SendData(I2C2,MemoryAddress);
while(I2C_GetFlagStatus(I2C2,I2C_ISR_TC) == RESET);
I2C_TransferHandling(I2C2,DeviceAddress,1,I2C_AutoEnd_Mode,I2C_Generate_Start_Read); // I2C_Generate_Start_read
while(I2C_GetFlagStatus(I2C2,I2C_ISR_RXNE) == RESET);
Data = I2C_ReceiveData(I2C2);
while(I2C_GetFlagStatus(I2C2,I2C_ISR_STOPF) == RESET);
I2C_ClearFlag(I2C2, I2C_ICR_STOPCF);
return Data;
} |
|