下面是
STM32f411的串口数据发送代码,上位机通过串口调试助手进行接收,始终无法得到正确的数据,请大神们帮忙看一下:
#include <stdio.h>
#include "main.h"
void USART1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//PB6->TX PB7->Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
}
void USART1_Config(void)
{
USART1_GPIO_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;//9600;
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
int fputc(int ch,FILE *f)
{
USART1->SR;
//一个一个发送字符
USART_SendData(USART1, (unsigned char) ch);
//等待发送完成
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
return(ch);
}
int main(void)
{
USART1_Config();
printf("hello world!");
}
收到了数据,但是与发送的不一致。比如发送字符a,得到的数据以十六进制显示为1C 00 FC,令人不解
一周热门 更多>