Blame view

main.c 4.22 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
  
  // For the serial port
  
cf8950d2   rguillom   suppression magic...
6
7
8
9
10
  #define CPU_FREQ	16000000L   // Assume a CPU frequency of 16Mhz
  #define tempo	25
  #define debit	9600		//débit liaison série en bauds
  #define ADC0	0
  #define ADC1	1
3966cf83   rguillom   modifs diverses :...
11
  #define debut_serial_tx	0x40		//pour détecter la transmission d'une trame pour le 16u2
142ecdaf   rguillom   first commit
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  
  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)
  {
01b3f0af   rguillom   PAD.c modif fonct...
30
31
  	loop_until_bit_is_set(UCSR0A, UDRE0);
  	UDR0 = c;
142ecdaf   rguillom   first commit
32
33
34
  }
  
  unsigned char get_serial(void) {
01b3f0af   rguillom   PAD.c modif fonct...
35
36
  	loop_until_bit_is_set(UCSR0A, RXC0);
  	return UDR0;
142ecdaf   rguillom   first commit
37
38
39
40
41
42
  }
  
  // For the AD converter
  
  void ad_init(unsigned char channel)   
  {   
01b3f0af   rguillom   PAD.c modif fonct...
43
44
45
46
  	ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);   
  	ADMUX|=(1<<REFS0)|(1<<ADLAR);
  	ADMUX=(ADMUX&0xf0)|channel;   
  	ADCSRA|=(1<<ADEN);
142ecdaf   rguillom   first commit
47
48
49
50
  }   
  
  //récupère la valeur de l'ADCH
  unsigned int ad_sample(void){
01b3f0af   rguillom   PAD.c modif fonct...
51
52
53
  	ADCSRA|=(1<<ADSC);
  	while(bit_is_set(ADCSRA, ADSC));
  	return ADCH;
142ecdaf   rguillom   first commit
54
55
56
57
  }
  
  // For the I/O 
  void output_init(void){
01b3f0af   rguillom   PAD.c modif fonct...
58
  	DDRB |= 0b00111111; // PIN 8-13 as output (LED)
142ecdaf   rguillom   first commit
59
60
  }
  
db0bdba2   rguillom   boutons+joy OK
61
  /*
142ecdaf   rguillom   first commit
62
  void output_set(unsigned char value){
01b3f0af   rguillom   PAD.c modif fonct...
63
  	if(value==0) PORTB &= 0xfe; else PORTB |= 0x01;
142ecdaf   rguillom   first commit
64
  }
db0bdba2   rguillom   boutons+joy OK
65
  */
142ecdaf   rguillom   first commit
66
67
  
  void input_init(void){
db0bdba2   rguillom   boutons+joy OK
68
69
  	DDRD &= 0b10000011;	// PIN 2-6 as input (Bouton Joystick + boutons)
  	PORTD |= 0x7C; // Pull up de 0 à 1
142ecdaf   rguillom   first commit
70
71
  }
  
5e9ff3d3   rguillom   supp comm inutiles
72
  /*
142ecdaf   rguillom   first commit
73
  unsigned char input_get(void){
01b3f0af   rguillom   PAD.c modif fonct...
74
  	return ((PIND&0x04)!=0)?1:0;
142ecdaf   rguillom   first commit
75
  }
5e9ff3d3   rguillom   supp comm inutiles
76
  */
142ecdaf   rguillom   first commit
77
  
48fa7247   rguillom   avancées code + f...
78
  /* Commande des LED */
65705671   rguillom   Début prog
79
80
  void commande_leds(){
  	unsigned char temp_serial, leds;
48fa7247   rguillom   avancées code + f...
81
82
83
84
  	temp_serial = UDR0;
  	if (temp_serial !=0){
  		leds = temp_serial;
  		// Allumer LED de 'A' à 'F'
df996f82   rguillom   fix + README
85
86
87
88
89
90
  		if (leds == 'A') PORTB = PORTB | 0x01; 
  		if (leds == 'B') PORTB = PORTB | 0x02;
  		if (leds == 'C') PORTB = PORTB | 0x04;
  		if (leds == 'D') PORTB = PORTB | 0x08;
  		if (leds == 'E') PORTB = PORTB | 0x10;
  		if (leds == 'F') PORTB = PORTB | 0x20;
48fa7247   rguillom   avancées code + f...
91
92
  		
  		// Eteindre LED de 'a' à 'f'
df996f82   rguillom   fix + README
93
94
95
96
97
98
  		if (leds == 'a') PORTB = PORTB & 0xfe;
  		if (leds == 'b') PORTB = PORTB & 0xfd;
  		if (leds == 'c') PORTB = PORTB & 0xfb;
  		if (leds == 'd') PORTB = PORTB & 0xf7;
  		if (leds == 'e') PORTB = PORTB & 0xef;
  		if (leds == 'f') PORTB = PORTB & 0xdf;
48fa7247   rguillom   avancées code + f...
99
  	}
65705671   rguillom   Début prog
100
101
  }
  
