AT89c52 wiegend

本文详细介绍了AT89X52微控制器中使用中断处理实现串口通信的方法,包括初始化UART、中断服务例程以及数据接收与处理流程。
AI助手已提取文章相关产品:

#include "at89x52.h"

typedef unsigned char BYTE;
typedef unsigned int WORD;

BYTE state;
BYTE wg0;
BYTE wg1;
BYTE wg2;
BYTE count;
BYTE cnt;
bit r_done;
bit firstbit;
bit lastbit;
bit D0_done;
bit D1_done;


sbit WG_IN1 = P3^2;
sbit WG_IN0 = P3^3;

void delay_30us(void)
{
TR0 = 0;
TH0 = (65536 - 20)/256; //定时30us
TL0 = ((65536 - 20)%256);
TF0 = 0;
ET0 = 0;
TR0 = 1;
while (!TF0) { ;}
TF0 = 0;
}

void Sys_init(void) //4800
{
SCON=0x50; // UART_mode=1,REN=1
PCON=0x00;
TMOD=0x21;
TL1=0xfa;
TH1=0xfa;
TR1=1;
// ES=1;
ES=0;
IT0=1;
IT1=1;
// IP=0x10;
EX0=1;
EX1=1;
EA=1;
}
void IT0_int(void) interrupt 0 //D1
{
if(count==0){
firstbit=1;
}else if(count<9){
wg0=(wg0<<1)|0x01;
}else if(count<17){
wg1=(wg1<<1)|0x01;
}else if(count<25){
wg2=(wg2<<1)|0x01;
}else {lastbit=1;r_done=1;}
count++;
if(count==26)
count=0;
}
void IT1_int(void) interrupt 2 //D0
{
if(count==0){
firstbit=0;
}else if(count<9){
wg0=(wg0<<1)&0xfe;
}else if(count<17){
wg1=(wg1<<1)&0xfe;
}else if(count<25){
wg2=(wg2<<1)&0xfe;
}else {lastbit=0;r_done=1;}
count++;
if(count==26)
count=0;
}

void T0_init(void)
{

// TMOD=0x01;
TH0=TL0=0;
ET0=1;
TR0=1;
IP=0x03;

}

void T0_Int(void) interrupt 1
{
if(cnt){
cnt--;
if(cnt==0){
count=0;
}
}
}

void wg_process(void)
{

// BYTE i;
BYTE sum=0;
SBUF=wg0;
while(TI==0);
TI=0;
SBUF=wg1;
while(TI==0);
TI=0;
SBUF=wg2;
while(TI==0);
TI=0;
}

void main(void)
{
Sys_init();
T0_init();
cnt=0;
while(1){
if(r_done){
r_done=0;
wg_process();
wg0=wg1=wg2=0;
}
}
}

您可能感兴趣的与本文相关内容

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值