MM32F0140 UART1空闲中断接收

目录:

1.MM32F0140简介

2.初始化MM32F0140 UART1空闲中断和NVIC中断

3.编写MM32F0140 UART1中断接收和空闲中断函数

4.编写MM32F0140 UART1发送字节和ASCII字符函数

5.编写MM32F0140 UART1处理空闲中断接收数据函数

6.MM32F0140 UART1发送UART1空闲中断接收到的数据到上位机串口助手

提要:

  学习MM32F0140 UART1空闲中断接收数据,通过上位机串口助手发送8字节的十六进制数据:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07;下位机MM32F0140的UART1空闲中断接收到一帧:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手显示出来。

内容:

1、MM32F0140简介

  (1)MM32F0140微控制器是基于Arm® Cortex®-M0内核,最高工作频率可达72MHz;

  (2)供电电压支持:2.0V - 5.5V;

  (3)多达64KB的Flash,8KB的SRAM;

  (4)1个I2C;

  (5)3个UART;

  (6)1个12位共13通道的ADC;

  (7)2个I2C或I2S;

  (8)1个16位高级定时,1个16位和1个32位的通用定时器,3个16位的基本定时器;

  (9)1个FlexCAN接口;

  (10)1个IWDG和1个WWDG看门狗。

2.初始化MM32F0140 UART1空闲中断和NVIC中断:

  MM32F0140 UART1的GPIO初始化,根据MM32F0140的DS数据手册选择PA9:UART1_TX,PA10:UART1_RX为UART1的发送和接收数据的引脚,具体配置步骤,及其初始化如下所示:

(1)使能GPIOA外设时钟;

(2)配置IO管脚GPIO_AFx复用为UART1功能;

(3)配置UARTx IO的管脚;

(4)配置GPIO的输出速度;

(5)配置IO管脚的工作模式;

(6)根据GPIOA配置的参数整体初始化GPIO各管脚的成员参数。

static void Bsp_UART1_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    //Enable GPIOA Clock
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
    //PA9 and PA10 AF UART1 Rx and Tx
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

    //UART1_TX   GPIOA.9
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //UART1_RX    GPIOA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

  MM32F0140 UART1和NVIC中断优先级初始化,并使能UART1空闲中断功能,具体配置步骤,及其初始化如下所示:

(1)使能UART1外设时钟;

(2)调用之前配置的UART1GPIO初始化函数;

(3)调配置UART1通信波特率为115200;

(4)配置UART1字长为8位;

(5)配置UART1收发数据为1位停止位;

(6)配置UART1收发数据为无奇偶校验位;

(7)配置UART1允许串口收发数据;

(8)根据以上配置参数初始化UART1结构体成员;

(9)使能UART1中断接收UART1空闲中断功能;

(10)配置UART1的NVIC中断优先级为0,并使能和初始化NVIC中断(优先级为0-3均可,参数越小优先级越高)。

  根据以上配置参数,则UART1初始化代码如下所示:

void Bsp_UART1_NVIC_Init(u32 baudrate)
{
    UART_InitTypeDef UART_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    //Enable UART1 Clock
    RCC_APB2PeriphClockCmd(RCC_APB2ENR_UART1,ENABLE);
    //UART1 GPIO Init
    Bsp_UART1_GPIO_Init();
    
    //Baud rate
    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);
    //Enable RXIEN and RXIDLE
    UART_ITConfig(UART1,UART_IT_RXIEN | UART_IER_RXIDLE, ENABLE);
    
    //UART1 NVIC
    NVIC_InitStruct.NVIC_IRQChannel = UART1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    
    UART_Cmd(UART1, ENABLE);
}

3.编写MM32F0140 UART1中断接收和空闲中断函数:

(1)定义UART1空闲中断接收和发送数据相关的的缓存,变量以及空闲中断标志,代码如下所示:

//UART1 Recv Buffer
u8 gUART1_Rx_Buf[UART1_RXD_LEN] = {0};
//UART1 Recv Count
u8 gUART1_Rx_Cnt = 0;
//UART1 IDLE Flag
u8 gUART1_IDLE_Flag = 0;

(2)头文件声明与UART1空闲中断接收和发送相关的宏,缓存、变量以及函数声明等,如下所示:

//UART1 Recv length
#define UART1_RXD_LEN   (255)
//UART1 Baudrate
#define UART1_BAUDRATE  (115200)

