野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11933|回复: 1

初学者刚学几天,为什么串口发送与接收的数据不一样?

[复制链接]
发表于 2020-3-12 14:23:07 | 显示全部楼层 |阅读模式
以下是代码:
bsp _USART.h
#ifndef BSP_USART_H
#define BSP_USART_H
#include "stm32f10x.h"=
void NVIC_Config();
void USART_SendByte(USART_TypeDef* USARTx,uint8_t Data);
#endif


bsp_USART.c
#include "bsp_USART.h"
#include "stm32f10x.h"

static void NVIC_Config()
{
   NVIC_InitTypeDef  NVIC_InitStruct;        
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
         NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
         NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
         NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
         NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
         NVIC_Init( &NVIC_InitStruct);
        
}
void USART_InitConfig()
{
  GPIO_InitTypeDef GPIO_InitStruct;
        USART_InitTypeDef USART_InitStruct;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
        
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
        GPIO_InitStruct.GPIO_Mode= GPIO_Mode_AF_PP;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
        
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
        
        
        USART_InitStruct.USART_BaudRate=115200;
        USART_InitStruct.USART_WordLength=USART_WordLength_8b;
        USART_InitStruct.USART_Parity= USART_Parity_No ;
        USART_InitStruct.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
        USART_InitStruct.USART_StopBits=USART_StopBits_1 ;
        USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_Init(USART1, &USART_InitStruct);
        NVIC_Config();
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_Init(USART1, &USART_InitStruct);
        USART_Cmd(USART1, ENABLE);        
}


void USART_SendByte(USART_TypeDef* USARTx,uint8_t Data)
{
  USART_SendData(USARTx,Data);
        while(USART_GetFlagStatus( USARTx, USART_FLAG_TXE)==RESET);
}


main.c
#include "stm32f10x.h"
#include "bsp_USART.h"
int main(void)
{
   USART_InitConfig();
         USART_SendByte(USART1,'0x35');
         
}

例如,发送“0x15”,发送后,在串口调试助手中16进制显示勾选的情况下,接收到的是35,这是怎末回事?求大神解答。
回复

使用道具 举报

 楼主| 发表于 2020-3-12 14:45:44 | 显示全部楼层
已经解决了,是串口调试助手有BUG,已经解决
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-19 18:17 , Processed in 0.040990 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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