Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/sleep.h>
- // global variable, # of overflows
- volatile uint16_t overflow_cnt;
- volatile uint16_t sec_cnt;
- volatile uint16_t min_cnt;
- // TIMER0 overflow interrupt service routine
- ISR(TIMER0_OVF_vect)
- {
- // keep a track of number of overflows
- overflow_cnt++;
- }
- void timer_init()
- {
- TCCR0 |= (1 << CS01);
- TCCR0 |= (1 << CS00); // set prescaler = 64
- TCNT0 = 132; // set TIMER0 = 132
- TIMSK |= (1<<TOIE0); // enable TIMER0 interrupts
- sei();
- }
- ////////////////////////////////////////////////////////
- //
- // clock frequency = 8MHz
- //
- // 64 = prescaler clock source
- // 132 = start value (125 clocks up to 257 overflow)
- //
- // 1 / (8MHz / 64 / 125) = 1ms * 1000 = 1s
- //
- ////////////////////////////////////////////////////////
- int main(void)
- {
- while(1)
- {
- timer_init();
- sleep_enable();
- sleep_cpu();
- sleep_disable();
- if (overflow_cnt >= 1000)
- {
- overflow_cnt = 0;
- sec_cnt++;
- }
- if (min_cnt >= 60){
- sec_cnt = 0;
- min_cnt++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement