初学STM32的串口实验,在接收时出现以下的乱码?是什么原因呢
[复制链接]
这是我接收到得乱码截图,请各位大侠指教下是什么原因呢?谢谢了啊
附上程序:
#include "stm32f10x.h" GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
unsigned char TxBuf1[100] = "神州三号,串口测试程序"; unsigned char i;
void gpio_init()
{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA , &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } void usart_init()
{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = 115200; //设置波特率为115200// USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置数据位为8位// USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置停止位为1位// USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位// USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送与接收// USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None; //没有硬件流控// USART_Init(USART1, &USART_InitStructure); //对串口1进行初始化 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收的中断使能打开: USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //发送的中断使能打开 USART_Cmd(USART1 , ENABLE);
}
int main() { usart_init(); gpio_init(); for( i = 0; TxBuf1 != '\0'; i++) { USART_SendData(USART1 , TxBuf1); while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); } }
|