//ICC-AVR application builder : 2011-12-31 오후 5:42:51
// Target : M128
// Crystal: 16.000Mhz
#include <iom128v.h>
#include <macros.h>
#include <stdio.h>
#define C1 523 // 도
#define C1_ 554
#define D1 587 // 레
#define D1_ 622
#define E1 659 // 미
#define F1 699 // 파
#define F1_ 740
#define G1 784 // 솔
#define G1_ 831
#define A1 880 // 라
#define A1_ 932
#define B1 988 // 시
#define C2 C1*2 // 도
#define C2_ C1_*2
#define D2 D1*2 // 레
#define D2_ D1_*2
#define E2 E1*2 // 미
#define F2 F1*2 // 파
#define F2_ F1_*2
#define G2 G1*2 // 솔
#define G2_ G1_*2
#define A2 A2*2 // 라
#define A2_ A2_*2
#define B2 B2*2 // 시
#define DLY_1 DLY_4*4 // 온음표
#define DLY_2 DLY_4*2 // 2분 음표
#define DLY_4 400 // 4분 음표
#define DLY_8 DLY_4/2 // 8분 음표
#define DLY_16 DLY_8/2 // 16분 음표
volatile int schoolsong[100]={G1,G1,A1,A1,G1,G1,E1,G1,G1,E1,E1,D1,G1,G1,A1,A1,G1,G1,E1,G1,E1,D1,E1,C1};
volatile int length[100]={DLY_4,DLY_4,DLY_4,DLY_4,DLY_4,DLY_4,DLY_2,DLY_4,DLY_4,DLY_4,DLY_4,DLY_1,DLY_4,DLY_4,DLY_4,DLY_4,DLY_4,DLY_4,DLY_2,DLY_4,DLY_4,DLY_4,DLY_4,DLY_1,};
volatile unsigned char TIMER=0x06;
volatile long value;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x80;
PORTG = 0x00;
DDRG = 0x10;
}
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1KHz
// actual value: 1.000KHz (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 = 0x06; //set count
OCR0 = 0xFA;
TCCR0 = 0x04; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = TIMER; //reload counter value
PORTF ^= 0x80;
}
int putchar(char c)
{
while (((UCSR0A>>UDRE0)&0x01) == 0) ; // UDRE, data register empty
UDR0 = c;
return c;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
//uart0_init();
timer0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x01; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay(int delaytime){
int i,j;
for(i=0;i<=1000;i++){
for(j=0;j<=delaytime;j++){
}
}
}
//
void main(void)
{
int freq = 1000, i,j;
init_devices();
//insert your functional code here...
while(1){
for(j=0;j<10000;j++){
for(i=0;i<20000;i++){
freq = (50-i)*10+j;
value = 255-(1000000/(8*freq)); // value = 255-((1/(2*freq)) / (4/1000000));
TIMER = value;
delay(100);
}
}
for(i=0;i<50;i++){
freq = i*100;
value = 255-(1000000/(8*freq)); // value = 255-((1/(2*freq)) / (4/1000000));
TIMER = value;
delay(1);
}
}
}