c
#include "cw32f030.h"
#include <string.h>
volatile uint8_t rxBuffer[256];
volatile uint16_t rxIndex = 0;
void UART_Configuration(void) {
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.BaudRate = 115200;
USART_InitStruct.WordLength = USART_WL_8BIT;
USART_InitStruct.StopBits = USART_STPBITS_1;
USART_InitStruct.Parity = USART_PRARITY_EVEN; // 启用奇偶校验
USART_InitStruct.HardwareFlowControl = USART_HWCONTROL_NONE;
USART_InitStruct.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART1, &USART_InitStruct);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void UART_Error_Interrupt_Enable(void) {
USART_ITConfig(USART1, USART_IT_PE, ENABLE);
USART_ITConfig(USART1, USART_IT_FE, ENABLE);
USART_ITConfig(USART1, USART_IT_NE, ENABLE);
USART_ITConfig(USART1, USART_IT_ORE, ENABLE);
}
void USART1_IRQHandler(void) {
if (USART_GetITStatus(USART1, USART_IT_PE) != RESET) {
uint8_t data = USART_ReceiveData(USART1);
printf("PE Error: 0x%02X\n", data);
}
if (USART_GetITStatus(USART1, USART_IT_FE) != RESET) {
uint8_t data = USART_ReceiveData(USART1);
printf("FE Error!\n");
}
if (USART_GetITStatus(USART1, USART_IT_ORE) != RESET) {
volatile uint16_t temp = USART1->SR;
temp = USART1->DR;
printf("ORE Error!\n");
memset(rxBuffer, 0, sizeof(rxBuffer));
rxIndex = 0;
}
}
int main(void) {
SystemInit();
UART_Clock_Enable();
GPIO_Configuration();
UART_Configuration();
NVIC_Configuration();
UART_Error_Interrupt_Enable();
while (1) {
// 主循环逻辑
}
} |
|