单片机串口使能中断C语言,STC15w4k58s4单片机4串口中断收发通信程序源码(C语言和汇编都有)...

本文提供了一个使用C语言实现的STC15w4k58s4单片机4个串口全双工中断方式通信的程序,详细定义了各个串口的波特率,并通过中断处理接收和发送数据。程序包括接收缓冲区管理、波特率配置和中断服务函数。
摘要由CSDN通过智能技术生成

/*------------------------------------------------------------------*/

/* --- STC MCU International Limited -------------------------------*/

/* --- STC 1T Series MCU RC Demo -----------------------------------*/

/* If you want to use the program or the program referenced in the  */

/* article, please specify in which data and procedures from STC    */

/*------------------------------------------------------------------*/

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

#define MAIN_Fosc                22118400L        //定义主时钟

//#define MAIN_Fosc                11059200L        //定义主时钟

#include        "STC15Fxxxx.H"

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

4串口全双工中断方式收发通讯程序。

通过PC向MCU发送数据, MCU收到后通过串口把收到的数据原样返回.

默认参数:

所有设置均为 1位起始位, 8位数据位, 1位停止位, 无校验.

每个串口可以使用不同的波特率.

串口1(P3.0 P3.1): 115200bps.

串口2(P1.0 P1.1):  57600bps.

串口3(P0.0 P0.1):  38400bps.

串口4(P0.2 P0.3):  19200bps.

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

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

#define        RX1_Length        128                /* 接收缓冲长度 */

#define        RX2_Length        128                /* 接收缓冲长度 */

#define        RX3_Length        128                /* 接收缓冲长度 */

#define        RX4_Length        128                /* 接收缓冲长度 */

#define        UART_BaudRate1        115200UL         /* 波特率 */

#define        UART_BaudRate2         57600UL         /* 波特率 */

#define        UART_BaudRate3         38400UL         /* 波特率 */

#define        UART_BaudRate4         19200UL         /* 波特率 */

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

u8        xdata        RX1_Buffer[RX1_Length];        //接收缓冲

u8        xdata        RX2_Buffer[RX2_Length];        //接收缓冲

u8        xdata        RX3_Buffer[RX3_Length];        //接收缓冲

u8        xdata        RX4_Buffer[RX4_Length];        //接收缓冲

u8        TX1_read,RX1_write;        //读写索引(指针).

u8        TX2_read,RX2_write;        //读写索引(指针).

u8        TX3_read,RX3_write;        //读写索引(指针).

u8        TX4_read,RX4_write;        //读写索引(指针).

bit        B_TX1_Busy,B_TX2_Busy,B_TX3_Busy,B_TX4_Busy;        // 发送忙标志

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

void        UART1_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.

void        UART2_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.

void        UART3_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.

void        UART4_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.

void         PrintString1(u8 *puts);

void         PrintString2(u8 *puts);

void         PrintString3(u8 *puts);

void         PrintString4(u8 *puts);

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

void main(void)

{

P0n_standard(0xff);        //设置为准双向口

P1n_standard(0xff);        //设置为准双向口

P2n_standard(0xff);        //设置为准双向口

P3n_standard(0xff);        //设置为准双向口

P4n_standard(0xff);        //设置为准双向口

P5n_standard(0xff);        //设置为准双向口

UART1_config(1);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.

UART2_config(2);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.

UART3_config(3);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.

UART4_config(4);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.

EA = 1;

PrintString1("S

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值