Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 second blink example done with timers now.
- Loading branch information
Showing
3 changed files
with
73 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef __MAIN_H__ | ||
#define __MAIN_H__ | ||
|
||
/* Assuming a clock rate of 16Mhz and a CLK/64 prescaler, | ||
* we will see 250 ticks per millisecond. If we want to | ||
* have the timer overlow ever millisecond, we need to | ||
* initialize the counter to 5 after each tick. */ | ||
#define TIMER_RESET_VAL 5 | ||
|
||
void configure(void); | ||
void task(void); | ||
int main(void); | ||
|
||
#endif /* __MAIN_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,65 @@ | ||
/* | ||
* This example was borrowed from: | ||
* http://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/ | ||
*/ | ||
|
||
#include <avr/io.h> | ||
#include <util/delay.h> | ||
#include <avr/interrupt.h> | ||
#include <stdint.h> | ||
|
||
#include "main.h" | ||
|
||
int main(void) | ||
{ | ||
configure(); | ||
|
||
while(1) {;} | ||
|
||
return 0; | ||
} | ||
|
||
void configure(void) | ||
{ | ||
/* disable interrupts */ | ||
cli(); | ||
|
||
/* configure TIMER0 to use the CLK/64 prescaler. */ | ||
TCCR0B = _BV(CS00) | _BV(CS01); | ||
|
||
/* enable the TIMER0 overflow interrupt */ | ||
TIMSK0 = _BV(TOIE0); | ||
|
||
enum { | ||
BLINK_DELAY_MS = 1000, | ||
}; | ||
/* set the initial timer counter value. */ | ||
TCNT0 = TIMER_RESET_VAL; | ||
|
||
/* confiure PB5 as an output. */ | ||
DDRB |= _BV(DDB5); | ||
|
||
/* turn off surface mount LED on */ | ||
PORTB &= ~_BV(PORTB5); | ||
|
||
/* enable interrupts. */ | ||
sei(); | ||
} | ||
|
||
void task(void) | ||
{ | ||
static uint16_t tick = 0; | ||
|
||
/* toggle every thousand ticks */ | ||
if (tick >= 1000) | ||
{ | ||
/* toggle the LED */ | ||
PORTB ^= _BV(PORTB5); | ||
|
||
/* reset the tick */ | ||
tick = 0; | ||
} | ||
|
||
tick++; | ||
} | ||
|
||
int main (void) | ||
ISR(TIMER0_OVF_vect) | ||
{ | ||
/* set pin 5 of PORTB for output*/ | ||
DDRB |= _BV(DDB5); | ||
|
||
while(1) { | ||
/* set pin 5 high to turn led on */ | ||
PORTB |= _BV(PORTB5); | ||
_delay_ms(BLINK_DELAY_MS); | ||
|
||
/* set pin 5 low to turn led off */ | ||
PORTB &= ~_BV(PORTB5); | ||
_delay_ms(BLINK_DELAY_MS); | ||
} | ||
|
||
return 0; | ||
/* preload the timer. */ | ||
TCNT0 = TIMER_RESET_VAL; | ||
|
||
/* call our periodic task. */ | ||
task(); | ||
} | ||
|