#include <reg52.h>
#include <intrins.h>
#include "./delay/delay.h"
unsigned int irtime;
bit start_flag = 0;
bit irok = 0;
unsigned char irdata[33];
unsigned char bitnum = 0;
void time0_init()
{
EA = 1;
TMOD |= 0x02;
TH0 = 0;
ET0 = 1; //可以进入中断
TR0 = 1; //开始计数
}
void int0_init()
{
IT0 = 1; //设置外部中断的触发方式,下降沿触发
EA = 1; //打开总中断
EX0 = 1; //打开外部中断0
}
void timer0_isr() interrupt 1
{
irtime++;
}
void int0_isr() interrupt 0 //打开中断服务程序
{
if(start_flag)
{
if((irtime >= 40) && (irtime <= 60))
{
bitnum = 0;
}
irdata[bitnum] = irtime + '0';
bitnum++;
irtime = 0;
if(bitnum == 33)
{
irok = 1;
irdata[33] = '\0';
bitnum = 0;
start_flag = 0;
}
}
else
{
irtime = 0;
start_flag = 1;
}
//irtime++; //0.256ms 引导码 : (9 + 4.5) / 0.256 = 53 0码 : 4 1码 : 9
}
void uart_init()
{
SCON = 0x50; //设置出口工作方式,打开接收允许
//SM0 = 0; SM1 = 1; SM2 = 0;REN = 1;
TMOD |= 0x20; //设置定时器1为工作方式2
TH1 = 0xfd; //波特率为9600bits/s
//ET1 = 1; //打开定时器1中断允许
TR1 = 1; //开始计数
}
void uart_send_byte(unsigned char byte)
{
SBUF = byte;
//TI位自动置1,手动清0;
while(!TI); //while(TI != 1);
TI = 0;
}
void uart_send_string(char *buf)
{
while(*buf != '\0')
{
uart_send_byte(*buf);
buf++;
}
}
unsigned char buf[33];
void main()
{
time0_init();
int0_init();
uart_init();
while(1)
{
if(irok == 1)
{
for(bitnum = 0;bitnum < 33;bitnum++)
{
if(irdata[bitnum+1] <= 6+ '0')
{
buf[bitnum] = 0 + '0';
}
else
{
buf[bitnum] = 1 + '0';
}
}
buf[32] = '\n';
//if(irok == 1)
//{
for(bitnum = 0;bitnum < 33;bitnum++)
{
uart_send_byte(buf[bitnum]);
}
// uart_send_string(irdata);
// uart_send_byte('\n');
irok = 0;
}
}
}
串口返回红外键值
最新推荐文章于 2024-09-12 10:31:54 发布