【RISC-V MCU CH32V103测评】IIC硬件测试(二)
[复制链接]
经过WCH的技术人员的指导,发现了我程序中的问题。我的程序前后使用的I2C设备不一致,文后我把正确的文件传上来,供大家参考。
我有检查了SPI的程序但还是没有进展。我在努力一下看看是怎么回事。
这是这次的照片,可以看到是正确的。而且刷新速度还是不错的。下面把程序的主要测试步骤说一下。
/*******************************************************************************
* Function Name : IIC_Init
* Description : Initializes the IIC peripheral.
* Input : None
* Return : None
*******************************************************************************/
void IIC_Init( u32 bound, u16 address )
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitTSturcture;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOB, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOB, &GPIO_InitStructure );
I2C_InitTSturcture.I2C_ClockSpeed = bound;
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitTSturcture.I2C_OwnAddress1 = address;
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;//
//I2C_InitTSturcture.I2C_Ack = I2C_Ack_Disable;//
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init( I2C1, &I2C_InitTSturcture );
I2C_Cmd(I2C1, ENABLE );
I2C_AcknowledgeConfig( I2C1, ENABLE );
}
I2C初始化程序没有错误和上次一样。但是写数据程序有错误,这次的如下
void Write_IIC_Byte(u16 WriteAddr,u8 DataToWrite)
{
while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );
I2C_GenerateSTART( I2C1, ENABLE );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
I2C_Send7bitAddress( I2C1, 0x78, I2C_Direction_Transmitter );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );
#if (Address_Lenth == Address_8bit)
I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
#elif (Address_Lenth == Address_16bit)
I2C_SendData( I2C1, (u8)(WriteAddr>>8) );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
#endif
if( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) != RESET )
{
I2C_SendData( I2C1, DataToWrite);
}
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
I2C_GenerateSTOP( I2C1, ENABLE );
}
上次是把没有把I2C2替换,造成了测试失败。
void Write_IIC_Byte(u16 WriteAddr,u8 DataToWrite)
{
while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );
I2C_GenerateSTART( I2C1, ENABLE );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
I2C_Send7bitAddress( I2C1, 0x78, I2C_Direction_Transmitter );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );
#if (Address_Lenth == Address_8bit)
I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
#elif (Address_Lenth == Address_16bit)
I2C_SendData( I2C1, (u8)(WriteAddr>>8) );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
#endif
if( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) != RESET )
{
I2C_SendData( I2C1, DataToWrite);
}
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
I2C_GenerateSTOP( I2C1, ENABLE );
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
Write_IIC_Byte(0x00,IIC_Command);
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
Write_IIC_Byte(0x40,IIC_Data);
}
三个主要的函数没有任何改变。
上次测试的主要失败原因是,没有把程序好好的梳理,再次感谢网友@我芯永恒
IIC_SSD1306.zip
(147.04 KB, 下载次数: 2)
|