Précédent   Forum du club des développeurs et IT Pro > Général Développement > Programmation système > Embarqué
Embarqué Forum d'entraide sur la programmation des systèmes embarqués
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 28/12/2012, 11h50   #1
arthurdubois
Invité régulier
 
Homme arthur
Étudiant
Inscription : mars 2012
Messages : 110
Détails du profil
Informations personnelles :
Nom : Homme arthur
Localisation : Allemagne

Informations professionnelles :
Activité : Étudiant
Secteur : Enseignement

Informations forums :
Inscription : mars 2012
Messages : 110
Points : 9
Points : 9
Par défaut Echange de donnees entre un microcontrôleur et moteur

Bonjour,

je bosse avec un microcontrôleur possedant deux USARTs(USART0 et USART1). USART0 recoit sucessivement des donnees en ASCII du PC sous la forme: DX,YF . X, Y prennent les valeurs comprises entre 0 et 9. Cependant X et Y contienent au maximum 5Bytes chacun. Exemple: D10021,12425F ou D12,2356F.

Les donnees recues par l`USART0 sont ecris continuellement dans un tableau 2D. Des lors que l`USART0 ne recoit plus de donnees du pc, alors je vide progressivement le tableau 2D en envoyant les donnees au moteur via USART1. Jusqu´ici ca va !!

Chaque fois que le moteur recoit via USART1 une instruction du microcontrôleur, le moteur retourne aussitôt via USART1 une reponse au microcontrôleur pour dire si oui ou non l´instruction sera execute.

Si j´envoie 10 instructions via USART1 au moteur, le moteur recevra la 1er instruction et ensuite il retournera via USART1 au microntrôleur une reponse pour dire si oui ou non l´instruction est correct. Apres le moteur pourra recevoir la 2ieme instruction et le processus sera pareil pour les autres instructions.

Exemple:
Miconcontrôleur envoie:---> "#1A\r"
Reponse moteur:----------> "1A\r" : instruction correct( '?' est absent)

Miconcontrôleur envoie:---> "#1°\r"
Reponse moteur:----------> "1°?\r" : instruction pas correct( '?' est present)

J´ai essaye de synchroniser l´envoie des instructions via USART1 vers le moteur et le retour de reponses du moteur pour chaque instruction recue. Mais je pense que je m´y prends mal au niveau de la fonction ISR(USART1_RX_vect) qui est charge de recevoir les reponses du moteur et ensuite d´activer l´envoie des autres instructions.

Pourriez-vous m´aider s´il vous plaît !! Merci d´avance.

PS: Dans mon programme, je me contente de gerer les reponses du moteur qui sont corrects au niveau de la fonction ISR(USART1_RX_vect).

Programme:

Code :
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
103
104
105
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <util/delay.h>

#define F_CPU 3686400UL
#define FOSC 7372000 // Clock Speed
#define BAUD 9600UL
#define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1)   // clever runden
///////////////////////////////////////////////////////////////////
#define usart_buffer_max_size 16u
#define usart_command_max_size 16u
#define ROW 200
#define COLUMN 12
#define MAX 10
///////////////////////////////////////////////////////////////////
char Table_2D[ROW][COLUMN]; // table in dimensional two
char Table_1D[COLUMN] = {0}; // table in dimensional one
char motor_command[COLUMN];
char usart0_tx_buffer[usart_buffer_max_size];
char usart1_tx_buffer[usart_buffer_max_size];
volatile uint8_t usart0_tx_buffer_size = 0;
volatile uint8_t usart0_tx_buffer_start = 0;
volatile uint8_t usart1_tx_buffer_size = 0;
volatile uint8_t usart1_tx_buffer_start = 0;
volatile uint8_t command_execution = 0;
volatile uint8_t command_start = 0;
volatile uint8_t COMMAND_SIZE = 0;
volatile uint8_t command_ready = 0;
/////////////////////////////////////////////////////////////////////
int i = 0; // Number of elements in a row
int j = 0; // Number of rows
int N = sizeof(Table_2D) / sizeof(Table_2D[0]); // Number of rows
int M = sizeof(Table_2D) / sizeof(Table_2D[j][0]); // Number elements of a row
/////////////////////////////////////////////////////////////////////
// Configuration USART0, USART1 and setting Baudrate

void USART_Init(unsigned int ubrr)
{
  UBRR0H = (unsigned char)(ubrr>>8);
  UBRR0L = (unsigned char) ubrr;
  UBRR1H = (unsigned char)(ubrr>>8);
  UBRR1L = (unsigned char) ubrr;
  UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0);
  UCSR0C = (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00); // 1 Stopbit, data = 8 Bits
  UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (0<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1);
  UCSR1C = (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10); // 1 Stopbit, data = 8 Bits
}
/////////////////////////////////////////////////////////////////////

