stm32 串口通讯不成功的解决办法
扫描二维码
随时随地手机看文章
首先要注意所用到的USART是否用到了复用功能
千万别忘了打开复用时钟!!!!!!!!!
代码如下:Hello! everyone,welcome to class!
#include
void delay_ms(u16 x)
{
u8 t;
while(x--){for(t=0;t<120;t++);}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
}
void uart_init(u32 bound)
{
USART_InitTypeDef USART_InitStructure;//
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl= USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
int main(void)
{
u8 tab[]="nHello! everyone,welcome to class!";
u8 i;
GPIO_Configuration();
uart_init(9600);
while(1)
{
for(i=0;i<33;i++)
{
USART_SendData(USART3, tab[i]);
delay_ms(3000);
while((USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET));
}
}
}