1 | #include <avr/io.h>
|
2 |
|
3 | #include <stdlib.h>
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | #include <util/delay.h>
|
17 | #include <avr/interrupt.h>
|
18 |
|
19 |
|
20 | volatile int16_t odometer_links;
|
21 | volatile int16_t odometer_rechts;
|
22 | volatile int8_t richtung_links=1;
|
23 | volatile int8_t richtung_rechts=1;
|
24 |
|
25 |
|
26 |
|
27 | #define F_CPU 15000000
|
28 | /*Das Schlüsselwort volatile teilt dem Compiler mit, dass die Variablen
|
29 | durch Ereignisse außerhalb der Kontrolle des Programms verändert werden kann.
|
30 | Der Wert der Variablen muss deshalb vor jedem Zugriff neu aus dem
|
31 | Hauptspeicher eingelesen werden, d.h. er darf nicht in einem Register des
|
32 | Prozessors zwischengespeichert werden. */
|
33 |
|
34 | void led_init()
|
35 |
|
36 | {
|
37 | // Datenrichtungsregister für LED 0 bis LED 4 setzen und PB4 für LINE_EN
|
38 | DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2) | (1<<PB3) | (1 << PB4);
|
39 | }
|
40 |
|
41 |
|
42 | void led_set(uint8_t led, uint8_t status)
|
43 | {
|
44 | if(led == 0)
|
45 | {
|
46 | if(status == 0)
|
47 | {
|
48 | PORTB &= ~(1<<PB0);
|
49 | }
|
50 | else
|
51 | {
|
52 | PORTB |= (1<<PB0);
|
53 | }
|
54 | }
|
55 | if(led == 1)
|
56 | {
|
57 | if(status == 0)
|
58 | {
|
59 | PORTB &= ~(1<<PB1);
|
60 | }
|
61 | else
|
62 | {
|
63 | PORTB |= (1<<PB1);
|
64 | }
|
65 | }
|
66 | if(led == 2)
|
67 | {
|
68 | if(status == 0)
|
69 | {
|
70 | PORTB &= ~(1<<PB2);
|
71 | }
|
72 | else
|
73 | {
|
74 | PORTB |= (1<<PB2);
|
75 | }
|
76 | }
|
77 | if(led == 3)
|
78 | {
|
79 | if(status == 0)
|
80 | {
|
81 | PORTB &= ~(1<<PB3);
|
82 | }
|
83 | else
|
84 | {
|
85 | PORTB |= (1<<PB3);
|
86 | }
|
87 | }
|
88 | }
|
89 |
|
90 |
|
91 | void motoren_init()
|
92 | {
|
93 | // Datenrichtungsregister für Motoren:
|
94 | DDRD |= (1<<PD4) | (1<<PD5) | (1<<PD6) | (1<<PD7);
|
95 | // PWM Register setzen:
|
96 | TCCR1A |= (1<<COM1A1)|(1<<COM1B1)|(1<<WGM10);
|
97 | TCCR1B |= (1<<CS10); // Prescale = 1 und Start Timer/Counter1AB
|
98 | OCR1AL=255; //Motoren aus
|
99 | OCR1BL=255;
|
100 | }
|
101 |
|
102 | void motor_setPower(int8_t power_links, int8_t power_rechts)
|
103 | {
|
104 |
|
105 | if(power_links < 0) {
|
106 | PORTD &= ~(1<<PD6); //Motorendrehrichtungen auf rückwärts
|
107 | OCR1AL=255+255*power_links/100;
|
108 | richtung_links=-1;
|
109 | }
|
110 | else {
|
111 | PORTD |= (1<<PD6); //Motorendrehrichtungen auf vorwärts
|
112 | OCR1AL=255-255*power_links/100;
|
113 | richtung_links=1;
|
114 |
|
115 | }
|
116 | if(power_rechts < 0) {
|
117 | PORTD |= (1<<PD7); //Motorendrehrichtungen auf rückwärts
|
118 | OCR1BL=255+255*power_rechts/100;
|
119 | richtung_rechts=-1;
|
120 |
|
121 | }
|
122 | else {
|
123 | PORTD &= ~(1<<PD7); //Motorendrehrichtungen auf vorwärts
|
124 | OCR1BL=255-255*power_rechts/100;
|
125 | richtung_rechts=1;
|
126 |
|
127 | }
|
128 |
|
129 |
|
130 | }
|
131 |
|
132 | void sens_init(void){
|
133 | DDRC &= ~0xF0; // PC4-PC7 als Eingang
|
134 | PORTC |= 0xF0; // Pullup aktivieren
|
135 | }
|
136 |
|
137 | int sens_getRechts(void){
|
138 | if(!(PINC & 0b10000000)) return 1;
|
139 | if(!(PINC & 0b01000000)) return -1;
|
140 | return 0;
|
141 | }
|
142 | int sens_getLinks(void){
|
143 | if(!(PINC & 0b00100000)) return 1;
|
144 | if(!(PINC & 0b00010000)) return -1;
|
145 | return 0;
|
146 | }
|
147 |
|
148 | void odometry_init()
|
149 | {
|
150 | // Die Odometrie Sensoren erzeugen ca. 172 Unterbrechungen pro Meter,
|
151 | MCUCR |= (1<<ISC11) | (1<<ISC01); // INT0 und INT1 auf fallende Flanke konfigurieren
|
152 | GICR |= (1<<INT1) | (1<<INT0); // INT0 und INT1 aktivieren
|
153 | odometer_links = 0;
|
154 | odometer_rechts = 0;
|
155 | sei();
|
156 | }
|
157 |
|
158 | void odometer_set(int16_t links,int16_t rechts)
|
159 | {
|
160 | odometer_links = links;
|
161 | odometer_rechts = rechts;
|
162 | }
|
163 |
|
164 | // Unterbrechung vom linken Odometrie Sensor
|
165 | ISR(INT0_vect)
|
166 | {
|
167 | if(richtung_links ==1)
|
168 | {
|
169 | odometer_links++;
|
170 | }
|
171 | else
|
172 | {
|
173 | odometer_links--;
|
174 | }
|
175 | }
|
176 | // Unterbrechung vom rechten Odometrie Sensor
|
177 | ISR(INT1_vect)
|
178 | {
|
179 | if(richtung_rechts ==1)
|
180 | {
|
181 | odometer_rechts++;
|
182 | }
|
183 | else
|
184 | {
|
185 | odometer_rechts--;
|
186 | }
|
187 | }
|
188 |
|
189 | void USART_Init( unsigned int ubrr)
|
190 | {
|
191 | /*Set baud rate */
|
192 | UBRRH = (unsigned char)(ubrr>>8);
|
193 | UBRRL = (unsigned char)ubrr;
|
194 | /*Enable receiver and transmitter */
|
195 | UCSRB = (1<<RXEN)|(1<<TXEN);
|
196 | /* Set frame format: 8data, 1stop bit */
|
197 | UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0);
|
198 | }
|
199 |
|
200 | void USART_Transmit( unsigned char data )
|
201 | {
|
202 | /* Wait for empty transmit buffer */
|
203 | while ( !( UCSRA & (1<<UDRE)) );
|
204 | /* Put data into buffer, sends the data */
|
205 | UDR = data;
|
206 | }
|
207 |
|
208 |
|
209 | void SerPrint (char *data)
|
210 | {
|
211 | unsigned char i = 0;
|
212 |
|
213 | while (data [i] != 0x00)
|
214 | USART_Transmit(data [i++]);
|
215 | }
|
216 |
|
217 | void PrintInt (uint16_t wert)
|
218 | {
|
219 | char text [7];
|
220 | itoa (wert, text, 10);
|
221 | SerPrint (text);
|
222 | }
|
223 |
|
224 | void PrintFloat(float wert, char vorkomma, char nachkomma)
|
225 | {
|
226 | // vorkomma + nachkomma max 7
|
227 | char text [10];
|
228 | dtostrf(wert, vorkomma, nachkomma, text);
|
229 | SerPrint(text);
|
230 | }
|