// Entry of circular buffer for USART0
void USART0_QueueIn(char c)
{
  int i;

  if (usart0_tx_buffer_size < usart_buffer_max_size)
  {
    i = (usart0_tx_buffer_size + usart0_tx_buffer_start) % usart_buffer_max_size;
    usart0_tx_buffer[i] = c;
    ++usart0_tx_buffer_size;
  }
  
}

// Exit of circular buffer for USART0
char USART0_QueueOut(void)
{
  char c;

  if (usart0_tx_buffer_size == 0)
    return 0;
  c = usart0_tx_buffer[usart0_tx_buffer_start];
  --usart0_tx_buffer_size;
  usart0_tx_buffer_start = (usart0_tx_buffer_start + 1) % usart_buffer_max_size;
  return c;
}

// Entry of circular buffer for USART1
void USART1_QueueIn(char c)
{
  int i;

  if (usart1_tx_buffer_size < usart_buffer_max_size) // Circular buffer is not full
  {
    i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
    usart1_tx_buffer[i] = c;
    ++usart1_tx_buffer_size;
  }
  
}

// Exit of circular buffer for USART1
char USART1_QueueOut(void)
{
  char c;

  if (usart1_tx_buffer_size == 0)
    return 0;
  c = usart1_tx_buffer[usart1_tx_buffer_start];
  --usart1_tx_buffer_size;
  usart1_tx_buffer_start = (usart1_tx_buffer_start + 1) % usart_buffer_max_size;
  return c;
}

// Sending response via circular buffer to the USART0
static void USART0_Send(const char *s)
{
  int i;
 
  for (i = 0; s[i] != 0; ++i)
    USART0_QueueIn(s[i]);
  if (usart0_tx_buffer_size > 0)
    UCSR0B |= 1 << UDRIE0;
}

// Sending a command via circular buffer to the USART1
static void USART1_Send(const char *s)
{
  int i;
 
  for (i = 0; s[i] != 0; ++i)
    USART1_QueueIn(s[i]);
  if (usart1_tx_buffer_size > 0)
    UCSR1B |= 1 << UDRIE1;
}


// Treatment of command
static void ProcessCommand(void)
{
  int i;
  int k;
  int x;
  int y;
  int pos_x;
  int pos_y;
 
  char x_motor[12];
  char y_motor[12];

     for (i = 0; i < COMMAND_SIZE; ++i)
       if (motor_command[i] == ',')
         break;

     if (i <= 0 || i >= COMMAND_SIZE - 1)
     {
   
       USART0_Send("\x15");  // NAK, Sending didn´t work.
       COMMAND_SIZE = 0;
       return;
     }
  // Conversion of x and y in integer for the execution of calculation,if it is necessary
  // Extraction of X and Y
    
       motor_command[i] = 0;
       motor_command[COMMAND_SIZE] = 0;
       x = atoi(motor_command);
       y = atoi(motor_command + i + 1);

       COMMAND_SIZE = 0;   

    // Conversion in stepping motor
       pos_x= x * 127/500;
       pos_y= y * 127/500;
    // Conversion in ascii
       itoa(pos_x, x_motor, 10);
       itoa(pos_y, y_motor, 10);

       if(command_start == 0)
	   {
          // Sending position x_moteur
         USART1_Send("#1s");
         USART1_Send(x_motor);
         USART1_Send("\r");
       }

	   if(command_start == 1)
	   {
         USART1_Send("#1A\r"); // the plate can move in axis x
       }
		  
	   if(command_start == 2)
	   {
               // Sending of position y_moteur
         USART1_Send("#2s");
         USART1_Send(y_motor);
         USART1_Send("\r");
       }
	   if(command_start == 3)
	   {
         USART1_Send("#2A\r"); // the plate can move in axis y
	   }

	   
    wait(3000); //wait 3000ms ---> 3s
  
}

ISR(USART1_RX_vect)
{
 
 char response[MAX] = {0};
 int n,m;
 
 for(n = 0; n < MAX; n++)
 {
  response[n] = UDR1;
 }
  
    if(strcmp(response, "1D0\r") == 0)
    {
      for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	     command_ready = 1;
    } 

    if(strcmp(response, "2D0\r") == 0)
    {
      for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	   command_ready = 2;
    }
	
	
    if(strcmp(response, "1p2\r") == 0)
    {
     for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	    command_ready = 3;
    } 

	
    if(strcmp(response, "2p2\r") == 0)
    {
     for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	   command_ready = 0;
    } 

	
    if(strcmp(response, "1sx_motor\r") == 0)
    {
     for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	    command_start = 1;
    }
	
	 
    if(strcmp(response, "1A\r") == 0)
    {
      for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	  
    }

	if(strcmp(response, "Ziel erreicht") == 0)
	{
	  for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	    command_start = 2;
	} 
	
	
    if(strcmp(response, "1sy_motor\r") == 0)
    {
     for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	     command_start = 3;
    } 

	 
    if(strcmp(response, "2A\r") == 0)
    {
      for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	 
    } 
    
	if(strcmp(response, "Ziel erreicht") == 0)
	{
	  for(m = 0; m < MAX; m++)
	  {
        response[m] = '\0'; // String is empty
	  }
	    command_start = 0;
	 }
 

}

