stm32串口2 发送数据正确但是接收数据不正确 中断方式

2019-03-23 18:47发布

/* Includes ------------------------------------------------------------------*/
#include "main.h"

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

/************************类型函数********************************/
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;

/************************数据定义********************************/
unsigned char RxData;
unsigned char TxData;

/************************定义函数********************************/
void RCC_Configuration (void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Uart_Configuration(void);

/****************************************************************
函数名:串口1接受中断函数
功能:接收到什么就将什么发送回去
参数:输入:无
输出:无
数据处理:无
需要的时间:需要的时间=
版本:
最后修改时间:
作者:
***********************************************************/

void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
/****************************************************************
函数名:总初始实话函数
功能:初始化
参数:输入:无
输出:无
数据处理:无
需要的时间:需要的时间=
版本:
最后修改时间:
作者:
***********************************************************/
void stm32_Init (void)
{
RCC_Configuration ();//时钟配置
Uart_Configuration();//串口初始化
NVIC_Configuration();//中断配置
}


int main (void)
{
stm32_Init();
printf(" USART2 IS OK ");
while(1)
{

}
}

/****************************************************************
函数名:时钟初始实话函数
功能:时钟初始化
参数:输入:无
输出:无
数据处理:无
需要的时间:需要的时间=
版本:
最后修改时间:
作者:
***********************************************************/
void RCC_Configuration (void)
{
RCC_DeInit();//将RCC外设寄存器设置为缺省值
RCC_HSEConfig(RCC_HSE_ON);//设置外部晶振
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能预取缓冲区
FLASH_SetLatency(FLASH_Latency_2); //Flash2个等待周期
RCC_HCLKConfig(RCC_SYSCLK_Div1);//AHB时钟 = 系统时钟
RCC_PCLK1Config(RCC_HCLK_Div2);//APB1时钟 = HCLK / 2
RCC_PCLK2Config(RCC_HCLK_Div1);//APB2时钟 = HCLK
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//设置PLL时钟源为外部的8M,倍频6倍即为72M
RCC_PLLCmd(ENABLE);//使能PLL时钟

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//等待PLL准备好
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);// 选择PLL做为系统时钟源
while(RCC_GetSYSCLKSource() != 0x08);//
}

//SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //SysTick时钟源为AHB时钟除以8,及为9M

/************************使能外设时钟********************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//使能GPIOA时钟

}

/****************************************************************
函数名:中断初始实话函数
功能:中断初始化
参数:输入:无
输出:无
数据处理:无
需要的时间:需要的时间=
版本:
最后修改时间:
作者:
***********************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级分组形式1,即抢占级占一位,优先级占3位

/****************UART2全局中断使能***********************/
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/****************************************************************
函数名:I/O初始实话函数
功能:I/O初始化
参数:输入:无
输出:无
数据处理:无
需要的时间:需要的时间=
版本:
最后修改时间:
作者:
***********************************************************/
void GPIO_Configuration (void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/*********************************配置PA口********************************
PA2: USART2发送端口(复用功能推挽式输出)
PA3: USART2接收端口(浮空输入模式)
*************************************************************************/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);

}

/****************************************************************
函数名:Uart初始实话函数
功能:Uart初始化
参数:输入:无
输出:无
数据处理:无
需要的时间:需要的时间=
版本:
最后修改时间:
作者:
***********************************************************/
void Uart_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//使能UART2时钟
GPIO_Configuration();
USART_InitStructure.USART_BaudRate=9600;//波特率为9600
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//8位数据位
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_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);//配置串口2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//使能串口2接受中断
USART_Cmd(USART2, ENABLE);//使能外设串口2
}

PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART2, (u8) ch);

/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{}
return ch;
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
RxData = USART_ReceiveData(USART2);//接收8位的数据
TxData = RxData;
printf("串口2收到的数据是: ");
USART_SendData(USART2, TxData);//将接收到的数据发送出去
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){}
printf(" ");
}
//溢出-如果发生溢出需要先读SR,再读DR寄存器则可清除不断入中断的问题[牛人说要这样]
if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART2,USART_FLAG_ORE); //读SR其实就是清除标志
USART_ReceiveData(USART2); //读DR
}
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d ", file, line) */

while (1)
{}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
13条回答
HelloFantacy
1楼-- · 2019-03-25 16:17
我的是STM32FI03ZET6的芯片,现在调试UART5(串4已经调试成功了),但是串5能正确发送数据,却不能接收数据,我用G-PROBE RS232做U转串,同时也是用GPROBE调试(公司写代码配合GPROBE测试,调试时可以屏蔽大部分代码)!串口5除了管脚部分,配置几乎和串4一样啊!怎么会出不来呢?求大神帮忙啊!如果能解决问题,愿意支付一定的酬金!(加急啊!)

一周热门 更多>