独立键盘几个练习题目和程序
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit wei = P2^7;
sbit duan = P2^6;
sbit S2 = P3^0;
uchar counter, time_counter;
unsigned char 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(uint z)
{
uintx,y;
for(x= z;x > 0;x--)
for(y= 120; y > 0; y--);
}
void display(uchar i)
{
//1
P0= 0xff; //清除断码
wei= 1;
P0= 0xfe;//点亮第1位数码管
wei= 0;
duan= 1;
P0= leddata;
duan= 0;
delay(1);
//2
P0 = 0xff;
wei= 1;
P0= 0xfd;//点亮第2位数码管
wei= 0;
duan= 1;
P0= leddata;
duan= 0;
delay(1);
//3
P0= 0xff;
wei= 1;
P0= 0xfb;//点亮第3位数码管
wei= 0;
duan= 1;
P0= leddata;
duan= 0;
delay(1);
//4
P0= 0xff;
wei= 1;
P0= 0xf7;//点亮第4位数码管
wei= 0;
duan= 1;
P0= leddata;
duan= 0;
delay(1);
}
void timer_init()
{
TMOD= 0x10;//定时器1 工作模式1 16位模式定时器
TH1= 0x4b;
TL1= 0xfe; //定时50ms
TR1= 1; //启动定时器
}
void main()
{
timer_init();
while(1)
{
if(TF1== 1)
{
TF1= 0;
TH1= 0x4b;
TL1= 0xfe; //初始化值,定时50ms
counter++;
}
if(counter== 2)
{
counter= 0;
time_counter++;
}
if(time_counter== 15)
time_counter= 0;
display(time_counter);
if(S2== 0)
{
delay(5); //软件消抖
if(S2== 0)
{
while(!S2 )
{
display(time_counter);// 松手检测
}
}
}
}
}
另一种写法: //推荐这种写法,上面是我自己写的,虽然实现了功能,但是写的很不简约#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbitwe = P2^7;
sbitdu = P2^6;
sbitS2 = P3^0;
uchar i,count;
unsigned char 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 //自定义
};
voiddelay(uint z)
{
uintx,y;
for(x= z;x > 0;x--)
for(y= 120; y > 0; y--);
}
voidinit()
{
we = 1;
P0= 0xf0;
we= 0;
du= 1;
TMOD= 0x01; //设置定时器0在工作模式1
TH0= 0x4b;
TL0= 0xfd; //定时50ms
TR0= 1; //启动定时器
}
voidmain()
{
init();
while(1)
{
if(TF0 == 1)
{
TF0=0;
TH0= 0x4b;
TL0= 0xfd; //定时50ms
count++;
}
if(count== 2)
{
count= 0;
i++;
if(i > 15 )
i=0;
P0= leddata;
}
if(S2== 0)
{
delay(5);
if(S2== 0)
{
TR0= 0;
while(!S2);
TR0= 1;
}
}
}
}
2. 让后四位数码管以0.01秒(利用定时器)的速度从0显示到9999。按下S2开始跑数。按下S3停止跑数。按下S4数码管的数值加1(需要在停止跑数时执行才有效)。按下S5数码管的数值减1(需要在停止跑数时执行才有效)。#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbitwe = P2^7;
sbitdu = P2^6;
sbitS2 = P3^0;
sbitS3 = P3^1;
sbitS4 = P3^2;
sbitS5 = P3^3;
uchar i;
uintcount;
unsigned char 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 //自定义
};
voiddelay(uint z)
{
uintx,y;
for(x= z;x > 0;x--)
for(y= 120; y > 0; y--);
}
voidtimer_init()
{
TMOD= 0x01; //设置定时器0在工作模式1
TH0= 0xdb;
TL0= 0xff; //定时10ms
// TR0= 1; //启动定时器
}
voiddisplay(uint i)
{
ucharqian,bai,shi,ge;
qian= i / 1000;
bai= (i % 1000) / 100;
shi= (i % 100) /10;
ge= i % 10;
//1
P0= 0xff; //清除断码
we= 1;
P0= 0x7f;//点亮第8位数码管
we= 0;
du= 1;
P0= leddata[ge];
du= 0;
delay(1);
//2
P0 = 0xff;
we= 1;
P0= 0xbf;//点亮第7位数码管
we= 0;
du= 1;
P0= leddata[shi];
du= 0;
delay(1);
//3
P0= 0xff;
we= 1;
P0= 0xdf;//点亮第6位数码管
we= 0;
du= 1;
P0= leddata[bai];
du= 0;
delay(1);
//4
P0= 0xff;
we= 1;
P0= 0xef;//点亮第5位数码管
we= 0;
du= 1;
P0= leddata[qian];
du= 0;
delay(1);
}
voidmain()
{
timer_init();
while(1)
{
if(TF0 == 1)
{
TF0=0;
TH0= 0xdb;
TL0= 0xff; //定时10ms
count++;
if(count> 9999)
count= 0;
}
display(count);
if(S2== 0)
{
delay(5);//消抖
if(S2== 0)
{
TR0= 1;
while(!S2);
}
}
if(S3== 0)
{
delay(5);//消抖
if(S3== 0)
{
TR0= 0;
while(!S3); //松手检测
}
}
if(S4== 0)
{
delay(5);//消抖
if(S4== 0 && TR0 == 0)
{
count++;
while(!S4); //松手检测
}
}
if(S5== 0)
{
delay(5);//消抖
if(S5== 0 && TR0 == 0)
{
count--;
while(!S5); //松手检测
}
}
}
}
怎么后面代码成斜体了.
XZ CXCX