当前位置:网站首页>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
推荐阅读:
欢迎关注公众号"嵌入式技术开发",大家可以后台给我留言沟通交流。如果觉得该公众号对你有所帮助,也欢迎推荐分享给其他人。
边栏推荐
- Vs code plug-in korofileheader
- Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
- Host based intrusion system IDS
- Open vsftpd port under iptables firewall
- Solution to long waiting time of SSH connection to remote host
- C语言字符串练习
- Kotlin学习快速入门(7)——扩展的妙用
- Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
- New features of C 10
- 浅谈拉格朗日插值及其应用
猜你喜欢

CC2530 common registers for port interrupts

What material is sa537cl2? Analysis of mechanical properties of American standard container plate

How do large consumer enterprises make digital transformation?

大消费企业怎样做数字化转型?
![[JDBC] API parsing](/img/75/0f69a4e246a571688355bb13e2cd73.jpg)
[JDBC] API parsing

ANOVA example

设计电商秒杀

13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties

New features of C 10
![[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.](/img/a0/4fc0e0741aad2885873e60f2af3387.jpg)
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
随机推荐
Depth first search of graph
Redis:关于列表List类型数据的操作命令
Kotlin learning quick start (7) -- wonderful use of expansion
Leetcode: lucky number in matrix
Talk about several methods of interface optimization
Mysql database -dql
跨境电商:外贸企业做海外社媒营销的优势
LeetCode 1657. Determine whether the two strings are close
How to delete a specific line from a text file using the SED command?
CC2530 common registers for serial communication
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
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
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
Solution to long waiting time of SSH connection to remote host
基于主机的入侵系统IDS
Atom QT 16_ audiorecorder
[2. Basics of Delphi grammar] 2 Object Pascal data type
Simple configuration of postfix server
Kindeditor editor upload image ultra wide automatic compression -php code
[combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)