Revision b2554c5c scout_avr/bom/bom.c

View differences:

scout_avr/bom/bom.c
18 18
#define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR)
19 19

  
20 20
#define ADDRESS 0x3 
21

  
22
static void bom_start(void) {
23
  data = 0;
24
  count = 0;
25
  last_bit = 1;
26
  // TODO disable interrupt
27
  // TODO start timer0
28
}
29

  
30
static void bom_stop(void) {
31
  // TODO stop timer
32
  // TODO restart INT0
33
}
21
#define DETECT PB2
22
#define EMIT PB4
34 23

  
35 24
static void bom_init(void) {
36 25

  
......
38 27
  //EICRA |= _BV(ISC01) | _BV(ISC00);
39 28
  //EIMSK |= _BV(INT0);
40 29

  
41
  // setup timer 0
42
  TCCR0B |= _BV(CS01);
43
  TIMSK |= _BV(OCIE0A);
30
  init_timer0();
31
  start_timer0();
44 32

  
45
  // setup timer 1 for output
46
  OCR1B = TIME_US(320); // Set match b to fire every 320 us
47
  TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); // Enable interrupt for match a and b
48
  TCCR1 |= _BV(CTC1);   // Enables resetting timer1 after matches match c
33
  init_timer1();
49 34

  
50 35
  // setup output pin
51 36
  SET_BIT(DDRB, 4);
......
54 39
  bom_start();
55 40
}
56 41

  
42
////////////////////////////////// Receiving ///////////////////////////////////
43

  
44
static void init_timer0(void) {
45
  TCCROA |= _BV(WGM01);    // enable reset on compare A
46
  TIMSK  |= _BV(OCIE0A);   // enable timer0 compare match A
47
}
48

  
49
static void start_timer0(void) {
50
  TCNT0 = 0; // Reset timer 0 count to 0
51
  TCCR0B |= _BV(CS01);     // setup clkio and start the timer
52
}
53

  
54
static void start_detect(void) {
55
  data = 0;
56
  count = 0;
57
  last_bit = 1;
58
  start_timer0();
59
}
60

  
61
static void stop_detect(void) {
62
  TCCR0B &= ~0x07; // Stop timer0
63
}
64

  
57 65
ISR(TIMER0_COMPA_vect) {
58 66

  
59
  char this_bit = READ_BIT(PINB, PB2);
67
  char this_bit = READ_BIT(PINB, DETECT);
60 68

  
61 69
  if (this_bit == 0) {
62 70
    if (last_bit == 0) {
63
      bom_stop();
71
      stop_detect();
64 72
    }
65 73
  } else {
66 74
    data = data << 1 | (~last_bit & 1);
......
70 78
  last_bit = this_bit;
71 79

  
72 80
  if (count == 15) {
81
    stop_detect();
73 82
    smb_send_data(&data, sizeof(data));
74
    bom_stop();
75 83
  }
76 84
}
77 85

  
86
////////////////////////////////////////////////////////////////////////////////
87

  
88
//////////////////////////////////// Sending ///////////////////////////////////
89

  
90
static void start_timer1() {
91
  TCNT1 = 0; // Reset timer 1 count to 0
92
  TCCR1 |= _BV(CS12) // Set prescalar to 8 and start timer1
93
}
94

  
95
static void stop_timer1() {
96
  TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1
97
}
98

  
99
static void init_timer1() {
100
  OCR1B = TIME_US(320); // Set match b to fire every 320 us
101
  TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); // Enable interrupt for match a and b
102
  TCCR1 |= _BV(CTC1);   // Enables resetting timer1 after matches match c
103
}
104

  
105
static void send_data(uint16_t data) {
106
  sending_data = data;
107
  sending_counter = 0;
108
  currently_sending = 1;
109
  SET_BIT(PORTB, EMIT);
110

  
111
  start_timer1();
112
  send_next_bit();
113
}
114

  
78 115
static void send_next_bit() {
79 116
  char next_bit = sending_data >> (14 - sending_counter) & 1;
80 117
  char ocr_temp = next_bit ? TIME_US(2000-40) : TIME_US(1000-20);
81
  OCR1A = ocr_temp;
82
  OCR1C = ocr_temp;
118
  OCR1A = ocr_temp; // Compare A - Turns on transmitter
119
  OCR1C = ocr_temp; // Reset the timer when match a fires
83 120
}
84 121

  
122
// Compare A - Turns on transmitter
85 123
ISR(TIMER1_COMPA_vect) {
86
  SET_BIT(PORTB, PB4);
124
  SET_BIT(PORTB, EMIT); 
87 125
  sending_counter++;
88 126
  send_next_bit();
89 127
}
90 128

  
129
// Compare B - Turns off transmitter after 320us
91 130
ISR(TIMER1_COMPB_vect) {
92
  CLEAR_BIT(PORTB, PB4);
131
  CLEAR_BIT(PORTB, EMIT);
93 132
  if (sending_counter >= 15) {
94 133
    currently_sending = 0;
95
    TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1
134
    stop_timer1();
96 135
  }
97 136
}
98 137

  
99
static void send_data(uint16_t data) {
100
  sending_data = data;
101
  sending_counter = 0;
102
  currently_sending = 1;
103
  TCNT1 = 0;
104
  TCCR1 |= _BV(CS12);  // Set prescalar to 8 and start timer
105
  SET_BIT(PORTB, PB4);
106
  send_next_bit();
107
}
138
////////////////////////////////////////////////////////////////////////////////
139

  
108 140

  
109 141
ISR(INT0_vect) {
110 142
  bom_start();

Also available in: Unified diff