Blame view

main.c 3.93 KB
142ecdaf   rguillom   first commit
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
  #include <avr/io.h>		// for the input/output register
  
  // For the serial port
  
  #define CPU_FREQ        16000000L       // Assume a CPU frequency of 16Mhz
  
  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){
48fa7247   rguillom   avancées code + f...
52
  DDRB |= 0b00111111; // PIN 8-13 as output
142ecdaf   rguillom   first commit
53
54
55
56
57
58
59
  }
  
  void output_set(unsigned char value){
  if(value==0) PORTB &= 0xfe; else PORTB |= 0x01;
  }
  
  void input_init(void){
48fa7247   rguillom   avancées code + f...
60
  DDRD |= 0b01111100;  // PIN 2-6 as input (Bouton Joystick + boutons)
65705671   rguillom   Début prog
61
  DDRC |= 0x03;		// PIN 0-1 analogiques comme input (x et y joystick analogique)
142ecdaf   rguillom   first commit
62
63
  }
  
48fa7247   rguillom   avancées code + f...
64
  /*
142ecdaf   rguillom   first commit
65
66
67
  unsigned char input_get(void){
  return ((PIND&0x04)!=0)?1:0;
  }
48fa7247   rguillom   avancées code + f...
68
  */
142ecdaf   rguillom   first commit
69
  
48fa7247   rguillom   avancées code + f...
70
  /* Commande des LED */
65705671   rguillom   Début prog
71
72
  void commande_leds(){
  	unsigned char temp_serial, leds;
48fa7247   rguillom   avancées code + f...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  	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
92
93
  }
  
48fa7247   rguillom   avancées code + f...
94
95
96
97
98
  /* Récupération de la valeur des boutons et mise en forme */
  int get_buttons(void){
  	unsigned boutons = ((PIND & 0x7C)>>2) | 0b00100000;
  	return(boutons);
  }
142ecdaf   rguillom   first commit
99
  
48fa7247   rguillom   avancées code + f...
100
101
102
103
104
  /* Met en forme l'octet de l'axe pos du joystick */
  int shape_joy(unsigned char pos){
  	pos = (pos >> 3) | 0b00100000;
  	return(pos);
  }
65705671   rguillom   Début prog
105
  
48fa7247   rguillom   avancées code + f...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  /* Récupération de la valeur de l'axe du joystick sur la chaîne channel du CAN */
  int get_joystick(int channel){
  	  unsigned char axis;
  	  ad_init(channel);
      axis = ad_sample();
      shape_joy(axis);
      return(axis);
  }
  
  /* Dummy main */
  int main(void){
    init_serial(9600);
    output_init();
    input_init();
    
    unsigned char boutons, boutons_anc;
    unsigned char joystick_x, joystick_x_anc;
    unsigned char joystick_y, joystick_y_anc;
  
  	//Récupération des valeurs des boutons et joystick, et mise en forme
    boutons = get_buttons();
    joystick_x = get_joystick(1);
    joystick_y = get_joystick(0);
65705671   rguillom   Début prog
129
      
48fa7247   rguillom   avancées code + f...
130
131
132
    //send_serial(boutons);
    
    while(1){
65705671   rguillom   Début prog
133
      
48fa7247   rguillom   avancées code + f...
134
135
136
  		boutons_anc = boutons;
  		joystick_x_anc = joystick_x;
  		joystick_y_anc = joystick_y;
142ecdaf   rguillom   first commit
137
      
48fa7247   rguillom   avancées code + f...
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
     	//Récupération des valeurs des boutons et joystick, et mise en forme
    	boutons = get_buttons();
    	joystick_x = get_joystick(1);
    	joystick_y = get_joystick(0);
      	
  
      send_serial(boutons+2);
      //retour chariot
      send_serial(0x0a);
      send_serial(0x0d);
      	
      //Cas où aucun caractère n'est reçu
      if ((UCSR0A & (1<<RXC0)) == 0){
      	
      	//if (boutons_anc != boutons){
      	//if (joystick_y_anc != joystick_y){
      	//if ((boutons_anc != boutons) || (joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y)){
      		//send_serial(boutons);
        	//send_serial(joystick_y);
        	//send_serial(joystick_y);
        	
        	//send_serial('r');
      	  	
        	//retour chariot
        	//send_serial(0x0a);
        	//send_serial(0x0d);
  			//}
  		}
65705671   rguillom   Début prog
166
  			
48fa7247   rguillom   avancées code + f...
167
168
169
170
171
172
  		else{
  		//Cas où un caractère attent dtre reçu
  		commande_leds();
  		}
    }
   		  
65705671   rguillom   Début prog
173
  	return 0;
142ecdaf   rguillom   first commit
174
  }
65705671   rguillom   Début prog