#include "reg51.h"
#define uint unsigned int
#define uchar unsigned char
#define KEYPORT P1
#define SMGPORT P0
#define delayValue 15//延时
sbit buzzer=P2^3;
uint value=0;
uchar code dis[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数码管
uint code tone[8]={0xfc44,0xfcac,0xfd09,0xfd34,0xfd82,0xfdc8,0xfe06,0xfe22};//音色
uchar code button_Sel[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//按键执行,0有效
void Button_Scan(void);
void Handler(void);
void delay_ms(uint k);
void delay_ms(uint k)
{
uint j=0,i=0;
for(j=0;j<k;j++)
for(i=0;i<118;i++);
}
void Button_Scan(void)
{
KEYPORT=0xff;//给按键高电平
if(KEYPORT==button_Sel[0])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[0])
{
value=tone[0];//播放音节
SMGPORT=dis[1];//数码管亮1
}
}
if(KEYPORT==button_Sel[1])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[1])
{
value=tone[1];
SMGPORT=dis[2];
}
}
if(KEYPORT==button_Sel[2])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[2])
{
value=tone[2];
SMGPORT=dis[3];
}
}
if(KEYPORT==button_Sel[3])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[3])
{
value=tone[3];
SMGPORT=dis[4];
}
}
if(KEYPORT==button_Sel[4])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[4])
{
value=tone[4];
SMGPORT=dis[5];
}
}
if(KEYPORT==button_Sel[5])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[5])
{
value=tone[5];
SMGPORT=dis[6];
}
}
if(KEYPORT==button_Sel[6])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[6])
{
value=tone[6];
SMGPORT=dis[7];
}
}
if(KEYPORT ==button_Sel[7])
{
delay_ms(delayValue);
if(KEYPORT==button_Sel[7])
{
value=tone[7];
SMGPORT=dis[8];
}
}
}
void Handler(void)
{
if(KEYPORT==0xff)
{
P2=0xff;
TR1=0;
buzzer=1;//蜂鸣器响
}
else
{
TR1=1;
}
}
void main(void)
{
ET1=1;
TMOD |=0x10;//使用定时器T1的模式1
TH1=(65536-500)/256;//取初值的高八位数
TL1=(65536-500)%256;//取初值的底八位数
P0=0x00;
EA=1;
while(1)
{
Button_Scan();//按键扫描函数
Handler();
}
}
void Time1_Handler(void) interrupt 3 using 1
{
buzzer=~buzzer; //取反
TH1=value/256;
TL1=value%256;
}
|