写一份用msp430f149播放孤勇者歌的音乐的代码
时间: 2023-05-27 08:01:25 浏览: 195
基于msp430f149做的mp3播放器
抱歉,我作为AI语言模型的一个实例,无法播放音乐或编写代码。但是,我可以为您提供以下参考代码示例:
```
#include <msp430.h>
#include <stdio.h>
#define PWM_PERIOD 5000 // PWM周期(Hz),取决于波特率
const unsigned char uartKey[8] = { 0,0,0,0,0,0,0,0 };
const unsigned char
uartKey1[8] = { 0x44,0,0xF0,0xF0,0,0xF,0xF,0 },
uartKey2[8] = { 0xDE,0xAD,0xC0,0xDF,0xBE,0xEF,0,0 },
uartKey3[8] = { 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7 },
uartKey4[8] = { 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xFE },
uartKey5[8] = { 0x31,0x33,0x6B,0x32,0x71,0x12,0x11,0x13 };
const unsigned char songData[] = { // 孤勇者音乐数据
62,94,1,0,59,64,1,0,62,94,1,0,64,94,1,0,66,94,1,0,62,94,1,0,64,94,1,0,66,94,1,0,67,94,1,0,71,94,1,0,74,94,1,0,78,94,1,0,79,94,1,0,81,94,1,0,83,94,1,0,77,94,1,0,79,94,1,0,81,94,1,0,83,94,1,0,81,94,1,0,77,94,1,0,78,94,1,0,74,94,1,0,71,94,1,0,79,94,1,0,74,94,1,0,71,94,1,0,67,94,1,0,62,94,1,0,69,94,1,0,62,94,1,0,64,94,1,0,66,94,1,0,67,94,1,0,71,94,1,0,74,94,1,0,78,94,1,0,79,94,1,0,81,94,1,0,83,94,1,0,77,94,1,0,79,94,1,0,81,94,1,0,83,94,1,0,81,94,1,0,77,94,1,0,78,94,1,0,74,94,1,0,71,94,1,0,79,94,1,0,74,94,1,0,71,94,0,0
};
unsigned char *songPtr;
unsigned char noteDuration;
void setNoteDuration(unsigned char duration) {
noteDuration = duration; // hold time for note, in 10ms units
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P3SEL |= 0x30; // P3.4 = SMCLK, P3.5 = TA0.1
P3DIR |= 0x20; // P3.5 = output
// 波特率计算,因为UART需要和音乐同步
UCA0CTL1 = UCSSEL_2;
UCA0BR0 = 104; // 9600 baud @ 1MHz
UCA0BR1 = 0;
UCA0MCTL = UCBRS0;
TA0CCTL1 = OUTMOD_7; // PWM输出
TA0CCR0 = PWM_PERIOD - 1; // number of PWM cycles in one second
TA0CCR1 = 0; // 初始值
songPtr = (unsigned char*)songData;
noteDuration = 0;
_EINT(); // enable interrupts
while (1) {
unsigned char c;
// 从串口读取键值
while (!(IFG2 & UCA0RXIFG));
c = UCA0RXBUF & 0x7F; // 忽略最高位,避免负数溢出
if (c == uartKey[0] || c == uartKey1[0] || c == uartKey2[0] || c == uartKey3[0] || c == uartKey4[0] || c == uartKey5[0]) { // 如果有正确的键值时开始播放
if (noteDuration == 0) {
// start playing next note
unsigned char note = *songPtr++;
noteDuration = *songPtr++;
TA0CCR1 = ((note + 1) * PWM_PERIOD) / 128; // 计算对应的PWM值
}
} else {
noteDuration = 0;
TA0CCR1 = 0;
}
// 减少音符持续时间,直到0
if (noteDuration > 0) {
noteDuration--;
}
}
return 0;
}
#pragma vector=TIMERA1_VECTOR // Overflow ISR
__interrupt void TimerA1_ISR(void) {
// restart playing the same note
if (noteDuration != 0) {
TA0CCR1 = 0;
__delay_cycles(500); // 等待500个计数周期
TA0CCR1 = TA0CCR1 + 1; // 重新播放
}
TAIV = 0; // clear interrupt flags
}
#pragma vector=TIMERA0_VECTOR // Start ISR
__interrupt void TimerA0_ISR(void) {
if (noteDuration != 0) { // enable second PWM cycle
TA0CCTL1 |= CCIE; // enable PWM ISR
} else { // disable second PWM cycle
TA0CCTL1 &= ~CCIE; // disable PWM ISR
}
}
```
注:此代码示例仅用于演示用途,具体应用需要根据实际情况进行调整,并考虑深度学习模型的使用、时间对齐、音调转换等问题。
阅读全文