当前位置:网站首页>SWM32系列教程4-端口映射及串口应用
SWM32系列教程4-端口映射及串口应用
2022-07-03 17:07:00 【天外飞仙CUG】

今天来介绍一下SWM32单片机的端口映射和串口的应用。
1.端口映射
之前的文章中介绍过,SWM32系列单片机的端口映射比较灵活。一个外设可以映射到多个不同的GPIO,对于PCB布线来说,就方便很多。
可以通过PORTx_SEL寄存器来配置某个外设映射到哪个端口。如下图:

以PC口为例,PC0可以配置PORTC_SEL寄存器,映射为FUNMUX0功能,PC1可以映射为FUNMUX1功能,而FUNMUX0和FUNMUX1功能又可以对应不同的外设。因此PC0和PC1就可以灵活的配置为各种数字外设的功能。

但这个也不是无限灵活的,一般遵循奇偶分配的原则,即偶数端口对应FUNMUX0功能,奇数端口对应FUNMUX1功能。
实际编程时,直接调用相关的函数即可,以串口为例,要将UART0功能映射到PC2和PC3端口,调用以下函数即可。函数具体的参数说明可以看注释。
PORT_Init(PORTC, PIN2, FUNMUX0_UART0_RXD, 1); //GPIOC.2配置为UART0输入引脚
PORT_Init(PORTC, PIN3, FUNMUX1_UART0_TXD, 0); //GPIOC.3配置为UART0输出引脚2.串口应用
串口(UART)是嵌入式中最常用的接口之一。该单片机的UART口,拥有深度为8的收发FIFO,可以减少 CPU 中断的频率,提高运行效率。
一般情况下,我们都是采用中断的方式来收发数据,这里也主要介绍怎样实现中断方式收发数据。直接看程序:
void SerialInit(void)
{
UART_InitStructure UART_initStruct;
PORT_Init(PORTC, PIN2, FUNMUX0_UART0_RXD, 1); //GPIOC.2配置为UART0输入引脚
PORT_Init(PORTC, PIN3, FUNMUX1_UART0_TXD, 0); //GPIOC.3配置为UART0输出引脚
UART_initStruct.Baudrate = 115200;
UART_initStruct.DataBits = UART_DATA_8BIT;
UART_initStruct.Parity = UART_PARITY_NONE;
UART_initStruct.StopBits = UART_STOP_1BIT;
UART_initStruct.RXThreshold = 3; //接收FIFO门限设置
UART_initStruct.RXThresholdIEn = 1; //使能接收中断
UART_initStruct.TXThreshold = 3; //发送FIFO门限设置
UART_initStruct.TXThresholdIEn = 0; //发送中断
UART_initStruct.TimeoutTime = 10; //10个字符时间内未接收到新的数据则触发超时中断
UART_initStruct.TimeoutIEn = 1; //使能超时中断
UART_Init(UART0, &UART_initStruct);
UART_Open(UART0);
NVIC_EnableIRQ(UART0_IRQn); //打开串口中断
}波特率及数据位、校验位等设置不必多说,主要看后面的中断部分的设置。
接收和超时中断
首先需要设置接收FIFO门限RXThreshold, 同时RXThresholdIEn=1使能接收中断。当接收FIFO中的数据个数>RXThreshold时产生接收中断。不像其他不带FIFO的单片机,接收一个字节就产生一次中断。
其次,还需设置接收超时时间TimeoutTime,以及使能超时中断TimeoutIEn=1。当FIFO中数据≤RXThreshold时,且超时未接收到新数据,会产生超时中断。两个中断配合即可完成一帧数据的读取。
发送和发送完成中断
发送中断与接收中断类似,也需要设置FIFO门限TXThreshold以及使能中断TXThresholdIEn=1。需要注意的是初始化时不必使能中断,在需要发数据时使能即可。
数据发送完成后,可以再使能发送完成中断,发送完成中断在485半双工应用时很有用。
完整的工程参考:
链接:https://pan.baidu.com/s/1KLbtE_FP1ZPY5UqlZwdY9Q?pwd=5ij6
提取码:5ij6
推荐阅读:
欢迎关注公众号"嵌入式技术开发",大家可以后台给我留言沟通交流。如果觉得该公众号对你有所帮助,也欢迎推荐分享给其他人。
边栏推荐
- Bcvp developer community 2022 exclusive peripheral first bullet
- 29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
- [combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
- Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
- LeetCode 1657. Determine whether the two strings are close
- Redis: operation commands for list type data
- 27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
- 浅谈拉格朗日插值及其应用
- 设计电商秒杀
- CC2530 common registers for port interrupts
猜你喜欢

手把手带你入门 API 开发

CC2530 common registers for serial communication

图之深度优先搜索

UCORE overview

MySQL Basics

The largest matrix (H) in a brush 143 monotone stack 84 histogram

utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library

CC2530 common registers for port initialization

2022.02.14_ Daily question leetcode five hundred and forty

新库上线 | CnOpenData中国保险机构网点全集数据
随机推荐
vs code 插件 koroFileHeader
Execute script unrecognized \r
One brush 142 monotone stack next larger element II (m)
CC2530 common registers
C语言字符串练习
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
Vs code plug-in korofileheader
[combinatorial mathematics] recursive equation (example of recursive equation 2 Hanoi Tower | example of recursive equation 3 insertion sequencing)
LeetCode 1656. Design ordered flow
大消费企业怎样做数字化转型?
CC2530 common registers for crystal oscillator settings
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
聊聊接口优化的几个方法
执行脚本不认\r
Rsync远程同步
How to delete a specific line from a text file using the SED command?
Host based intrusion system IDS
CC2530 common registers for port initialization
C language string inversion