// function wait

void wait(unsigned int time)
{
  unsigned int counter = 20;
    for(int i = 0; i < counter; i++)
    {
      _delay_ms(time); 
    }
}



// function receive interrupt of Byte
// the fuction receice interrupt is High, if RXCIE0 = 1 
ISR(USART0_RX_vect)
{
  char data;
  data = UDR0;

 // Reception of command -->("D X,Y F")
 // X = x_motor and Y = y_motor
 
  if (data == 'F')
  {            
    ++j;
    USART0_Send("\x06"); // ACK ---> PC can send next data
   
   }

   else if (data == 'D')
   {
    // Generation of a new command
    i = 0;
   }

  
  else
  {
    // If we not receive  'F' or 'D', we store
    // the positions of coordinate in a table
   
    if(j < ROW)
    {
      if(i < COLUMN)
      {
        Table_2D[j][i] = data;
        ++i;
      }
     
     }
     else
     {
       //USART0_Send("\x04");// EOT --> PC must wait a few minuts   
      // j = 0;
     }   
   }
  command_execution = 1; // Sending of data begin to USART1
}

void Copytable(char Originaltable2D[N][M], char Copytable1D[], int N1, int M1)
{

  int j1;
  int i1;
  for(j1 = 0; j1 < N1; j1++)
  {
    for(i1 = 0; i1 < M1; i1++)
    {
      Copytable1D[i1] = Originaltable2D[j1][i1];
      motor_command[COMMAND_SIZE] = Copytable1D[i1];
      ++COMMAND_SIZE;
    }

    ProcessCommand();
  }

}
 


// The function interrupt for sending of Byte
// The function interrupt is active, if UDRIE0 = 1
ISR(USART0_UDRE_vect)
{
  UDR0 = USART0_QueueOut();
  // Stop sending,if we don´t have data to send.
  if (usart0_tx_buffer_size == 0)
    UCSR0B &= ~(1 << UDRIE0);
}

// The function interrupt for sending of Byte
// The function interrupt is active, if UDRIE1 = 1
ISR(USART1_UDRE_vect)
{
  UDR1 = USART1_QueueOut();
  // Stop sending,if we don´t have data to send.
  if (usart1_tx_buffer_size == 0)
    UCSR1B &= ~(1 << UDRIE1);
}


void ConfigurationMotor(void)
 {
   //USART1_Send("#1|0\r"); // Deactivation of response motor-control Nr 1
   //USART1_Send("#2|0\r"); // Deactivation of response motor-control Nr 2
   //USART1_Send("#1:baud=7"); // Baudrate = 9600
   //USART1_Send("#2:baud=7"); // Baudrate = 9600
   USART1_Send("#1D0\r"); // The actual position of plate is the origin in axis x
   																																																																																																																																																																																																																			            
 }
 

int main (void)
{
  sei(); // Activation of function Interrupt
  USART_Init(UBRR_VAL); // Initialisation of USART0 and USART1
  ConfigurationMotor(); // Initialisaton of Motor-control
  
  
  
 
  while (1) // Infinite loop
  {
    
    if(command_ready == 1)
	{
      USART1_Send("#2D0\r"); // The actual position of plate is the origin in axis y
	}

	if(command_ready == 2)
	{
      USART1_Send("#1p2\r"); // Mode: Positioning absolut --> p=2
	}
		
	if(command_ready == 3)
	{
      USART1_Send("#2p2\r"); // Mode: Positioning absolut --> p=2	 
	}
	  
	  
	 if(command_execution == 1)
    {
      Copytable(Table_2D, Table_1D, ROW, COLUMN);
      command_execution = 0;   
    }
        
  }
 
}
arthurdubois est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2013, 18h38   #2
arthurdubois
Invité régulier
 
Homme arthur
Étudiant
Inscription : mars 2012
Messages : 110
Détails du profil
Informations personnelles :
Nom : Homme arthur
Localisation : Allemagne

Informations professionnelles :
Activité : Étudiant
Secteur : Enseignement

Informations forums :
Inscription : mars 2012
Messages : 110
Points : 9
Points : 9
Bonsoir,

A l´aide du deboggage, j´ai teste mon programme lorsque le microcontrôleur est alimente par une source de tension de 5V.
Les fonctions USART_Init(UBRR_VAL), Sei() et Sendtomotor(firststring2D) que j´appelle dans mon main fonctionne comme je le desire.

Mais la fonction
Code :
Copytable(Table_2D, Table_1D, ROW, COLUMN)
qui se trouve a l´interieur de la fonction me cause des soucis.

Lors du deboggage, le debogguer entre dans la fonction

