然后配置定时器3中断和第二课第三小节一样,我就不多说了
void T3_Init(void)//系统默认为16M
{
T3IE = 1; //开总中断和T3中断
T3CTL|=0XE8; //128分频,128/16000000*N=0.5S,N=62500
T3CTL &= ~0X03; //自动重装,选择自由模式 00->0xff 62500/256=244(次)
T3CTL |=0X10; //启动
EA = 1;
}
播放的音乐,祝你平安
另有指示灯一闪一闪指示
源程序为:
/**************************************************
** 本程序只供学习使用,未经作者许可,不得用于其它任何用途
** 作者: shizhe
** 创始时间:2014-05-08
** 修改人:shizhe
** 修改时间:2014-05-08
** 修改人:shizhe
** 修改时间:2014-05-08
** 描述:
** 音乐播放祝你平安…
** 版权所有,盗版必究。
** Copyright(C) 联控智能 2014-2020
** All rights reserved
*************************************************/
#include
#define LED1 P1_0
#define SPEAK P1_6
unsigned char Count;
unsigned char SONG[] ={ //祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
};
void Gpio_Init()
{
P1SEL &= ~0X61;
P1DIR |= 0X61;
}
{
T3IE = 1; //开总中断和T3中断
T3CTL|=0X18; //128分频,128/16000000*N=0.5S,N=62500
T3CTL &= ~0X03; //自动重装,选择自由模式 00->0xff 62500/256=244(次)
T3CTL |=0X10; //启动
EA = 1;
}
#pragma vector = T3_VECTOR //定时器T1
__interrupt void T3_ISR(void)
{
static unsigned int js =0;
IRCON = 0x00; //清中断标志, 也可由硬件自动完成
if(js++ >900){
js =0;
Count++;
}
}
/*-------------------------------------------------
功能:1MS延时子程序
-------------------------------------------------*/
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for( i =0;i < x;i++ )
{
for( j =0;j<3;j++ );
}
}
/*-------------------------------------------------
功能:歌曲播放子程序i为播放哪一段曲目
-------------------------------------------------*/
void Play_Song(unsigned char i)
{
unsigned char Temp1,Temp2;
unsigned int Addr;
Count = 0; //中断计数器清0
Addr = i * 217;
while(1)
{
Temp1 = SONG[Addr++];
if ( Temp1 == 0xFF ) //休止符
{
T3CTL &=~0X10; //停止
Delay_xMs(100);
}
else if ( Temp1 == 0x00 ) //歌曲结束符
{
return;
}
else
{
Temp2 = SONG[Addr++];
T3CTL |=0X10; //启动
while(1)
{
SPEAK = ~SPEAK;
Delay_xMs(Temp1);
if ( Temp2 == Count )
{
Count = 0;
LED1 = ~LED1;
break;
}
}
}
}
}
/*-------------------------------------------------
功能:主程序
-------------------------------------------------*/
void main()
{
Gpio_Init(); //引脚初始化
Time3_Init(); //定时器0中断初始化
while(1)
{
Play_Song(0); //播放//祝你平安
}
}