AnsweredAssumed Answered

USART2 does n't work on STM32F302

Question asked by Bont_17 Gtm on Jul 11, 2017
Latest reply on Jul 11, 2017 by Clive One

Hello Everyone,

I am trying to send  string on USART2 of STM32F302 but I am not succeed ,  my Code worked on USART1 and USART3. Does any one help me ?

 

 

#ifdef __GNUC__
   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
      set to 'Yes') calls __io_putchar() */
 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
 //Place your implementation of fputc here
 //e.g. write a character to the USART
 USART_SendData(USART2, (uint8_t) ch);
 //Loop until the end of transmission
 while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
 {}
 return ch;
}

 

 

 void usart1_init (void)

 {
  USART_InitTypeDef  USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Bit configuration structure for GPIOC PIN4 and PIN5 */
     GPIO_InitTypeDef GPIO_InitStructure;
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
         GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_Mode_OUT   GPIO_Mode_AF
         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;    //   GPIO_Speed_Level_2    GPIO_Speed_50MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;             //  GPIO_PuPd_UP;
         GPIO_Init(GPIOA, &GPIO_InitStructure);
 // Configure USART2 setting:
           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);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
           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);
}
int main(void)
{
  SystemInit();
  gpio_init();
  usart1_init ();

  // printf("Hello\n\r");
 while (1)
 {
        printf("BIGS\n\r");
          delay_ms(200);

 }
}

Outcomes