Revision 812788aa scout_avr/src/Atmega128rfa1.cpp

View differences:

scout_avr/src/Atmega128rfa1.cpp
1 1
#include "Atmega128rfa1.h"
2
#include "bom.h"
2 3

  
3 4
extern "C"
4 5
{
......
7 8
  void __cxa_pure_virtual(void) {}
8 9
}
9 10

  
11
#define T0_KHZ 76
12
#define T0_OCR (F_CPU / 1000 / T0_KHZ) // 210 ticks
13
#define T0_ERROR (F_CPU / 1000 - T0_OCR*T0_KHZ) // 40 ticks/ms
14

  
15
unsigned char t0_count, t0_error;
10 16
unsigned long millis;
11 17

  
12 18
int rx_start = 0, rx_end = 0;
......
18 24

  
19 25
ISR(TIMER0_COMPA_vect)
20 26
{
21
  millis++;
27
  bom_isr();
28

  
29
  // F_CPU = T0_OCR * T0_KHZ + T0_ERROR
30
  // every millisecond, accumulate T0_ERROR, and when it reaches T0_OCR skip
31
  // one iteration
32
  t0_count++;
33
  if (t0_count >= T0_KHZ) {
34
    t0_error += T0_ERROR;
35
    if (t0_error < T0_OCR) {
36
      t0_count = 0;
37
    } else {
38
      t0_count = -1;
39
      t0_error -= T0_OCR;
40
    }
41
    millis++;
42
  }
22 43
}
23 44

  
24 45
ISR(USART0_RX_vect)
......
54 75
  // === init time ===
55 76
  // COM0x = 0, pin OC0x not used
56 77
  // WGM0 = 2, clear timer on compare match, TOP = OCRA
57
  // CS0 = 3, 64 prescaler
78
  // CS0 = 1, no prescaler
58 79
  TCCR0A = _BV(WGM01);
59
  TCCR0B = _BV(CS01) | _BV(CS00);
80
  TCCR0B = _BV(CS00);
60 81
  // enable interrupt on compare match A
61 82
  TIMSK0 = _BV(OCIE0A);
62
  // 1 ms with 1/64 prescaler
63
  OCR0A = F_CPU / 1000 / 64;
83
  OCR0A = T0_OCR;
64 84
  millis = 0;
65 85

  
66 86
  sei();

Also available in: Unified diff