以下是代码:
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,这是怎末回事?求大神解答。 |