Code :
Copytable(Table_2D, Table_1D, ROW, COLUMN
sans probleme. Une fois a l´interieur de la fonction Copytable, le debogguer est sense entrer dans les deux boucles for, puis entrer dans la fonction ProcessCommand.

Au lieu de cela, le debogguer entre d´abord dans la fonction ProcessCommand et execute les instructions char s1_x = "#1s"; char s1_y = "#2s"; strcat(s1_x ,x_motor ); strcat(s1_y ,y_motor ); , puis il revient dans la fonction Copytable et cette fois le debogguer entre dans les deux boucles for, ensuite il entre normalement dans la fonction ProcessCommand.

Je ne comprend pas ce disfonctionement de mon programme.

pourriez vous m´aider svp.??

Programme:

Code :
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
103
104
105
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define F_CPU 3686400UL
#include <util/delay.h>

#define FOSC 7372000 // Clock Speed
#define BAUD 9600UL

#define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1)   // clever runden
///////////////////////////////////////////////////////////////////
#define usart_buffer_max_size 16u
//#define usart_command_max_size 16u
#define ROW 1
#define COLUMN 13
#define MAX 11
#define LINE 4

///////////////////////////////////////////////////////////////////
char Table_2D[ROW][COLUMN]; // table in dimensional two
char Table_1D[COLUMN] = {0}; // table in dimensional one
char motor_command[COLUMN];
char usart0_tx_buffer[usart_buffer_max_size];
char usart1_tx_buffer[usart_buffer_max_size];
char responsemotor[MAX];
char firststring2D[LINE][MAX] = {"#1D0\r", "#2D0\r", "#1p2\r", "#2p2\r"};

volatile uint8_t usart0_tx_buffer_size = 0;
volatile uint8_t usart0_tx_buffer_start = 0;
volatile uint8_t usart1_tx_buffer_size = 0;
volatile uint8_t usart1_tx_buffer_start = 0;
volatile uint8_t command_execution = 0;
volatile uint8_t command_ready = 0;
volatile uint8_t COMMAND_SIZE = 0;
/////////////////////////////////////////////////////////////////////
int i; // Number of elements in a row
int j = 0; // Number of rows
int N = sizeof(Table_2D) / sizeof(Table_2D[0]); // Number of rows
int M = sizeof(Table_2D) / sizeof(Table_2D[j][0]); // Number of elements in a row
/////////////////////////////////////////////////////////////////////
// Configuration USART0, USART1 and setting Baudrate

void USART_Init(unsigned int ubrr)
{
  UBRR0H = (unsigned char)(ubrr>>8);
  UBRR0L = (unsigned char) ubrr;
  UBRR1H = (unsigned char)(ubrr>>8);
  UBRR1L = (unsigned char) ubrr;
  UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0);
  UCSR0C = (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00); // 1 Stopbit, data = 8 Bits
  UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (0<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1);
  UCSR1C = (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10); // 1 Stopbit, data = 8 Bits
}

////////////////////////////////////////////////////////////////////////////////

// Entry of circular buffer for USART0
void USART0_QueueIn(char c)
{
  int i;

  if (usart0_tx_buffer_size < usart_buffer_max_size)
  {
    i = (usart0_tx_buffer_size + usart0_tx_buffer_start) % usart_buffer_max_size;
    usart0_tx_buffer[i] = c;
    ++usart0_tx_buffer_size;
  }
 
}

// Exit of circular buffer for USART0
char USART0_QueueOut(void)
{
  char c;

  if (usart0_tx_buffer_size == 0)
    return 0;
  c = usart0_tx_buffer[usart0_tx_buffer_start];
  --usart0_tx_buffer_size;
  usart0_tx_buffer_start = (usart0_tx_buffer_start + 1) % usart_buffer_max_size;
  return c;
}

// Entry of circular buffer for USART1
void USART1_QueueIn(char c)
{
  int i;

  if (usart1_tx_buffer_size < usart_buffer_max_size) // Circular buffer is not full
  {
    i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
    usart1_tx_buffer[i] = c;
    ++usart1_tx_buffer_size;
  }
 
}

// Exit of circular buffer for USART1
char USART1_QueueOut(void)
{
  char c;

  if (usart1_tx_buffer_size == 0)
    return 0;
  c = usart1_tx_buffer[usart1_tx_buffer_start];
  --usart1_tx_buffer_size;
  usart1_tx_buffer_start = (usart1_tx_buffer_start + 1) % usart_buffer_max_size;
  return c;
}

// Sending response via circular buffer to the USART0
static void USART0_Send(const char *s)
{
  int i;
 
  for (i = 0; s[i] != 0; ++i)
    USART0_QueueIn(s[i]);
  if (usart0_tx_buffer_size > 0)
    UCSR0B |= 1 << UDRIE0;
}

// Sending a command via circular buffer to the USART1
static void USART1_Send(const char *s)
{
  int i;
 
  for (i = 0; s[i] != 0; ++i)
    USART1_QueueIn(s[i]);
  if (usart1_tx_buffer_size > 0)
    UCSR1B |= 1 << UDRIE1;
     ++command_ready;
}


// Treatment of command
static void ProcessCommand(void)
{
  int i;
  int k;
  int x;
  int y;
  int pos_x;
  int pos_y;
  char x_motor[12];
  char y_motor[12];
  char s1_x[20] = "#1s";
  char s1_y[20] = "#2s";

     for (i = 0; i < COMMAND_SIZE; ++i)
       if (motor_command[i] == ',')
         break;

     if (i <= 0 || i >= COMMAND_SIZE - 1)
     {
   
       USART0_Send("\x15");  // NAK, Sending didn´t work.
       COMMAND_SIZE = 0;
       return;
     }
  // Conversion of x and y in integer for the execution of calculation,if it is necessary
  // Extraction of X and Y
    
       motor_command[i] = 0;
       motor_command[COMMAND_SIZE] = 0;
       x = atoi(motor_command);
       y = atoi(motor_command + i + 1);

       COMMAND_SIZE = 0;   

    // Conversion in stepping motor
       pos_x= x * 127/500;
       pos_y= y * 127/500;
    // Conversion in ascii
       itoa(pos_x, x_motor, 10);
       itoa(pos_y, y_motor, 10);

       strcat(s1_x ,x_motor );
       strcat(s1_x , "\r");

       strcat(s1_y ,y_motor );
       strcat(s1_y , "\r");

        // Sending position x_moteur
      //USART1_Send("#1s");
      //USART1_Send(x_motor);
      //USART1_Send("\r");

      Deletecaracter(s1_x); // Delete the symbo '#' in s1_x
      USART1_Send(s1_x);

      if(command_ready == 1)
      {
         USART1_Send("#1A\r"); // the plate can move in axis x
      }
      if(command_ready == 2)
      {
        // Sending of position y_moteur
        //USART1_Send("#2s");
        //USART1_Send(y_motor);
        //USART1_Send("\r");
        Deletecaracter(s1_y);
        USART1_Send(s1_y);
       }
      if(command_ready == 3)
      {
        USART1_Send("#2A\r"); // the plate can move in axis y
      }

       command_ready = 0;

       wait();
	     
}

// function wait

void wait(void)
{
  unsigned int counter = 6;
    for(int i = 0; i < counter; i++)
    {
      _delay_ms(2000); // Wait 2000ms --> 2s
    }
}


// function receive interrupt of Byte
// the fuction receice interrupt is High, if RXCIE0 = 1 
ISR(USART0_RX_vect)
{
 

 // Reception of command -->("D X,Y F")
 // X = x_motor and Y = y_motor

  char data;
  data = UDR0;
 
 
   if (data == 'F')
   {             
     ++j;
     USART0_Send("\x06"); // ACK ---> PC can send next data
      command_execution = 1; // Activation sending of position
    }

    else if (data == 'D')
    {
     // Generation of a new command
     i = 0;
    }
   
   else
   {
      // If we not receive  'F' or 'D', we store
      // the positions of coordinate in a table2D
   
     if(j < ROW)
     {
       if(i < COLUMN)
       {
        Table_2D[j][i] = data;
         ++i;
       }
     
     }
      else
      {
       //USART0_Send("\x04");// EOT --> PC must wait a few minuts   
      // j = 0;
      }   
    }
  

}

void Copytable(char Originaltable2D[N][M], char Copytable[M], int N1, int M1)
{

  int j1;
  int i1;
  for(j1 = 0; j1 < N1; j1++)
  {
    for(i1 = 0; i1 < M1; i1++)
    {
      Copytable[i1] = Originaltable2D[j1][i1];
      motor_command[COMMAND_SIZE] = Copytable[i1];
      ++COMMAND_SIZE;
    }

    ProcessCommand();
  }

}
 


// The function interrupt for sending of Byte
// The function interrupt is active, if UDRIE0 = 1
ISR(USART0_UDRE_vect)
{
  UDR0 = USART0_QueueOut();
  // Stop sending,if we don´t have data to send.
  if (usart0_tx_buffer_size == 0)
    UCSR0B &= ~(1 << UDRIE0);
}

// The function interrupt for sending of Byte
// The function interrupt is active, if UDRIE1 = 1
ISR(USART1_UDRE_vect)
{
  UDR1 = USART1_QueueOut();
  // Stop sending,if we don´t have data to send.
  if (usart1_tx_buffer_size == 0)
    UCSR1B &= ~(1 << UDRIE1);
}

