Kod: Markera allt
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
volatile unsigned int step;
volatile unsigned int duration;
volatile unsigned int i=0;
volatile unsigned int p=0;
volatile unsigned int pause=0;
volatile unsigned long int PauseCounter=0;
// This array contains the number of interrupts needed to produce the
// correct note at interrupt frequency 16Mhz
volatile unsigned char track1[299] = {
47,47,47,60,47,40,60,80,95,71,63,67,71,80,47,40,36,45,40,47,60,53,63,60,
80,95,71,63,67,71,80,47,40,36,45,40,47,60,53,63,40,42,45,50,47,75,71,60,
71,60,53,40,42,45,50,47,30,30,30,40,42,45,50,47,75,71,60,71,60,53,50,53,
60,40,42,45,50,47,75,71,60,71,60,53,40,42,45,50,47,30,30,30,40,42,45,50,
47,75,71,60,71,60,53,50,53,60,60,60,60,60,53,47,60,71,80,60,60,60,60,53,
47,60,60,60,60,53,47,60,71,80,47,47,47,60,47,40,60,80,95,71,63,67,71,80,
47,40,36,45,40,47,60,53,63,60,80,95,71,63,67,71,80,47,40,36,45,40,47,60,
53,63,47,60,80,75,71,45,45,71,63,36,36,36,40,45,47,60,71,80,47,60,80,75,
71,45,45,71,63,45,45,45,47,53,60,47,60,80,75,71,45,45,71,63,36,36,36,40,
45,47,60,71,80,47,60,80,75,71,45,45,71,63,45,45,45,47,53,60,60,60,60,60,
53,47,60,71,80,60,60,60,60,53,47,60,60,60,60,53,47,60,71,80,47,47,47,60,
47,40,47,60,80,75,71,45,45,71,63,36,36,36,40,45,47,60,71,80,47,60,80,75,
71,45,45,71,63,45,45,45,47,53,60};
// This array contains all the delays between the notes
volatile unsigned char pause1[298] = {
8,32,32,8,32,176,56,56,56,32,32,8,32,16,16,16,32,8,32,32,8,8,56,56,56,56,
32,32,8,32,16,16,16,32,8,32,32,8,8,104,8,8,8,32,32,8,8,32,8,8,56,8,8,8,32,
32,32,8,128,8,8,8,32,32,8,8,32,8,8,56,56,56,224,8,8,8,32,32,8,8,32,8,8,56,
8,8,8,32,32,32,8,128,8,8,8,32,32,8,8,32,8,8,56,56,56,176,8,32,32,8,32,8,32,
8,80,8,32,32,8,8,200,8,32,32,8,32,8,32,8,80,8,32,32,8,32,176,56,56,56,32,
32,8,32,16,16,16,32,8,32,32,8,8,56,56,56,56,32,32,8,32,16,16,16,32,8,32,
32,8,8,56,8,32,56,32,8,32,8,80,16,16,16,16,16,16,8,32,8,80,8,32,56,32,8,
32,8,80,8,32,8,16,16,16,176,8,32,56,32,8,32,8,80,16,16,16,16,16,16,8,32,
8,80,8,32,56,32,8,32,8,80,8,32,8,16,16,16,176,8,32,32,8,32,8,32,8,80,
8,32,32,8,8,200,8,32,32,8,32,8,32,8,80,8,32,32,8,32,176,8,32,56,32,8,
32,8,80,16,16,16,16,16,16,8,32,8,80,8,32,56,32,8,32,8,80,8,32,8,16,16,16};
int main(void)
{
TCCR0B = 1; // Prescler = 1 --> counter frequency 62,5kHz (8bit counter)
TIMSK0 = 1; // Enable timer/counter0 overflow interrupt
DDRD = 0xFF; // All pins outputs
DDRB = 0xFF;
sei(); //enable interrupts
while(1)
{
//infinite loop to keep interrupts running.
}
}
//interrupt overflow vector. This interrupt has a frequency of 16 MHz / 256 = 62500 Hz.
//The period is 16uS. (prescaler = 1, 8 bit register)
ISR(TIMER0_OVF_vect)
{
if (pause == 1) //if pause=1 pause between notes
{
PauseCounter++;
if(PauseCounter==pause1[p]*100) //the end of pause has been reached.
{
pause=0; //Clear pause flag
PauseCounter=0;
p++; //jump to next pause element
if(p==297)
{
p=0; //if end of array has been reached. restart.
}
}
}
else if(pause == 0) //if pause=0 we should play notes
{
// This loop toggles the pin to the correct frequency. the track array contains
// the number of interrupts to be played. This is based on an interrupt freq
// of 62,5 kHz.
if(step == track1[i])
{
PORTD ^= (1<<0);
step=0; //clear counter.
}
step++; //increase interrupt counter
duration++; //Increase duration counter.
if(duration == 35625) //Every note should be played for 0,57s --> 35625 * 0,16us = 0.57s
{
i++; //Go to the next tone
duration = 0; //Cclear duration counter
pause=1; //set the pause flag to pause between notes
if (i==298)
{
i=0; //if end of track has been reached. restart.
}
}
}
}
EDIT: OK, ser nu att det verkar vara lite kortare i ditt andra försök, men jag får i
alla fall scrolla vänster/höger fortfarande. Lite mindre dåligt, men inte bra...
Skit samma, det är inte helt avgörande, du har fått en del andra frågor/tips som du kanske
ska jobba med istället. Men ta med det till nästa gång, så att du fixar formatteringen då...