Author: Not specified Language: c
Description: Not specified Timestamp: 2017-10-09 16:40:40 +0000
View raw paste Reply
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
int f; // flag variable to check if LED is on or off
int n=10, s=10; // numbers to modify the output compare match register
int main(void){
        sei(); // enables interrupts
        f=0;
        DDRD = 0xFF; // sets all pins in PORTD to output
        TCNT1 = 1; // sets the counter of Timer 1 to 1
        TIMSK1 = 0b00000010; // enables interrupts through output compare match A
        TCCR1B = 0b00000001; // sets the clock source without prescaler
        OCR1A = 31250; // sets the output compare match register at 31250
        for(;;);
}


ISR(TIMER1_COMPA_vect){
        if(f==0){
                PORTD ^= 0xFF; // toggles all pins in PORTD
                OCR1A = n; // sets the output compare match register at n
                f=1; // toggles flag variable
                n=n+s; // increments or decrements n by 10
                if(n>=20000){ // check for the max when incrementing n
                        s=-10; // set s to decrement n
                }
                if(n<=1){ //check for the min when decrementing n
                        s=10; // set s to increment n
                }
        }
        else{
                PORTD ^= 0xFF; // toggles all pins in PORTD
                OCR1A = 31250; // sets the output compare match register at 31250
                f=0; // toggles flag variable
        }
}
View raw paste Reply