ISR(USART1_RX_vect)
{
  char response[MAX] = {UDR1};

 /* char response[MAX] = {0};
  int m;
  for(m = 0; m < MAX; m++)
  {
    response[m] = UDR1;
  } */

  if(strcmp(response, responsemotor) == 0)
  {
    memset(response, 0, sizeof(response)); // string is empty
    memset(responsemotor, 0, sizeof(responsemotor)); // string is empty
  }
  if(strcmp(response, "1A\r") == 0)
  {
    memset(response, 0, sizeof(response)); // string is empty   
  }
 
  if(strcmp(response, "2A\r") == 0)
  {
    memset(response, 0, sizeof(response)); // string is empty
  }

}

 void Sendtomotor(char string2D[LINE][MAX])
 {
   char stepmotor[MAX];
   int i, j;

   for(j = 0; j < LINE; j++)
   {
     for(i = 0; i < MAX; i++)
     {
       stepmotor[i] = string2D[j][i];
     }

     Deletecaracter(stepmotor);
     USART1_Send(stepmotor);
   }
   command_ready = 0;
 }

void Deletecaracter(char * string)
{
  int i;

  for(i = 0; string[i] != 0 ; i++)
  {
     if(string[i] == '#')
     {
       // We not need the caracter '#'
     }
     else
     {
       responsemotor[i] = string[i];
     }
  }
}

void Sendingposition(void)
{
  if(command_execution == 1)
    {
      Copytable(Table_2D, Table_1D, ROW, COLUMN);
      command_execution = 0;   
    }
}
 

int main (void)
{

  USART_Init(UBRR_VAL); // Initialisation of USART0 and USART1
  sei(); // Activation of function Interrupt
  Sendtomotor(firststring2D); // Initialisaton of Motor-control
  //Sendingposition(); // Sending of position to USART1
 

  while (1) // Infinite loop
  {
     //ConfigurationMotor(); // Initialisaton of Motor-control
     Sendingposition();     
  }
 
}
Merci d´avance.
arthurdubois est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/01/2013, 14h04   #3
nnovic
Membre éprouvé
 
Développeur en systèmes embarqués
Inscription : mars 2009
Messages : 219
Détails du profil
Informations personnelles :
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Développeur en systèmes embarqués
Secteur : Industrie

Informations forums :
Inscription : mars 2009
Messages : 219
Points : 469
Points : 469
Ce n'est pas forcément un problème de ton programme, mais peut-être que ton déboggueur est perdu... As-tu vérifié que les optimisations sont désactivées lorsque tu compiles ton code pour le débogguer ?
nnovic est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/01/2013, 15h01   #4
arthurdubois
Invité régulier
 
Homme arthur
Étudiant
Inscription : mars 2012
Messages : 110
Détails du profil
Informations personnelles :
Nom : Homme arthur
Localisation : Allemagne

Informations professionnelles :
Activité : Étudiant
Secteur : Enseignement

Informations forums :
Inscription : mars 2012
Messages : 110
Points : 9
Points : 9
Bonsoir,

J´ai deboggue a plusieurs reprises mon programme et je me suis rendu compte que la synchronisation entre l´envoie des donnees depuis le microcontrôleur via la fonction USART1_Send et la reception automatique des reponses du moteur par le microcontrôleur via la fonction interrupt ISR(USART1_RX_vect) ne marche pas bien.

J´ai effectue a nouveau des modifications dans mon prog et le resultat a ete un succes.

Programme:

Code :
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
103
104
105
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <util/delay.h>

#define F_CPU 3686400UL
#define FOSC 7372000 // Clock Speed
#define BAUD 9600UL
#define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1)   // clever runden
/////////////////////////////////////////////////////////////////////
#define usart_buffer_max_size 64u
#define lines 10
#define cols 11
#define MAX 6
#define LINE 4
/////////////////////////////////////////////////////////////////////
char table2D[lines][cols + 1]; // table in dimension two
char usart0_tx_buffer[usart_buffer_max_size];
char usart1_tx_buffer[usart_buffer_max_size];
char responsemotor[cols] = {0};
volatile char response[cols] = {0};
char firststring2D[LINE][MAX] = {"#1D0\r", "#2D0\r", "#1p2\r", "#2p2\r"};
//////////////////////////////////////////////////////////////////////
volatile uint8_t usart0_tx_buffer_size = 0;
volatile uint8_t usart0_tx_buffer_start = 0;
volatile uint8_t usart1_tx_buffer_size = 0;
volatile uint8_t usart1_tx_buffer_start = 0;
volatile uint8_t command_execution = 0;
volatile uint8_t command_ready = 0;
volatile uint8_t flag = 0;
volatile uint8_t counter = 0;
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
unsigned int i = 0; // Number of line
unsigned int j ; // Number of byte in a line
unsigned int N = sizeof(table2D) / sizeof(table2D[0]); // Number of rows
unsigned int M = sizeof(table2D) / sizeof(table2D[i][0]); // Number of elements in a row
/////////////////////////////////////////////////////////////////////
// Configuration USART0, USART1 and setting Baudrate

