包括数码管显示,按键,板子初始化。
#include <STC15F2K60S2.h>
#define uchar unsigned char
#define uint unsigned int
sbit s7=P3^0;
sbit s6=P3^1;
uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
//1 2 3 4 5 6 7 8 9 - 灭
uchar code dottab[]={0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
//带小数点的数字,程序主要用第二位,第六位 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
uchar yi,er,san,si,wu,liu,qi,ba;
void Delay1ms(uchar ms);
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 allinit();
void keyscan();
void main()
{
allinit();
yi=1,er=2,san=3,si=4,wu=5,liu=6,qi=7,ba=8;
while(1)
{
display1( yi, er);
display2( san, si);
display3( wu, liu);
display4( qi, ba);
keyscan();
}
}
void Delay1ms(uchar ms) //@12.000MHz 最多延时255ms
{
unsigned char i, j;
uchar k;
for(k=ms;k>0;k--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void display1(uchar yi,uchar er)
{
P2=0XC0;
P0=0X01;
P2=0XE0;
P0=tab[yi];
Delay1ms(1);
P2=0XC0;
P0=0X02;
P2=0XE0;
P0=tab[er];
Delay1ms(1);
}
void display2(uchar san,uchar si)
{
P2=0XC0;
P0=0X04;
P2=0XE0;
P0=tab[san];
Delay1ms(1);
P2=0XC0;
P0=0X08;
P2=0XE0;
P0=tab[si];
Delay1ms(1);
}
void display3(uchar wu,uchar liu)
{
P2=0XC0;
P0=0X10;
P2=0XE0;
P0=tab[wu];
Delay1ms(1);
P2=0XC0;
P0=0X20;
P2=0XE0;
P0=tab[liu];
Delay1ms(1);
}
void display4(uchar qi,uchar ba)
{
P2=0XC0;
P0=0X40;
P2=0XE0;
P0=tab[qi];
Delay1ms(1);
P2=0XC0;
P0=0X80;
P2=0XE0;
P0=tab[ba];
Delay1ms(1);
P2=0XC0;//位选
P0=0XFF;
P2=0XE0;//段选
P0=0XFF;//将所有数码管关闭,使其亮度一致,都点亮1ms
}
void allinit()
{
P2=0X80;
P0=0XFF;//关LED
P2=0XC0;
P0=0XFF;
P2=0XE0;
P0=0XFF;//关数码管
P2=0XA0;
P0=0X00;//关蜂鸣器 继电器
}
void keyscan()
{
if(s7==0)
{
Delay1ms(5);//延时消抖
if(s7==0)
{
yi=11,er=11,san=11,si=11,wu=11,liu=11,qi=11,ba=11;//所有数码管灭
}
while(!s7);
}
else if(s6==0)
{
Delay1ms(5);
if(s6==0)
{
yi=0,er=0,san=0,si=0,wu=0,liu=0,qi=0,ba=0;//所有数码管为0
}
while(!s6);
}
}