Blame view

main.c 4.31 KB
142ecdaf   rguillom   first commit
1
  #include <avr/io.h>		// for the input/output register
db0bdba2   rguillom   boutons+joy OK
2
  #include <util/delay.h>
142ecdaf   rguillom   first commit
3
4
5
6
  
  // For the serial port
  
  #define CPU_FREQ        16000000L       // Assume a CPU frequency of 16Mhz
db0bdba2   rguillom   boutons+joy OK
7
8
  #define tempo						25
  #define debit						9600						//débit liaison série en bauds 
142ecdaf   rguillom   first commit
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
  
  void init_serial(int speed)
  {
  /* Set baud rate */
  UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1;
  
  /* Enable transmitter & receiver */
  UCSR0B = (1<<TXEN0 | 1<<RXEN0);
  
  /* Set 8 bits character and 1 stop bit */
  UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);
  
  /* Set off UART baud doubler */
  UCSR0A &= ~(1 << U2X0);
  }
  
  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;
  }
  
  // For the AD converter
  
  void ad_init(unsigned char channel)   
  {   
  ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);   
  ADMUX|=(1<<REFS0)|(1<<ADLAR);
  ADMUX=(ADMUX&0xf0)|channel;   
  ADCSRA|=(1<<ADEN);
  }   
  
  //récupère la valeur de l'ADCH
  unsigned int ad_sample(void){
  ADCSRA|=(1<<ADSC);
  while(bit_is_set(ADCSRA, ADSC));
  return ADCH;
  }
  
  // For the I/O 
  void output_init(void){
db0bdba2   rguillom   boutons+joy OK
55
  DDRB |= 0b00111111; // PIN 8-13 as output (LED)
142ecdaf   rguillom   first commit
56
57
  }
  
db0bdba2   rguillom   boutons+joy OK
58
  /*
142ecdaf   rguillom   first commit
59
60
61
  void output_set(unsigned char value){
  if(value==0) PORTB &= 0xfe; else PORTB |= 0x01;
  }
db0bdba2   rguillom   boutons+joy OK
62
  */
142ecdaf   rguillom   first commit
63
64
  
  void input_init(void){
db0bdba2   rguillom   boutons+joy OK
65
66
67
  	DDRD &= 0b10000011;	// PIN 2-6 as input (Bouton Joystick + boutons)
  	PORTD |= 0x7C; // Pull up de 0 à 1
  	//DDRC &= 0b11111100;	// PIN 0-1 analogiques comme input (x et y joystick)
142ecdaf   rguillom   first commit
68
69
  }
  
db0bdba2   rguillom   boutons+joy OK
70
  
142ecdaf   rguillom   first commit
71
72
73
  unsigned char input_get(void){
  return ((PIND&0x04)!=0)?1:0;
  }
db0bdba2   rguillom   boutons+joy OK
74
  
142ecdaf   rguillom   first commit
75
  
48fa7247   rguillom   avancées code + f...
76
  /* Commande des LED */
65705671   rguillom   Début prog
77
78
  void commande_leds(){
  	unsigned char temp_serial, leds;
48fa7247   rguillom   avancées code + f...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  	temp_serial = UDR0;
  	if (temp_serial !=0){
  		leds = temp_serial;
  		// Allumer LED de 'A' à 'F'
  		if (leds == 65) PORTB = PORTB | 0x01; 
  		if (leds == 66) PORTB = PORTB | 0x02;
  		if (leds == 67) PORTB = PORTB | 0x04;
  		if (leds == 68) PORTB = PORTB | 0x08;
  		if (leds == 69) PORTB = PORTB | 0x10;
  		if (leds == 70) PORTB = PORTB | 0x20;
  		
  		// Eteindre LED de 'a' à 'f'
  		if (leds == 97) PORTB = PORTB & 0xfe;
  		if (leds == 98) PORTB = PORTB & 0xfd;
  		if (leds == 99) PORTB = PORTB & 0xfb;
  		if (leds == 100) PORTB = PORTB & 0xf7;
  		if (leds == 101) PORTB = PORTB & 0xef;
  		if (leds == 102) PORTB = PORTB & 0xdf;
  	}
65705671   rguillom   Début prog
98
99
  }
  
48fa7247   rguillom   avancées code + f...
100
  /* Récupération de la valeur des boutons et mise en forme */
db0bdba2   rguillom   boutons+joy OK
101
102
103
104
105
106
107
108
  unsigned char get_buttons(void){
  	//unsigned char boutons = (((PIND & 0x7C)>>2) &0x3f) | 0b00100000;
  	unsigned char boutons = PIND;
  	boutons = boutons >>2;
  	boutons = boutons & 0b00111111;
  	boutons = boutons | 0b00100000;
  	
  	return boutons;
48fa7247   rguillom   avancées code + f...
109
  }
