51單片機-紅外遙控解碼
這段代碼仍然有問題,正在調(diào)試中,路過的大牛希望給與指導(dǎo):
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint irtime,startflag,i,bitnum,decodeok,receiveok;
uchar irdata[33],disp[8],vcode[4];
unsigned char code duan[]={ 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char code wei[]={ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
void delay(int k){
int i,j;
for(i = 0; i < k; i++)
for(j = 0; j < 0xff; j++)
;
}
void Time0_Init(){
EA = 1;
ET0 = 1;
TH0 = 0x00;
TL0 = 0x00;
TMOD = 0x02;
TR0 = 1;
}
void Int0_Init(){
EA = 1;
IT0 = 1;
EX0 = 1;
}
void Irpro(){
disp[0] = vcode[0] / 16; //把十六進制的數(shù)轉(zhuǎn)化成兩位顯示
disp[1] = vcode[0] % 16;
disp[2] = vcode[1] / 16;
disp[3] = vcode[1] % 16;
disp[4] = vcode[2] / 16;
disp[5] = vcode[2] % 16;
disp[6] = vcode[3] / 16;
disp[7] = vcode[3] % 16;
}
void display(){
for(i = 0; i < 8; i++){
P1 = wei[ i];
P2 = duan[disp[ i]];
delay(1);
}
}
void decode(){
uint k,i,j;
uchar value;
j = 1;
for(k = 0; k < 4; k++){
value = 0;
for(i = 0; i < 8; i++){
if(irdata[j]>6) //如果大于6個irtime是高電平
value |= 0x01 << i; j++;
}
vcode[k] = value;
}
decodeok = 1;
}
void main(){
Time0_Init(); //對定時器中斷進行初始化
Int0_Init(); //對下降沿中斷進行初始化
while(1){
if(receiveok == 1){ //如果接受成功進行解碼
receiveok = 0;
decode();
}
if(decodeok == 1){ //如果解碼成功,轉(zhuǎn)換一下格式準備顯示
decodeok = 0;
Irpro();
}
display();
}
}
void time0() interrupt 1{ //由于是模式2,8位自動重裝,所以一個irtime為256us
irtime++;
}
void int0() interrupt 0{
if(startflag){
if(irtime > 32){ //如果是引導(dǎo)信息,則bitnum清零
bitnum = 0;
}
irdata[bitnum] = irtime; //裝載數(shù)據(jù),包括1位引導(dǎo)位,16位用戶碼,8位數(shù)據(jù)碼和8位數(shù)據(jù)碼反碼
irtime = 0;
bitnum++;
if(bitnum == 33){ //裝載數(shù)據(jù)完成
bitnum = 0;
receiveok = 1;
}
}
else{
startflag = 1; //遇到第一個下降沿讓irtime為0,遇到第二個時計算用了多少個irtime
irtime = 0;
評論