Revision 5f7a1707 scout_avr/bom/bom.c

View differences:

scout_avr/bom/bom.c
1 1
#include <stdint.h>
2 2
#include <avr/io.h>
3 3
#include <avr/interrupt.h>
4
#include <util/delay.h>
4 5
#include "tiny-twi.h"
5 6
#include "bomi2c.h"
6 7

  
......
29 30
#endif
30 31

  
31 32
static void bom_init(void) {
33

  
34
  SET_BIT(DDRB, EMIT);
35

  
32 36
  init_detect();
33 37

  
34 38
  init_timer1();
......
41 45
static void init_detect(void) {
42 46
  TCCR0A |= _BV(WGM01);    // enable reset on compare A
43 47
  TIMSK  |= _BV(OCIE0A);   // enable timer0 compare match A
48
  OCR0A   = TIME_US(1000); // trigger every 1ms
44 49

  
50
  GIFR   |= _BV(PCIF);   // Clear any previous detections
45 51
  GIMSK  |= _BV(PCIE);   // Enable pin change interrupt for detection
46 52
  PCMSK  |= _BV(PCINT_DETECT); // Enable interrupt on detect pin
47 53
}
......
61 67
  data = 0;
62 68
  count = 0;
63 69
  last_bit = 1;
70
  _delay_us(160);
64 71
  start_timer0();
65 72
}
66 73

  
......
76 83
ISR(TIMER0_COMPA_vect) {
77 84

  
78 85
  char this_bit = READ_BIT(PINB, DETECT);
86
  smb_send_data((uint8_t*)&this_bit, 1);
79 87

  
80
  if (this_bit == 0) {
81
    if (last_bit == 0) {
88
  if (this_bit == 1) {
89
    if (last_bit == 1) {
82 90
      restart_detect();
83 91
    }
84 92
  } else {
85
    data = data << 1 | (~last_bit & 1);
93
    data = data << 1 | (last_bit & 1);
86 94
    count++;
87 95
  }
88 96

  

Also available in: Unified diff