IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Embarqué Discussion :

Communication entre un microcontrôleur et un moteur de commande


Sujet :

Embarqué

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    110
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2012
    Messages : 110
    Points : 27
    Points
    27
    Par défaut Communication entre un microcontrôleur et un moteur de commande
    Bonsoir,

    je desire transferer plusieurs commandes du microcontrôleur jusqu´au moteur de commande. Cependant le moteur de commande retourne toujours une reponse lorsqu´il recoit une commande. Si je desire envoyer 5 commandes au moteur de commande, alors le moteur de commande recevra d´abord la 1er commande, ensuite il retournera la reponse au microcontrôleur. Ensuite on peut passer a l´envoie de la 2ieme commade et le moteur de commande retournera aussi la reponse. Le procede sera pareil pour les autres commandes. Ainsi, le moteur retourne une reponse entre deux commandes. Lorsque le moteur recoit une commande, il est capable de retourner automatiquement une reponse en precisant si oui ou non une commande est valide.

    Exemple: Envoie de la commande-----> "#2A\r"
    Reponse du moteur----------> "2A\r"-----> commande valide
    Reponse du moteur----------> "2A?\r"----> commande invalide

    Explication du protocôle d´une commande: "#2A\r"
    #: debut d´une commande
    2: Nummero du moteur
    A: instruction a effectuer
    \r: fin d´une commande

    PS: J´envoie les commandes au moteur via USART1 et le moteur de commande retourne les reponses via l´USART1

    Actuellement, je n´arrive pas a synchroniser mon ISR(USART1_RX_vect) et mon ISR(USART0_RX_vect). Du moins l´echange de donnees entre le microcontrôleur et le moteur ne marche. Se serait vraiment sympa, si vous me filez un coup de main. merci.

    Mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #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 9.6e6
    #define FOSC 8000000 // Clock Speed
    #define BAUD 115200UL
    
    #define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1)   // clever runden
    
    /* 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<<UDRIE0);
      UCSR0C = (1<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00);
      UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (0<<RXCIE1) | (0<<UDRIE1);
      UCSR1C = (1<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10);
    } 
    
    /**********************************************************/
    
    /* Entree de la file */
    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;
      }
    }
    
    /* Sortie de la file */
    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;
    }
    
    /* Entree de la file */
    void USART1_QueueIn(char c)
    {
      int i;
    
      if (usart1_tx_buffer_size < usart_buffer_max_size)
      {
        i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
        usart1_tx_buffer[i] = c;
        ++usart1_tx_buffer_size;
      }
    }
    
    /* Sortie de la file */
    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;
    }
    
    /* Envoie d´une reponse via la file a 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;
    }
    
    /* Envoie une commande via la file a 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;
    }
    
    ISR(USART0_RX_vect)
    {
     
      USART1_Send("#2A\r"); 
      UCSR1B = (1<<RXCIE1); 
    
      USART1_Send("#2C\r"); 
      UCSR1B = (1<<RXCIE1);
    
      USART1_Send("#2D\r"); 
      UCSR1B = (1<<RXCIE1);
    }
    
    ISR(USART1_RX_vect)
    {
     char stringresponse[30] ;
     stringresponse[30] = UDR1;
    
     switch(stringresponse[30])
     {
       case '2A\r':
       stringresponse[0] = '\0'; // Vider la chaîne
       UCSR1B = (0<<RXCIE1); 
       break;
    
       case '2c\r':
       stringresponse[0] = '\0';
       UCSR1B = (0<<RXCIE1);
       break;
    
      case '2D\r':
      stringresponse[0] = '\0';
      UCSR1B = (0<<RXCIE1);
      break;
    
     default:
     
     }
    }
    
    int main (void)
    { 
      USART_Init(UBRR_VAL); // Initialisation de USART0 et USART1
      sei();  
      while (1)
      {
       	 
      }
      
    }

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    110
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2012
    Messages : 110
    Points : 27
    Points
    27
    Par défaut
    Sorry j´ai omis une fonction d´interruption. Je complete mon code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #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 9.6e6
    #define FOSC 8000000 // Clock Speed
    #define BAUD 115200UL
    
    #define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1)   // clever runden
    
    /* 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<<UDRIE0);
      UCSR0C = (1<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00);
      UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (0<<RXCIE1) | (0<<UDRIE1);
      UCSR1C = (1<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10);
    } 
    
    /**********************************************************/
    
    /* Entree de la file */
    void USART1_QueueIn(char c)
    {
      int i;
    
      if (usart1_tx_buffer_size < usart_buffer_max_size)
      {
        i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
        usart1_tx_buffer[i] = c;
        ++usart1_tx_buffer_size;
      }
    }
    
    /* Sortie de la file */
    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;
    }
    
    
    /* Envoie une commande via la file a 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;
    }
    
    ISR(USART0_RX_vect)
    {
     
      USART1_Send("#2A\r"); 
      UCSR1B = (1<<RXCIE1); 
    
      USART1_Send("#2C\r"); 
      UCSR1B = (1<<RXCIE1);
    
      USART1_Send("#2D\r"); 
      UCSR1B = (1<<RXCIE1);
    }
    
    ISR(USART1_RX_vect)
    {
     char stringresponse[30] ;
     stringresponse[30] = UDR1;
    
     switch(stringresponse[30])
     {
       case '2A\r':
       stringresponse[0] = '\0'; // Vider la chaîne
       UCSR1B = (0<<RXCIE1); 
       break;
    
       case '2c\r':
       stringresponse[0] = '\0';
       UCSR1B = (0<<RXCIE1);
       break;
    
      case '2D\r':
      stringresponse[0] = '\0';
      UCSR1B = (0<<RXCIE1);
      break;
    
     default:
     // ignorer
     }
    }
    
    /* La fonction d´interruption d´envoi de byte */
    /* Cette fonction est active lorsque UDRIE1 = 1 */
    ISR(USART1_UDRE_vect)
    {
      UDR1 = USART1_QueueOut();
      /* S'il n'y a plus de données à envoyer on arrete l'emission */
      if (usart1_tx_buffer_size == 0)
        UCSR1B &= ~(1 << UDRIE1);
    }
    
    
    int main (void)
    { 
      USART_Init(UBRR_VAL); // Initialisation de USART0 et USART1
      sei();  
      while (1)
      {
       	 
      }
      
    }

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    110
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2012
    Messages : 110
    Points : 27
    Points
    27
    Par défaut
    programme:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #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)
      {
        asm volatile("nop");
      }
       _delay_ms(20);
    }
    
    
    
    // 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();    
      }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Echange de donnees entre un microcontrôleur et moteur
    Par arthurdubois dans le forum Embarqué
    Réponses: 3
    Dernier message: 23/01/2013, 15h01
  2. Lecture standard et communication entre processus!
    Par Tartar Ukid dans le forum C++Builder
    Réponses: 5
    Dernier message: 05/07/2003, 16h37
  3. Communication entre processus
    Par markopolo dans le forum C++Builder
    Réponses: 2
    Dernier message: 26/06/2003, 16h21
  4. Réponses: 5
    Dernier message: 25/03/2003, 19h43
  5. communication entre programmes
    Par jérôme dans le forum C
    Réponses: 12
    Dernier message: 16/04/2002, 08h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo