#include <STC15F2K60S2.h>
#define uchar unsigned char
#define uint unsigned int
uchar code SMG_duanma[18] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”
//分别是“0.-9.”
uchar code SMG_Dot_AC[10] =
{0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
//系统初始化
void Initsys();
//配置HC138
void SelectHC138(uchar channel);
//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value);
//数码管8位码显示
void SMG_Display();
//数码管的延时
void Delay_one_ms_SMG();
//独立按键(BTN)
void Alone_Key();
//按键消抖延时
void Delay_five_ms_Key();
//矩阵键盘(KBD)
void Matrix_Key();
void main()
{
Initsys();
while(1)
{
Matrix_Key();
//Alone_Key();
//SMG_Display();
}
}
//矩阵键盘(KBD)
void Matrix_Key()
{
uchar temp;
//第一列
P3 = 0X7F;P44 = 0; P42 = 1;
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
Delay_five_ms_Key();
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
temp = P3;
switch(temp)
{
case 0X7E:
DisplaySMG_Bit(0, SMG_duanma[1]);
break;
case 0X7D:
DisplaySMG_Bit(1, SMG_duanma[1]);
break;
case 0X7B:
DisplaySMG_Bit(2, SMG_duanma[1]);
break;
case 0X77:
DisplaySMG_Bit(3, SMG_duanma[1]);
break;
}
while(temp != 0X0F)
{
temp = P3;
temp = temp & 0X0F;
}
}
}
//第二列
P3 = 0XBF;P44 = 1; P42 = 0;
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
Delay_five_ms_Key();
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
temp = P3;
switch(temp)
{
case 0XBE:
DisplaySMG_Bit(0, SMG_duanma[2]);
break;
case 0XBD:
DisplaySMG_Bit(1, SMG_duanma[2]);
break;
case 0XBB:
DisplaySMG_Bit(2, SMG_duanma[2]);
break;
case 0XB7:
DisplaySMG_Bit(3, SMG_duanma[2]);
break;
}
while(temp != 0X0F)
{
temp = P3;
temp = temp & 0X0F;
}
}
}
//第三列
P3 = 0XDF;P44 = 1; P42 = 1;
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
Delay_five_ms_Key();
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
temp = P3;
switch(temp)
{
case 0XDE:
DisplaySMG_Bit(0, SMG_duanma[3]);
break;
case 0XDD:
DisplaySMG_Bit(1, SMG_duanma[3]);
break;
case 0XDB:
DisplaySMG_Bit(2, SMG_duanma[3]);
break;
case 0XD7:
DisplaySMG_Bit(3, SMG_duanma[3]);
break;
}
while(temp != 0X0F)
{
temp = P3;
temp = temp & 0X0F;
}
}
}
//第四列
P3 = 0XEF;P44 = 1; P42 = 1;
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
Delay_five_ms_Key();
temp = P3;
temp = temp & 0X0F;
if(temp != 0X0F)
{
temp = P3;
switch(temp)
{
case 0XEE:
DisplaySMG_Bit(0, SMG_duanma[4]);
break;
case 0XED:
DisplaySMG_Bit(1, SMG_duanma[4]);
break;
case 0XEB:
DisplaySMG_Bit(2, SMG_duanma[4]);
break;
case 0XE7:
DisplaySMG_Bit(3, SMG_duanma[4]);
break;
}
while(temp != 0X0F)
{
temp = P3;
temp = temp & 0X0F;
}
}
}
}
//按键消抖延时
void Delay_five_ms_Key()
{
uint i,j;
for(i = 0; i < 5; i++)
for(j = 845; j > 0; j--);
}
//独立按键(BTN)
void Alone_Key()
{
//S7按键
if(P30 == 0)
{
Delay_five_ms_Key();
if(P30 == 0)
{
DisplaySMG_Bit(0, SMG_duanma[0]);
}
while(!P30);
}
//S6按键
else if(P31 == 0)
{
Delay_five_ms_Key();
if(P31 == 0)
{
DisplaySMG_Bit(1, SMG_duanma[1]);
}
while(!P31);
}
//S5按键
else if(P32 == 0)
{
Delay_five_ms_Key();
if(P32 == 0)
{
DisplaySMG_Bit(2, SMG_duanma[2]);
}
while(!P32);
}
//S4按键
else if(P33 == 0)
{
Delay_five_ms_Key();
if(P33 == 0)
{
DisplaySMG_Bit(3, SMG_duanma[3]);
}
while(!P33);
}
}
//配置HC138
void SelectHC138(uchar channel)
{
switch(channel)
{
case 4: //LED
P2 = (P2 & 0X1F) | 0X80;
break;
case 5: //蜂鸣器和继电器
P2 = (P2 & 0X1F) | 0Xa0;
break;
case 6: //位码
P2 = (P2 & 0X1F) | 0Xc0;
break;
case 7: //段码
P2 = (P2 & 0X1F) | 0Xe0;
break;
case 0: //锁住所有寄存器
P2 = (P2 & 0X1F) | 0X00;
break;
}
}
//系统初始化
void Initsys()
{
SelectHC138(5);
P0 = 0X00;//关闭蜂鸣器和继电器
SelectHC138(4);
P0 = 0XFF;//关闭LED
SelectHC138(6);
P0 = 0XFF; //选择所有数码管
SelectHC138(7);
P0 = 0XFF; //关闭所有数码管
}
//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
SelectHC138(6);
P0 = 0X01 << pos;
SelectHC138(7);
P0 = value;
}
//数码管8位码显示
void SMG_Display()
{
DisplaySMG_Bit(0, SMG_duanma[0]);
Delay_one_ms_SMG();
DisplaySMG_Bit(1, SMG_duanma[1]);
Delay_one_ms_SMG();
DisplaySMG_Bit(2, SMG_duanma[2]);
Delay_one_ms_SMG();
DisplaySMG_Bit(3, SMG_duanma[3]);
Delay_one_ms_SMG();
DisplaySMG_Bit(4, SMG_duanma[4]);
Delay_one_ms_SMG();
DisplaySMG_Bit(5, SMG_duanma[5]);
Delay_one_ms_SMG();
DisplaySMG_Bit(6, SMG_duanma[6]);
Delay_one_ms_SMG();
DisplaySMG_Bit(7, SMG_duanma[7]);
Delay_one_ms_SMG();
}
//数码管的延时
void Delay_one_ms_SMG()
{
uint j;
for(j = 845; j > 0; j--);
}