Revision 10936c07

View differences:

toolbox/current.c
1
#include "current.h"
2
#include <avr/io.h>
3

  
4
void current_init() {
5
  /*
6
   * REFS = 0, Vcc reference (set to 2 for internal 1.1V reference)
7
   * MUX = 8, PB3(ADC8)
8
   */
9
  ADMUX = _BV(MUX3);
10
  /* TODO reduce power consumption with DIDR* */
11
  /*
12
   * ADLAR = 0, right adjust result
13
   * ADTS = 0, free running mode
14
   */
15
  ADCSRB = 0;
16
  /*
17
   * ADEN = 1, enable
18
   * ADSC = 1, start now
19
   * ADATE = 1, auto trigger
20
   * ADIE = 0, disable interrupt
21
   * ADPS = 3, prescale clock by 8
22
   */
23
  ADCSRA |= _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADPS1) | _BV(ADPS0);
24
}
25

  
26
int current_read() {
27
  return ADC;
28
}
toolbox/current.h
1
#ifndef CURRENT_H
2
#define CURRENT_H
3

  
4
void current_init();
5
int current_read();
6

  
7
#endif
toolbox/main.c
7 7
#include "tooltron_mb.h"
8 8
#include "rfid.h"
9 9
#include "led.h"
10
#include "current.h"
10 11

  
11 12
enum toolstate_t {
12 13
  TS_INIT,
......
22 23
static uint8_t coils;
23 24
static uint8_t latest_reading[RFID_SERNO_SIZE];
24 25
static uint8_t current_user[RFID_SERNO_SIZE];
26
static uint16_t current;
25 27

  
26 28
static inline void set_coil(char coil, char bit) {
27 29
  coils = (coils & ~(1 << coil)) | (bit << coil);
......
235 237
      }
236 238

  
237 239
    case MB_INP_CURRENT:
238
      *reg_buf++ = 0;
239
      *reg_buf++ = 0;
240
      *reg_buf++ = (uint8_t)(current >> 8);
241
      *reg_buf++ = (uint8_t)current;
240 242
      n_regs--;
241 243
      if (n_regs == 0) {
242 244
        return MB_ENOERR;
......
275 277
      rfid_get_serno(latest_reading);
276 278
      rfid_start_read();
277 279
    }
280
    current = current_read();
278 281
    tool_main();
279 282
    eMBPoll();
280
    _delay_ms(50);
283
    _delay_ms(100);
281 284
  }
282 285

  
283 286
  return 0;

Also available in: Unified diff