【MT32F006】MT32F006之串口 本文最后修改时间2025年01月09日一、本节简介本文介绍如何使用MT32F006的串口。二、实验平台库版本V1.0.0编译软件MDK5.37硬件平台MT32F006开发板主芯片MT32F006仿真器JLINK三、版权声明1作者甜甜的大香瓜2声明喝水不忘挖井人转载请注明出处。3本文出处原创连载资料《简单粗暴学MT32F006》四、实验前提1、在进行本文步骤前请先阅读以下章节1《简单粗暴学MT32F006》的“第一章和第二章”章节​。2、在进行本文步骤前请先实现以下章节暂无五、基础知识暂无六、硬件原理MT32F006的串口有两组分别是1、PIO1_7UART0_TX和PIO2_0UART0_RX2、PIO0_1UART0_TX和PIO0_2UART0_RX本文使用这一组七、实验步骤1、编写并添加驱动//********************************************************************** //name: GUA_UART.c //introduce: 串口驱动 //author: 甜甜的大香瓜 //email: 897503845qq.com //shop: opengua.taobao.com //changetime: 2022.12.29 //********************************************************************** #include mt006.h #include GUA_UART.h #include string.h /*********************宏定义************************/ #define GUA_UART0_CLOCK_DIV 1 /*********************全局变量************************/ uint8_t gnGUA_UART_rx_buf[GUA_UART_RX_SIZE] {0}; uint8_t gnGUA_UART_tx_buf[GUA_UART_TX_SIZE] {0}; //********************************************************************** //name: fputc //introduce: 将printf函数与串口对应起来 //parameter: ch:要发送的字符串 // *f:文件 //return: none //author: 甜甜的大香瓜 //email: 897503845qq.com //shop: opengua.taobao.com //changetime: 2022.12.29 //********************************************************************** int32_t fputc(int32_t ch, FILE *f) { /* Place your implementation of fputc here */ /* e.g. write a character to the UART */ UART_SendData((uint8_t) ch); /* Loop until the end of transmission */ while (UART_GetFlagStatus(UART_FLAG_TXFE) RESET) {} return ch; } //********************************************************************** //name: GUA_UART_Send //introduce: 串口发送 //parameter: nGUA_Data:串口发送数据 //return: none //author: 甜甜的大香瓜 //email: 897503845qq.com //shop: opengua.taobao.com //changetime: 2022.12.29 //********************************************************************** void GUA_UART_Send(uint8_t *pGUA_Data, uint8_t nGUA_Len) { for(uint8_t i 0; inGUA_Len; i) { UART_SendData(pGUA_Data[i]); while(UART_GetFlagStatus(UART_FLAG_TXFE) RESET); } } //********************************************************************** //name: GUA_UARTx_Reveive //introduce: 串口接收 //parameter: none //return: 返回接收的数据长度 //author: 甜甜的大香瓜 //email: 897503845qq.com //shop: opengua.taobao.com //changetime: 2022.12.29 //********************************************************************** uint8_t GUA_UARTx_Reveive(void) { uint8_t i 0; //接收数据超时中断 if(UART_GetITStatus(UART_IT_RTMIS) SET) { // 清除接收中断标志位和接收超时标志位 UART_ClearITPendingBit(UART_IT_RTMIS); //清空接收缓冲区 memset(gnGUA_UART_rx_buf, 0, GUA_UART_RX_SIZE); while(UART_GetFlagStatus(UART_FLAG_RXFE) ! SET) { //防止接收缓冲区溢出 if(i GUA_UART_RX_SIZE) { //读取数据 gnGUA_UART_rx_buf[i] UART_ReceiveData(); } } } //返回接收到的数据长度 return i; } //********************************************************************** //name: GUA_UART_Init //introduce: 串口初始化 //parameter: none //return: none //author: 甜甜的大香瓜 //email: 897503845qq.com //shop: opengua.taobao.com //changetime: 2022.12.29 //********************************************************************** void GUA_UART_Init(void) { UART_InitTypeDef UART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //uart clk RCC_APBPeriphClockCmd(RCC_AHBCLKCTRL_UART0, ENABLE); RCC_UARTCLKSel(RCC_UARTCLK_SOURCE_12M_IRC); RCC_SETCLKDivider(RCC_CLOCKFREQ_UART0CLK, GUA_UART0_CLOCK_DIV); //PIO04 Rx、PIO05 Tx注意不能同时初始化多个IO GPIO_InitStructure.GPIO_Pin GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed GPIO_Speed_Level_2; GPIO_InitStructure.GPIO_Mode GPIO_Mode_AF_6; //复用功能6也就是串口 GPIO_InitStructure.GPIO_PuPd GPIO_PuPd_UP; GPIO_Init(GPIO0, GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin GPIO_Pin_5; GPIO_Init(GPIO0, GPIO_InitStructure); //uart init UART_DeInit(); UART_StructInit(UART_InitStructure); //115200 UART_Init(UART_InitStructure); //Enable Interrupt RX and RX timeout UART_FIFOLevelConfig(UART_TXIFLSEL_8 , UART_RXIFLSEL_8); UART_ClearITPendingBit(UART_IT_RXMIS | UART_IT_RTMIS | UART_IT_TXMIS); UART_ITConfig(UART_IT_RTIM, ENABLE); //这里用接收超时中断而不用接收中断是因为接收到1个bit就进中断会导致接收比发送快、导致丢包 NVIC_EnableIRQ(UART0_IRQn); //enable uart UART_Cmd(ENABLE); }1编写驱动GUA_UART.c存放在“……\Projects\MT006_StdPeriph_Templates\GUA”2编写驱动头文件GUA_UART.h存放在“……\Projects\MT006_StdPeriph_Templates\GUA”//********************************************************************** //name: GUA_UART.h //introduce: 串口驱动的头文件 //author: 甜甜的大香瓜 //email: 897503845qq.com //shop: opengua.taobao.com //changetime: 2022.12.29 //********************************************************************** #ifndef _GUA_UART_H_ #define _GUA_UART_H_ /*********************头文件************************/ #include stdio.h #include stdint.h /*********************宏定义************************/ //串口打开的宏要注意与电池充电芯片引脚冲突只用于调试 #define GUA_UART_OPEN 1 //如果有此宏则表示打开串口 //串口缓冲区大小 #define GUA_UART_RX_SIZE 128 #define GUA_UART_TX_SIZE 128 /*********************全局变量************************/ extern uint8_t gnGUA_UART_rx_buf[GUA_UART_RX_SIZE]; extern uint8_t gnGUA_UART_tx_buf[GUA_UART_TX_SIZE]; /*********************外部函数声明************************/ extern int32_t fputc(int32_t ch, FILE *f); extern void GUA_UART_Send(uint8_t *pGUA_Data, uint8_t nGUA_Len); extern uint8_t GUA_UARTx_Reveive(void); extern void GUA_UART_Init(void); #endif3工程中添加驱动文件4在MDK设置中添加驱动源文件路径2、添加库的驱动1添加库的驱动文件为了方便一次性都添加进来已有则不用添加2添加库的驱动头文件mt006_conf.h 中为了方便一次性都添加进来已有则不用添加3、在应用层中调用1添加驱动头文件main.c中#include GUA_UART.h2添加驱动初始化代码main.c的main函数中//串口初始化GUA_UART_Init();4、在应用层中调用1添加驱动头文件mt006_it.c中#include “GUA_UART.h”2添加串口中断处理代码mt006_it.c中//串口中断void UART0_IRQHandler(void){//数据接收GUA_UARTx_Reveive();//进入通信处理事件//gnGUA_Function FUNC_GUA_COMMUNICAION_PROCESS_EVT;}八、注意事项暂无九、实验结果暂无