// Header: Copyright (c) L.Luo
// File Name: Music.c via LM3S8962 Kit
// Author: L.Luo
// Date: 10.05,2010,Eveing
#include <LM3Sxxxx.h>
// 定义系统时钟 8M
#define TheSysClock 8000000
// 全局变量
unsigned long g_ulFrequency;
unsigned long g_ulDutyCycle;
// 定义低音音名(数值单位:Hz)
#define L1 262 // c
#define L2 294 // d
#define L3 330 // e
#define L4 349 // f
#define L5 392 // g
#define L6 440 // a1
#define L7 494 // b1
// 定义中音音名
#define M1 523 // c1
#define M2 587 // d1
#define M3 659 // e1
#define M4 698 // f1
#define M5 784 // g1
#define M6 880 // a2
#define M7 988 // b2
// 定义高音音名
#define H1 1047 // c2
#define H2 1175 // d2
#define H3 1319 // e2
#define H4 1397 // f2
#define H5 1568 // g2
#define H6 1760 // a3
#define H7 1976 // b3
// 定义时值单位,决定演奏速度(数值单位:ms)
#define T 3600
// 定义音符结构
typedef struct
{
short mName; // 音名:取值L1~L7、M1~M7、H1~H7分别表示低音、中音、高音的
// 1234567,取值0表示休止符
short mTime; // 时值:取值T、T/2、T/4、T/8、T/16、T/32分别表示全音符、
// 二分音符、四分音符、八分音符…,取值0表示演奏结束
}tNote;
// 定义乐曲:《化蝶》(梁祝)
const tNote MyScore[] =
{
{L3, T/4},
{L5, T/8+T/16},
{L6, T/16},
{M1, T/8+T/16},
{M2, T/16},
{L6, T/16},
{M1, T/16},
{L5, T/8},
{M5, T/8+T/16},
{H1, T/16},
{M6, T/16},
{M5, T/16},
{M3, T/16},
{M5, T/16},
{M2, T/2},
{M2, T/8},
{M2, T/16},
{M3, T/16},
{L7, T/8},
{L6, T/8},
{L5, T/8+T/16},
{L6, T/16},
{M1, T/8},
{M2, T/8},
{L3, T/8},
{M1, T/8},
{L6, T/16},
{L5, T/16},
{L6, T/16},
{M1, T/16},
{L5, T/2},
{M3, T/8+T/16},
{M5, T/16},
{L7, T/8},
{M2, T/8},
{L6, T/16},
{M1, T/16},
{L5, T/8},
{L5, T/4},
{L3, T/16},
{L5, T/16},
{L3, T/8},
{L5, T/16},
{L6, T/16},
{L7, T/16},
{M2, T/16},
{L6, T/4+T/8},
{L5, T/16},
{L6, T/16},
{M1, T/8+T/16},
{M2, T/16},
{M5, T/8},
{M3, T/8},
{M2, T/8},
{M3, T/16},
{M2, T/16},
{M1, T/8},
{L6, T/16},
{L5, T/16},
{L3, T/4},
{M1, T/4},
{L6, T/16},
{M1, T/16},
{L6, T/16},
{L5, T/16},
{L3, T/16},
{L5, T/16},
{L6, T/16},
{M1, T/16},
{L5, T/2},
{ 0, T/4},
{ 0, T/4},
{ 0, 0} // 结束
};
// 蜂鸣器外设初始化
void
io_init(void)
{
unsigned long ulPWMClock;
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC |
SYSCTL_XTAL_8MHZ | SYSCTL_OSC_MAIN);
// Enable GPIO bank F to allow control of the LED.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Configure Port F0 for as an output for the status LED.
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_0);
// Initialize LED to OFF (0)
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0);
// Enable Port G1 for PWM output.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
GPIOPinTypePWM(GPIO_PORTG_BASE,GPIO_PIN_1);
// Enable the PWM generator.
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM);
// Configure the PWM generator for count down mode with immediate updates
// to the parameters.
PWMGenConfigure(PWM_BASE, PWM_GEN_0,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
// Divide the PWM clock by 1.
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
// Get the PWM clock.
ulPWMClock = SysCtlClockGet();
// Intialize the PWM frequency and duty cycle.
g_ulFrequency = 440;
g_ulDutyCycle = 25;
// Set the period of PWM1.
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, ulPWMClock / g_ulFrequency);
// Set the pulse width of PWM1.
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1,
((ulPWMClock * g_ulDutyCycle)/100) / g_ulFrequency);
// Start the timers in generator 0.
PWMGenEnable(PWM_BASE, PWM_GEN_0);
}
// Set the status LED on or off.
void
io_set_led(tBoolean bOn)
{
// Turn the LED on or off as requested.
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, bOn ? GPIO_PIN_0 : 0);
}
// Turn PWM on/off
void
io_set_pwm(tBoolean bOn)
{
// Enable or disable the PWM1 output.
PWMOutputState(PWM_BASE, PWM_OUT_1_BIT, bOn);
}
// Set PWM Frequency
void
io_pwm_freq(unsigned long ulFreq)
{
unsigned long ulPWMClock;
// Get the PWM clock
ulPWMClock = SysCtlClockGet();
// Set the global frequency
g_ulFrequency = ulFreq;
// Set the period.
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, ulPWMClock / g_ulFrequency);
// Set the pulse width of PWM1
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1,
((ulPWMClock * g_ulDutyCycle)/100) / g_ulFrequency);
}
// Set PWM Duty Cycle
void
io_pwm_dutycycle(unsigned long ulDutyCycle)
{
unsigned long ulPWMClock;
// Get the PWM clock
ulPWMClock = SysCtlClockGet();
// Set the global duty cycle
g_ulDutyCycle = ulDutyCycle;
// Set the period.
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, ulPWMClock / g_ulFrequency);
// Set the pulse width of PWM1
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1,
((ulPWMClock * g_ulDutyCycle)/100) / g_ulFrequency);
}
// 演奏乐曲, 在PWM发声的同时点亮LED灯,反之熄灭
void MusicPlay(void)
{
int i = 0;
for (;;)
{
if (MyScore[i].mTime == 0)
{
break;
}
io_pwm_freq(MyScore[i].mName);
io_set_pwm(1);
io_set_led(1);
SysCtlDelay(MyScore[i].mTime * (TheSysClock / 2000));
i++;
io_set_pwm(0);
io_set_led(0);
SysCtlDelay(10 * (TheSysClock / 2000));
}
}
// 主程序,演奏《梁祝》
int main()
{
io_init();
io_set_led(1);
io_set_pwm(1);
while(1)
{
MusicPlay();
SysCtlDelay(2000 * (TheSysClock / 2000));
};
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
music.rar (21个子文件)
music
music.Opt 3KB
music.c 6KB
music.lnp 264B
music.__i 136B
music.Uv2 2KB
music.crf 210KB
music.htm 26KB
music.o 178KB
music_Uv2.Bak 2KB
music.tra 950B
music.d 2KB
Startup.o 3KB
music.plg 3KB
music.axf 32KB
music_Target 1.dep 2KB
music.hex 7KB
Startup.s 10KB
music.map 63KB
music_Opt.Bak 1KB
music.sct 479B
Startup.lst 26KB
共 21 条
- 1
资源评论
- wlj198909022012-08-04程序简单实用,很好!
- tanyu_sky2012-10-24简单,对我等初学者很有好处。谢谢
- chedanxueye2015-06-29找了好久 终于找到了 不错 挺实用的
yuluyangguang
- 粉丝: 4
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功