car004f/main.c

Sun, 22 Dec 2013 00:08:46 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sun, 22 Dec 2013 00:08:46 +0100
changeset 148
08cb88614d69
parent 147
f66c5b3b3ed2
child 149
1c3425af9aa0
permissions
-rw-r--r--

car firmware: finished pwm drive and lights switching

147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
1 #include <avr/interrupt.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
2 #include <avr/io.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
3 #include <avr/wdt.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
4 #include <avr/eeprom.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
5 #include <stdlib.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
6 #include <stdint.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
7 #include <avr/pgmspace.h>
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
8
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
9 #include "main.h"
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
10 #include "util/delay.h"
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
11
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
12
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
13 ISR ( USART_RXC_vect ) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
14 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
15
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
16 #define PULSE_PORT PORTD
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
17 #define PULSE_BIT PD2
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
18
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
19 typedef struct {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
20 uint8_t slot;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
21 uint8_t light;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
22 unsigned program:1; // programming mode active
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
23 } config_t;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
24
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
25
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
26 volatile uint16_t data = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
27 volatile uint8_t data_len = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
28 volatile uint8_t bitbuf_len = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
29 volatile uint16_t bitbuf = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
30 volatile uint8_t car_speed[8];
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31 volatile uint8_t car_switch[8];
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
32
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
33 volatile uint8_t timeout = 0;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
34 volatile uint8_t brake_timeout = 0;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
35
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
36
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
37 uint8_t my_switch;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
38 uint8_t my_speed;
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
39 config_t config;
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
40
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
41 ISR ( INT0_vect ) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
42 GICR &= ~_BV(INT0) ; // Disable INT0
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
43 // Startsignal erkannt, ab hier den Timer2 starten,
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
44 // der liest dann alle 50µs den Zustand ein und schreibt das
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
45 // empfangene Bit in den Puffer
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
46 bitbuf = 0; // init
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
47 bitbuf_len = 0b10000000; // init 1 pulse received
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
48 TCNT2 = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
49 TIMSK |= _BV(OCIE2); //enable timer2 interrupt
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
50 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
51
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
52 ISR ( TIMER2_COMP_vect ) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
53 uint8_t clock;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
54 uint8_t state;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
55 uint8_t state2;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
56 if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57 if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
58 if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0xff; else state2 = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
59
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
60 if (clock) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
61 bitbuf_len &= ~_BV(7); // switch clock to low
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
62 // second pulse of bit
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
63 if ((state==state2) & state2) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
64 // two cycles high: packet end received
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
65 data_len = (bitbuf_len & 0b00111111);
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
66 TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
67 GICR |= _BV(INT0) ; // Enable INT0
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
68
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
69 //data = bitbuf; // output data
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
70 // write data of controllers to array
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
71 if (data_len == 10) { // controller data packet
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
72 clock = (bitbuf >> 6) & 0b00000111;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
73 car_speed[clock] = (bitbuf >> 1) & 0x0F;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
74 car_switch[clock] = (bitbuf >> 5) & 1;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
75 // current response for this car?
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
76 /*
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
77 if (response != 0) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
78 if ( ((response & 0b00001110) >> 1) == clock) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
79 // add our ID to response:
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
80 send_response(response | self_id << 6);
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
81 response = 0;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
82 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
83 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
84 */
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
85 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
86
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
87
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
88 } else {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
89 bitbuf_len++; // increment bit counter
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
90 bitbuf = bitbuf << 1; // shift bits
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
91 if (state2 == 0) bitbuf |= 1; // receive logic one
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
92 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
93 } else {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
94 bitbuf_len |= _BV(7); // switch clock to high
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
95 // first pulse of bit
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
96 if (state2) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
97 bitbuf_len |= _BV(6); // store new state
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
98 } else {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
99 bitbuf_len &= ~_BV(6); // store new state
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
100 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
101 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
102 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
103
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
104 ISR (INT1_vect) {
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
105 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
106
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
107 ISR (TIMER0_OVF_vect) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
108 TCNT0 = 100; // TIMER0 vorladen mit 100
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
109 if (brake_timeout > 1) brake_timeout--;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
110 if (timeout > 1) timeout--;
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
111 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
112
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
113 #define LIGHT_PORT PORTC
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
114 #define LIGHT_FRONT 2
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
115 #define LIGHT_BRAKE 4
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
116
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
117 #define IR_PORT PORTB
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
118 #define IR_LED 3
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
119
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
120 #define BRAKE_PORT PORTB
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
121 #define BRAKE 0
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
122
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
123 #define LIGHT_MODES 1 // anzahl der lichtmodi (ohne den modus "aus")
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
124
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
125
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
126 void config_save(void) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
127 eeprom_write_block( (void*)&config, 0, sizeof(config) );
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
128 }
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
129
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
130
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
131 void brake_on(void) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
132 LIGHT_PORT |= _BV(LIGHT_BRAKE); // brake light on
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
133 BRAKE_PORT |= _BV(BRAKE); // brake on
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
134 brake_timeout = 50;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
135 }
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
136
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
137 void brake_off(void) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
138 LIGHT_PORT &= ~_BV(LIGHT_BRAKE); // brake light off
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
139 BRAKE_PORT &= ~_BV(BRAKE); // brake off
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
140 brake_timeout = 0;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
141 }
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
142
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
143 int main(void)
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
144 {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
145 // setup data bit timer2
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
146 TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
147 OCR2 = TIMER2_50US;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
148
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
149
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
150 // enable both external interrupts
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
151 // int 0 = data RX
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
152 MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
153 GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
154
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
155
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
156 DDR(LIGHT_PORT) |= _BV(LIGHT_FRONT) | _BV(LIGHT_BRAKE);
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
157 DDR(BRAKE_PORT) |= _BV(BRAKE);
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
158
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
159 // config (from eeprom!)
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
160 eeprom_read_block( (void*)&config, (const void*)0, sizeof(config) );
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
161
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
162
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
163 TCCR1A = (1<<WGM10)|(1<<COM1A1) // Set up the two Control registers of Timer1.
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
164 |(1<<COM1B1); // Wave Form Generation is Fast PWM 8 Bit,
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
165
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
166 TCCR1B = (1<<WGM12)|(1<<CS10); // OC1A and OC1B are cleared on compare match
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
167 // and set at BOTTOM. Clock Prescaler is 1.
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
168
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
169
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
170 //OCR1A = 63; // Dutycycle of OC1A = 25%
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
171 //OCR1B = 127; // Dutycycle of OC1B = 50%
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
172 OCR1A = 0;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
173 OCR1B = 0;
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
174
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
175 // configure TIMER0 to overflow every 10ms at 4 MHz
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
176 TIMSK = _BV(TOIE0); // Timer0 Overflow INT erlauben
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
177 TCNT0 = 100; // TIMER0 vorladen mit 100
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
178 TCCR0 = _BV(CS02) ; // Vorteiler auf 256, ab hier läuft der TIMER0
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
179
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
180 sei();
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
181
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
182
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
183 config.slot = 1;
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
184
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
185 while (1) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
186 // main loop
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
187
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
188 if (brake_timeout == 1) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
189 DDRB &= ~_BV(2); // PB2 PWM Output disable
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
190 brake_off();
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
191 }
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
192
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
193 if (my_speed != car_speed[config.slot]) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
194 my_speed = car_speed[config.slot];
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
195 OCR1B = (int) ((float)0xff * (float)((float)my_speed / (float)15));
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
196 if (my_speed == 0) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
197 DDRB &= ~_BV(2); // PB2 PWM Output disable
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
198 brake_on();
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
199 } else {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
200 brake_off();
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
201 DDRB |= _BV(2); // PB2 PWM Output enable
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
202 }
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
203 }
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
204
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
205 // Light cycle if switch pressed without speed
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
206 if (car_speed[config.slot] == 0) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
207 if (my_switch != car_switch[config.slot]) {
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
208 my_switch = car_switch[config.slot];
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
209 if (my_switch != 0) {
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
210 // cycle light
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
211 if (config.light == LIGHT_MODES) config.light = 0; else config.light++;
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
212 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
213 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
214 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
215
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
216 switch (config.light) {
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
217 case 0:
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
218 LIGHT_PORT &= ~_BV(LIGHT_FRONT); // switch lights off
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
219 break;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
220 case 1:
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
221 LIGHT_PORT |= _BV(LIGHT_FRONT); // switch lights on
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
222 break;
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
223 }
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
224
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
225
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
226 /*
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
227 _delay_ms(100);
148
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
228 _delay_ms(100);
08cb88614d69 car firmware: finished pwm drive and lights switching
Malte Bayer <mbayer@neo-soft.org>
parents: 147
diff changeset
229 */
147
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
230
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
231
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
232
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
233 } // main loop end
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
234 };
f66c5b3b3ed2 started firmware for car004f hardware
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
235

mercurial