目录:
1.MM32F0020简介
2.初始化MM32F0020 UART1和NVIC中断
3.编写MM32F0020 UART1中断接收函数
4.编写MM32F0020 UART1发送字节和ASCII字符函数
5.编写MM32F0020 UART1处理中断接收到的数据函数
6.MM32F0020 UART1发送UART1中断接收到的数据到上位机串口助手
提要:
学习MM32F0020 UART1中断接收数据,通过上位机串口助手发送8字节的十六进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55;下位机MM32F0020的UART1中断接收到一帧:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手显示出来。
内容:
1、MM32F0020简介:
(1)MM32F0020微控制器是基于Arm® Cortex®-M0内核,最高工作频率可达48MHz;
(2)供电电压支持:2.0V - 5.5V;
(3)多达32KB的Flash,2KB的SRAM;
(4)1个I2C;
(5)2个UART;
(6)1个12位的ADC;
(7)1个I2C或I2S;
(8)1个高级定时,1个通用定时器,1个基本定时器。
2.初始化MM32F0020 UART1和NVIC中断:
MM32F0020 UART1的GPIO初始化,根据MM32F0020的DS数据手册选择PA12:UART1_TX,PA3:UART1_RX做为UART1的发送和接收数据的引脚,具体配置步骤,及其初始化如下所示:
(1)使能GPIOA外设时钟;
(2)配置IO的管脚;
(3)配置GPIO的输出速度;
(4)配置IO管脚的工作模式;
(5)根据GPIOA配置的参数整体初始化GPIO各管脚的成员参数。
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和NVIC中断优先级初始化,具体配置步骤,及其初始化如下所示:
(1)使能UART1外设时钟;
(2)调用之前配置的UART1GPIO初始化函数;
(3)调配置UART1通信波特率为115200;
(4)配置UART1字长为8位;
(5)配置UART1收发数据为1位停止位;
(6)配置UART1收发数据为无奇偶校验位;
(7)配置UART1允许串口收发数据;
(8)根据以上配置参数初始化UART1结构体成员;
(9)使能UART1中断接收功能;
(10)配置UART1的NVIC中断优先级为0,并使能和初始化NVIC中断(优先级为0-3均可,参数越小优先级越高)。
根据以上配置参数,则UART1初始化代码如下所示:
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.编写MM32F0020 UART1中断接收函数:
(1)定义UART1接收和发送数据相关的的缓存,变量如下所示:
//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)头文件声明与UART1接收和发送相关的宏,缓存、变量以及函数声明,如下所示:
//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)编写UART1中断接收数据函数,如下所示:
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.编写MM32F0020 UART1发送字节和ASCII字符函数:
(1)MM32F0020 UART1发送字节函数如下所示:
void Bsp_UART_SendByte(UART_TypeDef* uart,u8 data) { UART_SendData(uart,data); while(!UART_GetFlagStatus(uart, UART_FLAG_TXEPT)); }
(2)MM32F0020 UART1发送多字节函数如下所示:
void Bsp_UART_SendBytes(UART_TypeDef* uart,u8 *buf, u16 len) { while(len--) { Bsp_UART_SendByte(uart,*buf++); } }
(3)MM32F0020 UART1发送ASCII字符串函数如下所示:
void Bsp_UART_SendASCII(UART_TypeDef* uart,char *str) { while(*str) { Bsp_UART_SendByte(uart,*str++); } }
5.编写MM32F0020 UART1处理中断接收到的数据函数:
MM32F0020 UART1处理中断接收到的数据函数如下所示,当上位机串口助手发送8字节的十六进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55;下位机MM32F0020的UART1中断接收到一帧:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手。
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发送UART1中断接收到的数据到上位机串口助手:
当上位机串口助手发送8字节的十六进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55;下位机MM32F0020的UART1中断接收到一帧:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手。
(1)在main函数中调用UART1 NVIC初始化函数;
(2)在main函数的while(1)主循环中调用UART1处理中断接收到的数据函数,循环检测UART1的接收中断是否接收到上位机串口助手下发的数据,如有收到就原样发送到上位机串口助手上显示出来;
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)上位机串口助手发送8字节16进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55,下位机MM32F0020 UART1中断接收到一帧数据后原样把接收到的数据发送到上位机上显示出来,如下图1所示:
图1
总结:
学习MM32F0020 UART1中断接收数据,通过上位机串口助手发送8字节的十六进制数据:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55;下位机MM32F0020的UART1中断接收到一帧:0xAA,0x01,0x02,0x03,0x04,0x05,0x06,0x55 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手显示出来。
注意事项:
(1)MM32F0020每个外设都有自己独立的时钟,需使能UART1 发送和接收引脚的GPIO时钟;
(2)使能UART1外设时钟;
(3)使能UART1接收中断;
(4)使能UART1 NVIC中断;
(5)UART2的操作方法与UART1的方法一样,可参考以上UART1把对应的UART1参数改成UART2,使能相应外设时钟即可。