当前位置:网站首页>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
推荐阅读:
欢迎关注公众号"嵌入式技术开发",大家可以后台给我留言沟通交流。如果觉得该公众号对你有所帮助,也欢迎推荐分享给其他人。
边栏推荐
- What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
- PHP online confusion encryption tutorial sharing + basically no solution
- An example of HP array card troubleshooting
- UCORE overview
- Installation and configuration of network hard disk NFS
- Redis:关于列表List类型数据的操作命令
- [combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
- Web crawler knowledge day03
- How do large consumer enterprises make digital transformation?
- Apache service suspended asynchronous acceptex failed
猜你喜欢

Atom QT 16_ audiorecorder

ucore概述

What is your income level in the country?

CC2530 common registers for port initialization

What is the material of sa302grc? American standard container plate sa302grc chemical composition

Redis:关于列表List类型数据的操作命令

Take you to API development by hand

The most complete postman interface test tutorial in the whole network, API interface test

Great changes! National housing prices fell below the 10000 yuan mark

New features of C 10
随机推荐
Open vsftpd port under iptables firewall
Redis: operation commands for list type data
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
简单配置PostFix服务器
How SVN views modified file records
[combinatorial mathematics] recursive equation (example of recursive equation 2 Hanoi Tower | example of recursive equation 3 insertion sequencing)
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
深入理解 SQL 中的 Grouping Sets 语句
比亚迪、长城混动市场再“聚首”
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
線程池:業務代碼最常用也最容易犯錯的組件
Great changes! National housing prices fell below the 10000 yuan mark
聊聊接口优化的几个方法
Rsync remote synchronization
mysql用户管理
LeetCode 1657. Determine whether the two strings are close
LeetCode 1658. Minimum operand to reduce x to 0
Solution to long waiting time of SSH connection to remote host
在iptables防火墙下开启vsftpd的端口