#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1 = P3^0;
sbit key2 = P3^1;
sbit key3 = P3^2;
sbit key4 = P3^3;
sbit ir = P3^7;//红外发射端口
uint count,endcount;//count用于计数,endcount用于停止计数
uchar ad1 = ~0x55,ad2 = ~0x55;//地址码固定不变
void set_timer0 () {
TMOD = 0x02;
TH0 = 0xdb;//37us进入一次中断
TL0 = 0xdb;
ET0 = 1;
TR0 = 1;//开始计数;
EA = 1;
}
void delay(uint i) {
while(i--);
}
void duan_la(uchar duan) {
P0 = duan;
}
void wei_la(uchar wei) {
P2 = wei;
}
void disp_play(uchar dat1,uchar dat2) {
wei_la(1);
duan_la((~ad1)/16);
delay(200);
wei_la(2);
duan_la((~ad1)%16);
delay(200);
wei_la(4);
duan_la((~ad2)/16);
delay(200);
wei_la(8);
duan_la((~ad2)%16);
delay(200);
wei_la(16);
duan_la(dat1/16);
delay(200);
wei_la(32);
duan_la(dat1%16);
delay(200);
wei_la(64);
duan_la(dat2/16);
delay(200);
wei_la(128);
duan_la(dat2%16);
delay(200);
}
uchar send_key() {
if(key1 == 0) {
delay(10);
if(key1 == 0) {
return 1;
while(!key1);
}
}
if(key2 == 0) {
delay(10);
if(key2 == 0) {
return 2;
while(!key2);
}
}
if(key3 == 0) {
delay(10);
if(key3 == 0) {
return 3;
while(!key3);
}
}
if(key4 == 0) {
delay(10);
if(key4 == 0) {
return 4;
while(!key4);
}
}
return 0;
}
void send_date(uchar date) {
int i ;
uchar adder1 = ad1 ; //地址码,固定不变的码
uchar adder2 = ad2 ; //地址码,固定不变的码
uchar irdate = date;
/*9ms引导码*/
endcount = 223;
count = 0;
ir = 0;
while(count<endcount);//9ms引导码低电平
//发送4.5ms的结果码
endcount = 117;
count = 0;
ir = 1;
while(count<endcount);//4.5ms引导码高电平
for(i = 0 ; i<8 ; i++) {//发送第一个地址码
endcount = 10;
count = 0;
ir = 0;
while(count<endcount); //先发送0.56ms红外波(即编码中0.56ms的低电平)
if(adder1&0x01) {
endcount = 15;//如果为0发0.56ms高电平
}else {
endcount = 41;//如果为1发1.125高电平
}
count = 0;
ir = 1;
while(count<endcount);
adder1 >>= 1;
}
for(i = 0 ; i<8 ; i++) {//发送第二个地址码
endcount = 10;
count = 0;
ir = 0;
while(count<endcount);
if(adder2&0x01) {
endcount = 15;//如果为0发0.56ms高电平
}else {
endcount = 41;//如果为1发1.125低电平
}
count = 0;
ir = 1;
while(count<endcount);
adder2 >>= 1;
}
irdate = ~irdate;
for(i = 0 ; i<8 ; i++) {//发送数据码
endcount = 10;
count = 0;
ir = 0;
while(count<endcount);
if(irdate&0x01) {
endcount = 15;//如果为0发0.56ms高电平
}else {
endcount = 41;//如果为1发1.125低电平
}
count = 0;
ir = 1;
while(count<endcount);
irdate >>= 1;
}
irdate = date;
for(i = 0 ; i<8 ; i++) {//发送数据反码
endcount = 10;
count = 0;
ir = 0;
while(count<endcount);
if(irdate&0x01) {
endcount = 15;//如果为0发0.56ms高电平
}else {
endcount = 41;//如果为1发1.125低电平
}
count = 0;
ir = 1;
while(count<endcount);
irdate >>= 1;
}
endcount = 10;
count = 0;
ir = 0;
while(count<endcount);
ir =1;
}
void main() {
uchar key,play_date2,play_date1;
count = 0;
ir = 1;
set_timer0();
while(1) {
key = send_key();
if(key == 1) {
send_date(0x86);
play_date1 = 0x86;
play_date2 = 0x79;
}else if(key == 2) {
send_date(0x97);
play_date1 = 0x97;
play_date2 = 0x68;
}else if(key == 3) {
send_date(0x68);
play_date1 = 0x68;
play_date2 = 0x97;
}else if(key == 4){
send_date(0x66);
play_date1 = 0x66;
play_date2 = 0x99;
}
disp_play(play_date1,play_date2);
}
}
void timer0() interrupt 1 {
count++;
}
- 1
- 2
- 3
- 4
前往页