Project

General

Profile

Revision 94548bf4

ID94548bf49434731d0315d219ebc4ab322bfa41e0
Parent 10936c07
Child d47be08d

Added by Thomas Mullins over 11 years ago

Moved timing to time.c, and increased period of rfid polling

View differences:

toolbox/led.c
1 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)
2
#include "time.h"
11 3

  
12 4
char blink_count;
13 5
enum color_t blink_color;
14
uint16_t blink_period;
15

  
16
uint16_t ms;
17
uint16_t error;
6
uint16_t blink_period, blink_ticks;
18 7

  
19 8
static void led_color(enum color_t color) {
20 9
  switch (color) {
......
33 22
  }
34 23
}
35 24

  
36
static void blink() {
37
  blink_count--;
38
  if (blink_count % 2) {
39
    led_color(blink_color);
40
  } else {
41
    led_off();
42
  }
43
}
44

  
45
ISR(TIMER0_COMPA_vect) {
46
  error += ERROR;
47
  if (error >= 1000) {
48
    error -= 1000;
49
    OCR0A = OCR+1;
50
  } else {
51
    OCR0A = OCR;
52
  }
53
  ms++;
54
  if (ms == blink_period) {
55
    blink();
56
    if (blink_count == 0) {
57
      TCCR0B = 0;
25
void led_tick() {
26
  if (blink_count) {
27
    if (++blink_ticks >= blink_period) {
28
      blink_ticks = 0;
29
      if (--blink_count % 2) {
30
        led_color(blink_color);
31
      } else {
32
        led_off();
33
      }
58 34
    }
59
    ms = 0;
60 35
  }
61 36
}
62 37

  
63 38
void led_blink_start(unsigned int period_ms, char n_times, enum color_t color) {
64

  
65
  ms = 0;
66
  error = 0;
67

  
68 39
  blink_count = n_times*2-1;
69
  blink_period = period_ms/2;
40
  blink_period = period_ms/2/TICK_MS;
41
  blink_ticks = 0;
70 42
  blink_color = color;
71 43
  led_color(color);
72

  
73
  OCR0A = OCR;
74
  TIMSK = _BV(OCIE0A);
75
  TCCR0A = _BV(WGM01);
76
  TCCR0B = CLOCK_SEL;
77

  
78 44
}
79 45

  
80 46
char led_blink_done() {
toolbox/led.h
19 19
/* Returns nonzero if blinking has finished */
20 20
char led_blink_done();
21 21

  
22
/* Call this every TICK_MS to handle LED blinking */
23
void led_tick();
24

  
22 25
#endif
toolbox/main.c
8 8
#include "rfid.h"
9 9
#include "led.h"
10 10
#include "current.h"
11
#include "time.h"
11 12

  
12 13
enum toolstate_t {
13 14
  TS_INIT,
......
58 59
  memcpy(dest, src, RFID_SERNO_SIZE);
59 60
}
60 61

  
61
static void tool_main() {
62
static void tool_tick() {
62 63

  
63 64
  switch (toolstate) {
64 65

  
......
261 262
}
262 263

  
263 264
int main() {
265
  char rfid_ticks = 0;
264 266

  
267
  time_init();
265 268
  led_init();
266 269
  tool_init();
267 270
  rfid_init();
271
  current_init();
268 272

  
269 273
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
270 274
  eMBEnable();
......
273 277

  
274 278
  rfid_start_read();
275 279
  while (1) {
276
    if (rfid_poll()) {
277
      rfid_get_serno(latest_reading);
278
      rfid_start_read();
280
    if (++rfid_ticks >= RFID_PERIOD/TICK_MS)
281
    {
282
      rfid_ticks = 0;
283
      if (rfid_poll()) {
284
        rfid_get_serno(latest_reading);
285
        rfid_start_read();
286
      } else {
287
        /* TODO count # times rfid_poll returns each value and see how often we
288
         * have to wait */
289
      }
279 290
    }
280 291
    current = current_read();
281
    tool_main();
292
    tool_tick();
293
    led_tick();
282 294
    eMBPoll();
283
    _delay_ms(100);
295
    time_wait();
284 296
  }
285 297

  
286 298
  return 0;
toolbox/time.c
1
#include <stdint.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include "time.h"
5

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

  
10
static char ready, ms;
11
static uint16_t error;
12

  
13
ISR(TIMER0_COMPA_vect) {
14
  error += ERROR;
15
  if (error >= 1000) {
16
    error -= 1000;
17
    OCR0A = OCR+1;
18
  } else {
19
    OCR0A = OCR;
20
  }
21
  if (++ms == TICK_MS) {
22
    ready = 1;
23
    ms = 0;
24
  }
25
}
26

  
27
void time_init() {
28
  OCR0A = OCR;
29
  TIMSK = _BV(OCIE0A);
30
  TCCR0A = _BV(WGM01);
31
  TCCR0B = CLOCK_SEL;
32
}
33

  
34
void time_wait() {
35
  while (!ready);
36
  ready = 0;
37
}
toolbox/time.h
1
#ifndef TIME_H
2
#define TIME_H
3

  
4
#define TICK_MS 50
5
#define RFID_PERIOD 300
6

  
7
#define PRESCALE 64
8
#define CLOCK_SEL 3
9

  
10
/* Initializes timer 0 */
11
void time_init();
12

  
13
/* Waits until TICK_MS have passed since the previous call returned */
14
void time_wait();
15

  
16
#endif

Also available in: Unified diff