//UART1 Recv Buffer
extern u8 gUART1_Rx_Buf[UART1_RXD_LEN];
//UART1 Recv Count
extern u8 gUART1_Rx_Cnt;
//UART1 IDLE Flag
extern u8 gUART1_IDLE_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_Send_Byte(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;

    if(UART_GetITStatus(UART1, UART_IT_RXIEN) != RESET) 
    {
        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(UART_GetITStatus(UART1, UART_IER_RXIDLE) != RESET)
    {
        UART_ClearITPendingBit(UART1, UART_IER_RXIDLE);
        gUART1_IDLE_Flag = 1;
    }    
}

4.编写MM32F0140 UART1发送字节和ASCII字符函数:

(1)MM32F0140 UART1发送字节函数如下所示:

void Bsp_UART_SendByte(UART_TypeDef* uart,u8 data)
{
    UART_SendData(uart, data);
    while(!UART_GetFlagStatus(uart, UART_FLAG_TXEPT));
}

(2)MM32F0140  UART1发送多字节函数如下所示:

void Bsp_UART_SendBytes(UART_TypeDef* uart,u8* buf, u16 len)
{
    while(len--)
    {
       Bsp_UART_SendByte(uart,*buf++);
    }
}

(3)MM32F0140  UART1发送ASCII字符串函数如下所示:

void Bsp_UART_SendASCII(UART_TypeDef* uart,char* str)
{
    while(*str)
    {
        Bsp_UART_SendByte(uart,*str++);
    }
}

5.编写MM32F0140 UART1处理空闲中断接收数据函数

  MM32F0140 UART1处理空闲中断接收到的数据函数如下所示,当上位机串口助手发送8字节的十六进制数据:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07;下位机MM32F0140的UART1中断接收到一帧:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手。

void Bsp_UART1_Recv_Task(void)
{
    //UART1 IDLE Flag
    if(gUART1_IDLE_Flag == 1)
    {
        gUART1_IDLE_Flag = 0;
        
        if((gUART1_Rx_Buf[0] == 0x55) && (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] == 0x07))
        {
            LED1_TOGGLE();
            Bsp_UART_SendBytes(UART1,gUART1_Rx_Buf,8);
        }
        gUART1_Rx_Cnt = 0;        
        memset(gUART1_Rx_Buf,0,sizeof(gUART1_Rx_Buf));          
    }    
}

6.MM32F0140 UART1发送UART1空闲中断接收到的数据到上位机串口助手

  当上位机串口助手发送8字节的十六进制数据:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07;下位机MM32F0140的UART1空闲中断接收到一帧:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07 共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手。

(1)在main函数中调用UART1 NVIC空闲中断使能初始化函数;

(2)在main函数的while(1)主循环中调用UART1处理空闲中断接收到的数据函数,循环检测UART1的空闲中断接收是否接收到上位机串口助手下发的数据,如有收到就原样发送到上位机串口助手上显示出来;

int main(void)
{
    //UART1 Init Baudrate:115200
    Bsp_UART1_NVIC_Init(UART1_BAUDRATE);
    
    while(1) 
    {
        //Test UART1 Recv IDLE
        Bsp_UART1_Recv_Task();
    }
}

(3)上位机串口助手发送8字节16进制数据:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07,下位机MM32F0020 UART1空闲中断接收到一帧数据后原样把接收到的数据发送到上位机上显示出来,如下图1所示:

MM32F0140 UART1空闲中断接收_第1张图片

                图1

总结:

  学习MM32F0140 UART1空闲中断接收数据,通过上位机串口助手发送8字节的十六进制数据:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07;下位机MM32F0140的UART1空闲中断接收到一帧:0x55,0x01,0x02,0x03,0x04,0x05,0x06,0x07共8字节数据后,通过UART1发送多字节函数,原样发送到串口助手显示出来。

注意事项:

  (1)MM32F0140每个外设都有自己独立的时钟,需使能UART1 发送和接收引脚的GPIO时钟;

  (2)使能UART1外设时钟;

  (3)配置GPIOA的 PA9和PA10复用成UART1功能

  (4)使能UART1接收中断和空闲中断接收;

  (5)使能UART1 NVIC中断,使能UART1;

  (6)UART2和UART3空闲中断接收数据的操作方法与UART1的方法一样,可参考以上UART1把对应的UART1参数改成UART2或UART3,使能相应外设时钟即可。

 

你可能感兴趣的:(MM32F0140 UART1空闲中断接收)