显示效果如下:
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit SPK = P3^7; //P3.7外接扬声器
uint FreqTemp;
unsigned int code Freqtab[] = { //定时半周期的初始值
64021,64103,64260,64400, //低音3 4 5 6
64524,64580,64684,64777, //低音7,中音1 2 3
64820,64898,64968,65030, //中音4 5 6 7
65058,65110,65157,65178}; //高音1 2 3 4
uchar Keyscan(void) /* 按键扫描程序 */
{
uchar i, j, temp, Buffer[4] = {0xfe, 0xfd, 0xfb, 0xf7};
for(j = 0; j < 4; j++) { //循环四次,扫描四行
P1 = Buffer[j]; //在低四位分别输出一个低电平
_nop_();
temp = 0x80; //计划先读出P1.7位
for(i = 0; i < 4; i++) { //循环四次,检查四列
if(!(P1 & temp)) { //从高四位,截取1位
return (i + j * 4); //返回取得的按键值
}
temp >>= 1; //换右边一位
} }
return 16; //没有键按下就返回16
}
void Display(uchar k)
{
P2 = ((k / 10) << 4) + (k % 10);
}
void main()
{
uchar Key_Value = 16, Key_Temp1, Key_Temp2;//读出的键值
TMOD = 0x01; //T0定时方式1
ET0 = 1; //允许T0中断
EX0 = 1; //允许X0中断
EA = 1;
while(1)
{
TR0 = 0; //暂不发音
Key_Temp1 = Keyscan(); //读入按键
if(Key_Temp1 != 16)
{
Display(Key_Value); //显示键值、延时消抖
Key_Temp2 = Keyscan(); //再读一次
if (Key_Temp1 == Key_Temp2) //两次相等
{
Key_Value = Key_Temp1; //就确认下来
FreqTemp = Freqtab[Key_Value]; //根据键值,取出定时半周期的初始值
Display(Key_Value); //显示
TR0 = 1; //启动定时器,发音
while (Keyscan() < 16); //等待释放
SPK = 1; //停止发音
}
}
}
}
void T0_INT(void) interrupt 1 /* 按键中断 */
{
TL0 = FreqTemp; //载入定时半周期的初始值
TH0 = FreqTemp >> 8;
SPK = ~SPK; //发音
}