#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#include "leds.h"
#include "key.h"
#include "beep.h"
#define DISABLE_TIMER1_INT IER &= 0xFFFE;
#define ENABLE_TIMER1_INT IER |= M_INT1;
Uint16 Musi[23]={ //单位 us,不同频率下,蜂鸣器发出不同音调的声音
0,
3816, //L_do
3496, //L_re
3215, //L_mi
2865, //L_fa
2551, //L_so
2272, //L_la
2024, //L_xi
1912, //do
1703, //re
1517, //mi
1432, //fa
1275, //so
1136, //la
1013, //xi
956, //H_do
851, //H_re
758, //H_mi
716, //H_fa
638, //H_so
568, //H_la
506, //H_xi
0xFF //STOP
};
Uint16 Song1[]={1};//2
Uint16 Song2[]={2};//3
Uint16 Song3[]={3};//4
Uint16 Song4[]={4};//5
Uint16 Song5[]={5};//6
Uint16 Song6[]={6};//7
Uint16 Song7[]={7};//8
Uint16 DT1[]={2}; //节拍
Uint16 DT2[]={3};
Uint16 DT3[]={4};
Uint16 DT4[]={5};
Uint16 DT5[]={6};
Uint16 DT6[]={7};
Uint16 DT7[]={8};
void Delay(Uint16 t) //延时函数
{
Uint32 i=0;
Uint32 gain = 300000; //延时增益
Uint32 base=0;
base=gain*t;
for(i=0;i<=base;i++);
}
interrupt void cpu_timer0_isr(void);
extern void Show1(void);
extern void Show2(void);
extern void Show3(void);
extern void Show4(void);
extern void Show5(void);
extern void Show6(void);
extern void Show7(void);
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Show1(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song1[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song1[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT1[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2);
addr++;
break;
}
}
}
void Show2(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song2[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song2[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT2[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2); //音乐停顿
addr++;
break;
}
}
}
void Show3(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song3[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song3[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT3[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2); //音乐停顿
addr++;
break;
}
}
}
void Show4(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song4[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song4[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT4[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2); //音乐停顿
addr++;
break;
}
}
}
void Show5(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song5[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song5[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT5[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2); //音乐停顿
addr++;
break;
}
}
}
void Show6(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song6[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song6[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT6[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2); //音乐停顿
addr++;
break;
}
}
}
void Show7(void)
{
Uint16 addr=0;
while(1)
{
if(Musi[Song7[addr]]==0xFF)
{
return; //音乐播放结束
}
else
{
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
ConfigCpuTimer(&CpuTimer0, 150, Musi[Song7[addr]+8]/2); //设置定时时间
StartCpuTimer0(); //重启定时器
ENABLE_TIMER1_INT; //使能定时中断
Delay(DT7[addr]); //音乐节拍延时
StopCpuTimer0(); //停止计数
DISABLE_TIMER1_INT; //不使能定时中断
BEEP_OFF; //关闭蜂鸣器
Delay(2); //音乐停顿
addr++;
break;
}
}
}
void main()
{
int i=0;
char key=0;
InitSysCtrl();
LED_Init();
KEY_Init();
BEEP_Init();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW; //解除寄存器保护
PieVectTable.TINT0 = &cpu_timer0_isr;
EDIS; // 添加寄存器保护
InitCpuTimers(); // 本例仅需要初始化CPU定时器
ConfigCpuTimer(&CpuTimer0, 150, 1000000);
StartCpuTimer0();
IER |= M_INT1;
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
EINT; // 使能全局中断INTM
ERTM; // 使能全局实时中断DBGM
while(1)
{
key=KEY_Scan(0);
switch(key)
{
case KEY1_PRESS: LED1_TOGGLE;Show1();break;
case KEY2_PRESS: LED2_TOGGLE;Show2();break;
case KEY3_PRESS: LED3_TOGGLE;Show3();break;
case KEY4_PRESS: LED4_TOGGLE;Show4();break;
case KEY5_PRESS: LED5_TOGGLE;Show5();break;
case KEY6_PRESS: LED6_TOGGLE;Show6();break;
case KEY7_PRESS: LED7_TOGGLE;Show7();break;
}
i++;
if(i%2000==0)
{
LED1_OFF;LED2_OFF;LED3_OFF;LED4_OFF;LED5_OFF;LED6_OFF;LED7_OFF;
}
DELAY_US(100);
}
}
interrupt void cpu_timer0_isr(void)
{
CpuTimer0.InterruptCount++;
BEEP_TOGGLE;
// Acknowledge this interrupt to receive more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}