void USART_Init(unsigned int ubrr)
{
  UBRR0H = (unsigned char)(ubrr>>8);
  UBRR0L = (unsigned char) ubrr;
  UBRR1H = (unsigned char)(ubrr>>8);
  UBRR1L = (unsigned char) ubrr;
  UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0);
  UCSR0C = (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00); // 1 Stopbit, data = 8 Bits
  UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1);
  UCSR1C = (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10); // 1 Stopbit, data = 8 Bits
  DDRD = (1<<PD4) | (1<<PD5);
  PORTD = (1<<PD4); // RS485-Sender einschalten
}

////////////////////////////////////////////////////////////////////////////////

// Entry of circular buffer for USART0
void USART0_QueueIn(char c)
{
  int i;

  if (usart0_tx_buffer_size < usart_buffer_max_size)
  {
    i = (usart0_tx_buffer_size + usart0_tx_buffer_start) % usart_buffer_max_size;
    usart0_tx_buffer[i] = c;
    ++usart0_tx_buffer_size;
  }
 
}

// Exit of circular buffer for USART0
char USART0_QueueOut(void)
{
  char c;

  if (usart0_tx_buffer_size == 0)
    return 0;
  c = usart0_tx_buffer[usart0_tx_buffer_start];
  --usart0_tx_buffer_size;
  usart0_tx_buffer_start = (usart0_tx_buffer_start + 1) % usart_buffer_max_size;
  return c;
}

// Entry of circular buffer for USART1
void USART1_QueueIn(char c)
{
  int i;

  if (usart1_tx_buffer_size < usart_buffer_max_size) // Circular buffer is not full
  {
    i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
    usart1_tx_buffer[i] = c;
    ++usart1_tx_buffer_size;
  }
 
}

// Exit of circular buffer for USART1
char USART1_QueueOut(void)
{
  char c;

  if (usart1_tx_buffer_size == 0)
    return 0;
  c = usart1_tx_buffer[usart1_tx_buffer_start];
  --usart1_tx_buffer_size;
  usart1_tx_buffer_start = (usart1_tx_buffer_start + 1) % usart_buffer_max_size;
  return c;
}

// Sending response via circular buffer to the USART0
static void USART0_Send(const char *s)
{
  int i;
 
  for (i = 0; s[i] != 0; ++i)
    USART0_QueueIn(s[i]);
  if (usart0_tx_buffer_size > 0)
    UCSR0B |= 1 << UDRIE0;
}

// Sending a command via circular buffer to the USART1
static void USART1_Send(const char *s)
{
  int i;
 
  for (i = 0; s[i] != 0; ++i)
    USART1_QueueIn(s[i]);
  if (usart1_tx_buffer_size > 0)
    UCSR1B |= 1 << UDRIE1;
       ++command_ready;
}


// Treatment of command
void ProcessCommand(char *p)
{
  char buffer[cols + 1] = {0};
  char *separator = {","};
  char *x_pos = NULL ;
  char *y_pos = NULL ;
  char x_motor[12] = {0};
  char y_motor[12] = {0};
  char s1_x[20] = "#1s";
  char s1_y[20] = "#2s";
  char s_x[20] = {0};
  char s_y[20] = {0};
  int x = 0, y = 0;
  int x1 = 0, y1 = 0;

  // Copy of coordinate(x,y)
      strcpy(buffer, p);

 // buffer = strdup(p);

 // Extraction of position x and y
      x_pos = strtok(buffer, separator);
      y_pos = strtok(NULL, separator);

      //x = atoi(x_pos);
      //y = atoi(y_pos);

      //x1 = x * 127 / 500;
      //y1 = y * 127 / 500;

      //itoa(x1, x_motor,10);
      //itoa(y1, y_motor,10);
      
      // copy of string
      strcpy(x_motor, x_pos);
      strcpy(y_motor, y_pos);


      strcat(x_motor, "\r" ); // concatenation of string
      strcat(s1_x , x_motor);
      strcpy(s_x, s1_x);     // copy of string

      strcat(y_motor, "\r" );
      strcat(s1_y , y_motor);
      strcpy(s_y, s1_y);
      
        // Sending position x_moteur
      
      if(flag == 0)
      {
        Deletecaracter(s1_x); // Delete the caracter '#' in s1_x
        USART1_Send(s_x);
        delay();

          if(strcmp(response, responsemotor) == 0)
          {
           memset(response, 0, sizeof(response)); // string is empty
           memset(responsemotor, 0, sizeof(responsemotor)); // string is empty
           counter = 0;
          }
          if(flag == 1)
          {
            flag = 0;
            USART1_Send("#1A\r");
            delay();
            if(strcmp(response, "1A\r") == 0)
            {
             memset(response, 0, sizeof(response)); // string is empty
             flag = 0;
             counter = 0;
            }
          }
        }

      if(flag == 0)
      {
        Deletecaracter(s1_y); // Delete the caracter '#' in s1_x
        USART1_Send(s_y);
        delay();

          if(strcmp(response, responsemotor) == 0)
          {
           memset(response, 0, sizeof(response)); // string is empty
           memset(responsemotor, 0, sizeof(responsemotor)); // string is empty
           counter = 0;
          }
          if(flag == 1)
          {
            flag = 0;
            USART1_Send("#2A\r");
            delay();
            if(strcmp(response, "2A\r") == 0)
            {
             memset(response, 0, sizeof(response)); // string is empty
             flag = 0;
             counter = 0;
             command_ready = 0;
             memset(p, 0, sizeof(p));
            }
          }
        }

         wait();
                 
}

