#include "system.h" #include "stc8a8k64s4a12.h" #include "Timer0.h" sbit K1=P3^4; void Init_Serial(void) { SCON=0X50; PCON=0X80; ES=1; EA=1; // TMOD=0X20; TMOD |= 0x20; AUXR=0X00; TL1=243; //9600 TH1=243; TR1=1; } void UART_SendByte(unsigned char Byte) { SBUF=Byte; while(TI==0); TI=0; } void main() { System_Init(); P3_Mode_PullUp(PIN_0|PIN_1); P0_Mode_OUT_PP(0XFF); Init_Serial(); Timer0Init(); while(1) { } } void Timer0_Poutine() interrupt 1//������P3^4������ж� { static unsigned int T0Count; TL0=(65536-3)%256; TH0=(65536-3)/256; P0=~P0; UART_SendByte(0xaa); } void s_int(void) interrupt 4 { if(TI==1) { TI=0; } if(RI==1) { RI=0; if(SBUF==0XAA){P2=0XFE;} if(SBUF==0X55){P2=0XFF;} UART_SendByte(SBUF); } }标注每一行代码的注释
时间: 2023-11-08 09:04:09 浏览: 58
// 引入系统头文件
#include "system.h"
// 引入STC8A8K64S4A12芯片的头文件
#include "stc8a8k64s4a12.h"
// 引入Timer0头文件
#include "Timer0.h"
// 定义P3^4为按键K1
sbit K1=P3^4;
// 初始化串口
void Init_Serial(void) {
SCON=0X50; // 设置串口为工作模式1,8位数据,可变波特率
PCON=0X80; // 波特率加倍
ES=1; // 使能串口中断
EA=1; // 使能总中断
TMOD=0X20; // 设置Timer1为工作模式2
TL1=243; //9600波特率下,Timer1初值为243
TH1=243; // 9600波特率下,Timer1重载值为243
TR1=1; // 启动Timer1
}
// 串口发送一个字节
void UART_SendByte(unsigned char Byte) {
SBUF=Byte; // 把数据保存到SBUF寄存器
while(TI==0); // 等待发送完成
TI=0; // 清除中断标志位
}
void main() {
System_Init(); // 系统初始化
P3_Mode_PullUp(PIN_0|PIN_1); // 设置P3.0和P3.1为上拉输入模式
P0_Mode_OUT_PP(0XFF); // 设置P0口为输出模式
Init_Serial(); // 初始化串口
Timer0Init(); // 初始化定时器0
while(1) { // 主循环
}
}
// 定时器0中断服务函数
void Timer0_Poutine() interrupt 1 {
static unsigned int T0Count; // 定义静态变量,用于计数
TL0=(65536-3)%256; // 设置定时器0初值
TH0=(65536-3)/256; // 设置定时器0重载值
P0=~P0; // 取反P0口状态
UART_SendByte(0xaa); // 发送0xaa,用于测试
}
// 串口中断服务函数
void s_int(void) interrupt 4 {
if(TI==1) { // 判断是否为发送中断
TI=0; // 清除中断标志位
}
if(RI==1) { // 判断是否为接收中断
RI=0; // 清除中断标志位
if(SBUF==0XAA){P2=0XFE;} // 如果收到0xaa,P2口输出0xFE
if(SBUF==0X55){P2=0XFF;} // 如果收到0x55,P2口输出0xFF
UART_SendByte(SBUF); // 发送接收到的数据
}
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)