原代码
//ICC-AVR application builder
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
#include <STRING.H>
#include <stdlib.h>
int n;
unsigned int adc,tc0no;
//一段声音
__flash char sound[373]={0x89,0x96,0x95,0x94,0xA2,0xB3,0xBE,0xC6,0xC5,0xC2,0xC5,0xD8,
0xE5,0xE5,0xE7,0xE3,0xDE,0xE0,0xDF,0xDF,0xDA,0xDB,0xDC,0xDA,0xD5,0xD4,0xD3,0xD4,
0xD8,0xD4,0xD0,0xCE,0xCC,0xCF,0xD0,0xCC,0xB9,0xA8,0x9D,0x88,0x7B,0x70,0x5F,0x53,
0x50,0x4D,0x48,0x3F,0x47,0x60,0x69,0x60,0x56,0x44,0x41,0x4E,0x51,0x4D,0x4A,0x51,
0x56,0x49,0x46,0x44,0x44,0x4F,0x59,0x59,0x66,0x72,0x76,0x75,0x83,0x90,0x98,0xA2,
0xAD,0xB6,0xBB,0xB8,0xB8,0xBC,0xC0,0xC4,0xC2,0xBF,0xC1,0xD0,0xD2,0xCB,0xC6,0xCD,
0xCE,0xCB,0xC2,0xC2,0xC1,0xBB,0xAB,0xB0,0xB7,0xB9,0xA4,0xA4,0xB0,0xAC,0xAB,0xB2,
0xA7,0x9A,0x86,0x71,0x6A,0x6D,0x5A,0x41,0x34,0x31,0x25,0x11,0x08,0x06,0x08,0x0D,
0x11,0x14,0x16,0x12,0x13,0x13,0x1E,0x2F,0x32,0x33,0x40,0x47,0x3C,0x2B,0x26,0x21,
0x33,0x3F,0x3C,0x3F,0x3C,0x3F,0x57,0x66,0x5E,0x61,0x77,0x90,0x98,0x8B,0x88,0x90,
0x9A,0xA3,0xA5,0xA6,0xA7,0xAF,0xAE,0xB2,0xBC,0xC7,0xC6,0xCD,0xD3,0xDD,0xDA,0xDA,
0xDF,0xDE,0xDF,0xDC,0xD4,0xD4,0xD7,0xD3,0xD3,0xD3,0xD5,0xC8,0xB9,0xA4,0x97,0x8D,
0x75,0x65,0x61,0x66,0x61,0x50,0x44,0x3B,0x2E,0x2C,0x37,0x3A,0x41,0x49,0x44,0x38,
0x33,0x40,0x43,0x49,0x57,0x6C,0x6A,0x64,0x67,0x65,0x57,0x55,0x52,0x56,0x5E,0x64,
0x69,0x72,0x7B,0x8C,0x98,0x9A,0xAA,0xC1,0xC8,0xC7,0xC0,0xC6,0xD0,0xCE,0xCD,0xCA,
0xC9,0xC8,0xC8,0xC9,0xCD,0xCA,0xCD,0xC6,0xC9,0xCE,0xCE,0xCC,0xD0,0xD3,0xD7,0xCA,
0xBE,0xBB,0xB7,0xB1,0xA1,0x91,0x88,0x7E,0x7F,0x73,0x5E,0x50,0x35,0x31,0x3B,0x35,
0x29,0x24,0x11,0x0B,0x0B,0x0E,0x12,0x15,0x17,0x15,0x13,0x17,0x1C,0x20,0x20,0x2C,
0x3D,0x3A,0x31,0x29,0x1C,0x27,0x42,0x48,0x45,0x41,0x48,0x55,0x5E,0x6A,0x78,0x84,
0x8F,0x97,0x93,0x99,0xA5,0xA8,0xA6,0xB8,0xAD,0x9B,0xB2,0xC9,0xC8,0xC3,0xBB,0xBD,
0xC9,0xD5,0xD1,0xD1,0xD3,0xDB,0xDE,0xDA,0xD9,0xD8,0xD7,0xDA,0xD8,0xD4,0xCF,0xCA,
0xC7,0xC1,0xB4,0x9A,0x81,0x7A,0x70,0x67,0x5A,0x44,0x3A,0x35,0x28,0x18,0x0F,0x13,
0x28,0x31,0x24,0x1E,0x20,0x1E,0x30,0x3F,0x3D,0x3D,0x4D,0x52,0x4A,0x4A,0x4C,0x44,
0x49,0x53,0x52,0x52,0x5C,0x68,0x6E,0x75,0x80};
//根据电位器的数值调整播放速度
__flash char tc0start[33]={0x82,0x87,0x8C,0x91,0x95,0x99,0x9D,0xA0,0xA4,0xA7,
0xA9,0xAC,0xAF,0xB1,0xB3,0xB5,0xB7,0xB9,0xBB,0xBC,0xBE,0xC0,0xC1,0xC2,0xC4,
0xC5,0xC6,0xC7,0xC8,0xCA,0xCB,0xCC,0xCD};
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x02; //PB1 --> OC1A output
PORTC = 0x00; //m103 output only
DDRC = 0x00; // PC.0 ADC
PORTD = 0x00;
DDRD = 0x00;
}
//ADC initialize
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x40; //AVcc ADC0 0100 0000
ACSR = 0x80; //analogy comparer 1000 0000
ADCSR = 0xEF; //1110 1111 self trigger
}
#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
adc=(unsigned int)ADCL; //Read 8 low bits first (important)
adc|=((unsigned int)ADCH << 8); //read 2 high bits and shift into top byte
tc0no=(adc>>5);
}
//TIMER0 initialize - prescale:8
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x82; //set count
TCCR0 = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = tc0start[tc0no]; //播放速度
OCR1AH =0x00;
OCR1AL = sound[n];
n++; //声音循环播放
if (n-372>0)
{
n=0;
}
}
//TIMER1 initialize - prescale:1
// WGM: 5) PWM fast, TOP=0xFF
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x01;
OCR1AH = 0x00;
OCR1AL = 0x80;
OCR1BH = 0x00;
OCR1BL = 0x80;
ICR1H = 0x00;
ICR1L = 0xFF;
TCCR1A = 0xA1; //1010 0010
TCCR1B = 0x09; //start Timer 0000 1001
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
adc_init();
timer0_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
n=0;tc0no=0;
init_devices();
while (1)
{
} //while(1)
} //main
有不对的地方请指出,我也是初学者,见笑了 |