142ecdaf   rguillom   first commit
110
  
48fa7247   rguillom   avancées code + f...
111
  /* Met en forme l'octet de l'axe pos du joystick */
db0bdba2   rguillom   boutons+joy OK
112
113
114
115
116
117
  unsigned char shape_joy(unsigned char pos){
  	//pos = ((pos >> 3) | &0b00111111) 0b00100000;
  	pos = pos >>3;
  	pos = pos & 0b00111111;
  	pos = pos | 0b00100000;
  	return pos;
48fa7247   rguillom   avancées code + f...
118
  }
65705671   rguillom   Début prog
119
  
48fa7247   rguillom   avancées code + f...
120
  /* Récupération de la valeur de l'axe du joystick sur la chaîne channel du CAN */
db0bdba2   rguillom   boutons+joy OK
121
  unsigned char get_joystick(int channel){
48fa7247   rguillom   avancées code + f...
122
123
124
  	  unsigned char axis;
  	  ad_init(channel);
      axis = ad_sample();
db0bdba2   rguillom   boutons+joy OK
125
126
      axis = shape_joy(axis);
      return axis;
48fa7247   rguillom   avancées code + f...
127
128
129
130
  }
  
  /* Dummy main */
  int main(void){
db0bdba2   rguillom   boutons+joy OK
131
132
133
134
135
136
137
  
  	//INITIALISATIONS  
    unsigned char boutons, boutons_anc;
    unsigned char joystick_x=0x10, joystick_x_anc=0x10;
    unsigned char joystick_y=0x10, joystick_y_anc=0x10;
    
    init_serial(debit);
48fa7247   rguillom   avancées code + f...
138
139
    output_init();
    input_init();
48fa7247   rguillom   avancées code + f...
140
141
142
  
  	//Récupération des valeurs des boutons et joystick, et mise en forme
    boutons = get_buttons();
db0bdba2   rguillom   boutons+joy OK
143
144
    joystick_x = get_joystick(0);
    joystick_y = get_joystick(1);
48fa7247   rguillom   avancées code + f...
145
146
    
    while(1){
48fa7247   rguillom   avancées code + f...
147
148
149
  		boutons_anc = boutons;
  		joystick_x_anc = joystick_x;
  		joystick_y_anc = joystick_y;
142ecdaf   rguillom   first commit
150
      
48fa7247   rguillom   avancées code + f...
151
152
     	//Récupération des valeurs des boutons et joystick, et mise en forme
    	boutons = get_buttons();
db0bdba2   rguillom   boutons+joy OK
153
154
155
156
    	joystick_x = get_joystick(0);
    	_delay_ms(tempo);
    	joystick_y = get_joystick(1);
      
48fa7247   rguillom   avancées code + f...
157
      	
db0bdba2   rguillom   boutons+joy OK
158
      //Port série libre
48fa7247   rguillom   avancées code + f...
159
160
161
      if ((UCSR0A & (1<<RXC0)) == 0){
      	
      	//if (boutons_anc != boutons){
db0bdba2   rguillom   boutons+joy OK
162
      	//if (joystick_x_anc != joystick_x){
48fa7247   rguillom   avancées code + f...
163
      	//if (joystick_y_anc != joystick_y){
db0bdba2   rguillom   boutons+joy OK
164
165
166
167
168
      	if ((boutons_anc != boutons) || (joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y)){
      		send_serial(boutons);
      		//if ((joystick_x > 126) || (joystick_x <33)) send_serial('e');
        	send_serial(joystick_x);
        	send_serial(joystick_y);
48fa7247   rguillom   avancées code + f...
169
170
171
172
        	
        	//send_serial('r');
      	  	
        	//retour chariot
db0bdba2   rguillom   boutons+joy OK
173
174
175
        	send_serial(0x0a);
        	send_serial(0x0d);
  			}
65705671   rguillom   Début prog
176
  			
db0bdba2   rguillom   boutons+joy OK
177
178
179
  		}
  		
  		//Port série occupé	
48fa7247   rguillom   avancées code + f...
180
  		else{
48fa7247   rguillom   avancées code + f...
181
182
  		commande_leds();
  		}
db0bdba2   rguillom   boutons+joy OK
183
    }  
65705671   rguillom   Début prog
184
  	return 0;
142ecdaf   rguillom   first commit
185
  }
65705671   rguillom   Début prog