************************** Copyright(C) CaKe ****************************************
========================================================================================
==============**版权信息 : CaKen工作室 ==============
==============**文件名 : main.c ==============
==============**作者 : CaKen ==============
==============**版本号 : V1.0 ==============
==============**时间 : 2014.08.23 ==============
==============**功能描述 : 16键电子琴实验测试 ==============
========================================================================================
****************************************************************************************
#include"reg52.h"
#define uchar unsigned char //宏定义
#define uint unsigned int
#define CaKen_KEY1 P1 //定义按键
#define CaKen_KEY2 P2
sbit CaKen_SPEAKER = P3^7; //蜂鸣器
uchar CaKen_MUSIC; //全局变量
uchar CaKen_STH0;
uchar CaKen_STL0;
unsigned int code Tab[]= //电子琴音频符
{
64021,64103,64260,64400, //低音3开始
64524,64580,64684,64777,
64820,64898,64968,65030,
65058,65110,65157,65178
};
=====================================================================================
==================函数名: main
======================================================================================
void main()
{
TMOD = 0x01; //定时器0工作模式=方式1 (0 0 0 1) 16位定时器/计数器
ET0 = 1; //定时器0中断允许
EA = 1; //开启总中断
CaKen_KEY1=0xff; //按键初始化,高电平
while(1) //进入循环
{
if(CaKen_KEY1!=0xff|CaKen_KEY2!=0xff)
{
switch(~CaKen_KEY1) //显示的列位置
{
case 0x01: CaKen_MUSIC = 1;break;
case 0x02: CaKen_MUSIC = 2;break;
case 0x04: CaKen_MUSIC = 3;break;
case 0x08: CaKen_MUSIC = 4;break;
case 0x10: CaKen_MUSIC = 5;break;
case 0x20: CaKen_MUSIC = 6;break;
case 0x40: CaKen_MUSIC = 7;break;
case 0x80: CaKen_MUSIC = 8;break;
default:break;
}
switch(~CaKen_KEY2)
{
case 0x01: CaKen_MUSIC = 9 ;break;
case 0x02: CaKen_MUSIC = 10;break;
case 0x04: CaKen_MUSIC = 11;break;
case 0x08: CaKen_MUSIC = 12;break;
case 0x10: CaKen_MUSIC = 13;break;
case 0x20: CaKen_MUSIC = 14;break;
case 0x40: CaKen_MUSIC = 15;break;
case 0x80: CaKen_MUSIC = 16;break;
default:break;
}
CaKen_STH0 = Tab[CaKen_MUSIC]/256;
CaKen_STL0 = Tab[CaKen_MUSIC]%6;
TR0 = 1; //开启定时器0 进入定时器中断函数
}
else
{
CaKen_SPEAKER = 1; //关蜂鸣器
TR0 = 0; //关定时器
}
}
}
======================================================================================
==================函数名:定时器中断服务函数
=======================================================================================
void CaKen_QQ1145266160_TIMER0(void) interrupt 1
{
TH0 = CaKen_STH0;
TL0 = CaKen_STL0;
CaKen_SPEAKER = ~CaKen_SPEAKER;
}
=======================================================================================
------------------- CaKen工作室 原创作品 --------------------
------------------- CaKen工作室 版权所有 --------------------
========================================================================================