Skip to content

Instantly share code, notes, and snippets.

@giljr
Last active August 26, 2018 18:25
Show Gist options
  • Save giljr/f82757297afba2a40c338338441b1288 to your computer and use it in GitHub Desktop.
Save giljr/f82757297afba2a40c338338441b1288 to your computer and use it in GitHub Desktop.
working_w_CTC_w_INTERRUPTION_w_OCRnx.c https://youtu.be/Umo5PFym7wM
/* Project Ardu_Serie # 43
* Working w/CTC w/Interruption w/ OCnx Toggle Pin Mode Part III - Atmega328p IC - Arduino MC
*
* Use: TIMER2
*
* Objective: We need to flash an LED every 100 ms (10 Hertz).
* Using Interruption instead of polling technique!
* Getting hid of ISR Method! Use OCnx PIN (Toggle mode)
*/
#include <avr/io.h>
#include <avr/interrupt.h> // include int library
// initialize timer, interrupt and variable
void timer2_init()
{
// set up timer with prescaler = 1024 and CTC mode
TCCR2A |= (1 << WGM21) | (0 << WGM20);
// 1ª Modification: insert (1 << COM2A0)
TCCR2B |= (1 << COM2A0) | (0 << WGM22) | (1 << CS22) | (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A); // enable CTC, Timer2, Ch A interruptions
TCNT2 = 0;
// initialize compare value to 10 Hz
OCR2A = 156;
sei(); // set Global Interrupt Enable;
// Arduino.h courtesy:)
}
// 2ª Modification: Delete ISR altogether!
// hence, toggle led here itself..
/*ISR (TIMER2_COMPA_vect)
{
PORTB ^= (1 << PINB5); // toggles the inbuild_LED - Arduino pin 13
}*/
int main(void)
{
//3ª Modification: connect led to pin PB3 - Arduino PIN D11
DDRB |= (1 << PINB3);
// initialize timer
timer2_init();
// loop forever (or never?)
while(1)
{
// Nothing here :D
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment