|
#include<reg52.h>
sbit pwm=P3^0;//PWM信号输出口
sbit up=P3^7;//角度增加按键检测I/O口
sbit down=P3^6;//角度减少按键检测I/O口
sbit we=P2^7;
sbit du=P2^6;
unsigned char angle;//角度标识
unsigned char count;//0.5ms次数标识,可修改
unsigned code leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
void delay(unsigned char z)
{
unsigned char x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Time0_Init()//定时器初始化
{
TMOD=0x01;//定时器0工作在方式1
IE=0x82;
TH0=0xfe;
TL0=0x33;//11.0592MHz晶振,0.5ms
TR0=1;//定时器开始
}
void Time0_Int() interrupt 1
{
TH0=0xfe;
TL0=0x33;
if(count<angle) //判断0.5ms次数是否小于角度标识
pwm=1;//确实小于,pwm输出高电平
else
pwm=0;//大于则输出低电平
count=(count+1);//0.5ms次数加1
count=count%40;//次数始终保持为40即保持周期为20ms
}
void keyscan()//按键扫描
{
if(up==0)//角度增加按键是否按下
{
delay(10);//按下延时,消抖
if(up==0)//确实按下
{
angle++;//角度标识加1
count=0;//按键按下则20ms周期从新开始
if(angle==6)
angle=5;//已经是180度,则保持?
while(up==0);//等待按键放开?
}
}
if(down==0)//角度减小按键是否按下
{
delay(10);
if(down==0)
{
angle--;//角度标识减1
count=0;
if(angle==0)
angle=1;//已经是0度,则保持
while(down==0);
}
}
}
void display()
{
unsigned char bai,shi,ge;
switch(angle)
{
case 1:bai=0;shi=0;ge=0;break;
case 2:bai=0;shi=4;ge=5;break;
case 3:bai=0;shi=9;ge=0;break;
case 4:bai=1;shi=3;ge=5;break;
case 5:bai=1;shi=8;ge=0;break;
}
du=0;
P0=leddata[bai];
du=1;
du=0;
we=0;
P0=0xfe;
we=1;
we=0;
delay(5);
P0=leddata[shi];
du=1;
du=0;
P0=0xfd;
we=1;
we=0;
delay(5);
P0=leddata[ge];
du=1;
du=0;
P0=0xfb;
we=1;
we=0;
delay(5);
}
void main()
{
angle=1;
count=0;
Time0_Init();
while(1)
{
keyscan();//按键扫描
display();
}
}
|
|