#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
//***************************************** Setup 8-bit Timer 0 *****************************************//
TCCR0A = 0b00000010; // |COM0A1|COM0A0|COM0B1|COM0B0|0|0|WGM01|WGM00|
// COMA and COMB are set to normal, OC0A and OC0B are disconnected respectively
// WGM is set to CTC - Clear Timer on Compare (of OCR0A) Mode#2 = 010
TCCR0B = 0b00000000; // |FOC0A|FOC0B|0|0|WGM02|CS02|CS01|CS00|
// FOC set to default - WGM02 set to 0 - Clock set to off
TIMSK0 = 0b00000110; // |0|0|0|0|0|OCIE0B|OCIE0A|TOIE0|
// Timer Output Compare Match Interrupt A and B are enabled
TIFR0 = 0b00000000; // |0|0|0|0|0|OCFOB|OCF0A|TOV0|
// Flags are set to 0 - Default settings
// IE If running at 1 mhz (1000000 ticks per second), and selection set to 1024.
// Duration of one timer tick - 1024/1000000 sec = 0.001024
OCR0A = 244; // Set to trip every 0.249856 sec (244 * 0.001024) Which is about 0.25sec, 1/4 of a second.
OCR0B = 122; // Set to trip every 0.124928 sec (122 * 0.001024) Which is about 0.125sec, 1/8 of a second.
// We need to set Port B's Data Direction Register for pin PB0 for output.
DDRB = DDRB | 0b00000001; // Leave all the other bits alone, just set bit 0 for output
sei(); // Enables the global registers.
TCCR0B = 0b00000101; // |FOC0A|FOC0B|0|0|WGM02|CS02|CS01|CS00|
// Turn On Clock and set speed selection to Clock/1024
while(1)
{
}
}
ISR#TIMER0_COMPA_vect#
{
PORTB = PORTB & 0b11111110; // Turn off pin PB0 on Port B, leave all other bits alone.
}
ISR#TIMER0_COMPB_vect)
{
PORTB = PORTB | 0b00000001; // Turn on pin PB0 on Port B, leave all other bits alone.
}
|