ch32v203f8p6 使用i2c時(shí)卡死在while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );

ch32v203f8p6 使用i2c時(shí)卡死在while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );。

我使用的是官方給的例程,原本i2c兩個(gè)端口一個(gè)拉高一個(gè)拉低,通過(guò)看其他帖子發(fā)現(xiàn)需要把i2c上拉,就自己焊了電阻連到vcc上,現(xiàn)在空閑時(shí)是sda(pb7)大概4v,scl(pb6)大概5v。(因?yàn)槲夜╇娛?v,在板子上也把5v和vcc用跳線帽連起來(lái)了)。請(qǐng)問(wèn)這個(gè)是什么問(wèn)題?

代碼如下所示:

/********************************** (C) COPYRIGHT *******************************

?* File Name? ? ? ? ? : main.c

?* Author? ? ? ? ? ? ?: WCH

?* Version? ? ? ? ? ? : V1.0.0

?* Date? ? ? ? ? ? ? ?: 2021/06/06

?* Description? ? ? ? : Main program body.

?*********************************************************************************

?* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.

?* Attention: This software (modified or not) and binary are used for?

?* microcontroller manufactured by Nanjing Qinheng Microelectronics.

?*******************************************************************************/


/*

?*@Note

?*7-bit addressing mode, master/slave mode, transceiver routine:

?*I2C1_SCL(PB8)\I2C1_SDA(PB9).

?*This routine demonstrates that Master sends and Slave receives.

?*Note: The two boards download the Master and Slave programs respectively,

?*and power on at the same time.

?*? ? Hardware connection:PB8 -- PB8

?*? ? ? ? ? ? ? ? ? ? ? ? PB9 -- PB9

?*

?*/


#include "debug.h"


/* I2C Mode Definition */

#define HOST_MODE? ?0

#define SLAVE_MODE? ?1


/* I2C Communication Mode Selection */

#define I2C_MODE? ?HOST_MODE

//#define I2C_MODE? ?SLAVE_MODE


/* Global define */

#define Size? ?6

#define RXAdderss? ?0x02

#define TxAdderss? ?0x02


/* Global Variable */

u8 TxData[Size] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };

u8 RxData[5][Size];


/*********************************************************************

?* @fn? ? ? IIC_Init

?*

?* @brief? ?Initializes the IIC peripheral.

?*

?* @return? none

?*/

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 );

GPIO_PinRemapConfig(GPIO_Remap_I2C1, 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_16_9;

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 );


#if (I2C_MODE == HOST_MODE)

I2C_AcknowledgeConfig( I2C1, ENABLE );


#endif

}


/*********************************************************************

?* @fn? ? ? main

?*

?* @brief? ?Main program.

?*

?* @return? none

?*/

int main(void)

{

? ? u8 i = 0;

u8 j = 0;

u8 p = 0;

? ? SystemCoreClockUpdate();

? ? Delay_Init();

? ? USART_Printf_Init( 115200 );

? ? printf( "SystemClk:%d\r\n", SystemCoreClock );

? ? printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );


#if (I2C_MODE == HOST_MODE)

? ? printf("IIC Host mode\r\n");

IIC_Init( 80000, TxAdderss);


for( j =0; j < 5; j++)

{

? ? while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );

? ? printf("I2C_GetFlagStatus\r\n");

I2C_GenerateSTART( I2C1, ENABLE );

printf("I2C_GenerateSTART\r\n");

while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );

printf("I2C_EVENT_MASTER_MODE_SELECT\r\n");

I2C_Send7bitAddress( I2C1, 0x02, I2C_Direction_Transmitter );

printf("I2C_Send7bitAddress\r\n");

? ? while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );

? ? printf("I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED\r\n");

? ? for( i=0; i< 6;i++ )

? ? {

? ? ? ? if( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) !=? RESET )

? ? ? ? {

? ? Delay_Ms(100);

? ? ? ? ? ? I2C_SendData( I2C1, TxData[i] );

? ? ? ? }

? ? }


? ? while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

? ? I2C_GenerateSTOP( I2C1, ENABLE );

Delay_Ms(1000);

}


