用1602显示解码遥控器,程序出现乱码!求助。
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar irtime;
- uchar startflag;
- uchar irdata[33];
- uchar bitnum;
- uchar irreceok;
- uchar ircode[4];
- uchar irprosok;
- uchar disp[8];
- //++++++++++++++++++++++++++++++++++++++++++++++++++
- //1602
- sbit rs=P2^6;
- sbit rw=P2^5;
- sbit e=P2^7;
- char i,j,k;
- void delay(char a)
- {
- for(k=0;k<a;k++)
- for(i=0;i<=110;i++);
- }
- void write(char r,char com)
- {
- rw=0;
- rs=r;
- P0=com;
- delay(1);
- e=1;
- delay(1);
- e=0;
- }
- void init()
- {
- e=0;
- write(0,0x38);
- write(0,0x0c);
- write(0,0x06);
- write(0,0x01);
- }//+++++++++++++++++++++++++++++++++++
- void timer0init(void)
- {
- TMOD=0x02;
- TH0=0x00;
- TL0=0x00;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void int0init(void)
- {
- IT0=1;
- EX0=1;
- EA=1;
- }
- void irwork(void)
- {
- char i;
- disp[0]=ircode[0]/16;
- disp[1]=ircode[0]%16;
- disp[2]=ircode[1]/16;
- disp[3]=ircode[1]%16;
- disp[4]=ircode[2]/16;
- disp[5]=ircode[2]%16;
- disp[6]=ircode[3]/16;
- disp[7]=ircode[3]%16;
- for(i=0;i<8;i++)
- {
- if(disp<i>>9)
- disp<i>=disp<i>+0x37;//实验
- else
- disp<i>=disp<i>+0x30;
- }
- }
- void irpros(void) //0x1c
- {
- uchar k,i,j;
- uchar value;
- k=1;
- for(j=0;j<4;j++)
- {
- for(i=0;i<8;i++)
- {
- value=value>>1; //7
- if(irdata[k]>6) //8
- {
- value=value | 0x80;
- }
- k++;
- }
- ircode[j]=value;
- }
- irprosok=1;
- }
- void main()
- {
- char i;
- timer0init();
- int0init();
- if(irreceok)
- {
- irpros();
- irreceok=0;
- }
- if(irprosok)
- {
- irwork();
- irprosok=0;
- }
- init();
- write(0,0x80);
- for(i=0;i<8;i++)
- {
- write(1,disp<i>);
- }
- while(1);
- }
- void timer0 () interrupt 1
- {
- irtime++; //255
- }
- void int0 () interrupt 0
- {
- if(startflag)
- {
- if(irtime>52) //检测引导码
- {
- bitnum=0;
- }
- irdata[bitnum]=irtime;
- irtime=0;
- bitnum++;
- if(bitnum==33)
- {
- bitnum=0;
- irreceok=1;
- }
- }
- else
- {
- startflag=1;
- irtime=0;
- }
- }</i></i></i></i></i></i>
采集
采集
亲 可以把注释给写下来吗? 看起来很费力了
OK我小改后 变成数据显示成功但是 同一个键按多次会有不同的值。请大家看看。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar irtime;
uchar startflag;
uchar irdata[33];
uchar bitnum;
uchar irreceok;
uchar ircode[4];
uchar irprosok;
uchar disp[8];
//++++++++++++++++++++++++++++++++++++++++++++++++++
//1602
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
char i,j,k;
void delay(char a)//延时
{
for(k=0;k<a;k++)
for(i=0;i<=110;i++);
}
void write(char r,char com)//1602写控制码和数据
{
rw=0;
rs=r;
P0=com;
delay(1);
e=1;
delay(1);
e=0;
}
void init()//1602初始化
{
e=0;
write(0,0x38);
write(0,0x0c);
write(0,0x06);
write(0,0x01);
}
//+++++++++++++++++++++++++++++++++++
void timer0init(void)//定时器0初始化
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
EA=1;
TR0=1;
}
void int0init(void)//外部中断初始化
{
IT0=1;
EX0=1;
EA=1;
}
void irwork(void)//将2进制转化为16进制 并把高位和地位分开转化为ASCII码
{
char i;
disp[0]=ircode[0]/16;
disp[1]=ircode[0]%16;
disp[2]=ircode[1]/16;
disp[3]=ircode[1]%16;
disp[4]=ircode[2]/16;
disp[5]=ircode[2]%16;
disp[6]=ircode[3]/16;
disp[7]=ircode[3]%16;
for(i=0;i<8;i++)
{
if(disp[i]>9)
disp[i]=disp[i]+0x37;
else
disp[i]=disp[i]+0x30;
}
}
void irpros(void) //通过检测时间长度 判断电平 并存在数组里
{
uchar k,i,j;
uchar value;
k=1;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{
value=value>>1; //7
if(irdata[k]>6) //8
{
value=value | 0x80;
}
k++;
}
ircode[j]=value;
}
irprosok=1;//完成标志物
}
void main()
{
char i;
timer0init();
int0init();
while(1)
{
if(irreceok)//电平提取完
{
irpros();
irreceok=0;
}
if(irprosok)//数据转化完成
{
irwork();
irprosok=0;
}
init();
write(0,0x80);
for(i=0;i<8;i++)
{
write(1,disp[i]);
}
}
}
void timer0 () interrupt 1//定时器0中断
{
irtime++; //检测电平持续时间
}
void int0 () interrupt 0//外部中断 下降沿促发
{
if(startflag)//开始标志物(不重要)
{
if(irtime>52) //检测引导码
{
bitnum=0;
}
irdata[bitnum]=irtime;//存储各电平时间
irtime=0;
bitnum++;
if(bitnum==33)//全部数据接受完
{
bitnum=0;
irreceok=1;//完成标志物
}
}
else
{
startflag=1;
irtime=0;
}
}