🎬 秋野酱:《个人主页》
🔥 个人专栏:《Java专栏》《Python专栏》
⛺️心若有所向往,何惧道阻且长
以下模板代码均基于官方2023.07.17版本。
程序结构
#include "Config.h"
#include "GPIO.h"
#include "Delay.h"
int main() {
EAXSFR();
GPIO_config();
EA = 1;
while(1) {
}
}
GPIO操作
初始化所有
P0M1 = 0; P0M0 = 0;
P1M1 = 0; P1M0 = 0;
P2M1 = 0; P2M0 = 0;
P3M1 = 0; P3M0 = 0;
P4M1 = 0; P4M0 = 0;
P5M1 = 0; P5M0 = 0;
P6M1 = 0; P6M0 = 0;
P7M1 = 0; P7M0 = 0;
使用宏配置IO口
#define GPIO_Pin_0 0x01 //IO引脚 Px.0
#define GPIO_Pin_1 0x02 //IO引脚 Px.1
#define GPIO_Pin_2 0x04 //IO引脚 Px.2
#define GPIO_Pin_3 0x08 //IO引脚 Px.3
#define GPIO_Pin_4 0x10 //IO引脚 Px.4
#define GPIO_Pin_5 0x20 //IO引脚 Px.5
#define GPIO_Pin_6 0x40 //IO引脚 Px.6
#define GPIO_Pin_7 0x80 //IO引脚 Px.7
#define GPIO_Pin_LOW 0x0F //IO低4位引脚
#define GPIO_Pin_HIGH 0xF0 //IO高4位引脚
#define GPIO_Pin_All 0xFF //IO所有引脚
//准双向口 P01为例
P0_MODE_IO_PU(GPIO_Pin_1);
//高阻输入 P01为例
P0_MODE_IN_HIZ(GPIO_Pin_1);
//漏极开路 P01为例
P0_MODE_OUT_OD(GPIO_Pin_1);
//推挽输出 P01为例
P0_MODE_OUT_PP(GPIO_Pin_1);
使用函数配置IO口
void GPIO_config(void) {
GPIO_InitTypeDef GPIO_InitStructure; //结构定义
GPIO_InitStructure.Pin = GPIO_Pin_3; //指定要初始化的IO,
GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
GPIO_Inilize(GPIO_P5, &GPIO_InitStructure);//初始化
}
UART操作
添加NVIC.c UART.c UART_Isr.c
配置EA = 1
头文件
#include "UART.h"
#include "NVIC.h"
#include "Switch.h"
初始化
void UART_config(void) {
// >>> 记得添加 NVIC.c, UART.c, UART_Isr.c <<<
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer1; //选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
UART_Configuration(UART1, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
NVIC_UART1_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART1_SW(UART1_SW_P30_P31); // 引脚选择, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
}
void UART_config(void) {
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
UART_Configuration(UART2, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
NVIC_UART2_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART2_SW(UART2_SW_P10_P11); // 引脚选择, UART2_SW_P10_P11,UART2_SW_P46_P47
}
void UART_config(void) {
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer3; //选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
UART_Configuration(UART3, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
NVIC_UART3_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART3_SW(UART3_SW_P00_P01); // 引脚选择, UART3_SW_P00_P01,UART3_SW_P50_P51
}
void UART_config(void) {
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer4; //选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
UART_Configuration(UART4, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
NVIC_UART4_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART4_SW(UART4_SW_P02_P03); // 引脚选择, UART4_SW_P02_P03,UART4_SW_P52_P53
}
● UART_BaudRate:波特率
● UARTx_SW: 引脚
● UART_BRT_Use: 发生器
● UART_Configuration中的UART1
接收逻辑
if(COM1.RX_TimeOut > 0) {
//超时计数
if(--COM1.RX_TimeOut == 0) {
if(COM1.RX_Cnt > 0) {
// 这里处理收到的数据,做具体的逻辑,可以调用自己的on_uart1_recv
for(i=0; i<COM1.RX_Cnt; i++) {
// RX1_Buffer[i]存的是接收的每个字节,写出用 TX1_write2buff
}
}
COM1.RX_Cnt = 0;
}
}
// 不要处理的太快
delay_ms(10);
if(COM2.RX_TimeOut > 0) {
//超时计数
if(--COM2.RX_TimeOut == 0) {
if(COM2.RX_Cnt > 0) {
for(i=0; i<COM2.RX_Cnt; i++) {
// RX2_Buffer[i]存的是接收的数据,写出用 TX2_write2buff
// TODO: 做具体的逻辑 on_uart2_recv
}
}
COM2.RX_Cnt = 0;
}
}
if(COM3.RX_TimeOut > 0) {
//超时计数
if(--COM3.RX_TimeOut == 0) {
if(COM3.RX_Cnt > 0) {
for(i=0; i<COM3.RX_Cnt; i++) {
// TODO: RX3_Buffer[i]存的是接收的数据
// TODO: 做具体的逻辑 on_uart3_recv
}
}
COM3.RX_Cnt = 0;
}
}
if(COM4.RX_TimeOut > 0) {
//超时计数
if(--COM4.RX_TimeOut == 0) {
if(COM4.RX_Cnt > 0) {
for(i=0; i<COM4.RX_Cnt; i++) {
// TODO: RX4_Buffer[i]存的是接收的数据
// TODO: 做具体的逻辑 on_uart4_recv
}
}
COM4.RX_Cnt = 0;
}
}
发送
UART1
TX1_write2buff(xx);// 写一个byte
PrintString1(""); // 写字符串
UART2
TX2_write2buff(xx);// 写一个byte
PrintString2(""); // 写字符串
UART3
TX3_write2buff(xx);// 写一个byte
PrintString3(""); // 写字符串
UART4
TX4_write2buff(xx);// 写一个byte
PrintString4(""); // 写字符串