串口通信---串口2定时器2

main.c

cpp 复制代码
#include "Ai8051U.h"
#include "Delay.h"
#include "Uart.h"
#include <stdint.h>

// ---- 自定义全局变量 ----
uint16_t count = 0; // 全局计数变量(未使用)
extern char wptr;
extern char rptr;
extern char buf[16];
extern __bit busy;
extern void UartIsr(void) __interrupt(8);
// ----------------------

void main(void)
{
    CKCON = 0;
    P0M0 = 0x00; P0M1 = 0x00; // 设置P0为准双向口
    P1M0 = 0x00; P1M1 = 0x00; // 设置P1为准双向口
    P2M0 = 0x00; P2M1 = 0x00; // 设置P2为准双向口
    P3M0 = 0x00; P3M1 = 0x00; // 设置P3为准双向口
    P4M0 = 0x00; P4M1 = 0x00; // 设置P4为准双向口
    P5M0 = 0x00; P5M1 = 0x00; // 设置P5为准双向口

    Uart_Init(115200UL); // 初始化UART,波特率115200

    UartSendString("FCXi!\n");

    while (1)
    {
        if(rptr != wptr)
        {
            UartSend(buf[rptr++]);
            rptr &= 0x0F;
            UartSendString("\r\n");
        }
        // UartSendString("KCXi!\r\n");
        // delay_ms(1000);
    }
}

Uart.c

cpp 复制代码
#include "Uart.h"

__bit busy;
char wptr;
char rptr;
char buf[16];

// 定时器2 串口2
// 默认管脚 P12(RxD) P13(TxD)
void Uart_Init(uint32_t baudrate)
{
    EA = 0;         // 关闭总中断
    P_SW2 |= EAXFR; // 使能扩展寄存器访问
    AUXR |= T2x12;  // 定时器2为1T模式
    S2CON = 0x50;
    T2L = (65536 - (FOSC / baudrate + 2) / 4) & 0xFF;
    T2H = (65536 - (FOSC / baudrate + 2) / 4) >> 8;
    AUXR |= T2R; // 启动定时器2
    wptr = 0;
    rptr = 0;
    busy = 0;

    // 管脚配置
    // P_SW2 |= S2_S; // 选择为P42(RxD) P43(TxD)
    P_SW2 &= ~S2_S; // 选择为P12(RxD) P13(TxD). --- 默认配置(不写也可) ---

    // 明确使能串口2中断(关键:补充中断使能)
    IE2 |= 0x01; // 使能串口2中断(根据芯片手册确认位定义)
    EA = 1;      // 开启总中断
}

void UartIsr(void) __interrupt(8)
{
    if (S2CON & S2TI)
    {
        // S2TI = 0;
        S2CON &= ~S2TI; // 取值  0x02->1111_1101
        busy = 0;
    }
    if (S2CON & S2RI)
    {
        S2CON &= ~S2RI; // 取值  0x01->1111_1110
        char recv_char = S2BUF;
        // 缓冲区保护:只有wptr和rptr不同时,才写入(避免覆盖未读数据)
        char next_wptr = (wptr + 1) & 0x0F;
        if (next_wptr != rptr)
        {
            buf[wptr] = recv_char;
            wptr = next_wptr;
        }
    }
}

void UartSend(char c)
{
    while (busy)
        ;
    busy = 1;
    S2BUF = c;
}

void UartSendString(char *str)
{
    while (*str)
    {
        UartSend(*str++);
    }
}

效果!

注意事项!

中断函数在外部文件写后需要在 main函数主文件中 继承函数名!

1,extern void 中断函数名(void)__interrupt(x);

2,或者在响应头文件声明!

相关推荐
于小猿Sup9 小时前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶
chao18984411 小时前
STM32 HAL库驱动AT24C02 EEPROM例程
stm32·单片机·嵌入式硬件
不会武功的火柴12 小时前
SystemVerilog语法(8)-有限状态机(FSM)
嵌入式硬件·fpga开发·自动化·ic验证·rtl·uvm方法学
猫猫的小茶馆14 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
feifeigo12314 小时前
STM32矩阵键盘驱动(库函数版)实现
stm32·矩阵·计算机外设
嵌入式小站15 小时前
STM32 零基础可移植教程 05:按键消抖,为什么按一次会触发好几次
chrome·stm32·嵌入式硬件
czhaii15 小时前
跟我动手学FX系列PLC GX2环境
嵌入式硬件
拾知_H16 小时前
STM32/Delay延时函数编程思路
stm32·单片机·时钟·延时
2zcode17 小时前
基于STM32的智能扫地机器人设计与实现
stm32·嵌入式硬件·机器人
jllllyuz18 小时前
单相并网逆变器控制代码实现(STM32版)
stm32·单片机·嵌入式硬件