当前位置:网站首页>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
推荐阅读:
欢迎关注公众号"嵌入式技术开发",大家可以后台给我留言沟通交流。如果觉得该公众号对你有所帮助,也欢迎推荐分享给其他人。
边栏推荐
- C语言按行修改文件
- Build your own website (23)
- Execute script unrecognized \r
- 手把手带你入门 API 开发
- One brush 148 force deduction hot question-5 longest palindrome substring (m)
- SVN如何查看修改的文件记录
- Rsync remote synchronization
- CC2530 common registers for timer 1
- CC2530 common registers for crystal oscillator settings
- [combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe
猜你喜欢
PHP online confusion encryption tutorial sharing + basically no solution
Thread pool: the most common and error prone component of business code
Simulink oscilloscope data is imported into Matlab and drawn
What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
建立自己的网站(23)
CC2530 common registers for crystal oscillator settings
Web crawler knowledge day03
線程池:業務代碼最常用也最容易犯錯的組件
How do large consumer enterprises make digital transformation?
Redis: operation commands for list type data
随机推荐
网络硬盘NFS的安装与配置
One brush 144 force deduction hot question-1 sum of two numbers (E)
27. Input 3 integers and output them in descending order. Pointer method is required.
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
An example of HP array card troubleshooting
Great changes! National housing prices fell below the 10000 yuan mark
數據分析必備的能力
Necessary ability of data analysis
[combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
比亚迪、长城混动市场再“聚首”
Overview of satellite navigation system
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
Talk about several methods of interface optimization
One brush 148 force deduction hot question-5 longest palindrome substring (m)
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
線程池:業務代碼最常用也最容易犯錯的組件
C语言按行修改文件
Svn full backup svnadmin hotcopy
BYD and great wall hybrid market "get together" again
CC2530 common registers for ADC single channel conversion