在线时间14 小时
UID1243538
ST金币0
蝴蝶豆0
注册时间2015-7-4
初级会员
- 最后登录
- 2015-8-15
|
a0a.1 32b0c
STM32F303RE 串口接收中断处理:当从串口发数据时,MCU只能接收到一半数据,请教各位大神,这是怎么一回事?如图1
//中断处理程序
void USART2_IRQHandler(void)
{
CLI(); //disable all interrupts
/* checks whether the specified USART interrupt has occurred or not */
if (USART_GetITStatus(USART2, USART_IT_RXNE)) {
/* clear the RXNE pending bit */
USART_RequestCmd(USART2, USART_Request_RXFRQ, ENABLE);
//USART_SendData(USART2, USART_ReceiveData(USART2));
HW_USART_SendOneChar(USART2, HW_USART_ReceOneChar(USART2));
}
SEI(); //enable all interrupts
}
当中断里直接用库函数USART_SendData(USART2, USART_ReceiveData(USART2))时接收正常
//串口程序
//115200 -8-1-0-0
#include "HW_USART.h"
static void RCC_Configuration(void);
static void NVIC_Configuration(void);
static void GPIO_Configuration(GPIO_TypeDef* port);
void HW_USART_Init(USART_TypeDef* usartnum, FunctionalState state)
{
USART_InitTypeDef USART_InitStructure;
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration(Light_Run_Port);
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = WordLength;
USART_InitStructure.USART_StopBits = StopBits;
USART_InitStructure.USART_Parity = ParityChecking;
USART_InitStructure.USART_HardwareFlowControl = FlowControl;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_DeInit(usartnum);
USART_Init(usartnum, &USART_InitStructure);
USART_ITConfig(usartnum, USART_IT_RXNE, state); //enable RXNE interrupt
USART_ClearFlag (USART2, (USART_FLAG_TXE | USART_FLAG_TC)); //clear TXE TC flag
USART_Cmd(usartnum, state);
}
void RCC_Configuration(void)
{
/* GPIOA Periph clock enable */
RCC_AHBPeriphClockCmd(GPIOA, ENABLE);
/* USART2 Periph clock enable */
RCC_APB1PeriphClockCmd(USART2, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void GPIO_Configuration(GPIO_TypeDef* port)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = (USART_TXpin | USART_RXpin); //RX TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Alternate function mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //High speed
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Output push-pull
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Pull-up
GPIO_Init(port, &GPIO_InitStructure);
/* select the pins to be used as Alternate function */
GPIO_PinAFConfig(port, GPIO_PinSource2, GPIO_AF_7);
GPIO_PinAFConfig(port, GPIO_PinSource3, GPIO_AF_7);
}
void HW_USART_SendOneChar(USART_TypeDef* usartnum, uint8_t data)
{
USART_SendData(usartnum, data);
/* wait until transmission is complete */
while (!(USART_GetFlagStatus(usartnum, USART_FLAG_TC))) {
;
}
}
void HW_USART_SendChars(USART_TypeDef* usartnum, uint16_t len, void *buff)
{
uint16_t i;
uint8_t *ptr = (uint8_t *)buff;
for (i = 0; i < len; i++) {
HW_USART_SendOneChar(usartnum, ptr[i]);
}
}
void HW_USART_SendString(USART_TypeDef* usartnum, void *buff)
{
uint8_t *ptr = (uint8_t *)buff;
while (*ptr != '\0') {
HW_USART_SendOneChar(usartnum, *ptr++);
}
}
uint16_t HW_USART_ReceOneChar(USART_TypeDef* usartnum)
{
/* Wait until data to be received */
while (!(USART_GetFlagStatus(usartnum, USART_FLAG_RXNE))) {
;
}
return USART_ReceiveData(usartnum);
}
|
-
|