48fa7247   rguillom   avancées code + f...
102
  /* Récupération de la valeur des boutons et mise en forme */
db0bdba2   rguillom   boutons+joy OK
103
  unsigned char get_buttons(void){
db0bdba2   rguillom   boutons+joy OK
104
105
106
107
108
109
  	unsigned char boutons = PIND;
  	boutons = boutons >>2;
  	boutons = boutons & 0b00111111;
  	boutons = boutons | 0b00100000;
  	
  	return boutons;
48fa7247   rguillom   avancées code + f...
110
  }
142ecdaf   rguillom   first commit
111
  
48fa7247   rguillom   avancées code + f...
112
  /* Met en forme l'octet de l'axe pos du joystick */
db0bdba2   rguillom   boutons+joy OK
113
  unsigned char shape_joy(unsigned char pos){
5e9ff3d3   rguillom   supp comm inutiles
114
  	pos = pos >>3;	//On récupère les 5 bits de poids forts de l'ADCH (diminuer sensibilité)
db0bdba2   rguillom   boutons+joy OK
115
116
117
  	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){
01b3f0af   rguillom   PAD.c modif fonct...
122
123
124
125
126
  	unsigned char axis;
  	ad_init(channel);
  	axis = ad_sample();
  	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
  
  	//INITIALISATIONS  
01b3f0af   rguillom   PAD.c modif fonct...
133
134
135
  	unsigned char boutons, boutons_anc;
  	unsigned char joystick_x=0x10, joystick_x_anc=0x10;
  	unsigned char joystick_y=0x10, joystick_y_anc=0x10;
db0bdba2   rguillom   boutons+joy OK
136
    
01b3f0af   rguillom   PAD.c modif fonct...
137
138
139
  	init_serial(debit);
  	output_init();
  	input_init();
48fa7247   rguillom   avancées code + f...
140
141
  
  	//Récupération des valeurs des boutons et joystick, et mise en forme
01b3f0af   rguillom   PAD.c modif fonct...
142
  	boutons = get_buttons();
cf8950d2   rguillom   suppression magic...
143
144
  	joystick_x = get_joystick(ADC0);
  	joystick_y = get_joystick(ADC1);
48fa7247   rguillom   avancées code + f...
145
    
01b3f0af   rguillom   PAD.c modif fonct...
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
      
01b3f0af   rguillom   PAD.c modif fonct...
151
152
  		//Récupération des valeurs des boutons et joystick, et mise en forme
  		boutons = get_buttons();
cf8950d2   rguillom   suppression magic...
153
  		joystick_x = get_joystick(ADC0);
01b3f0af   rguillom   PAD.c modif fonct...
154
  		_delay_ms(tempo);
cf8950d2   rguillom   suppression magic...
155
  		joystick_y = get_joystick(ADC1);
48fa7247   rguillom   avancées code + f...
156
      	
01b3f0af   rguillom   PAD.c modif fonct...
157
158
159
160
161
162
163
164
165
166
167
168
  		//Port série libre
  		if ((UCSR0A & (1<<RXC0)) == 0){
  			// Si une des grandeurs a changé
  			if ((boutons_anc != boutons) || (joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y)){
  				/* On envoie la trame des grandeurs */
  				send_serial(debut_serial_tx);	//début de trame
  				send_serial(boutons);			//état global des grandeurs
  				send_serial(joystick_x);
  				send_serial(joystick_y);    	  	
  				//retour chariot
  				send_serial(0x0a);
  				send_serial(0x0d);
db0bdba2   rguillom   boutons+joy OK
169
  			}
db0bdba2   rguillom   boutons+joy OK
170
171
172
  		}
  		
  		//Port série occupé	
48fa7247   rguillom   avancées code + f...
173
  		else{
01b3f0af   rguillom   PAD.c modif fonct...
174
  			commande_leds();	//On gère la commande des leds
48fa7247   rguillom   avancées code + f...
175
  		}
01b3f0af   rguillom   PAD.c modif fonct...
176
  	}  
65705671   rguillom   Début prog
177
  	return 0;
142ecdaf   rguillom   first commit
178
  }