STC12C5A60S2 单片机串口2的通信功能测试

编程入门 行业动态 更新时间:2024-10-12 08:23:12

STC12C5A60S2 单片机<a href=https://www.elefans.com/category/jswz/34/1769224.html style=串口2的通信功能测试"/>

STC12C5A60S2 单片机串口2的通信功能测试

根据手册说明,STC12C5A60S2 系列单片机可以直接使用 reg51.h 的头文件,只是在用到相应的特殊功能寄存器时,要做相应的定义即可。

笔记来自视频教程链接: /?spm_id_from=333.880.my_history.page.click&vd_source=b91967c499b23106586d7aa35af46413





开启两个串口,进行测试。

使用串口二的接收中断,要把串口二中断使能给加进来。

 IE2 = 0x01;             //Enable UART2 interrupt

uart2.c 的代码如下:

#include "uart2.h"extern void sendByte(unsigned char dat);void uart2_init(void)  //9600bps@11.0592MHz
{AUXR &= 0xF7;		//波特率不倍速S2CON = 0x50;		//8位数据,可变波特率AUXR &= 0xFB;		//定时器时钟12T模式BRT = 0xFD;			//设置定时重载值AUXR |= 0x10;		//启动独立波特率发射器IE2 = 0x01;             //Enable UART2 interrupt
}void uart2_SendByte(unsigned char dat)
{S2BUF = dat;while(!(S2CON & S2TI)); // S2CON & S2TI == 0 时,会一直等在这S2CON &= ~S2TI;         // 手动清 0
}#if 0
void uart2_SendString(unsigned char *dat)
{while(*dat != '\0') {uart2_SendByte(*dat++);}
}
#endifchar putchar(char c)
{uart2_SendByte(c);return c;
}/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8
{unsigned char dat;if (S2CON & S2RI){S2CON &= ~S2RI;     //Clear receive interrupt flagdat = S2BUF;sendByte(dat);}
}

uart2.h 的代码如下:

#ifndef _UART2_H_
#define _UART2_H_#include "stc12c5a60s2.h"#define S2RI  0x01          //S2CON.0
#define S2TI  0x02          //S2CON.1
#define S2RB8 0x04          //S2CON.2
#define S2TB8 0x08          //S2CON.3// 函数声明
extern void uart2_init(void);
extern void uart2_SendByte(unsigned char dat);
extern void uart2_SendString(unsigned char *dat);#endif

工程是在(基于串口超时接收用户自定义通讯协议的编程实现——协议内 CRC16 校验及接收应答处理)基础上改的,代码已上传至CSDN资料库。

12T – 12 分频 – 每12个时钟计数一次
1T – 不分频 – 每1个时钟计数一次

波特率设置为 115200

更多推荐

STC12C5A60S2 单片机串口2的通信功能测试

本文发布于:2024-02-07 08:32:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:串口   单片机   通信   功能   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!