Commit db0bdba2403451e2f7ba770fac857cb1e21629b4
1 parent
48fa7247
boutons+joy OK
Showing
1 changed file
with
55 additions
and
44 deletions
Show diff stats
1 | 1 | #include <avr/io.h> // for the input/output register |
2 | +#include <util/delay.h> | |
2 | 3 | |
3 | 4 | // For the serial port |
4 | 5 | |
5 | 6 | #define CPU_FREQ 16000000L // Assume a CPU frequency of 16Mhz |
7 | +#define tempo 25 | |
8 | +#define debit 9600 //débit liaison série en bauds | |
6 | 9 | |
7 | 10 | void init_serial(int speed) |
8 | 11 | { |
... | ... | @@ -49,23 +52,26 @@ return ADCH; |
49 | 52 | |
50 | 53 | // For the I/O |
51 | 54 | void output_init(void){ |
52 | -DDRB |= 0b00111111; // PIN 8-13 as output | |
55 | +DDRB |= 0b00111111; // PIN 8-13 as output (LED) | |
53 | 56 | } |
54 | 57 | |
58 | +/* | |
55 | 59 | void output_set(unsigned char value){ |
56 | 60 | if(value==0) PORTB &= 0xfe; else PORTB |= 0x01; |
57 | 61 | } |
62 | +*/ | |
58 | 63 | |
59 | 64 | void input_init(void){ |
60 | -DDRD |= 0b01111100; // PIN 2-6 as input (Bouton Joystick + boutons) | |
61 | -DDRC |= 0x03; // PIN 0-1 analogiques comme input (x et y joystick analogique) | |
65 | + DDRD &= 0b10000011; // PIN 2-6 as input (Bouton Joystick + boutons) | |
66 | + PORTD |= 0x7C; // Pull up de 0 à 1 | |
67 | + //DDRC &= 0b11111100; // PIN 0-1 analogiques comme input (x et y joystick) | |
62 | 68 | } |
63 | 69 | |
64 | -/* | |
70 | + | |
65 | 71 | unsigned char input_get(void){ |
66 | 72 | return ((PIND&0x04)!=0)?1:0; |
67 | 73 | } |
68 | -*/ | |
74 | + | |
69 | 75 | |
70 | 76 | /* Commande des LED */ |
71 | 77 | void commande_leds(){ |
... | ... | @@ -92,84 +98,89 @@ void commande_leds(){ |
92 | 98 | } |
93 | 99 | |
94 | 100 | /* Récupération de la valeur des boutons et mise en forme */ |
95 | -int get_buttons(void){ | |
96 | - unsigned boutons = ((PIND & 0x7C)>>2) | 0b00100000; | |
97 | - return(boutons); | |
101 | +unsigned char get_buttons(void){ | |
102 | + //unsigned char boutons = (((PIND & 0x7C)>>2) &0x3f) | 0b00100000; | |
103 | + unsigned char boutons = PIND; | |
104 | + boutons = boutons >>2; | |
105 | + boutons = boutons & 0b00111111; | |
106 | + boutons = boutons | 0b00100000; | |
107 | + | |
108 | + return boutons; | |
98 | 109 | } |
99 | 110 | |
100 | 111 | /* Met en forme l'octet de l'axe pos du joystick */ |
101 | -int shape_joy(unsigned char pos){ | |
102 | - pos = (pos >> 3) | 0b00100000; | |
103 | - return(pos); | |
112 | +unsigned char shape_joy(unsigned char pos){ | |
113 | + //pos = ((pos >> 3) | &0b00111111) 0b00100000; | |
114 | + pos = pos >>3; | |
115 | + pos = pos & 0b00111111; | |
116 | + pos = pos | 0b00100000; | |
117 | + return pos; | |
104 | 118 | } |
105 | 119 | |
106 | 120 | /* Récupération de la valeur de l'axe du joystick sur la chaîne channel du CAN */ |
107 | -int get_joystick(int channel){ | |
121 | +unsigned char get_joystick(int channel){ | |
108 | 122 | unsigned char axis; |
109 | 123 | ad_init(channel); |
110 | 124 | axis = ad_sample(); |
111 | - shape_joy(axis); | |
112 | - return(axis); | |
125 | + axis = shape_joy(axis); | |
126 | + return axis; | |
113 | 127 | } |
114 | 128 | |
115 | 129 | /* Dummy main */ |
116 | 130 | int main(void){ |
117 | - init_serial(9600); | |
131 | + | |
132 | + //INITIALISATIONS | |
133 | + unsigned char boutons, boutons_anc; | |
134 | + unsigned char joystick_x=0x10, joystick_x_anc=0x10; | |
135 | + unsigned char joystick_y=0x10, joystick_y_anc=0x10; | |
136 | + | |
137 | + init_serial(debit); | |
118 | 138 | output_init(); |
119 | 139 | input_init(); |
120 | - | |
121 | - unsigned char boutons, boutons_anc; | |
122 | - unsigned char joystick_x, joystick_x_anc; | |
123 | - unsigned char joystick_y, joystick_y_anc; | |
124 | 140 | |
125 | 141 | //Récupération des valeurs des boutons et joystick, et mise en forme |
126 | 142 | boutons = get_buttons(); |
127 | - joystick_x = get_joystick(1); | |
128 | - joystick_y = get_joystick(0); | |
129 | - | |
130 | - //send_serial(boutons); | |
143 | + joystick_x = get_joystick(0); | |
144 | + joystick_y = get_joystick(1); | |
131 | 145 | |
132 | 146 | while(1){ |
133 | - | |
134 | 147 | boutons_anc = boutons; |
135 | 148 | joystick_x_anc = joystick_x; |
136 | 149 | joystick_y_anc = joystick_y; |
137 | 150 | |
138 | 151 | //Récupération des valeurs des boutons et joystick, et mise en forme |
139 | 152 | boutons = get_buttons(); |
140 | - joystick_x = get_joystick(1); | |
141 | - joystick_y = get_joystick(0); | |
142 | - | |
143 | - | |
144 | - send_serial(boutons+2); | |
145 | - //retour chariot | |
146 | - send_serial(0x0a); | |
147 | - send_serial(0x0d); | |
153 | + joystick_x = get_joystick(0); | |
154 | + _delay_ms(tempo); | |
155 | + joystick_y = get_joystick(1); | |
156 | + | |
148 | 157 | |
149 | - //Cas où aucun caractère n'est reçu | |
158 | + //Port série libre | |
150 | 159 | if ((UCSR0A & (1<<RXC0)) == 0){ |
151 | 160 | |
152 | 161 | //if (boutons_anc != boutons){ |
162 | + //if (joystick_x_anc != joystick_x){ | |
153 | 163 | //if (joystick_y_anc != joystick_y){ |
154 | - //if ((boutons_anc != boutons) || (joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y)){ | |
155 | - //send_serial(boutons); | |
156 | - //send_serial(joystick_y); | |
157 | - //send_serial(joystick_y); | |
164 | + if ((boutons_anc != boutons) || (joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y)){ | |
165 | + send_serial(boutons); | |
166 | + //if ((joystick_x > 126) || (joystick_x <33)) send_serial('e'); | |
167 | + send_serial(joystick_x); | |
168 | + send_serial(joystick_y); | |
158 | 169 | |
159 | 170 | //send_serial('r'); |
160 | 171 | |
161 | 172 | //retour chariot |
162 | - //send_serial(0x0a); | |
163 | - //send_serial(0x0d); | |
164 | - //} | |
165 | - } | |
173 | + send_serial(0x0a); | |
174 | + send_serial(0x0d); | |
175 | + } | |
166 | 176 | |
177 | + } | |
178 | + | |
179 | + //Port série occupé | |
167 | 180 | else{ |
168 | - //Cas où un caractère attent d'être reçu | |
169 | 181 | commande_leds(); |
170 | 182 | } |
171 | - } | |
172 | - | |
183 | + } | |
173 | 184 | return 0; |
174 | 185 | } |
175 | 186 | ... | ... |