Statistics
| Branch: | Revision:

root / toolbox / led.c @ 1b054655

History | View | Annotate | Download (906 Bytes)

1
#include "led.h"
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4

    
5
#define PRESCALE 64
6
#define CLOCK_SEL 3
7

    
8
/* F_CPU / PRESCALE = OCR * 1000 + ERROR */
9
#define OCR (F_CPU / PRESCALE / 1000UL)
10
#define ERROR (F_CPU / PRESCALE - OCR * 1000UL)
11

    
12
char count;
13
uint16_t period;
14

    
15
uint16_t ms;
16
uint16_t error;
17

    
18
static void blink() {
19
  count--;
20
  if (count % 2) {
21
    led_yellow();
22
  } else {
23
    led_off();
24
  }
25
}
26

    
27
ISR(TIMER0_COMPA_vect) {
28
  error += ERROR;
29
  if (error >= 1000) {
30
    error -= 1000;
31
  } else {
32
    ms++;
33
    if (ms == period) {
34
      blink();
35
      if (count == 0) {
36
        TCCR0B = 0;
37
      }
38
      ms = 0;
39
    }
40
  }
41
}
42

    
43
void led_blink_start(unsigned int period_ms, char n_times) {
44
  led_yellow();
45
  ms = 0;
46
  error = 0;
47
  count = n_times*2-1;
48
  period = period_ms/2;
49
  OCR0A = OCR;
50
  TIMSK = _BV(OCIE0A);
51
  TCCR0A = _BV(WGM01);
52
  TCCR0B = CLOCK_SEL;
53
}
54

    
55
char led_blink_done() {
56
  return count == 0;
57
}