USART Rx problème
Répondre à la discussion
Affichage des résultats 1 à 5 sur 5

USART Rx problème



  1. #1
    amrouch

    USART Rx problème


    ------

    Bonjour a tous,
    J'ai un petit problème avec Rx stm32f407. J'ai essayé de mettre en place USART2, mais je ne peux pas faire recevoir de donné que j’ai envoyé par un émetteur.
    Voilà le code :

    Code:
    static void USART_Configuration(void);
    int main(void)
    { 
    USART_Configuration();
     while (1)
      {   
            if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
            {
           uint16_t x = USART_ReceiveData(USART2);
              switch(x)
                 {
                    case 0xA0A0 :
                     {
                        GPIO_SetBits(GPIOD,GPIO_Pin_14);
                        break;
                      }
                     Case 0xFF00 :
                       {
                         GPIO_SetBits(GPIOD,GPIO_Pin_12);
                         break;
                       }
                      default :
                        GPIO_SetBits(GPIOD,GPIO_Pin_13);
                         break;
      
               }
        }
      }
    }
    
    static void USART_Configuration(void)
    {
    	GPIO_InitTypeDef GPIO_InitStructure;
    	USART_InitTypeDef USART_InitStructure;
    	NVIC_InitTypeDef NVIC_InitStructure;
    
                  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    	      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
                  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
    
     	//  Tx
      	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
      	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // alternate function!
      	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      	GPIO_Init(GPIOA, &GPIO_InitStructure);
    
     	// Rx
      	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
      	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
                  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      	GPIO_Init(GPIOA, &GPIO_InitStructure);
                  //Led
                  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_12 | GPIO_Pin_13;
      	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // alternate function!
      	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      	GPIO_Init(GPIOD, &GPIO_InitStructure);
            
      	USART_InitStructure.USART_BaudRate = 9600;
      	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      	USART_InitStructure.USART_StopBits = USART_StopBits_1;
      	USART_InitStructure.USART_Parity = USART_Parity_No;
      	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    	USART_Init(USART2, &USART_InitStructure);
    
    	USART_Cmd(USART2, ENABLE);
    
    	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    
      	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
      	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
      	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
      	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
     	NVIC_Init(&NVIC_InitStructure);
     	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    }
    Penser à ajouter les balises codes pour faciliter la lisibilité

    -----
    Dernière modification par Jack ; 24/05/2012 à 10h28. Motif: Ajout balises code

  2. #2
    pixtache

    Re : USART Rx problème

    Bonjour,

    Je ne sais pas si c'est le seul problème mais il te manque la clock de l'AFIO pour commencer.

    Ensuite même si cela était bon, je ne comprend pas pourquoi tu configure le NVIC alors que tu n'utilises pas d'interruption.

    L'utilisation de l'interruption est la meilleure solution à mon sens.

    Tu as un exemple à télécharger ici : http://www.keil.com/download/docs/359.asp si tu utilise Keil

    Bon courage.

  3. #3
    amrouch

    Re : USART Rx problème

    Bonsoir,
    merci bien pour la réponse,en faite j'ai essayé de mettre l'horloge AFIO mais ça marche pas
    voila la commande que j'ai utilisé RCC_AHB1PeriphClockCmd(RCC_AHB 1Periph_AFIO,ENABLE);
    je suis entrain d'envoyé la lettre 'A' a l'aide d'un stm32f100rb en utilisant l'USART2 alors j'ai fait une liaison entre le PA2 de l'stm32f1
    et le PA3 de l'stm32F4.
    j'ai tester l' Emission par un oscilloscope et ça marche!!le problème est au niveau de la réception!!!!
    je veut tester le programme tout d'abord sans interruption si c'est bon je passe a l'interruption!!
    j'espère que vous m'aider pour trouver la bonne solution...
    merci

  4. #4
    amrouch

    Re : USART Rx problème

    voila le nouveau code :

    static void USART_Configuration(void);
    int main(void)
    {
    USART_Configuration();
    while (1)
    {
    while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);

    Uint8_t x = USART_ReceiveData(USART2);


    if(x== ‘A’)
    {
    GPIO_SetBits(GPIOD,GPIO_Pin_14 );

    }

    else
    GPIO_SetBits(GPIOD,GPIO_Pin_13 );


    }
    }

    static void USART_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB 1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB 1Periph_GPIOA | RCC_AHB1Periph_GPIOD,ENABLE);


    // Tx
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // alternate function!
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Rx
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //Led
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // alternate function!
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    USART_InitStructure.USART_Baud Rate = 9600;
    USART_InitStructure.USART_Word Length = USART_WordLength_8b;
    USART_InitStructure.USART_Stop Bits = USART_StopBits_1;
    USART_InitStructure.USART_Pari ty = USART_Parity_No;
    USART_InitStructure.USART_Hard wareFlowControl = USART_HardwareFlowControl_None ;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART2, &USART_InitStructure);

    USART_Cmd(USART2, ENABLE);
    }
    Dernière modification par amrouch ; 25/05/2012 à 20h19.

  5. A voir en vidéo sur Futura
  6. #5
    invitef1a530a3

    Re : USART Rx problème

    @Amrouch
    Bsr , t'as résolu ton problème ? je suis exactement entrain de faire la même chose que toi avec la carte STM32F4 , J'ai essayé plusieurs d'allumer les LD3.4.5.et 6 via l'hyperterminal mais rien :/

Discussions similaires

  1. probleme avec usart de mikroc
    Par radiadad dans le forum Électronique
    Réponses: 1
    Dernier message: 17/04/2012, 16h53
  2. [Programmation C] Problème réception USART
    Par invitee4337884 dans le forum Électronique
    Réponses: 5
    Dernier message: 13/04/2011, 21h57
  3. Probleme USART 16f887
    Par invite591e1a94 dans le forum Électronique
    Réponses: 1
    Dernier message: 29/01/2011, 16h15
  4. probleme usart, pic16f627a
    Par invite8ac507df dans le forum Électronique
    Réponses: 29
    Dernier message: 12/01/2011, 17h36
  5. Probleme USART PIC16F877
    Par invite2d9e7c03 dans le forum Électronique
    Réponses: 4
    Dernier message: 27/08/2009, 10h49
Découvrez nos comparatifs produits sur l'informatique et les technologies.