移植ICCAVR到WINAVR演奏出第一首歌(music.c)
扫描二维码
随时随地手机看文章
//music.h
/*音阶常数=65536-8000000/8/2/f*/
#defineDO64580//523
#defineRE64684//587
#defineMI64777//659
#defineFA64820//698
#defineSO64898//784
#defineLA64968//880
#defineTI65030//988
#defineDO_H65058//1046
#defineRE_H65110//1174
#defineMI_H65157//1318
#defineFA_H65178//1397
#defineSO_H65217//1568
#defineLA_H65252//1760
#defineTI_H65283//1976
#defineDO_HH65297//2093
/*位操作宏*/
#defineset_bit(x,y)(x|=(1<
//music.c
/*目标MCU:MEGA8晶振:外部(EXT)8MHZ*/
//使用说明:音乐输出端口:PORTC.0,音乐数据存放
//于ATMEGA8的EEPROM存贮器中,可以使用music.exe
//程序通过RS232通信口进行修改。
//#include
//#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include"music.h"
//externvoiduart_init(void);
//externvoidisp(void);
/*-------------------------------------------------------------------------------*/
#define__noinit____attribute__((section(".noinit")))//变量不初始化
#define__eeprom____attribute__((section(".eeprom")))//变量定义在eeprom中
#define__flash__PROGMEM//变量定义在flash中(写法1)
//#define__flash____attribute__((section(".progmem")))//变量定义在flash中(写法2)
//#define__flash____attribute__((progmem))//变量定义在flash中(写法3)
//prg_uchar等
#definepersistent__noinit__//变量不初始化,相当于PICC的persistent
#definecodePROGMEM//同__flash__,变量定义在flash中,相当于KeilC51的code
/*-------------------------------------------------------------------------------*/
/*音乐数据(卡秒莎)*/
/*四分之一拍为0x10,二分之一拍为0x20,一拍为0x40*/
/*低1-12-23-34-45-56-67-7*/
/*中1-82-93-A4-B5-C6-D7-E*/
/*高1-F*/
//#pragmadata:eeprom
/*
const__eeprom__unsignedcharsound[]={//正常节奏
//LATIDO_HLADO_HDO_HTILATIMI
0x66,0x27,0x68,0x26,0x28,0x28,0x27,0x26,0x47,0x43,
//TIDO_HRE_HTIRE_HRE_HDO_HTILALA
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__eeprom__unsignedcharsound[]={//全短音
//LATIDO_HLADO_HDO_HTILATIMI
0x26,0x27,0x28,0x26,0x28,0x28,0x27,0x26,0x27,0x23,
//TIDO_HRE_HTIRE_HRE_HDO_HTILALA
0x67,0x28,0x29,0x27,0x29,0x29,0x28,0x27,0x26,0x26,
0x2a,0x2d,0x2c,0x2d,0x2c,0x2b,0x2b,0x2a,0x29,0x2a,
0x26,0x20,0x2b,0x29,0x2a,0x28,0x27,0x23,0x28,0x27,
0x26,0x26,0x2a,0x2d,0x2c,0x2d,0x2c,0x2b,0x2b,0x2a,
0x29,0x2a,0x26,0x20,0x2b,0x29,0x2a,0x28,0x27,0x23,
0x28,0x27,0x26,0x26,0x00
};
/*
const__eeprom__unsignedcharsound[]={//全长音
//LATIDO_HLADO_HDO_HTILATIMI
0x66,0x67,0x68,0x66,0x68,0x68,0x67,0x66,0x67,0x63,
//TIDO_HRE_HTIRE_HRE_HDO_HTILALA
0x67,0x68,0x69,0x67,0x69,0x69,0x68,0x67,0x66,0x66,
0x6a,0x6d,0x6c,0x6d,0x6c,0x6b,0x6b,0x6a,0x69,0x6a,
0x66,0x60,0x6b,0x69,0x6a,0x68,0x67,0x63,0x68,0x67,
0x66,0x66,0x6a,0x6d,0x6c,0x6d,0x6c,0x6b,0x6b,0x6a,
0x69,0x6a,0x66,0x60,0x6b,0x69,0x6a,0x68,0x67,0x63,
0x68,0x67,0x66,0x66,0x00
};
*/
/*
const__eeprom__unsignedcharsound[]={//全低音
//LATIDO_HLADO_HDO_HTILATIMI
0x66,0x27,0x61,0x26,0x21,0x21,0x27,0x26,0x47,0x43,
//TIDO_HRE_HTIRE_HRE_HDO_HTILALA
0x67,0x21,0x62,0x27,0x22,0x22,0x21,0x27,0x46,0x46,
0x43,0x46,0x45,0x26,0x25,0x24,0x24,0x23,0x22,0x43,
0x46,0x20,0x44,0x22,0x63,0x21,0x27,0x23,0x21,0x27,
0x46,0x46,0x43,0x46,0x45,0x26,0x25,0x24,0x24,0x23,
0x22,0x41,0x46,0x20,0x44,0x22,0x63,0x21,0x27,0x23,
0x21,0x27,0x46,0x46,0x00
};
*/
//#pragmadata:data
/*音阶表*/
constunsignedintTONETABLE[15]={DO,RE,MI,FA,SO,LA,TI,DO_H,
RE_H,MI_H,FA_H,SO_H,LA_H,TI_H,DO_HH};
/*音阶重装常数*/
unsignedinttoneconst;
/*微秒级延时程序*/
voiddelay_us(inttime)
{
do
{
time--;
}
while(time>1);
}
/*毫秒级延时程序*/
voiddelay_ms(unsignedinttime)
{
while(time!=0)
{
delay_us(1000);
time--;
}
}
//#pragmainterrupt_handlertimer1_ovf_isr:iv_TIMER1_OVF
//voidtimer1_ovf_isr(void)
SIGNAL(SIG_OVERFLOW1)
{
TCNT1=toneconst;
//PORTC^=1;
PORTC^=(1<
voidmusic(void)
{
unsignedchartemp,temp1;
unsignedintsound_add=0;
TCCR1A=0x00;
TCCR1B=(1<
//temp1=eeprom_read_byte(sound[sound_add]);
temp1=eeprom_read_byte(sound+sound_add);
while(temp1!=0)//0为音乐结尾符
{
temp=temp1;
temp&=0x0f;//取低四位选择音调频率
if(temp!=0)
{
TIMSK|=(1<
toneconst=TONETABLE[temp];//取选择音调频率
TCNT1=toneconst;//发声定时开始
}
temp=temp1;
temp>>=4;//取高四位
//temp&=0x0f;//废话!!!
//delay_ms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间(等待)
delay_ms(temp*138);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间(等待138mS较好些)
TIMSK&=~(1<
//temp1=EEPROMread((int)&sound[sound_add]);
//temp1=eeprom_read_byte(sound[sound_add]);
temp1=eeprom_read_byte(sound+sound_add);//取一个音符或结尾符
}
delay_ms(1000);
}
/*主程序*/
voidmain(void)
{
DDRC=0x01;
PORTC=0xff;
//uart_init();
sei();
while(1)
{
music();
}
}