极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9449|回复: 0

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

[复制链接]
发表于 2016-6-5 15:30:49 | 显示全部楼层 |阅读模式
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[i]);
   //delay_ms(2);  
}
}

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

usart3_send_data(6, Data_Buffer);
      
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-24 09:40 , Processed in 0.040271 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表