|
單片機(jī)C語言實(shí)例-393-本站原創(chuàng) VB遙控播放器紅外遙控解碼
#include <regX52.h>
#define c(x) (x*110592/120000)
sbit Ir_Pin=P3^2;
sbit beep=P3^6;
#define INBUF_LEN 4 //數(shù)據(jù)長度
unsigned char inbuf1[INBUF_LEN]={'0','0','0','0'};//發(fā)送緩沖區(qū)
unsigned char inbuf2[50];//接收緩沖區(qū)
unsigned char count3;
void init_serialcomm( void )
{
SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
T2CON=0x30;
TH2=0x00;
TL2=0x00;
RCAP2H=0xFF;
RCAP2L=0xDC;
TR2=1;
}
//向串口發(fā)送一個(gè)字符
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0 );
TI= 0 ;
}
//向串口發(fā)送一個(gè)字符串,strlen 為該字符串長度
void send_string_com( unsigned char *str, unsigned int strlen)
{
unsigned int k= 0 ;
do
{
send_char_com(*(str + k));
k++;
} while (k < strlen);
}
//串口接收中斷函數(shù)
void serial () interrupt 4 using 3
{
if (RI) //RI==開始接收
{
unsigned char ch;
RI = 0 ; //軟件RI=0
ch=SBUF;
if (ch> 1 )
{
count3= 0 ;
inbuf2[count3]=ch;
}
else
{
count3++;
inbuf2[count3]=ch;
}
}
}
unsigned char code Led_Tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //共陽極數(shù)碼顯示碼0-F.
unsigned char code Led_Sel[]={0xef,0xdf,0xbf,0x7f};
unsigned char dis_8[17]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','-'};
unsigned char Led_Buf[4]; //顯示緩沖區(qū)
char Led_Index; //位選
unsigned char Ir_Buf[4]; //用于保存解碼結(jié)果
void delay_50ms(unsigned int t)
{
unsigned int j;
for(;t>0;t--)
for(j=6245;j>0;j--)
{;}
}
//==============================================================
//數(shù)碼管掃描
timer0() interrupt 1 using 1
{
TL0=65536-1000;
TH0=(65536-1000)/256; //定時(shí)器0設(shè)定約1000us中斷一次,用于數(shù)碼管掃描
//P0=0xff;
P2=Led_Sel[Led_Index]; //位選
P0=Led_Tab[Led_Buf[Led_Index]]; //段選
if(++Led_Index>3) Led_Index=0; //四個(gè)掃描完了,到第一個(gè)數(shù)碼管
}
//==============================================================
unsigned int Ir_Get_Low()
{
TL1=0;
TH1=0;
TR1=1;
while(!Ir_Pin && (TH1&0x80)==0);
TR1=0;
return TH1*256+TL1;
}
//=============================================================
unsigned int Ir_Get_High()
{
TL1=0;
TH1=0;
TR1=1;
while(Ir_Pin && (TH1&0x80)==0);
TR1=0;
return TH1*256+TL1;
}
//==============================================================
main()
{
unsigned int temp;
char i,j;
Led_Index=1;
init_serialcomm(); //初始化串口
TMOD=0x11;
TL0=65536-1000;
TH0=(65536-1000)/256; //定時(shí)器0設(shè)定約1000us中斷一次,用于數(shù)碼管掃描
EA=1;
ET0=1;
TR0=1;
Led_Buf[0]=0;
Led_Buf[1]=0;
Led_Buf[2]=0;
Led_Buf[3]=0; //顯示區(qū)設(shè)成0
do{
restart:
while(Ir_Pin);
temp=Ir_Get_Low();
if(temp<c(8500) || temp>c(9500)) continue;//引導(dǎo)脈沖低電平9000
temp=Ir_Get_High();
if(temp<c(4000) || temp>c(5000)) continue;//引導(dǎo)脈沖高電平4500
for(i=0;i<4;i++) //4個(gè)字節(jié)
for(j=0;j<8;j++) //每個(gè)字節(jié)8位
{
temp=Ir_Get_Low();
if(temp<c(200) || temp>c(800)) goto restart;
temp=Ir_Get_High();
if(temp<c(200) || temp>c(2000)) goto restart;
Ir_Buf>>=1;
if(temp>c(1120)) Ir_Buf|=0x80;
}
Led_Buf[0]=(Ir_Buf[2]/16)&0xf;
Led_Buf[1]=Ir_Buf[2]&0xf;
Led_Buf[2]=(Ir_Buf[3]/16)&0xf; //顯示結(jié)果
Led_Buf[3]=Ir_Buf[3]&0xf;
inbuf1[0]=dis_8[Led_Buf[0]];
inbuf1[1]=dis_8[Led_Buf[1]];
inbuf1[2]=0x0d;
inbuf1[3]=0x0a;
send_string_com(inbuf1,INBUF_LEN);
beep=0;
delay_50ms(2);
beep=1;
}
while(1);
}
更多詳情參考附件文檔
+08:00C135聯(lián)盟網(wǎng)8922.png (34.03 KB, 下載次數(shù): 1)
下載附件
保存到相冊
2022-4-18 10:15 上傳
|
|