这是我结合老师的两个示例结合到一起的,编译没错,运行不放歌,求各位大佬指点迷津。附件是我的project。
#include "MSP430G2553.h"
#include "DAC8411.h"
#include "TCA6416A.h"
const unsigned char SONG[3][999]={{ //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
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,0x00,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,0x06,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},{ //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
0x18, 0x30, 0x1C , 0x10,0x20, 0x40, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x10,0x1C, 0x10, 0x18 , 0x40,
0x1C, 0x20, 0x20 , 0x20,0x1C, 0x20, 0x18 , 0x20,0x20, 0x80, 0xFF , 0x20,0x30, 0x1C, 0x10 , 0x18,
0x20, 0x15, 0x20 , 0x1C,0x20, 0x20, 0x20 , 0x26,0x40, 0x20, 0x20 , 0x2B,0x20, 0x26, 0x20 , 0x20,
0x20, 0x30, 0x80 , 0xFF,0x20, 0x20, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,0x20, 0x20, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,0x20, 0x30, 0x1C , 0x10,0x18, 0x20, 0x15 , 0x20,0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,0x2B, 0x20, 0x26 , 0x20,0x20, 0x20, 0x30 , 0x80,0x20, 0x30, 0x1C , 0x10,
0x20, 0x10, 0x1C , 0x10,0x20, 0x20, 0x26 , 0x20,0x2B, 0x20, 0x30 , 0x20,0x2B, 0x40, 0x20 , 0x15,
0x1F, 0x05, 0x20 , 0x10,0x1C, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,0x18, 0x20, 0x15 , 0x20,0x1C, 0x20, 0x20 , 0x20,0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,0x20, 0x20, 0x30 , 0x30,0x20, 0x30, 0x1C , 0x10,0x18, 0x40, 0x1C , 0x20,
0x20, 0x20, 0x26 , 0x40,0x13, 0x60, 0x18 , 0x20,0x15, 0x40, 0x13 , 0x40,0x18, 0x80, 0x00},{
0x53,0x20,0x4a,0x20,0x40,0x20,0x39,0x20,
0x34,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,
0x22,0x20,0x20,0x20,0x1c,0x20,0x17,0x20,
0x18,0x20,0x14,0x20,0x10,0x20,0x0c,0x20,
0x09,0x20,0x05,0x20,0x01,0x20,0xFF}};
unsigned char counter;
const unsigned int loudness = 18000;
int SongNum = 3; // 总歌曲数
int this = 0; // 当前歌曲位置
int flag = 0; //暂停标志
void Play_Song(void);
void WDT_init();
void I2C_IODect();
void Time0_A_Init();
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ;
DAC8411_Init();
Time0_A_Init();
TCA6416A_Init(); // 初始化IO扩展口
WDT_init();
while(1) //循环演奏歌曲
{
Play_Song();
if(this < SongNum-1)
this++;
else
this = 0;
}
//return 0;
}
/********WDT初始化函数**********/
void WDT_init()
{
//-----设定WDT为-----------
WDTCTL=WDT_ADLY_16;
//-----WDT中断使能----------------------
IE1 |= WDTIE;
}
/***********WDT定时中断函数*************/
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{
//PinIN();
I2C_IODect();
//__bic_SR_register_on_exit(LPM0_bits );
//WDTIFG = 0;
}
void Delay_Nms(unsigned int n)
{
unsigned char i,j;
for( i = 0;i < n; i++ )
{
for(j=0;j<35; j++)
_NOP();
}
}
void Time0_A_Init()
{
TA0CCTL0 = CCIE; // 允许比较/捕获模块0的中断
TA0CCR0 = 10000; //设定TA定时周期
TA0CTL = TASSEL_2 + MC_1 + ID_3; // //TA0设为增计数模式,时钟=SMCLK
_enable_interrupts(); //开全局总中断
}
void Play_Song(void)
{
unsigned char Temp1,Temp2;
unsigned char addr = 0;
unsigned char tt = 0X01;
counter = 0; //中断计数器清0
int m = this;
while(1)
{
Temp1 = SONG[this][addr++];
if ( Temp1 == 0xFF ) //休止符
{
TA0CTL &=~MC_1; //停止计数
Delay_Nms(100);
}
else if ( Temp1 == 0x00 ) //歌曲结束符
{
Delay_Nms(1000);
return;
}
else
{
Temp2 = SONG[this][addr++];
TA0CTL |=MC_1; //开始计数
while(1)
{
tt ^= BIT0;
if (tt)
write2DAC8411(loudness); // 发音
else
write2DAC8411(0); // 不发音
Delay_Nms(Temp1); // 延时时间,用来控制频率
if ( Temp2 == counter ) // 控制节拍,即时间长短
{
counter = 0;
break;
}
}
}
if(m != this)
return;
}
}
void I2C_IO10_Onclick()
{
if(flag == 0)
{
_bis_SR_register(LPM3_bits);
flag = 1;
}
else
{
//__bic_SR_register_on_exit(LPM3_bits);,这里这个不正确,我想退出低功耗,不知道怎么写
flag = 0;
}
}
void I2C_IO11_Onclick()
{
if(this < SongNum - 1)
this++;
else
this = 0;
}
void I2C_IO12_Onclick()
{
if(this > 0)
this--;
else
this = 0;
}
void I2C_IODect() //检测事件确实发生了
{
static unsigned char KEY_Past=0,KEY_Now=0;
KEY_Past=KEY_Now;
//----判断I2C_IO10所连的KEY1按键是否被按下------
if((TCA6416A_InputBuffer&BIT8) == BIT8)
KEY_Now |=BIT0;
else
KEY_Now &=~BIT0;
if(((KEY_Past&BIT0)==BIT0)&&(KEY_Now&BIT0) !=BIT0)
I2C_IO10_Onclick();
//----判断I2C_IO11所连的KEY2按键是否被按下------
if((TCA6416A_InputBuffer&BIT9)== BIT9)
KEY_Now |=BIT1;
else
KEY_Now &=~BIT1;
if(((KEY_Past&BIT1)==BIT1)&&(KEY_Now&BIT1)!=BIT1)
I2C_IO11_Onclick();
//----判断I2C_IO12所连的KEY3按键是否被按下------
if((TCA6416A_InputBuffer&BITA) == BITA)
KEY_Now |=BIT2;
else
KEY_Now &=~BIT2;
if(((KEY_Past&BIT2)==BIT2)&&(KEY_Now&BIT2) ==0)
{
I2C_IO12_Onclick();
}
}