AnsweredAssumed Answered

USART problem on STM32L DISCOVERY

Question asked by Luca85 on Feb 9, 2014
Latest reply on Aug 27, 2015 by clift.michael
Hi, 
I have a problem with the USART... basically i just want to sent out one integer (i will use a FTDI adapter to read it).  unfortunately my code get stuck to the instruction .

while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

from debugging i noticed that the TXE bit is always 0.
Here more detailed information of the registers values: 
SR register = 0;
CR1 register = 8204;
CR2 register = 0;
CR3 register = 0;

Here there is my full code

#include <stm32l1xx_usart.h>
#include <stm32l1xx.h>
#include <stm32l1xx_rcc.h>
#include <stm32l1xx_gpio.h>
#include <misc.h>




void initgpio(void){


     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);


     GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource10, GPIO_AF_USART1);






     //initialize pin A9 (Tx)    and    pin A10 (RX) as AF
     GPIO_InitTypeDef gpiousart;
     gpiousart.GPIO_Pin =GPIO_Pin_9 | GPIO_Pin_10;
     gpiousart.GPIO_Speed = GPIO_Speed_2MHz;
     gpiousart.GPIO_Mode = GPIO_Mode_AF;
     gpiousart.GPIO_OType= GPIO_OType_PP;
     gpiousart.GPIO_PuPd = GPIO_PuPd_NOPULL;


     GPIO_Init(GPIOA,&gpiousart);


}


void usartinit(void){


USART_InitTypeDef USARTuna;
USARTuna.USART_BaudRate= 9600;
USARTuna.USART_StopBits = USART_StopBits_1;
USARTuna.USART_Parity = USART_Parity_No;
USARTuna.USART_WordLength = USART_WordLength_8b;
USARTuna.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USARTuna.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USARTuna);


USART_Cmd(USART1, ENABLE);




}




int main(void)
{


     
     initgpio();
     usartinit();


          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
          //initialize pin B6 led
          GPIO_InitTypeDef gpioled;
          gpioled.GPIO_Pin =GPIO_Pin_6;
          gpioled.GPIO_Speed = GPIO_Speed_2MHz;
          gpioled.GPIO_Mode = GPIO_Mode_OUT;
          gpioled.GPIO_OType = GPIO_OType_PP;
          GPIO_Init(GPIOB,&gpioled);


     volatile int j;
     char k=0;




     for (j = 0; j<10000; j++);


    while(1)
    {


         while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
         USART_SendData(USART1, k);


         GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
         for (j = 0; j<10000; j++);
         GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET);
         for (j = 0; j<10000; j++);
         k++;
    }
}


The FOR cycles are delays... 
Any idea why this happens???? 

Outcomes