#include <msp430f6638.h>
#include <math.h>
#define PI 3.1415926
int sin_table[10];
int *sin_data_pr;
double i=0;
int j,k;
char flag=1;
int key_table[22]={261,293,329,349,391,440,493,533,587,659,698,783,880,987,1046,1174,1318,1396,1567,1760,1975};
int period_table[21];
//两只老虎
//int song_tone_table[32]={7,8,9,7,7,8,9,7,9,10,11,9,10,11,11,12,11,10,9,7,11,12,11,10,9,7,7,11,7,7,11,7};
//int song_long_table[32]={4,4,4,4,4,4,4,4,4,4,8,4,4,8,2,2,2,2,4,4,2,2,2,2,4,4,4,4,8,4,4,8};
//生日歌
int song_tone_table[24]={4,4,5,4,7,6,4,4,5,4,8,7,4,4,11,9,7,6,10,10,9,7,8,7};
int song_long_table[24]={2,2,4,4,4,8,2,2,4,4,4,8,2,2,5,4,5,8,2,2,4,4,4,8};
int key=0;
int m,n;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
for(j=0;j<10;j++)
{
i+=PI/5;
sin_table[j]=(int)((sin(i)+1)*2048);
}
sin_data_pr=&sin_table[0];
for(j=0;j<20;j++)
{
period_table[j]=(int)(100000/key_table[j]);
}
DAC12_0CTL0 = DAC12IR + DAC12SREF_1 + DAC12AMP_5 + DAC12ENC + DAC12CALON+DAC12OPS;
P5DIR=BIT1;
P4DIR=BIT1;
P5OUT&=~BIT1;
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCR0 = period_table[0];
TA1CTL = TASSEL_2 + MC_2 + TACLR +TAIE; // SMCLK, upmode, clear TAR
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts
__no_operation(); // For debugger
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{
P4OUT ^= BIT1; // Toggle P1.0
DAC12_0DAT=*sin_data_pr++;
if (sin_data_pr >= &sin_table[10])
sin_data_pr = &sin_table[0];
DAC12_0DAT &= 0xFFF; // Modulo 4096
TA1CCR0 += period_table[song_tone_table[key]];
}
#pragma vector=TIMER1_A1_VECTOR
__interrupt void TIMER1_A1_ISR(void)
{
switch(__even_in_range(TA1IV,14))
{
case 0: break; // No interrupt
case 2: break; // CCR1 not used
case 4: break; // CCR2 not used
case 6: break; // reserved
case 8: break; // reserved
case 10: break; // reserved
case 12: break; // reserved
case 14:
if(k++==song_long_table[m])
{
k=0;
if(++m==24)
m=0;
DAC12_0DAT=0x000;
TA1CCTL0 &=~ CCIE;
if(++n==1)
{
n=0;
TA1CCTL0 = CCIE;
TA1CCR0 = period_table[song_tone_table[key++]];
if(key==24)
key=0;
}
}
break;
default: break;
}
}
此帖出自
小平头技术问答
一周热门 更多>