芯启元 发表于 2026-6-29 15:55:46

13-8个串口同时使用收发测试程序,STC32G144K246实验箱演示程序

13-8个串口同时使用收发测试程序,STC32G144K246实验箱演示程序
主程序main.C代码如下(汇编版本在附件中):

/*---------------------------------------------------------------------*/
/* --- Web: www.STCAI.com ---------------------------------------------*/
/* --- BBS: www.STCAIMCU.com-----------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序      */
/*---------------------------------------------------------------------*/

/*************功能说明    **************

本例程基于STC32G144K246为主控芯片的实验箱进行编写测试。

使用Keil C251编译器,Memory Model推荐设置XSmall模式,默认定义变量在edata,单时钟存取访问速度快。

edata建议保留1K给堆栈使用,空间不够时可将大数组、不常用变量加xdata关键字定义到xdata空间。

8组串口同时进行收发测试,每组串口将收到的数据原样输出。

用USB线连接USB2 Type-C接口到电脑,可识别出2个CDC接口。

使用串口助手软件打开CDC1、CDC2对应的COM口,可以分别与串口1、串口2进行收发通信。

下载时, 选择时钟 48MHz (用户可自行修改频率)。

******************************************/

#include "uart.h"

/****************用户定义宏****************/


/****************本地常量声明**************/


/****************本地变量声明**************/


/****************本地函数声明**************/


/***************** 主函数 *********************/
void main(void)
{
    u8 i;
       
    EAXFR = 1; //扩展寄存器(XFR)访问使能
    CKCON = 0; //提高访问XRAM速度

    P0M1 = 0x00;   P0M0 = 0xaa;   //设置P0.1,P0.3,P0.5,P0.7推挽输出
    P1M1 = 0x84;   P1M0 = 0x4a;   //设置P1.1,P1.3,P1.6推挽输出,设置P1.2,P1.7高阻输入
    P2M1 = 0x00;   P2M0 = 0xaa;   //设置P2.1,P2.3,P2.5,P2.7推挽输出
    P3M1 = 0x00;   P3M0 = 0x32;   //设置P3.1,P3.4,P3.5推挽输出
    P5M1 = 0x00;   P5M0 = 0xff;   //设置为推挽输出,设置P5.1,P5.3推挽输出
    P7M1 = 0x00;   P7M0 = 0xfa;   //设置P7.1,P7.3~P7.7推挽输出
    P8M1 = 0x00;   P8M0 = 0x80;   //设置P8.7推挽输出

    P0PU |= 0x55;       //P0.0,P0.2,P0.4,P0.6开启内部上拉
    P1PU |= 0x01;       //P10开启内部上拉
    P3PU |= 0x40;       //P36开启内部上拉
    P5PU |= 0x05;       //P5.0,P5.2开启内部上拉
    P8PU |= 0x40;       //P86开启内部上拉

    UART1_config(1);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
    UART2_config(2);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
    UART3_config(3);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
    UART4_config(4);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
    UART5_config(5);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer5做波特率.
    UART6_config(6);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer6做波特率.
    UART7_config(7);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer7做波特率.
    UART8_config(8);    // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer8做波特率.
    EA = 1;   //打开总中断

    printf("STC32G UART Test Programme!\r\n");//UART1发送一个字符串

    while (1)
    {
      if(B_RX1_TimeOut)   //超时计数
      {
            B_RX1_TimeOut = 0;

            for(i=0; i<RX1_Cnt; i++)    UART1_TxByte(RX1_Buffer);    //把收到的数据原样返回
            RX1_Cnt= 0;   //清除字节数
      }

      if(B_RX2_TimeOut)   //超时计数
      {
            B_RX2_TimeOut = 0;

            for(i=0; i<RX2_Cnt; i++)    UART2_TxByte(RX2_Buffer);    //把收到的数据原样返回
            RX2_Cnt= 0;   //清除字节数
      }

      if(B_RX3_TimeOut)   //超时计数
      {
            B_RX3_TimeOut = 0;

            for(i=0; i<RX3_Cnt; i++)    UART3_TxByte(RX3_Buffer);    //把收到的数据原样返回
            RX3_Cnt= 0;   //清除字节数
      }

      if(B_RX4_TimeOut)   //超时计数
      {
            B_RX4_TimeOut = 0;

            for(i=0; i<RX4_Cnt; i++)    UART4_TxByte(RX4_Buffer);    //把收到的数据原样返回
            RX4_Cnt= 0;   //清除字节数
      }

      if(B_RX5_TimeOut)   //超时计数
      {
            B_RX5_TimeOut = 0;

            for(i=0; i<RX5_Cnt; i++)    UART5_TxByte(RX5_Buffer);    //把收到的数据原样返回
            RX5_Cnt= 0;   //清除字节数
      }

      if(B_RX6_TimeOut)   //超时计数
      {
            B_RX6_TimeOut = 0;

            for(i=0; i<RX6_Cnt; i++)    UART6_TxByte(RX6_Buffer);    //把收到的数据原样返回
            RX6_Cnt= 0;   //清除字节数
      }

      if(B_RX7_TimeOut)   //超时计数
      {
            B_RX7_TimeOut = 0;

            for(i=0; i<RX7_Cnt; i++)    UART7_TxByte(RX7_Buffer);    //把收到的数据原样返回
            RX7_Cnt= 0;   //清除字节数
      }

      if(B_RX8_TimeOut)   //超时计数
      {
            B_RX8_TimeOut = 0;

            for(i=0; i<RX8_Cnt; i++)    UART8_TxByte(RX8_Buffer);    //把收到的数据原样返回
            RX8_Cnt= 0;   //清除字节数
      }
    }
}



页: [1]
查看完整版本: 13-8个串口同时使用收发测试程序,STC32G144K246实验箱演示程序