jpublic2008 发表于 2016-6-4 22:31:08

stm32f4串口发数,用pc接收的数据不对

stm32F427发送,用232转usb接到电脑上,用串口调试助手接收,发送6个字节是{3F,01,03,04,00,校验和},电脑接收的6个字节是{60 7F 7E DF 71 00},求助了。

程序代码如下:
void Uart_init(void)
{
    USART_InitTypeDef USART_InitStructure;
   GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;//RX
   GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;   //TX
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
USART_InitStructure.USART_BaudRate   = 115200;
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(USART3, &USART_InitStructure);

USART_Cmd(USART3, ENABLE);
}
void Usart3_SendData(USART_TypeDef* USARTx, unsigned char TempData)
{
    while(!(USARTx->SR&0x80));
//while(!(USARTx->SR&0x40)){};
//while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET){}
    USARTx->DR = (TempData & (uint16_t)0x01FF);
//while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET){}
//while((USARTx->SR&0x40)==0);
}

void usart3_send_data(unsigned char Send_number,unsigned char *Send_data)
{
unsigned char i;
for(i=0;i<Send_number;i++)
{

Usart3_SendData(USART3, Send_data);
//delay_ms(2);
}
}

void Sensor_Data_Generate_Send(void)
{
    unsigned char i=0;
Data_Buffer=0x3F;
Data_Buffer=0x01;
Data_Buffer=0x03;
Data_Buffer=0x04;
for(i=4;i<5;i++)
{
Data_Buffer=0x00;
}
   
    CHK_SUM=0;
   
    for(i=0;i<5;i++)
      CHK_SUM+=Data_Buffer;
      
    Data_Buffer=CHK_SUM % 255;


usart3_send_data(6, Data_Buffer);
      
}

jpublic2008 发表于 2016-6-5 08:55:33

盼回复,多谢了,调试了好久

xmshao 发表于 2016-6-5 15:56:35

注意波特率等配置参数一致;

另外,硬件上也确认下。

jpublic2008 发表于 2016-6-5 20:14:13

首先多谢回复。波特率应该没有问题,硬件上看什么呢?

lzts 发表于 2016-6-7 09:08:18

串口这玩意, 调试时端口配置时要普通模式, 复用模式换着试下. 硬件的话就是外部晶振了. 晶振的下地电容, 还是晶振损坏都会影响的

发表于 2016-6-7 11:25:33

波特率不对,楼主用示波器看一下波特率是多少。还有主频时钟的配置。

pener 发表于 2016-6-7 18:34:35

我也是401的串口弄不出来,直接用103的移过来也是不行

eefishing 发表于 2016-7-10 13:57:58

问题找到了吗?
页: [1]
查看完整版本: stm32f4串口发数,用pc接收的数据不对