#elif (I2C_MODE == SLAVE_MODE)

printf("IIC Slave mode\r\n");

IIC_Init( 80000, RXAdderss);


for( p =0; p < 5; p++)

{

? ? i = 0;

while( !I2C_CheckEvent( I2C1, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ) );

? ? while( i < 6 )

? ? {

? ? ? ? if( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) !=? RESET )

? ? ? ? {

? ? ? ? ? ? RxData[p][i] = I2C_ReceiveData( I2C1 );

? ? ? ? ? ? i++;

? ? ? ? }

? ? }

? ? ? ? I2C1->CTLR1 &= I2C1->CTLR1;

? ? }

? ? printf( "RxData:\r\n" );

for(p=0; p<5; p++)

? ?{

? ? ? ? for( i = 0; i < 6; i++ )

{

? ? ? ? ? ?printf( "%02x ", RxData[p][i] );

}

? ?printf( "\r\n ");

}

?

?

#endif


while(1);

}


測(cè)試板是在淘寶網(wǎng)微嵌電子買(mǎi)的,原理圖:image.png


您好,PB6、PB7引腳是IIC1的默認(rèn)引腳,不需要開(kāi)啟重映射功能,可將下圖圈出部分去掉試一下。此外注意一下PB6、PB7引腳是和PA13、PA14共用同一個(gè)引腳的,注意不要同時(shí)使用。注意一下工程文件的配置,ld文件中FLASH、RAM的大小配置以及啟動(dòng)文件的選擇。后續(xù)若有問(wèn)題,可通過(guò)郵箱(lzs@wch.cn)和我溝通。

image.png


原問(wèn)題已經(jīng)解決了,感謝!不過(guò)又出現(xiàn)了一些新問(wèn)題。(也給您在郵件發(fā)了一份)

測(cè)試條件:兩個(gè)ch32v203f8p6,一個(gè)做master,一個(gè)做slave。

測(cè)試的時(shí)候發(fā)現(xiàn),master在發(fā)送完一輪數(shù)據(jù),完成I2C_GenerateSTOP( I2C1, ENABLE );這個(gè)語(yǔ)句之后,我本來(lái)是想重新開(kāi)啟一輪發(fā)送數(shù)據(jù)的,也就是說(shuō)又進(jìn)入while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );這一句了。這個(gè)時(shí)候又出現(xiàn)了無(wú)法通過(guò)這一句代碼的狀況。我用電壓表測(cè)了一下,現(xiàn)在sda和scl都處于低電平,只有我把slave和master斷開(kāi)i2c的連接,電平才恢復(fù)正常。這是什么問(wèn)題?

代碼如下:

/********************************** (C) COPYRIGHT *******************************

?* File Name? ? ? ? ? : main.c

?* Author? ? ? ? ? ? ?: WCH

?* Version? ? ? ? ? ? : V1.0.0

?* Date? ? ? ? ? ? ? ?: 2021/06/06

?* Description? ? ? ? : Main program body.

?*********************************************************************************

?* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.

?* Attention: This software (modified or not) and binary are used for?

?* microcontroller manufactured by Nanjing Qinheng Microelectronics.

?*******************************************************************************/


/*

?*@Note

?*7-bit addressing mode, master/slave mode, transceiver routine:

?*I2C1_SCL(PB8)\I2C1_SDA(PB9).

?*This routine demonstrates that Master sends and Slave receives.

?*Note: The two boards download the Master and Slave programs respectively,

?*and power on at the same time.

?*? ? Hardware connection:PB8 -- PB8

?*? ? ? ? ? ? ? ? ? ? ? ? PB9 -- PB9

?*

?*/


#include "debug.h"


/* I2C Mode Definition */

#define HOST_MODE? ?0

#define SLAVE_MODE? ?1


/* I2C Communication Mode Selection */

//#define I2C_MODE? ?HOST_MODE

#define I2C_MODE? ?SLAVE_MODE


/* Global define */

#define Size? ?6

#define RXAdderss? ?0x02

#define TxAdderss? ?0x02


/* Global Variable */

u8 TxData[Size] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };

u8 RxData[5][Size];


