catalog:
1. Introduction to mm32f0020
2. Initialize MM32F0020 UART1 and NVIC interrupts
3. Write MM32F0020 UART1 interrupt receiving function
4. Write MM32F0020 UART1 sending byte and ASCII character function
5. Write the data function received by MM32F0020 UART1 processing interrupt
6.MM32F0020 UART1 sends the data received by UART1 interrupt to the serial port assistant of the upper computer
summary:
Learn MM32F0020 UART1 interrupt to receive data and send 8-byte hexadecimal data through the host computer serial port assistant: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55; The UART1 interrupt of the lower computer MM32F0020 receives a frame of 8 bytes of data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55, sends the multi byte function through UART1, and sends it to the serial port assistant as it is.
Content:
1. Introduction to MM32F0020:
(1) MM32F0020 microcontroller is based on Arm ® Cortex ®- M0 core, the maximum working frequency can reach 48MHz;
(2) power supply voltage support: 2.0V - 5.5V;
(3) up to 32KB Flash and 2KB SRAM;
(4) one I2C;
(5) 2 UART S;
(6) one 12 bit ADC;
(7) one I2C or I2S;
(8) 1 Advanced timer, 1 general timer and 1 Basic timer.
2. Initialize MM32F0020 UART1 and NVIC interrupts:
GPIO initialization of MM32F0020 UART1, select PA12: uart1 according to the DS data manual of MM32F0020_ TX,PA3:UART1_RX is used as the pin of uart1 to send and receive data. The specific configuration steps and initialization are as follows:
(1) Enable GPIOA peripheral clock;
(2) Configure IO pins;
(3) Configure the output speed of GPIO;
(4) Configure the working mode of IO pin;
(5) Initialize the member parameters of each pin of GPIO as a whole according to the parameters configured by GPIOA.
void Bsp_UART1_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE); //UART initialset GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_1); //UART1_TX GPIOA.12 GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStruct); //UART1_RX GPIOA.3 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStruct); }
MM32F0020 UART1 and NVIC interrupt priority initialization. The specific configuration steps and initialization are as follows:
(1) Enable UART1 peripheral clock;
(2) Call UART1GPIO initialization function configured before;
(3) Adjust UART1 communication baud rate to 115200;
(4) Configure UART1 word length to 8 bits;
(5) Configure UART1 transceiver data as 1-bit stop bit;
(6) Configure UART1 to send and receive data without parity bit;
(7) Configure UART1 to allow serial port to send and receive data;
(8) Initialize UART1 structure members according to the above configuration parameters;
(9) Enable UART1 interrupt receiving function;
(10) Configure the NVIC interrupt priority of UART1 to 0, and enable and initialize the NVIC interrupt (the priority can be 0-3. The smaller the parameter, the higher the priority).
According to the above configuration parameters, UART1 initialization code is as follows:
void Bsp_UART1_NVIC_Init(u32 baudrate) { UART_InitTypeDef UART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1ENR_UART1, ENABLE); Bsp_UART1_GPIO_Init(); UART_StructInit(&UART_InitStruct); UART_InitStruct.BaudRate = baudrate; //The word length is in 8-bit data format. UART_InitStruct.WordLength = UART_WordLength_8b; UART_InitStruct.StopBits = UART_StopBits_1; //No even check bit. UART_InitStruct.Parity = UART_Parity_No; //No hardware data flow control. UART_InitStruct.HWFlowControl = UART_HWFlowControl_None; UART_InitStruct.Mode = UART_Mode_Rx | UART_Mode_Tx; UART_Init(UART1, &UART_InitStruct); UART_ITConfig( UART1,UART_IT_RXIEN, ENABLE); //UART1 NVIC NVIC_InitStruct.NVIC_IRQChannel = UART1_IRQn; //UART1 Priority NVIC_InitStruct.NVIC_IRQChannelPriority = 0; //Enable UART1_IRQn NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(& NVIC_InitStruct); UART_Cmd(UART1, ENABLE); }
3. Write MM32F0020 UART1 interrupt receiving function:
(1) Define the cache related to UART1 receiving and sending data. The variables are as follows:
//UART1 Recv Buffer u8 gUART1_Rx_Buf[UART1_RXD_LEN]; //UART1 Recv Count u16 gUART1_Rx_Cnt; //UART1 Recv Timing Count u8 gUART1_Rx_TimeCnt = 0; //UART1 Recv Flag bool gUART1_Rx_Flag = false;
(2) The header file declares macros, caches, variables and function declarations related to UART1 receiving and sending, as follows:
//UART1 Baudrate #define UART1_BAUDRATE (115200) //UART1 Recv length #define UART1_RXD_LEN (200) //UART1 Recv Buffer extern u8 gUART1_Rx_Buf[UART1_RXD_LEN]; //UART1 Recv Count extern u16 gUART1_Rx_Cnt; //UART1 Recv Timing Count extern u8 gUART1_Rx_TimeCnt; //UART1 Recv Flag extern bool gUART1_Rx_Flag; //UART1 NVIC Init void Bsp_UART1_NVIC_Init(u32 baudrate); //Process UART1 Recv Task void Bsp_UART1_Recv_Task(void); //UART sends a byte data void Bsp_UART_SendByte(UART_TypeDef* uart,u8 data); //Send ASCII characters void Bsp_UART_SendASCII(UART_TypeDef* uart,char *str); //UART sends multi-byte data void Bsp_UART_SendBytes(UART_TypeDef* uart,u8 *buf, u16 len);
(3) Write UART1 interrupt receiving data function as follows:
void UART1_IRQHandler(void) { u8 Recv; //Check receive status if(UART_GetITStatus(UART1, UART_IT_RXIEN) == SET) { UART_ClearITPendingBit(UART1, UART_IT_RXIEN); Recv = UART_ReceiveData(UART1); gUART1_Rx_Buf[gUART1_Rx_Cnt] = Recv; if(gUART1_Rx_Cnt < UART1_RXD_LEN-1) { gUART1_Rx_Cnt++; } else { gUART1_Rx_Cnt = 0; } if(gUART1_Rx_Cnt >= 8) { gUART1_Rx_Flag = true; } } }
4. Write MM32F0020 UART1 send byte and ASCII character function:
(1) MM32F0020 UART1 send byte function is as follows:
void Bsp_UART_SendByte(UART_TypeDef* uart,u8 data) { UART_SendData(uart,data); while(!UART_GetFlagStatus(uart, UART_FLAG_TXEPT)); }
(2) MM32F0020 # UART1 sends multi byte functions as follows:
void Bsp_UART_SendBytes(UART_TypeDef* uart,u8 *buf, u16 len) { while(len--) { Bsp_UART_SendByte(uart,*buf++); } }
(3) MM32F0020 # UART1 sends ASCII string functions as follows:
void Bsp_UART_SendASCII(UART_TypeDef* uart,char *str) { while(*str) { Bsp_UART_SendByte(uart,*str++); } }
5. Write the data function received by MM32F0020 UART1 processing interrupt:
The data function received by MM32F0020 UART1 processing interrupt is as follows: when the host computer serial port assistant sends 8-byte hexadecimal data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55; The UART1 interrupt of the lower computer MM32F0020 receives a frame of 8 bytes of data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55, sends the multi byte function through UART1 and sends it to the serial port assistant as it is.
void Bsp_UART1_Recv_Task(void) { //UART1 Rx Flag if(gUART1_Rx_Flag == true) { gUART1_Rx_Flag = false; if((gUART1_Rx_Buf[0] == 0xAA) && (gUART1_Rx_Buf[1] == 0x01) && (gUART1_Rx_Buf[2] == 0x02) && (gUART1_Rx_Buf[3] == 0x03) && \ (gUART1_Rx_Buf[4] == 0x04) && (gUART1_Rx_Buf[5] == 0x05) && (gUART1_Rx_Buf[6] == 0x06) && (gUART1_Rx_Buf[7] == 0x55)) {
Bsp_UART_SendBytes(UART1,gUART1_Rx_Buf,gUART1_Rx_Cnt); } gUART1_Rx_Cnt = 0; memset(gUART1_Rx_Buf,0,sizeof(gUART1_Rx_Buf)); } }
6.MM32F0020 UART1 sends UART1 interrupt received data to host computer serial port assistant:
When the upper computer serial port assistant sends 8-byte hexadecimal data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55; The UART1 interrupt of the lower computer MM32F0020 receives a frame of 8 bytes of data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55, sends the multi byte function through UART1 and sends it to the serial port assistant as it is.
(1) calling UART1 NVIC initialization function in main function;
(2) calling the UART1 function in the while(1) main loop of the main function to deal with the data function received by the interrupt, and cyclic checking whether the UART1 receiving interrupt receives the data sent by the upper computer serial assistant, and if it receives it, it is sent to the upper computer serial assistant on the original to display it.
int main(void) { //UART1 NVIC Init Baudrate 115200 Bsp_UART1_NVIC_Init(UART1_BAUDRATE); while(1) { //Process UART1 Recv Task Bsp_UART1_Recv_Task(); } }
(3) The serial port assistant of the upper computer sends 8-byte hexadecimal data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55. The lower computer MM32F0020 UART1 interrupts receiving a frame of data and sends the received data to the upper computer as it is, as shown in Figure 1 below:
Figure 1
Summary:
Learn MM32F0020 UART1 interrupt to receive data and send 8-byte hexadecimal data through the host computer serial port assistant: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55; The UART1 interrupt of the lower computer MM32F0020 receives a frame of 8 bytes of data: 0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55, sends the multi byte function through UART1, and sends it to the serial port assistant as it is.
matters needing attention:
(1) each peripheral of MM32F0020 has its own independent clock. It is necessary to enable the GPIO clock of UART1 transmitting and receiving pins;
(2) enable UART1 peripheral clock;
(3) enable UART1 receiving interrupt;
(4) enable UART1 NVIC interrupt;
(5) the operation method of UART2 is the same as that of UART1. Refer to UART1 above to change the corresponding UART1 parameter to UART2 to enable the corresponding peripheral clock.