Revision f8c1522b
Added bom sending code (not yet tested)
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> |
|
5 | 4 |
#include "twi.h" |
6 | 5 |
#include "bomi2c.h" |
7 | 6 |
|
... | ... | |
13 | 12 |
char last_bit; |
14 | 13 |
char count; |
15 | 14 |
uint16_t data; |
15 |
volatile char currently_sending; |
|
16 |
char sending_counter; |
|
17 |
uint16_t sending_data; |
|
16 | 18 |
|
17 | 19 |
#define READ_BIT(pin, bit) (((pin) >> bit) & 1) |
18 | 20 |
#define SET_BIT(pin, bit) ((pin) |= (1 << (bit))) |
19 |
#define RESET_BIT(pin, bit) ((pin) &= ~(1 << (bit))) |
|
21 |
#define PRESCALAR 8 |
|
22 |
#define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR) |
|
20 | 23 |
|
21 | 24 |
static void send_bit(char bit); |
22 | 25 |
|
... | ... | |
55 | 58 |
//EIMSK |= _BV(INT0); |
56 | 59 |
|
57 | 60 |
// setup timer 0 |
58 |
//TCCR0B |= _BV(CS01); |
|
61 |
TCCR0B |= _BV(CS01); |
|
62 |
TIMSK |= _BV(OCIE0A); |
|
63 |
|
|
64 |
// setup timer 1 for output |
|
65 |
GTCCR |= _BV(COM1B1); // Set pin3 low when timer1 matches match b |
|
66 |
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 |
|
59 | 69 |
|
60 | 70 |
// setup output pin |
61 | 71 |
SET_BIT(DDRD, 5); |
... | ... | |
64 | 74 |
bom_start(); |
65 | 75 |
} |
66 | 76 |
|
77 |
|
|
78 |
|
|
67 | 79 |
ISR(TIMER0_COMPA_vect) { |
68 | 80 |
|
69 | 81 |
char this_bit = READ_BIT(PIND, PD2); |
... | ... | |
88 | 100 |
} |
89 | 101 |
} |
90 | 102 |
|
91 |
static void send_data(uint16_t data) { |
|
92 |
for (int i = 14; i >= 0; i--) { |
|
93 |
send_bit((data >> i) & 1); |
|
94 |
} |
|
95 |
send_bit(0); |
|
103 |
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); |
|
106 |
OCR1A = ocr_temp; |
|
107 |
OCR1C = ocr_temp; |
|
108 |
SET_BIT(PORTD, PD5); |
|
96 | 109 |
} |
97 | 110 |
|
98 |
static void send_bit(char bit) { |
|
99 |
SET_BIT(PORTD, PD5); |
|
100 |
_delay_us(320); |
|
101 |
RESET_BIT(PORTD, PD5); |
|
102 |
if (bit) { |
|
103 |
_delay_us(1680); |
|
111 |
ISR(TIMER1_COMPA_vect) { |
|
112 |
if (++counter > 15) { |
|
113 |
currently_sending = 0; |
|
114 |
TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1 |
|
104 | 115 |
} else { |
105 |
_delay_us(680);
|
|
116 |
send_next_bit();
|
|
106 | 117 |
} |
107 | 118 |
} |
108 | 119 |
|
120 |
static void send_data(uint16_t data) { |
|
121 |
sending_data = data; |
|
122 |
sending_counter = 0; |
|
123 |
currently_sending = 1; |
|
124 |
send_next_bit(); |
|
125 |
TCCR1 |= _BV(CS12); // Set prescalar to 8 and start timer |
|
126 |
while (currently_sending) { } |
|
127 |
} |
|
109 | 128 |
int main() { |
110 | 129 |
bom_init(); |
111 | 130 |
for (;;) { |
Also available in: Unified diff