// function wait

void wait(void)
{
  unsigned int counter = 10;
    for(int i = 0; i < counter; i++)
    {
      _delay_ms(3000); // Wait 3000ms --> 3s
    }
}

void delay(void)
{
  while(flag == 0)
  {
    unsigned int i = 1;
      while(i != flag)
      {
        // We have nothing to do
      }
  }
}



// function receive interrupt of Byte
// the fuction receice interrupt is High, if RXCIE0 = 1
ISR(USART0_RX_vect)
{
 
 // Reception of command -->("D X,Y F")

  char data;
  data = UDR0;
 
 
   if (data == 'F')
   {            
     ++i;
     USART0_Send("\x06"); // ACK ---> PC can send next data
     command_execution = 1; // Activation sending of position
    }

    else if (data == 'D')
    {
     // Generation of a new command
     j = 0;
    }
 
   else
   {
      // If we not receive  'F' or 'D', we store
      // the positions of coordinate in a table2D
 
     if(i < lines)
     {
       if(j < cols)
       {
        table2D[i][j] = data;
         ++j;
       }
    
     }
      else
      {
       //USART0_Send("\x04");// EOT --> PC must wait a few minuts  
      // i = 0;
      }  
    }
 
}



void Copytable(char table_2D[lines][cols + 1], int N, int M)
{
  int i, j;
  char table_1D[cols + 1] = {0};
 
  for(i = 0; i < N; i++)
  {
    for(j = 0; j < M; j++)
    {
      table_1D[j] = table_2D[i][j];
    }

    ProcessCommand(table_1D);
    memset(table_1D, 0, sizeof(table_1D));
  }
}

 


// The function interrupt for sending of Byte
// The function interrupt is active, if UDRIE0 = 1
ISR(USART0_UDRE_vect)
{
  UDR0 = USART0_QueueOut();
  // Stop sending,if we don´t have data to send.
  if (usart0_tx_buffer_size == 0)
    UCSR0B &= ~(1 << UDRIE0);
}

// The function interrupt for sending of Byte
// The function interrupt is active, if UDRIE1 = 1
ISR(USART1_UDRE_vect)
{
  UDR1 = USART1_QueueOut();
  // Stop sending,if we don´t have data to send.
  if (usart1_tx_buffer_size == 0)
    UCSR1B &= ~(1 << UDRIE1);
    
 
}

ISR(USART1_RX_vect)
{
   char data;
   data = UDR1;

   if(counter < cols)
   {
     response[counter] = data;
      
      if(response[counter] == '\r')
      {
        flag = 1;
      }
      else
      {
        ++counter;
      }
   }
}


void Sendtomotor(char string2D[LINE][MAX])
 {
   char stepmotor[MAX] = {0};
   char datamotor[MAX] = {0};
   
   int i, j;

   for(i = 0; i < LINE; i++)
   {
     for(j = 0; j < MAX; j++)
     {
        stepmotor[j] = string2D[i][j];
        datamotor[j] = stepmotor[j];
     }
     
     Deletecaracter(stepmotor);
     USART1_Send(datamotor);
     delay();
     if(flag == 1)  
      {  
       if(strcmp(response, responsemotor) == 0)
       {
         memset(response, 0, sizeof(response)); // string is empty
         memset(responsemotor, 0, sizeof(responsemotor)); // string is empty
         flag = 0;
         counter = 0;
       }
      }
   }
   command_ready = 0;
   
 }

// Delete the caracter '#'
void Deletecaracter(char * string)
{
  int i = 0, j = 0, size = 0;
  size = strlen(string); // Size of string without '\0'

  for(i = 0; string[i] != 0 ; i++)
  {
     if(string[i] == '#')
     {
       for(j = i; j < size; j++)
       {
         string[j] = string[j+1];
         responsemotor[j] = string[j];
       }
     }
  }
}

void Sendingposition(void)
{
  if(command_execution == 1)
    {
      Copytable(table2D, lines, cols);
      command_execution = 0;  
    }
}
 

int main (void)
{
  USART_Init(UBRR_VAL); // Initialisation of USART0 and USART1
  sei(); // Activation of function Interrupt
  Sendtomotor(firststring2D); // Initialisaton of Motor-control
 
  while (1) // Infinite loop
  {
     Sendingposition();    
  }
}
arthurdubois est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 13h09.


 
 
 
 
Partenaires

Hébergement Web