第二十三课 51音乐之三 音乐头文件的人性化设计
程序讲解:
1、音乐头文件的人性化设计 MIDI_2.H 的讲解
/*--------------------------------------------------------------*/
/*File: MIDI_2.H */
/*Date: 2009-5-6 */
/*Time: 21:04 */
/*MCU: STC89C51RC */
/*OSC: 12MHz */
/*Info: 中断发声程序 */
/*--------------------------------------------------------------*/
//防止被重复定义
#ifndef __MIDI_2_H__
#define __MIDI_2_H__
/*--------------------------------------------------------------*/
//接口定义
sbit beep = P3^6; //与硬件对应
/*--------------------------------------------------------------*/
//定时器选择T0/T1
#define T0_T1_select 1 //T0_T1_select = 0时选择T0
//T0_T1_select = 1时选择T1
#if T0_T1_select
#define music_on TR1 = 1; //音乐响起
#define music_off TR1 = 0; beep = 1; //音乐关闭
#else
#define music_on TR0 = 1; //音乐响起
#define music_off TR0 = 0; beep = 1; //音乐关闭
#endif
#if T0_T1_select
#define music_init T1_init(); //定时器T1初始化
#else
#define music_init T0_init(); //定时器T0初始化
#endif
/*--------------------------------------------------------------*/
//数据定义
unsigned char R_TH;
unsigned char R_TL;
unsigned char code tab_freq[][2] = //音符与计数值对应表 采用二维数组
{
0xF1,0x18, 0xF2,0xB7, 0xF4,0x2A, 0xF4,0xD7, 0xF6,0x09, 0xF7,0x20, 0xF8,0x18,//低音
0xFC,0x44, 0xFC,0xAD, 0xFD,0x0A, 0xFD,0x34, 0xFD,0x83, 0xFD,0xC8, 0xFE,0x06,//中音
0xFE,0x22, 0xFE,0x57, 0xFE,0x85, 0xFE,0x9B, 0xFE,0xC2, 0xFE,0xE4, 0xFF,0x03 //高音
};
/*--------------------------------------------------------------*/
//函数声明
#if T0_T1_select
void T1_init(void); //定时器初始化
#else
void T0_init(void); //定时器初始化
#endif
void delay100ms(void); //延时100000-3us 函数定义
void sound(unsigned char tone, unsigned char time);//固定音调发声节拍
/*--------------------------------------------------------------*/
//定时器中断服务程序
#if T0_T1_select
void T1_intservice(void) interrupt 3
{
TH1 = R_TH; //定时器赋初值
TL1 = R_TL;
beep = ~beep; //输出高低电平
}
#else
void T0_intservice(void) interrupt 1
{
TH0 = R_TH;
TL0 = R_TL;
beep = ~beep; //输出高低电平
}
#endif
/*--------------------------------------------------------------*/
//定时器初始化
#if T0_T1_select
void T1_init(void)
{
TMOD |= 0x10; //Timer1
IE |= 0x88; //EA = 1; ET1 = 1;
TH1 = 0xff;
TL1 = 0xff;
}
#else
void T0_init(void)
{
TMOD |= 0x01; //Timer0
IE |= 0x82; //EA = 1; ET0 = 1;
TH0 = 0xff;
TL0 = 0xff;
}
#endif
/*--------------------------------------------------------------*/
//延时100000-3us 函数定义
void delay100ms(void)
{
unsigned char i,j;
for(i=232;i>0;i--)
for(j=214;j>0;j--);
}
/*--------------------------------------------------------------*/
//固定音调发声节拍
void sound(unsigned char tone, unsigned char time)
{
unsigned char step;
music_init //定时器初始化
R_TH = tab_freq[tone][0]; //频率值高8位
R_TL = tab_freq[tone][1]; //频率低高8位
music_on //音乐响起
for(step = 0; step < time; step++) delay100ms(); //节拍(让音乐延时会再关闭)
music_off //音乐关闭
}
/*--------------------------------------------------------------*/
#endif
本节重点
1、语句
#if T
A
#else
B
#endif
条件选择语句,若T=1,则选择语句A否则选择语句B,用于实时的改变程序,以适应硬件的变化!这样会使程序灵活性更强,更具人性化!
演示视频:
http://v.youku.com/v_show/id_XOTIzNjQ4OTI=.html
相关程序:https://static.assets-stash.eet-china.com/album/old-resources/2009/6/2/0a971d9d-8af7-48e6-862d-ad7fe06eacae.rar
文章评论(0条评论)
登录后参与讨论