我想实现按一下遥控器,在16×16点阵上显示一个字,但是我得按很久才按出字来,按出字后一切正常(按一下显示一个字)。
代码如下:
#include<reg51.h>
#include<intrins.h>
sbit SIGNAL=P3^2; //控制遥控接收数据信号
sbit SRCLK=P3^6; //74HC595模拟SRCLK管脚
sbit RCLK=P3^5; //74HC595模拟RCK管脚
sbit SER=P3^4; //74HC595模拟SER管脚
#define uchar unsigned char
#define uint unsigned int
#define A 3000
uchar p=0; //先把p赋值为0
uchar value[6]; //用以红外接收用户码数据码数组
uchar time; //时间变量用以红外接收
void delay10us(void) // 10us
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
void delay100us(void) // 100us
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=47;a>0;a--);
}
void delay7ms(void) // 7ms
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=26;b>0;b--)
for(a=133;a>0;a--);
}
void delay(uchar i)
{
while(i--);
}
uchar code tablewei[]= //16×16点阵位选
{
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
};
uchar code tableduan1[]={ //16×16点阵段选
/*-- 文字: 祝 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x08,0x08,0x89,0xEE,0x98,0x00,0x7E,0x42,0xC2,0x42,0x42,0xC2,0x42,0x7E,0x00,0x00,
0x02,0x01,0x00,0xFF,0x80,0x43,0x20,0x18,0x07,0x00,0x00,0x3F,0x40,0x40,0x78,0x00,
};
uchar code tableduan2[]={
/*-- 文字: 你 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,
};
uchar code tableduan3[]={
/*-- 文字: 生 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x80,0x40,0x30,0x1E,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,
0x40,0x40,0x42,0x42,0x42,0x42,0x42,0x7F,0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00,
};
uchar code tableduan4[]={
/*-- 文字: 日 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x00,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xFF,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xFF,0x00,0x00,0x00,0x00,
};
uchar code tableduan5[]={
/*-- 文字: 快 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0xE0,0x00,0xFF,0x10,0x20,0x08,0x08,0x08,0xFF,0x08,0x08,0xF8,0x00,0x00,0x00,
0x01,0x00,0x00,0xFF,0x00,0x81,0x41,0x31,0x0D,0x03,0x0D,0x31,0x41,0x81,0x81,0x00,
};
uchar code tableduan6[]={
/*-- 文字: 乐 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x00,0xE0,0x9C,0x84,0x84,0x84,0xF4,0x82,0x82,0x83,0x82,0x80,0x80,0x00,0x00,
0x00,0x20,0x10,0x08,0x06,0x40,0x80,0x7F,0x00,0x00,0x02,0x04,0x08,0x30,0x00,0x00,
};
void Hc595SendByte(uchar dat1,uchar dat2,uchar dat3,uchar dat4) //74HC595芯片扩展显示汉字函数
{
uchar a;
SRCLK = 1;
RCLK = 1;
for(a=0;a<8;a++) //发送8位数
{
SER = dat1 >> 7; //从最高位开始发送
dat1 <<= 1;
SRCLK = 0; //发送时序
_nop_();
_nop_();
SRCLK = 1;
}
for(a=0;a<8;a++) //发送8位数
{
SER = dat2 >> 7; //从最高位开始发送
dat2 <<= 1;
SRCLK = 0; //发送时序
_nop_();
_nop_();
SRCLK = 1;
}
for(a=0;a<8;a++) //发送8位数
{
SER = dat3 >> 7; //从最高位开始发送
dat3 <<= 1;
SRCLK = 0; //发送时序
_nop_();
_nop_();
SRCLK = 1;
}
for(a=0;a<8;a++) //发送8位数
{
SER = dat4 >> 7; //从最高位开始发送
dat4 <<= 1;
SRCLK = 0; //发送时序
_nop_();
_nop_();
SRCLK = 1;
}
RCLK = 0;
_nop_();
_nop_();
RCLK = 1;
}
void init() //为红外遥控初始化
{
IT0=1;
EX0=1;
EA=1;
SIGNAL=1;
}
void main()
{
uchar i; //动态显示16×16点阵显示汉字
init();
while(p==5) //按下遥控按钮触发p+=5
{
for(i=0;i<16;i++)
{
Hc595SendByte(~tablewei[i+16],~tablewei[i],tableduan1[16+i],tableduan1[i]); //显示'祝'
delay(10);
}
}
while(p==10)
{
delay(A);
for(i=0;i<16;i++)
{
Hc595SendByte(~tablewei[i+16],~tablewei[i],tableduan2[16+i],tableduan2[i]);
delay(10);
}
}
while(p==15)
{
for(i=0;i<16;i++)
{
Hc595SendByte(~tablewei[i+16],~tablewei[i],tableduan3[16+i],tableduan3[i]);
delay(10);
}
}
while(p==20)
{
for(i=0;i<16;i++)
{
Hc595SendByte(~tablewei[i+16],~tablewei[i],tableduan4[16+i],tableduan4[i]);
delay(10);
}
}
while(p==25)
{
delay(A);
for(i=0;i<16;i++)
{
Hc595SendByte(~tablewei[i+16],~tablewei[i],tableduan5[16+i],tableduan5[i]);
delay(10);
}
}
while(p==30)
{
for(i=0;i<16;i++)
{
Hc595SendByte(~tablewei[i+16],~tablewei[i],tableduan6[16+i],tableduan6[i]);
delay(10);
}
}
}
void Read_Signal() interrupt 0 //外部中断函数用以接收遥控数据
{
uchar i,j;
uint count;
time=0;
delay7ms();
if(SIGNAL==0)
{
count =1000;
while((SIGNAL==0)&&(count>0)) //>9ms
{
delay10us();
count--;
}
if(SIGNAL==1)
{
count=500;
while((SIGNAL==1)&&(count>0)) //>4.5ms
{
delay10us();
count--;
}
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{
count=60;
while((SIGNAL==0)&&(count>0))
{
delay10us();
count--;
}
count=500;
while((SIGNAL==1)&&(count>0))
{
delay100us();
time++;
count--;
if(time>30)
{
return;
}
}
value[j]>>=1;
if(time>=8)
{
value[j]|=0x80;
}
time=0;
}
}
}
if(value[2]!=~value[3])
{
return;
}
delay(A);
p=p+5; /*按一下遥控触发p+5*/
}
}
|