/*********************************************************************

?* @fn? ? ? IIC_Init

?*

?* @brief? ?Initializes the IIC peripheral.

?*

?* @return? none

?*/


void GPIO_Toggle_INIT(void)

{

? ? GPIO_InitTypeDef GPIO_InitStructure = {0};


? ? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

? ? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

? ? GPIO_Init(GPIOA, &GPIO_InitStructure);

? ? Delay_Ms(10);

? ? GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);

}


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 );

// GPIO_PinRemapConfig(GPIO_Remap_I2C1, 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_16_9;

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 );


#if (I2C_MODE == HOST_MODE)

I2C_AcknowledgeConfig( I2C1, ENABLE );


#endif

}


/*********************************************************************

?* @fn? ? ? main

?*

?* @brief? ?Main program.

?*

?* @return? none

?*/

int main(void)

{

? ? u8 i = 0;

u8 j = 0;

u8 p = 0;

? ? SystemCoreClockUpdate();

? ? Delay_Init();

? ? USART_Printf_Init( 115200 );

? ? printf( "SystemClk:%d\r\n", SystemCoreClock );

? ? printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );


#if (I2C_MODE == HOST_MODE)

? ? printf("IIC Host mode\r\n");

//? ? GPIO_Toggle_INIT();


? ? Delay_Ms(10);

IIC_Init( 80000, TxAdderss);


for( j =0; j < 5; j++)

{


? ? while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );

? ? printf("I2C_GetFlagStatus\r\n");

I2C_GenerateSTART( I2C1, ENABLE );

printf("I2C_GenerateSTART\r\n");

while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );

printf("I2C_EVENT_MASTER_MODE_SELECT\r\n");

Delay_Ms(10);

I2C_Send7bitAddress( I2C1, 0x02, I2C_Direction_Transmitter );

printf("I2C_Send7bitAddress\r\n");

Delay_Ms(10);

? ? while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ){

? ? ? ? Delay_Ms(10);

? ? }

? ? printf("I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED\r\n");

? ? for( i=0; i< 6;i++ )

? ? {

? ? ? ? if( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) !=? RESET )

? ? ? ? {

? ? Delay_Ms(100);

? ? ? ? ? ? I2C_SendData( I2C1, TxData[i] );

? ? ? ? ? ? printf("I2C_SendData\r\n");

? ? ? ? }

? ? }


? ? while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

? ? printf("I2C_EVENT_MASTER_BYTE_TRANSMITTED\r\n");

? ? I2C_GenerateSTOP( I2C1, ENABLE );

? ? printf("I2C_GenerateSTOP\r\n");

Delay_Ms(1000);

}


#elif (I2C_MODE == SLAVE_MODE)

printf("IIC Slave mode\r\n");

IIC_Init( 80000, RXAdderss);


for( p =0; p < 5; p++)

{

? ? i = 0;

while( !I2C_CheckEvent( I2C1, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ) );

printf("I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED\r\n");

? ? while( i < 6 )

? ? {

? ? ? ? if( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) !=? RESET )

? ? ? ? {

? ? ? ? ? ? RxData[p][i] = I2C_ReceiveData( I2C1 );

? ? ? ? ? ? printf("I2C_ReceiveData\r\n");

? ? ? ? ? ? i++;

? ? ? ? }

? ? }

? ? while( !I2C_CheckEvent( I2C1, I2C_EVENT_SLAVE_STOP_DETECTED ) );

? ? printf("I2C_EVENT_SLAVE_STOP_DETECTED\r\n");

? ? I2C1->CTLR1 &= I2C1->CTLR1;

? ? printf("I2C1->CTLR1 &= I2C1->CTLR1;\r\n");

? ? }

? ? printf( "RxData:\r\n" );

for(p=0; p<5; p++)

? ?{

? ? ? ? for( i = 0; i < 6; i++ )

{

? ? ? ? ? ?printf( "%02x ", RxData[p][i] );

}

? ?printf( "\r\n ");

}

?

?

#endif


while(1);

}



您好,已郵件回復(fù)你,后續(xù)問(wèn)題可繼續(xù)通過(guò)郵箱溝通。


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

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