ch32v203使用IIC,一直卡在I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED這步驟

使用ch32v203,一直卡在"I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED",這步驟,會是什么原因造成呢,使用的歷程是使用iic寫AT24C08,

void?AT24CXX_WriteOneByte(u16?WriteAddr,?u8?DataToWrite)
{
	while(?I2C_GetFlagStatus(?I2C1,?I2C_FLAG_BUSY?)?!=?RESET?);
	I2C_GenerateSTART(?I2C1,?ENABLE?);
	printf("111111111\r\n");
	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_MODE_SELECT?)?);
	I2C_Send7bitAddress(?I2C1,?0XA0,?I2C_Direction_Transmitter?);
????printf("22222222\r\n");

	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED?)?);
????printf("3333333\r\n");

#if?(Address_Lenth??==?Address_8bit)
	I2C_SendData(?I2C1,?(u8)(WriteAddr&0x00FF)?);
	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED?)?);
????printf("444444\r\n");

#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?);
	}
????printf("555555\r\n");

	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED?)?);
	I2C_GenerateSTOP(?I2C1,?ENABLE?);
}



熱門產(chǎn)品 : USB3.0 HUB控制器:CH634

您好,按照下圖在標(biāo)注出加個延時即可解決問題,卡死原因是由于在判斷EV6事件的時候,EEPROM此時還在寫入數(shù)據(jù),我們發(fā)送了地址過去,但并沒有產(chǎn)生應(yīng)答

image.png


按照你的方式,添加了延時,還是一樣卡死。使用的是ch32v203的demo?board,使用的是PB6,PB7這組IIC,目的是像驗證硬件IIC的通訊是否正常。IIC的初始化的代碼如下

void?IIC_Init(u32?bound,?u16?address)
{
	GPIO_InitTypeDef?GPIO_InitStructure={0};
	I2C_InitTypeDef?I2C_InitTSturcture={0};

????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,?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_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_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_AcknowledgedAddress?=?I2C_AcknowledgedAddress_7bit;
	I2C_Init(?I2C1,?&I2C_InitTSturcture?);

	I2C_Cmd(?I2C1,?ENABLE?);

	I2C_AcknowledgeConfig(?I2C1,?ENABLE?);
}

/*********************************************************************
?*?@fn??????AT24CXX_Init
?*
?*?@brief???Initializes?AT24xx?EEPROM.
?*
?*?@return??none
?*/
void?AT24CXX_Init(void)
{
	IIC_Init(?100000,?0xA0);
}

更改過后的寫代碼如下

void?AT24CXX_WriteOneByte(u16?WriteAddr,?u8?DataToWrite)
{
	while(?I2C_GetFlagStatus(?I2C1,?I2C_FLAG_BUSY?)?!=?RESET?);

????printf("Generate?Start\r\n");
	I2C_GenerateSTART(?I2C1,?ENABLE?);
	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_MODE_SELECT?)?)
	????;

????printf("Send?Address\r\n");
	I2C_Send7bitAddress(?I2C1,?0XA0,?I2C_Direction_Transmitter?);
????Delay_Ms(30);
	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED?)?)
	;
????printf("Send?Reg\r\n");
#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
????printf("Send?Reg?Data\r\n");
	if(?I2C_GetFlagStatus(?I2C1,?I2C_FLAG_TXE?)?!=??RESET?)
	{
		I2C_SendData(?I2C1,?DataToWrite?);
	}
	while(?!I2C_CheckEvent(?I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED?)?);

????printf("Generate?Stop\r\n");
	I2C_GenerateSTOP(?I2C1,?ENABLE?);
}



您好,附件例程為我的測試?yán)蹋凑漳愕呐渲靡蟾臑镻B6、PB7引腳,測試沒問題,你可以參考一下。注意IIC引腳要接上拉電阻

icon_rar.gifCH32V203 IIC_EEPROM.zip

image.png


只有登錄才能回復(fù),可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频