attachment download/*****整版测试3:点阵+红外遥控*****/
//按位定义用杜邦线将HS0038接口接到单片机外部中断0
//也就是P3^2口
//此时按下相应的按键,速度会有相应的改变,同时LED也会闪烁
/****作者:爱因斯尼@SCUT***/
/****时间:2010.08.29******/
#include<reg52.h>//51单片机的头文件
#define uint unsigned int//必要的宏定义
#define uchar unsigned char
sbit DS=P1^0;//595串口数据输入端
sbit OE=P1^1;//使能端,低电平有效
sbit SH=P1^2;//移位寄存器的时钟输入
sbit ST=P1^3;//存储器的时钟输入
sbit bell=P1^4; //蜂鸣器
sbit lie=P1^7;//点阵的列扫描
sbit duan=P1^5;//数码管的段选端
sbit wei=P1^6;//数码管的位选端
uchar irtime;
uchar startflag;
uchar bitnum;
uchar irreceok;
uchar irprosok;
uchar ircode[4];
uchar irdata[33];
uchar disp[8];
uint circle_time=80;
uchar code table_ZIMO[]={
0xff,0x81,0x81,0x81,0x81,0x81,0x81,0xff,
0x00,0x7e,0x42,0x42,0x42,0x42,0x7e,0x00,
0x00,0x00,0x3c,0x24,0x24,0x3c,0x00,0x00,
0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,
0x7E,0x41,0x51,0x31,0x3E,0x41,0x41,0x3E,//GO 1
0x48,0x29,0x19,0xF,0x19,0x29,0x48,0x00,//天 2
0x48,0x29,0x19,0xF,0x19,0x29,0x48,0x00,//天 3
0x88,0x49,0x29,0x1F,0x9,0xFF,0x9,0x8, //开 4
0x1E,0x3F,0x7F,0xFE,0xFE,0x7F,0x3F,0x1E,//心 5
0x80,0x80,0x80,0xFF,0x88,0x88,0x88,0x80,//上 6
0x1,0x1,0x1,0xFF,0x5,0x9,0x11,0x21,//下 7
0x44,0x24,0x14,0x8C,0x97,0xF4,0x94,0x84,//左 8
0x44,0x24,0x14,0xFC,0x97,0x94,0xF4,0x4,//右 9
0x7E,0x81,0xB5,0xB1,0xB1,0xB5,0x81,0x7E,//猪头 10
};
uchar code table_lie[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code SMG[]={//数码管字模,存放在程序代码区
0xfc,0x60,0xda,0xf2,//0,1,2,3,
0x66,0xb6,0xbe,0xe0, //4,5,6,7,
0xfe,0xf6,0xee,0x3e, //8,9,A,b,
0x9c,0x7a,0x9e,0x8E,0x01};//C,d,E,F,小数点
/*********带形参的延时子函数***************/
void delay_ms(uint ms)//毫秒级别
{uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<141;j++);
}
/*********带形参的延时子函数***************/
void delay(uint m)
{uint i,j;
for(i=0;i<m;i++)
for(j=0;j<1148;j++); }
/*********数码管的初始化函数***************/
void Init_smg()
{
P0=0x00;
duan=1;
P0=0x00;//关段选端
duan=0;
P0=0xff;
wei=1;
P0=0xff;//关位选端
wei=0;
lie=1;
P0=0xff;//点阵全灭
lie=0;
}
/*****595串转并******/
void show_595(uchar num)
{
uchar i,zf;
Init_smg();
P1=0x10;
zf=table_ZIMO[num];
for(i=0;i<8;i++)
{
if(zf&0x80)DS=1;
else DS=0;
SH=1;//上升沿
SH=0;
zf<<=1;
}
ST=1;//上升沿
ST=0;
OE=0;
}
/******点阵显示第一圈******/
void show_circle1(uint cir_time)
{uint shu;
uchar i,j;
for(j=0;j<4;j++)
{
for(shu=0;shu<cir_time;shu++)
{
for(i=j*8;i<j*8+8;i++)
{
show_595(i);
lie=1;
P0=table_lie[i%8];
lie=0;}}
/*lie=1;
P0=0xff;
lie=0;
delay(delay_time);*/
}
}
/******点阵显示第二圈*****/
void show_circle2(uint cir_time)
{uint shu;
uchar i,j;
for(j=2;j>0;j--)
{
for(shu=0;shu<cir_time;shu++)
{
for(i=j*8;i<j*8+8;i++)
{
show_595(i);
lie=1;
P0=table_lie[i%8];
lie=0;}}
/*lie=1;
P0=0xff;
lie=0;
delay(delay_time); */
}
}
/*****带两个形参的数码管显示函数*******/
void show_smg(uchar num,uchar awei)//
{uchar A=0x80;//方便位选端的循环移位
duan=1; //开段选
P0=SMG[num];
duan=0; //关位选
wei=1; //开位选端
P0=~(A>>awei);
//P0=((awei<<wei)|(awei>>(8-wei)));//通过循环移位,令第wei位为低电平,有效
//delay_ms(2);
wei=0;//关位选
}
/*****定时器0的初始化设置*****/
void time0init()
{
TMOD=0x02;//定时器模式设置
TH0=0x00; //定时器0赋初值
TL0=0x00;
ET0=1; //允许定时器0中断请求
EA=1; //开启总中断允许
TR0=1; //启动定时器0
}
/*****外部中断0的初始化设置*****/
void int0init()
{
IT0=1;
EX0=1;
EA=1;
}
/*****定时器0处理子函数*****/
void time0() interrupt 1
{
irtime++;
}
/*****外部中断0处理子函数*****/
void int0() interrupt 0
{
if(startflag)
{
if(irtime>32)//检测引导码
{
bitnum=0;
}
irdata[bitnum]=irtime;
irtime=0;
bitnum++;
if(bitnum==33)
{
bitnum=0;
irreceok=1;
}
}
else
{
startflag=1;
irtime=0;
}
}
/*****处理*****/
void irpros()
{uchar i,j,k,value;
k=1;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{value>>=1;//7
if(irdata[k]>6)
{
value|=0x80;
}
k++;
}
ircode[j]=value;
}
irprosok=1;
}
/*****显示处理*****/
void irwork()
{
disp[7]=ircode[0]/16;//左边数起第一位数码管
disp[6]=ircode[0]%16;
disp[5]=ircode[1]/16;
disp[4]=ircode[1]%16;
disp[3]=ircode[2]/16;
disp[2]=ircode[2]%16;
disp[1]=ircode[3]/16;
disp[0]=ircode[3]%16;
}
/*****显示*****/
void display()
{uchar i;
for(i=0;i<8;i++)
{
show_smg(disp,i);
delay_ms(1);
}
}
/*****按键检测*****/
uchar test_num()
{ uchar key;
switch(disp[0]+disp[1])
{
/* case 13:key=2;break;//天
case 1:key=3;break; //天
case 14:key=4;break;//开
case 12:key=5;break;//心
case 10:key=6;break;//上
case 4:key=7;break;//下
case 8:key=8;break; //左
case 6:key=9;break;//右
case 7:key=10;break;//中*/
case 18:key=9;break;//1
case 21:key=8;break; //2
case 11:key=7;break;//3
case 22:key=6;break;//4
case 17:key=5;break;//5
case 15:key=4;break;//6
case 24:key=3;break; //7
case 23:key=2;break;//8
case 16:key=1;break;//9
default:key=10;//go
}
return key;
}
/*****主函数*****/
void main()
{bell=1;
time0init();
int0init();
Init_smg();
while(1)
{
if(irreceok)
{
irpros();
irreceok=0;
}
if(irprosok)
{
irwork();
irprosok=0;
}
//show_circle1(circle_time*10);
//show_circle2(circle_time*10);
// display(); 用数码管来判别按键编码?
show_circle1(test_num()*10);
show_circle2(test_num()*10);
P3=~P3;
}
}
欢迎光临我们的淘宝网店:http://item.taobao.com/auction/item_detail.htm?item_num_id=9447334857
用户1652773 2015-3-15 09:43