移植ICCAVR到WINAVR演奏出第一首歌(music.c)
//music.h
/*音階常數(shù)=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
/*目標(biāo)MCU:MEGA8晶振:外部(EXT)8MHZ*/
//使用說(shuō)明:音樂(lè)輸出端口:PORTC.0,音樂(lè)數(shù)據(jù)存放
//于ATMEGA8的EEPROM存貯器中,可以使用music.exe
//程序通過(guò)RS232通信口進(jìn)行修改。
//#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中(寫(xiě)法1)
//#define__flash____attribute__((section(".progmem")))//變量定義在flash中(寫(xiě)法2)
//#define__flash____attribute__((progmem))//變量定義在flash中(寫(xiě)法3)
//prg_uchar等
#definepersistent__noinit__//變量不初始化,相當(dāng)于PICC的persistent
#definecodePROGMEM//同__flash__,變量定義在flash中,相當(dāng)于KeilC51的code
/*-------------------------------------------------------------------------------*/
/*音樂(lè)數(shù)據(jù)(卡秒莎)*/
/*四分之一拍為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[]={//正常節(jié)奏
//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[]={//全長(zhǎng)音
//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};
/*音階重裝常數(shù)*/
unsignedinttoneconst;
/*微秒級(jí)延時(shí)程序*/
voiddelay_us(inttime)
{
do
{
time--;
}
while(time>1);
}
/*毫秒級(jí)延時(shí)程序*/
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為音樂(lè)結(jié)尾符
{
temp=temp1;
temp&=0x0f;//取低四位選擇音調(diào)頻率
if(temp!=0)
{
TIMSK|=(1<
toneconst=TONETABLE[temp];//取選擇音調(diào)頻率
TCNT1=toneconst;//發(fā)聲定時(shí)開(kāi)始
}
temp=temp1;
temp>>=4;//取高四位
//temp&=0x0f;//廢話!!!
//delay_ms(temp*129);//根據(jù)SOUNDTABLE中數(shù)據(jù)的高四位*5后,為音調(diào)持續(xù)時(shí)間(等待)
delay_ms(temp*138);//根據(jù)SOUNDTABLE中數(shù)據(jù)的高四位*5后,為音調(diào)持續(xù)時(shí)間(等待138mS較好些)
TIMSK&=~(1<
//temp1=EEPROMread((int)&sound[sound_add]);
//temp1=eeprom_read_byte(sound[sound_add]);
temp1=eeprom_read_byte(sound+sound_add);//取一個(gè)音符或結(jié)尾符
}
delay_ms(1000);
}
/*主程序*/
voidmain(void)
{
DDRC=0x01;
PORTC=0xff;
//uart_init();
sei();
while(1)
{
music();
}
}