0d065eeb
root
Suite
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#include <avr/io.h> //I-O registers
#include <avr/interrupt.h>
#include <util/delay.h> //_delay_ms
#define NB_TICK 104 //1563
#define CPU_FREQ 16000000L //Frequence du CPU
#define QUANTUM_ms 10
#define SAVE_CONTEXT() \
asm volatile ( \
"push r0 \n\t" \
"in r0, __SREG__ \n\t" \
"cli \n\t" \
"push r0 \n\t" \
"push r1 \n\t" \
"clr r1 \n\t" \
"push r2 \n\t" \
"push r3 \n\t" \
"push r4 \n\t" \
"push r5 \n\t" \
"push r6 \n\t" \
"push r7 \n\t" \
"push r8 \n\t" \
"push r9 \n\t" \
"push r10 \n\t" \
"push r11 \n\t" \
"push r12 \n\t" \
"push r13 \n\t" \
"push r14 \n\t" \
"push r15 \n\t" \
"push r16 \n\t" \
"push r17 \n\t" \
"push r18 \n\t" \
"push r19 \n\t" \
"push r20 \n\t" \
"push r21 \n\t" \
"push r22 \n\t" \
"push r23 \n\t" \
"push r24 \n\t" \
"push r25 \n\t" \
"push r26 \n\t" \
"push r27 \n\t" \
"push r28 \n\t" \
"push r29 \n\t" \
"push r30 \n\t" \
"push r31 \n\t" \
);
#define RESTORE_CONTEXT() \
asm volatile ( \
"pop r31 \n\t" \
"pop r30 \n\t" \
"pop r29 \n\t" \
"pop r28 \n\t" \
"pop r27 \n\t" \
"pop r26 \n\t" \
"pop r25 \n\t" \
"pop r24 \n\t" \
"pop r23 \n\t" \
"pop r22 \n\t" \
"pop r21 \n\t" \
"pop r20 \n\t" \
"pop r19 \n\t" \
"pop r18 \n\t" \
"pop r17 \n\t" \
"pop r16 \n\t" \
"pop r15 \n\t" \
"pop r14 \n\t" \
"pop r13 \n\t" \
"pop r12 \n\t" \
"pop r11 \n\t" \
"pop r10 \n\t" \
"pop r9 \n\t" \
"pop r8 \n\t" \
"pop r7 \n\t" \
"pop r6 \n\t" \
"pop r5 \n\t" \
"pop r4 \n\t" \
"pop r3 \n\t" \
"pop r2 \n\t" \
"pop r1 \n\t" \
"pop r0 \n\t" \
"out __SREG__, r0 \n\t" \
"pop r0 \n\t" \
);
/*
protocole de communication choisi
16u2 vers 328p (led) -> 1 bit = état d'une led
328p vers 16u2 (boutons + joysticks) -> enfonction du bit 7 :
à 0 : boutons puis un bit = état du bouton
à 1 : joysticks -> bit 6 = direction x ou y puis le reste, la valeur sur 6 bits
*/
struct task{
uint16_t sp_vise;
uint8_t state;
};
uint8_t cpt = 0;
uint8_t premier_lancement = 0;
|
ced5f74a
root
sauvegarde avant ...
|
103
|
struct task lecture_boutons = {0x300, 0};
|
0d065eeb
root
Suite
|
104
|
struct task lecture_joystick = {0x0500, 0};
|
ced5f74a
root
sauvegarde avant ...
|
105
|
struct task affiche_led = {0x0700, 0};
|
0d065eeb
root
Suite
|
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
//debug
void init_debug(void)//permet de detecter un reboot
{
DDRB |= 0x1F;
PORTB |= 0xFF;
_delay_ms(300);
PORTB &= 0x00;
}
//gestion de la liaison serie
void init_serial(int speed)
{
UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1;//Set baud rate
UCSR0B = (1<<TXEN0 | 1<<RXEN0);//Enable transmitter & receiver
UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);//Set 8 bits character and 1 stop bit
UCSR0A &= ~(1 << U2X0);//Set off UART baud doubler
}
void send_serial(unsigned char c)
{
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
}
unsigned char get_serial(void)
{
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}
//gestion du convertisseur analogique vers numerique
void init_ADC(void)
{
ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));//Clock prescaler at 128
ADMUX |= (1<<REFS0);
ADMUX &= ~(1<<REFS1);//Avcc(+5v) as voltage reference
ADMUX &= ~(1<<ADLAR);
ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0));//ADC in free-running mode
//ADCSRA |= (1<<ADATE);//Signal source, in this case is the free-running
ADCSRA |= (1<<ADEN);//Power up the ADC
ADCSRA |= (1<<ADSC);//Start converting
}
uint16_t ADC_read(uint8_t adcx)
{
ADMUX |= adcx;//ADC selection
ADCSRA |= _BV(ADSC);//ADC start conversion
while ( (ADCSRA & _BV(ADSC)) );//Wait until the conversion is finished
ADMUX &= ~adcx;//ADC deselection
return ADC;
}
//gestion du timer
void init_timer()
{
TCCR1B |= _BV(WGM12); // CTC mode with value in OCR1A
TCCR1B |= _BV(CS12); // CS12 = 1; CS11 = 1; CS10 =1 => CLK/1024 prescaler
TCCR1B |= _BV(CS10);
OCR1A = NB_TICK;
TIMSK1 |= _BV(OCIE1A);
}
//gestion de l'initialisation des differentes taches
void init_tasks(void)
{
DDRD |= 0x00;
PORTD |= 0xFF;
DDRB |= 0b00111111;
}
//gestion de l'execution des differentes taches
void task_lecture_boutons(void)
{
uint8_t tmp = 0x00;
uint8_t tmp2;
while(1){
tmp2 = 0x00;
if((PIND&(1<<2)) == 0) tmp2 |= 0b00000001;
if((PIND&(1<<3)) == 0) tmp2 |= 0b00000010;
if((PIND&(1<<4)) == 0) tmp2 |= 0b00000100;
if((PIND&(1<<5)) == 0) tmp2 |= 0b00001000;
if((PIND&(1<<6)) == 0) tmp2 |= 0b00010000;
if(tmp2 != tmp) { send_serial(tmp2); tmp = tmp2; }
_delay_ms(QUANTUM_ms);
}
}
void task_lecture_joystick(void)
{
uint8_t tmp = ADC_read(0)>>4;//code sur 6 bits (2 bits de descriptions necessaires)
uint8_t tmp2 = ADC_read(1)>>4;
uint8_t test = 0;
uint8_t tmp3;
uint8_t tmp4;
while(1){
tmp3 = ADC_read(0)>>4;
tmp4 = ADC_read(1)>>4;
if(test == 0 && tmp3 != tmp)
{
send_serial(tmp3 | 0b10000000);
tmp = tmp3;
}
else if(test == 1 && tmp4 != tmp2)
{
send_serial(tmp4 | 0b11000000);
tmp2 = tmp4;
}
if(test == 0) test = 1; else test = 0;
_delay_ms(QUANTUM_ms);
}
}
void task_affiche_led(void)
{
while(1){
PORTB = get_serial();
_delay_ms(QUANTUM_ms);
}
}
//gestion du contexte
ISR(TIMER1_COMPA_vect)
{
if(premier_lancement==0)
{
premier_lancement+= 1;
sei();
SP = affiche_led.sp_vise;
task_affiche_led();
}
else if(premier_lancement==1)
{
SAVE_CONTEXT();
affiche_led.sp_vise = SP;
premier_lancement+=1;
sei();
SP = lecture_joystick.sp_vise;
task_lecture_joystick();
}
else if(premier_lancement==2)
{
SAVE_CONTEXT();
lecture_joystick.sp_vise = SP;
premier_lancement+=1;
sei();
SP = lecture_boutons.sp_vise;
task_lecture_boutons();
}
else
{
if(cpt==0)
{
SAVE_CONTEXT();
lecture_boutons.sp_vise = SP;
SP = affiche_led.sp_vise;
RESTORE_CONTEXT();
cpt++;
}
else if(cpt==1)
{
SAVE_CONTEXT();
affiche_led.sp_vise = SP;
SP = lecture_joystick.sp_vise;
RESTORE_CONTEXT();
cpt++;
}
else if(cpt==2)
{
SAVE_CONTEXT();
lecture_joystick.sp_vise = SP;
SP = lecture_boutons.sp_vise;
RESTORE_CONTEXT();
cpt = 0;
}
}
sei();
}
int main(void)
{
init_debug();//allume toutes les leds au reset
init_serial(9600);
init_ADC();
init_timer();
init_tasks();
sei();
while(1)
{}
return 0;
}
|