📄 音乐.c
字号:
//内部晶振 8M
#include <iom16v.h>
#include <macros.h>
#include"delay.h"
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
#define DO 64580 //523
#define RE 64684 //587
#define MI 64777 //659
#define FA 64820 //698
#define SO 64898 //784
#define LA 64968 //880
#define XI 65030 //988
#define DO_H 65058 //1046
#define RE_H 65110 //1174
#define MI_H 65157 //1318
#define FA_H 65178 //1397
#define SO_H 65217 //1568
#define LA_H 65252 //1760
#define XI_H 65283 //1976
#define DO_HH 65297 //2093
const unsigned char sound[]={ //乐曲数据表
/****************两只老虎**********************/
0x48,0x49,0x4a,0x48,0x48,0x49,0x4a,0x48,0x4a,0x4b,0x8c,
0x4a,0x4b,0x8c,0x2c,0x2d,0x2c,0x2b,0x4a,0x48,
0x2c,0x2d,0x2c,0x2b,0x4a,0x48,0x48,0x45,0x88,0x48,0x45,0x88,
/*************梁祝****************/
0x40,0x4c,0x4a,0x49,0xc8,0x49,0x47,0x46,
0xe5,0x4e,0x4d,0x4e,0x6c,0x2d,0x4e,
0x4d,0x2c,0x2d,0x2e,0x2d,0x6f,0x2d,
0x2c,0x2d,0x2c,0x29,0x2a,0x2b,
0x2a,0x29,0xc8,0x4c,0x4e,0x49,0x46,0x48,
0xc5,0x26,0x28,0xe5,0x83,0x65,0x26,
0x68,0x29,0x26,0x28,0x45,0x6c,0x2f,0x3d,
0x1c,0x2a,0x2c,0xe9,0x69,0x2a,0x47,0x46,
0x65,0x26,0x48,0x49,0x43,0x48,0x36,0x15,
0x26,0x18,0xe5,0x6a,0x2c,0x47,0x49,0x26,
0x28,0x85,0x33,0x15,0x43,0x25,0x26,0x27,
0x29,0xc6,0x25,0x26,0x68,0x29,0x4c,0x4a,
0x49,0x2a,0x29,0x49,0x26,0x25,0x83,0x88,
0x26,0x28,0x26,0x25,0x23,0x25,0x26,0x28,
0xc5,0x04,0x04,
/*************希望*****************/
0x49,0x4a,0x4a,0x6a,0x29,0x48,0x46,0x68,
0x29,0xc8,0x49,0x4a,0x4a,0x6a,0x2c,0x4a,
0x4a,0x49,0x4a,0xca,0x4c,0x4d,0x4d,0x6d,
0x2c,0x4a,0x4a,0x4c,0x4d,0xcc,0x49,0x4a,
0x4a,0x69,0x2a,0x4a,0x49,0x4a,0x46,
0x18,0xe6,0x29,0x29,0x29,0x28,0x46,
0x29,0x29,0x29,0x28,0x46,0x49,0x4a,
0x48,0x69,0x2a,0x4c,0x2d,0x2d,0x2d,
0x2c,0x4a,0x29,0x29,0x29,0x28,0x46,
0x46,0x45,0x46,0xe6,
/***************喀秋莎********************/
0x66,0x27,0x68,0x26,0x28,0x28,0x27,0x26,0x47,0x43,
0x67,0x28,0x69,0x27,0x29,0x29,0x28,0x27,0x46,0x46,
0x4a,0x4d,0x4c,0x2d,0x2c,0x2b,0x2b,0x2a,0x29,0x4a,
0x46,0x20,0x4b,0x29,0x6a,0x28,0x27,0x23,0x28,0x27,
0x46,0x46,0x4a,0x4d,0x4c,0x2d,0x2c,0x2b,0x2b,0x2a,
0x29,0x4a,0x46,0x20,0x4b,0x29,0x6a,0x28,0x27,0x23,
0x28,0x27,0x46,0x46,
0x00};
/* 音阶表 */
const unsigned int TONETABLE[15]={DO,RE,MI,FA,SO,LA,XI,DO_H,
RE_H,MI_H,FA_H,SO_H,LA_H,XI_H,DO_HH};
/* 音阶重装常数 */
unsigned int toneconst;
//端口初始化
void port_init(void)
{
PORTA = 0x00;//设置端口方向、数据
DDRA = 0x03;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//定时器T0初始化
void timer0_init(void)
{
TCCR0 = 0x00;//停止定时器
TCNT0 = 0xFF;//初始值
OCR0 = 0xFE;//匹配值
TIMSK |= 0x00;//溢出中断不允许
TCCR0 = 0x02;//启动定时器
}
void init_devices(void)
{
CLI(); //禁止所有中断
MCUCR = 0x00;//外部中断触发方式
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;//外部中断使能
port_init();
timer0_init();
SEI();//开全局中断
}
//T0溢出中断服务程序
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0=toneconst;
PORTA^=BIT(PA0);
}
void music(void)
{
unsigned char temp=0,temp1=0;
unsigned int sound_add=0;
temp1=sound[sound_add];
while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
if(temp!=0)
{
TIMSK|=(1<<TOIE0);//根据SOUNDTABLE中数据的低四位选择音调频率
temp--;
toneconst=TONETABLE[temp];
TCNT0=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
DelayMS(temp);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<<TOIE0);
sound_add++;
temp1=sound[sound_add];
}
}
//主函数
void main(void)
{
init_devices();
//在这继续添加你的代码
while(1)
{
music();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -