MM32F0010A1T的串口2接收处理函数
时间: 2024-02-11 17:01:39 浏览: 228
MM32F0010A1T的串口2接收处理函数可以通过以下代码实现:
```c
#include "stdio.h"
#include "MM32F0010.h"
#define UART2_RX_BUF_SIZE 64
uint8_t UART2_RX_BUF[UART2_RX_BUF_SIZE];
uint8_t UART2_RX_STA = 0;
void UART2_IRQHandler(void)
{
if (USART_GetITStatus(UART2, USART_IT_RXNE) != RESET)
{
uint8_t res = USART_ReceiveData(UART2);
if ((UART2_RX_STA & 0x80) == 0)
{
if (UART2_RX_STA < UART2_RX_BUF_SIZE)
{
UART2_RX_BUF[UART2_RX_STA++] = res;
}
else
{
UART2_RX_STA |= 0x80;
UART2_RX_STA = 0;
}
}
}
}
int fputc(int ch, FILE *f)
{
USART_SendData(UART2, (uint8_t)ch);
while (USART_GetFlagStatus(UART2, USART_FLAG_TXE) == RESET)
;
return ch;
}
int main(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Rx | USART_Mode_Tx;
USART_Init(UART2, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = UART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART2, USART_IT_RXNE, ENABLE);
USART_Cmd(UART2, ENABLE);
while (1)
{
if ((UART2_RX_STA & 0x80) == 0x80)
{
printf("UART2_RX_BUF overflow\r\n");
UART2_RX_STA = 0;
}
if (UART2_RX_STA & 0x40)
{
printf("UART2_RX_BUF:\r\n");
for (int i = 0; i < UART2_RX_STA & 0x3f; i++)
{
printf("%c", UART2_RX_BUF[i]);
}
printf("\r\n");
UART2_RX_STA = 0;
}
}
}
```
阅读全文