In: Electrical Engineering
Question 1:
Answer the following questions related to AVR Timer/counter:
1.AVR program that flashes an LED (PC0) every 6 ms using CPU clock frequency of 32 kHz and TIMER0
#include<avr/io.h> Void timer0_init() { //set up timer with no prescale TCCR0 |= (1<< CS00); //Initialization of counter TCNT0 = 0; } int main(void) { // connecting led to pin PC0 DDRC |= (1<<0); timer0_init(); while(1) { if (TCNT0 >= 191) { PORTC ^= (1<<0); //LED toggles TCNT0 = 0; } } }
2.AVR program that flashes an LED (PC0) every 2 seconds using XTAL clock frequency of 16MHz and Timer1
#include <avr/io.h> #include <avr/interrupt.h> volatile uint8_t tot_overflow; // initializing timer, interrupt and variable void timer1_init() { TCCR1B |= (1 << CS11); TCNT1 = 0; TIMSK |= (1 << TOIE1); // enable the global interrupts sei(); // initializing overflow counter variable tot_overflow = 0; } // TIMER1 overflow interrupt service routine is called whenever TCNT1 overflows ISR(TIMER1_OVF_vect) { tot_overflow++; if (tot_overflow >= 61) { PORTC ^= (1 << 0); tot_overflow = 0; } } int main(void) { DDRC |= (1 << 0); timer1_init(); while(1) { // do nothing // comparison is done in the ISR itself } }
Thank YOU