Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1549 connectés 

  FORUM HardWare.fr
  Hardware
  Mini PC

  [C][Probleme de synchronisation d´envoie de donnees]

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C][Probleme de synchronisation d´envoie de donnees]

n°8563342
arthurdubo​is
King Arthur
Posté le 28-12-2012 à 12:09:34  profilanswer
 

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. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdint.h>
  7. #include <util/delay.h>
  8. #define F_CPU 3686400UL
  9. #define FOSC 7372000 // Clock Speed
  10. #define BAUD 9600UL
  11. #define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1)   // clever runden
  12. ///////////////////////////////////////////////////////////////////
  13. #define usart_buffer_max_size 16u
  14. #define usart_command_max_size 16u
  15. #define ROW 200
  16. #define COLUMN 12
  17. #define MAX 10
  18. ///////////////////////////////////////////////////////////////////
  19. char Table_2D[ROW][COLUMN]; // table in dimensional two
  20. char Table_1D[COLUMN] = {0}; // table in dimensional one
  21. char motor_command[COLUMN];
  22. char usart0_tx_buffer[usart_buffer_max_size];
  23. char usart1_tx_buffer[usart_buffer_max_size];
  24. volatile uint8_t usart0_tx_buffer_size = 0;
  25. volatile uint8_t usart0_tx_buffer_start = 0;
  26. volatile uint8_t usart1_tx_buffer_size = 0;
  27. volatile uint8_t usart1_tx_buffer_start = 0;
  28. volatile uint8_t command_execution = 0;
  29. volatile uint8_t command_start = 0;
  30. volatile uint8_t COMMAND_SIZE = 0;
  31. volatile uint8_t command_ready = 0;
  32. /////////////////////////////////////////////////////////////////////
  33. int i = 0; // Number of elements in a row
  34. int j = 0; // Number of rows
  35. int N = sizeof(Table_2D) / sizeof(Table_2D[0]); // Number of rows
  36. int M = sizeof(Table_2D) / sizeof(Table_2D[j][0]); // Number elements of a row
  37. /////////////////////////////////////////////////////////////////////
  38. // Configuration USART0, USART1 and setting Baudrate
  39. void USART_Init(unsigned int ubrr)
  40. {
  41.   UBRR0H = (unsigned char)(ubrr>>8);
  42.   UBRR0L = (unsigned char) ubrr;
  43.   UBRR1H = (unsigned char)(ubrr>>8);
  44.   UBRR1L = (unsigned char) ubrr;
  45.   UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0);
  46.   UCSR0C = (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00); // 1 Stopbit, data = 8 Bits
  47.   UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (0<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1);
  48.   UCSR1C = (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10); // 1 Stopbit, data = 8 Bits
  49. }
  50. /////////////////////////////////////////////////////////////////////
  51. // Entry of circular buffer for USART0
  52. void USART0_QueueIn(char c)
  53. {
  54.   int i;
  55.   if (usart0_tx_buffer_size < usart_buffer_max_size)
  56.   {
  57.     i = (usart0_tx_buffer_size + usart0_tx_buffer_start) % usart_buffer_max_size;
  58.     usart0_tx_buffer[i] = c;
  59.     ++usart0_tx_buffer_size;
  60.   }
  61.  
  62. }
  63. // Exit of circular buffer for USART0
  64. char USART0_QueueOut(void)
  65. {
  66.   char c;
  67.   if (usart0_tx_buffer_size == 0)
  68.     return 0;
  69.   c = usart0_tx_buffer[usart0_tx_buffer_start];
  70.   --usart0_tx_buffer_size;
  71.   usart0_tx_buffer_start = (usart0_tx_buffer_start + 1) % usart_buffer_max_size;
  72.   return c;
  73. }
  74. // Entry of circular buffer for USART1
  75. void USART1_QueueIn(char c)
  76. {
  77.   int i;
  78.   if (usart1_tx_buffer_size < usart_buffer_max_size) // Circular buffer is not full
  79.   {
  80.     i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
  81.     usart1_tx_buffer[i] = c;
  82.     ++usart1_tx_buffer_size;
  83.   }
  84.  
  85. }
  86. // Exit of circular buffer for USART1
  87. char USART1_QueueOut(void)
  88. {
  89.   char c;
  90.   if (usart1_tx_buffer_size == 0)
  91.     return 0;
  92.   c = usart1_tx_buffer[usart1_tx_buffer_start];
  93.   --usart1_tx_buffer_size;
  94.   usart1_tx_buffer_start = (usart1_tx_buffer_start + 1) % usart_buffer_max_size;
  95.   return c;
  96. }
  97. // Sending response via circular buffer to the USART0
  98. static void USART0_Send(const char *s)
  99. {
  100.   int i;
  101.   for (i = 0; s[i] != 0; ++i)
  102.     USART0_QueueIn(s[i]);
  103.   if (usart0_tx_buffer_size > 0)
  104.     UCSR0B |= 1 << UDRIE0;
  105. }
  106. // Sending a command via circular buffer to the USART1
  107. static void USART1_Send(const char *s)
  108. {
  109.   int i;
  110.   for (i = 0; s[i] != 0; ++i)
  111.     USART1_QueueIn(s[i]);
  112.   if (usart1_tx_buffer_size > 0)
  113.     UCSR1B |= 1 << UDRIE1;
  114. }
  115. // Treatment of command
  116. static void ProcessCommand(void)
  117. {
  118.   int i;
  119.   int k;
  120.   int x;
  121.   int y;
  122.   int pos_x;
  123.   int pos_y;
  124.   char x_motor[12];
  125.   char y_motor[12];
  126.      for (i = 0; i < COMMAND_SIZE; ++i)
  127.        if (motor_command[i] == ',')
  128.          break;
  129.      if (i <= 0 || i >= COMMAND_SIZE - 1)
  130.      {
  131.  
  132.        USART0_Send("\x15" );  // NAK, Sending didn´t work.
  133.        COMMAND_SIZE = 0;
  134.        return;
  135.      }
  136.   // Conversion of x and y in integer for the execution of calculation,if it is necessary
  137.   // Extraction of X and Y
  138.    
  139.        motor_command[i] = 0;
  140.        motor_command[COMMAND_SIZE] = 0;
  141.        x = atoi(motor_command);
  142.        y = atoi(motor_command + i + 1);
  143.        COMMAND_SIZE = 0; 
  144.     // Conversion in stepping motor
  145.        pos_x= x * 127/500;
  146.        pos_y= y * 127/500;
  147.     // Conversion in ascii
  148.        itoa(pos_x, x_motor, 10);
  149.        itoa(pos_y, y_motor, 10);
  150.        if(command_start == 0)
  151.    {
  152.           // Sending position x_moteur
  153.          USART1_Send("#1s" );
  154.          USART1_Send(x_motor);
  155.          USART1_Send("\r" );
  156.        }
  157.    if(command_start == 1)
  158.    {
  159.          USART1_Send("#1A\r" ); // the plate can move in axis x
  160.        }
  161.   
  162.    if(command_start == 2)
  163.    {
  164.                // Sending of position y_moteur
  165.          USART1_Send("#2s" );
  166.          USART1_Send(y_motor);
  167.          USART1_Send("\r" );
  168.        }
  169.    if(command_start == 3)
  170.    {
  171.          USART1_Send("#2A\r" ); // the plate can move in axis y
  172.    }
  173.  
  174.     wait(3000); //wait 3000ms ---> 3s
  175.  
  176. }
  177. ISR(USART1_RX_vect)
  178. {
  179. char response[MAX] = {0};
  180. int n,m;
  181. for(n = 0; n < MAX; n++)
  182. {
  183.   response[n] = UDR1;
  184. }
  185.  
  186.     if(strcmp(response, "1D0\r" ) == 0)
  187.     {
  188.       for(m = 0; m < MAX; m++)
  189.   {
  190.         response[m] = '\0'; // String is empty
  191.   }
  192.      command_ready = 1;
  193.     }
  194.     if(strcmp(response, "2D0\r" ) == 0)
  195.     {
  196.       for(m = 0; m < MAX; m++)
  197.   {
  198.         response[m] = '\0'; // String is empty
  199.   }
  200.    command_ready = 2;
  201.     }
  202.     if(strcmp(response, "1p2\r" ) == 0)
  203.     {
  204.      for(m = 0; m < MAX; m++)
  205.   {
  206.         response[m] = '\0'; // String is empty
  207.   }
  208.     command_ready = 3;
  209.     }
  210.     if(strcmp(response, "2p2\r" ) == 0)
  211.     {
  212.      for(m = 0; m < MAX; m++)
  213.   {
  214.         response[m] = '\0'; // String is empty
  215.   }
  216.    command_ready = 0;
  217.     }
  218.     if(strcmp(response, "1sx_motor\r" ) == 0)
  219.     {
  220.      for(m = 0; m < MAX; m++)
  221.   {
  222.         response[m] = '\0'; // String is empty
  223.   }
  224.     command_start = 1;
  225.     }
  226.     if(strcmp(response, "1A\r" ) == 0)
  227.     {
  228.       for(m = 0; m < MAX; m++)
  229.   {
  230.         response[m] = '\0'; // String is empty
  231.   }
  232.  
  233.     }
  234. if(strcmp(response, "Ziel erreicht" ) == 0)
  235. {
  236.   for(m = 0; m < MAX; m++)
  237.   {
  238.         response[m] = '\0'; // String is empty
  239.   }
  240.     command_start = 2;
  241. }
  242.     if(strcmp(response, "1sy_motor\r" ) == 0)
  243.     {
  244.      for(m = 0; m < MAX; m++)
  245.   {
  246.         response[m] = '\0'; // String is empty
  247.   }
  248.      command_start = 3;
  249.     }
  250.     if(strcmp(response, "2A\r" ) == 0)
  251.     {
  252.       for(m = 0; m < MAX; m++)
  253.   {
  254.         response[m] = '\0'; // String is empty
  255.   }
  256.     }
  257.    
  258. if(strcmp(response, "Ziel erreicht" ) == 0)
  259. {
  260.   for(m = 0; m < MAX; m++)
  261.   {
  262.         response[m] = '\0'; // String is empty
  263.   }
  264.     command_start = 0;
  265.  }
  266. }
  267. // function wait
  268. void wait(unsigned int time)
  269. {
  270.   unsigned int counter = 20;
  271.     for(int i = 0; i < counter; i++)
  272.     {
  273.       _delay_ms(time);
  274.     }
  275. }
  276. // function receive interrupt of Byte
  277. // the fuction receice interrupt is High, if RXCIE0 = 1  
  278. ISR(USART0_RX_vect)
  279. {
  280.   char data;
  281.   data = UDR0;
  282. // Reception of command -->("D X,Y F" )
  283. // X = x_motor and Y = y_motor
  284.   if (data == 'F')
  285.   {           
  286.     ++j;
  287.     USART0_Send("\x06" ); // ACK ---> PC can send next data
  288.  
  289.    }
  290.    else if (data == 'D')
  291.    {
  292.     // Generation of a new command
  293.     i = 0;
  294.    }
  295.  
  296.   else
  297.   {
  298.     // If we not receive  'F' or 'D', we store
  299.     // the positions of coordinate in a table
  300.  
  301.     if(j < ROW)
  302.     {
  303.       if(i < COLUMN)
  304.       {
  305.         Table_2D[j][i] = data;
  306.         ++i;
  307.       }
  308.    
  309.      }
  310.      else
  311.      {
  312.        //USART0_Send("\x04" );// EOT --> PC must wait a few minuts   
  313.       // j = 0;
  314.      } 
  315.    }
  316.   command_execution = 1; // Sending of data begin to USART1
  317. }
  318. void Copytable(char Originaltable2D[N][M], char Copytable1D[], int N1, int M1)
  319. {
  320.   int j1;
  321.   int i1;
  322.   for(j1 = 0; j1 < N1; j1++)
  323.   {
  324.     for(i1 = 0; i1 < M1; i1++)
  325.     {
  326.       Copytable1D[i1] = Originaltable2D[j1][i1];
  327.       motor_command[COMMAND_SIZE] = Copytable1D[i1];
  328.       ++COMMAND_SIZE;
  329.     }
  330.     ProcessCommand();
  331.   }
  332. }
  333. // The function interrupt for sending of Byte
  334. // The function interrupt is active, if UDRIE0 = 1
  335. ISR(USART0_UDRE_vect)
  336. {
  337.   UDR0 = USART0_QueueOut();
  338.   // Stop sending,if we don´t have data to send.
  339.   if (usart0_tx_buffer_size == 0)
  340.     UCSR0B &= ~(1 << UDRIE0);
  341. }
  342. // The function interrupt for sending of Byte
  343. // The function interrupt is active, if UDRIE1 = 1
  344. ISR(USART1_UDRE_vect)
  345. {
  346.   UDR1 = USART1_QueueOut();
  347.   // Stop sending,if we don´t have data to send.
  348.   if (usart1_tx_buffer_size == 0)
  349.     UCSR1B &= ~(1 << UDRIE1);
  350. }
  351. void ConfigurationMotor(void)
  352. {
  353.    //USART1_Send("#1|0\r" ); // Deactivation of response motor-control Nr 1
  354.    //USART1_Send("#2|0\r" ); // Deactivation of response motor-control Nr 2
  355.    //USART1_Send("#1:baud=7" ); // Baudrate = 9600
  356.    //USART1_Send("#2:baud=7" ); // Baudrate = 9600
  357.    USART1_Send("#1D0\r" ); // The actual position of plate is the origin in axis x
  358.                                                                                                                                                                                                                                  
  359. }
  360. int main (void)
  361. {
  362.   sei(); // Activation of function Interrupt
  363.   USART_Init(UBRR_VAL); // Initialisation of USART0 and USART1
  364.   ConfigurationMotor(); // Initialisaton of Motor-control
  365.  
  366.  
  367.  
  368.   while (1) // Infinite loop
  369.   {
  370.    
  371.     if(command_ready == 1)
  372. {
  373.       USART1_Send("#2D0\r" ); // The actual position of plate is the origin in axis y
  374. }
  375. if(command_ready == 2)
  376. {
  377.       USART1_Send("#1p2\r" ); // Mode: Positioning absolut --> p=2
  378. }
  379. if(command_ready == 3)
  380. {
  381.       USART1_Send("#2p2\r" ); // Mode: Positioning absolut --> p=2  
  382. }
  383.  
  384.  
  385.  if(command_execution == 1)
  386.     {
  387.       Copytable(Table_2D, Table_1D, ROW, COLUMN);
  388.       command_execution = 0; 
  389.     }
  390.        
  391.   }
  392. }

mood
Publicité
Posté le 28-12-2012 à 12:09:34  profilanswer
 

n°8563994
master71
ça manque de place.
Posté le 28-12-2012 à 21:01:43  profilanswer
 

euh... y a sûrement des forums développemenjt logiciel plus adaptés pour te répondre...
ici c'est le rayon MINI-PC, pas le rayon micro-contrôleur...


---------------
un jour, moi aussi, je serais grand...
n°8564023
arthurdubo​is
King Arthur
Posté le 28-12-2012 à 21:20:21  profilanswer
 

Okay, j´ai compris !!


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Hardware
  Mini PC

  [C][Probleme de synchronisation d´envoie de donnees]

 

Sujets relatifs
Problème de démarrage - Bios - Marvell - No hard disk is detectedASUS M3N78Pro problème SATA
Recuperation de données et HD Win7 crypté (bit locker)problème écran plasma
Problème Ati Radeon HD 5870[Résolu] Problème démarrage pc gamers
Besoin d'aide pour identifier un problèmeProblème pour retirer un disque dur externe
Problème carte graphie / Ecran / RéglageProblème Disque dur : URGENT
Plus de sujets relatifs à : [C][Probleme de synchronisation d´envoie de donnees]


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR