Revision c7445989 scout_avr/bom/bom.c

View differences:

scout_avr/bom/bom.c
18 18

  
19 19
#define READ_BIT(pin, bit) (((pin) >> bit) & 1)
20 20
#define SET_BIT(pin, bit) ((pin) |= (1 << (bit)))
21
#define CLEAR_BIT(pin, bit) ((pin) &= ~(1 << (bit)))
21 22
#define PRESCALAR 8
22 23
#define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR)
23 24

  
24
static void send_bit(char bit);
25

  
26 25
static void slave_tx(void) {
27 26
  twi_transmit((uint8_t*)queue, queue_idx * sizeof(queue[0]));
28 27
  queue_idx = 0;
......
62 61
  TIMSK |= _BV(OCIE0A);
63 62

  
64 63
  // setup timer 1 for output
65
  GTCCR |= _BV(COM1B1);  // Set pin3 low when timer1 matches match b
66 64
  OCR1B = TIME_US(320); // Set match b to fire every 320 us
67
  TIMSK |= _BV(OCIE1A); // Enable interrupt for match a
68
  TCCR1 |= _BV(CTC1);   // Enables resetting timer1 after matches match a
65
  TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); // Enable interrupt for match a and b
66
  TCCR1 |= _BV(CTC1);   // Enables resetting timer1 after matches match c
69 67

  
70 68
  // setup output pin
71
  SET_BIT(DDRD, 5);
69
  SET_BIT(DDRB, 4);
72 70

  
73
  //sei();
71
  sei();
74 72
  bom_start();
75 73
}
76 74

  
77

  
78

  
79 75
ISR(TIMER0_COMPA_vect) {
80 76

  
81
  char this_bit = READ_BIT(PIND, PD2);
77
  char this_bit = READ_BIT(PINB, PB2);
82 78

  
83 79
  if (this_bit == 0) {
84 80
    if (last_bit == 0) {
......
101 97
}
102 98

  
103 99
static void send_next_bit() {
104
  char next_bit = sending_data >> (14-counter) & 1;
105
  char ocr_temp  next_bit ? TIME_US(2000) : TIME_US(1000);
100
  char next_bit = sending_data >> (14 - sending_counter) & 1;
101
  char ocr_temp = next_bit ? TIME_US(2000-40) : TIME_US(1000-20);
106 102
  OCR1A = ocr_temp;
107 103
  OCR1C = ocr_temp;
108
  SET_BIT(PORTD, PD5);
109 104
}
110 105

  
111 106
ISR(TIMER1_COMPA_vect) {
112
  if (++counter > 15) {
107
  SET_BIT(PORTB, PB4);
108
  sending_counter++;
109
  send_next_bit();
110
}
111

  
112
ISR(TIMER1_COMPB_vect) {
113
  CLEAR_BIT(PORTB, PB4);
114
  if (sending_counter >= 15) {
113 115
    currently_sending = 0;
114 116
    TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1
115
  } else {
116
    send_next_bit();
117 117
  }
118 118
}
119 119

  
......
121 121
  sending_data = data;
122 122
  sending_counter = 0;
123 123
  currently_sending = 1;
124
  send_next_bit();
124
  TCNT1 = 0;
125 125
  TCCR1 |= _BV(CS12);  // Set prescalar to 8 and start timer
126
  SET_BIT(PORTB, PB4);
127
  send_next_bit();
126 128
  while (currently_sending) { }
127 129
}
130

  
131
ISR(INT0_vect) {
132
  bom_start();
133
}
134

  
135
// temporary for testing
136

  
137
#include <util/delay.h>
138

  
128 139
int main() {
129 140
  bom_init();
130 141
  for (;;) {
131
    send_data(45243);
142
    send_data(4513);
132 143
    _delay_ms(100);
133 144
  }
134 145
}
135

  
136
ISR(INT0_vect) {
137
  bom_start();
138
}

Also available in: Unified diff