模版代码,独立按键,数码管位段码,矩阵键盘
#include <STC15F2K60S2.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar yi,er,san,si,wu,liu,qi,ba,num,n,time;
bit s4,s5,s6,s7;
void Delayms(int ms);
void Allinit(void);
void Display1(uchar yi,uchar er);
void Display2(uchar san,uchar si);
void Display3(uchar wu,uchar liu);
void Display4(uchar qi,uchar ba);
void Keyscan(void);
void Keyscan16(void);
void Time0Init(void);
void main(void)
{
void Allinit();
yi=2;er=3;san=10;si=5;wu=9;liu=10;qi=5;ba=0;
while(1)
{
Keyscan();
//Keyscan16();
//yi=num;
if(s7==1)
{
yi=11;er=11;san=11;si=11;wu=11;liu=11;qi=2;ba=5;
}
if(s6==1)
{
yi=11;er=11;san=11;si=11;wu=11;liu=11;qi=5;ba=5;
}
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
}
}
void Keyscan16(void)
{
uchar temp;
//s4567
P44=0;P42=1;P3=0x7f;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
Delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0x7e:num=1;break;
case 0x7d:num=2;break;
case 0x7b:num=3;break;
case 0x77:num=4;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
//s8 9 10 11
P44=1;P42=0;P3=0xbf;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
Delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xbe:num=5;break;
case 0xbd:num=6;break;
case 0xbb:num=7;break;
case 0xb7:num=8;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
//s 12 13 14 15
P44=1;P42=1;P3=0xdf;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
Delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xde:num=9;break;
case 0xdd:num=10;break;
case 0xdb:num=11;break;
case 0xd7:num=12;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
//s16 17 18 19
P44=1;P42=1;P3=0xef;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
Delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xee:num=11;break;
case 0xed:num=12;break;
case 0xeb:num=13;break;
case 0xe7:num=14;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
}
void Keyscan(void)//独立按键
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
s7=1;
}
while(!P30);//松手检测
}
else if(P31==0)
{
Delayms(5);
if(P31==0)
{
s6=1;yi=4;
}
while(!P31);//松手检测
}
else if(P32==0)
{
Delayms(5);
if(P32==0)
{
s5=1;yi=3;
}
while(!P32);//松手检测
}
else if(P33==0)
{
Delayms(5);
if(P33==0)
{
s4=1;yi=2;
}
while(!P33);//松手检测
}
}
void Display1(uchar yi,uchar er)
{
P2=0xc0;//打开控制数码管位选的573
P0=0x01;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[yi];//
Delayms(1);
P2=0xc0;//打开控制数码管位选的573
P0=0x02;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[er];//
Delayms(1);
}
void Display2(uchar san,uchar si)
{
P2=0xc0;//打开控制数码管位选的573
P0=0x04;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[san];//
Delayms(1);
P2=0xc0;//打开控制数码管位选的573
P0=0x08;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[si];//
Delayms(1);
}
void Display3(uchar wu,uchar liu)
{
P2=0xc0;//打开控制数码管位选的573
P0=0x10;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[wu];//
Delayms(1);
P2=0xc0;//打开控制数码管位选的573
P0=0x20;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[liu];//
Delayms(1);
}
void Display4(uchar qi,uchar ba)
{
P2=0xc0;//打开控制数码管位选的573
P0=0x40;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[qi];//
Delayms(1);
P2=0xc0;//打开控制数码管位选的573
P0=0x80;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=tab[ba];//
Delayms(1);
}
void Allinit(void)//初始化
{
P2=0xa0;//打开控制蜂鸣器的573
P0=0x00;//关闭蜂鸣器继电器
P2=0xc0;//打开控制数码管位选的573
P0=0xff;//选中所有数码管
P2=0xE0;//打开控制数码管段选的573
P0=0xff;//关闭所有数码管
P2=0x80;//打开控制LED的573
P0=0xff;//关闭所有LED
}
void Delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j--);
}
void Time0Init()
{
AUXR |=0x80;//定时器时钟模式//AUXR=AUXR|0x80;0010 1010 |1000 0000=
TMOD &=0xF0;//设置定时器模式
TH0=0x28;//设置初始值
TL0=0x00;//设置初始值
TF0=0;//清楚TF0标志
TR0=1;//开始计时
EA=1;
ET0=1;
}
void time0() interrupt 1
{
TH0=0x28;//设置初始值
TL0=0x00;//设置初始值
time++;
if(time==200)
{
time=0;
n--;
if(n==0)
{
n==999;
}
}
}