单片机学习——独立按键之二
2016-01-20 来源:eefocus
题目:数码管前三位显示一个跑表,从000到999之间以0.01秒的速度运行,当按下一个独立按键时,跑表停止,按下第二个时计时开始,按下第三个是计数值清零从头开始。(采用定时器中断方式)
解决方案:(C语言编程)
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar bai,shi,ge,num;
uint shu;
sbit s=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit le=P3^6;
void delay(uint a);
void dis();
void display(uchar ba,uchar sh,uchar g);
uchar code tabledu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x86,0xc7,0xc7,0xc0,0xff};
uchar code tablewe[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void init()
{
shu=0;
bai=0;
shi=0;
ge=0;
TMOD=0x11;
TH0=0xd8;
TL0=0xf0;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
init();
while(1){
if(s==0){
delay(5);
if(s==0){
TR0=0;
dis();
}
}
if(s2==0){
delay(5);
if(s2==0){
TR0=1;
dis();
}
}
if(s3==0){
delay(5);
if(s3==0){
TR0=1;
shu=0;
dis();
}
}
dis();
}
}
void time0() interrupt 1
{
TH0=0xd8;
TL0=0xf0;
num++;
shu++;
if(shu==999)
shu=0;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
}
void display(uchar ba,uchar sh,uchar g)
{
le=1;
P2=tablewe[1];
le=0;
le=1;
P0=tabledu[ba];
le=0;
delay(5);
le=1;
P2=tablewe[2];
le=0;
le=1;
P0=tabledu[sh];
le=0;
delay(5);
le=1;
P2=tablewe[3];
le=0;
le=1;
P0=tabledu[g];
le=0;
delay(5);
}
void dis()
{
if(num%1==0)
{
display(bai,shi,ge);
}
if(num%10==0)
{
display(bai,shi,ge);
}
if(num==10)
{
num=0;
display(bai,shi,ge);
}
}
void delay(uint a) {
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--);
}
进入单片机查看更多内容>>
解决方案:(C语言编程)
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar bai,shi,ge,num;
uint shu;
sbit s=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit le=P3^6;
void delay(uint a);
void dis();
void display(uchar ba,uchar sh,uchar g);
uchar code tabledu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x86,0xc7,0xc7,0xc0,0xff};
uchar code tablewe[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void init()
{
}
void main()
{
}
void time0() interrupt 1
{
}
void display(uchar ba,uchar sh,uchar g)
{
}
void dis()
{
}
void delay(uint a) {
}
上一篇:单片机学习——独立按键之三
下一篇:单片机学习——独立按键P1^0
相关文章