This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USART

Hi I'm having issue getting the data send from the usart1; I initialised properly, but the problem is not the rightdata is display on my PC terminal; I'm using stm32f407VG

#include "stm32f4xx.h"
#include <stdio.h>

void init_GPIO(void){


        GPIO_InitTypeDef GPIO_InitStruct;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12; // we want to configure all LED GPIO pins
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;              // we want the pins to be an output
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;   // this sets the GPIO modules clock speed
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;     // this sets the pin type to push / pull (as opposed to open drain)
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;   // this sets the pullup / pulldown resistors to be inactive
        GPIO_Init(GPIOD, &GPIO_InitStruct);                         // this finally passes all the values to the GPIO_Init function which takes care of setting the corresponding bits.



        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;            // we want to configure PA0
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;         // we want it to be an input
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;//this sets the GPIO modules clock speed
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;   // this sets the pin type to push / pull (as opposed to open drain)
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;   // this enables the pulldown resistor --> we want to detect a high level
        GPIO_Init(GPIOA, &GPIO_InitStruct);                   // this passes the configuration to the Init function which takes care of the low level stuff
}

void initUART (uint32_t baud){



 GPIO_InitTypeDef GPIO_InitStructureTx;
 GPIO_InitTypeDef GPIO_InitStructureRx;
 USART_InitTypeDef USART_InitStructure;

 // Enable Clocks //
        //USART_Cmd(USART1, DISABLE);
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);  // Enable clocks
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

 GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

 GPIO_InitStructureTx.GPIO_Pin = GPIO_Pin_6;                // Configure TX pin
 GPIO_InitStructureTx.GPIO_Mode = GPIO_Mode_AF;              // Push-Pull
 GPIO_InitStructureTx.GPIO_OType = GPIO_OType_PP;              // Push-Pull
 GPIO_InitStructureTx.GPIO_Speed = GPIO_Speed_2MHz;;              // 2MHz
 GPIO_Init(GPIOB, &GPIO_InitStructureTx);                // Initialise

 GPIO_InitStructureRx.GPIO_Pin = GPIO_Pin_7;               // Configure RX pin
 GPIO_InitStructureRx.GPIO_Mode = GPIO_Mode_IN;             // Push-Pull
 GPIO_InitStructureRx.GPIO_Speed = GPIO_Speed_2MHz;              // 2MHz
 GPIO_Init(GPIOB, &GPIO_InitStructureRx);                // Initialise

 USART_InitStructure.USART_BaudRate = baud;                // Set USART1 Baud Rate
 USART_InitStructure.USART_WordLength = USART_WordLength_8b;            // Set USART1 Word Length
 USART_InitStructure.USART_StopBits = USART_StopBits_1;             // Configure USART1 Stop Bits
 USART_InitStructure.USART_Parity = USART_Parity_No;              // Configure USART1 Parity Bits
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;       // Configure USART1 Flow Control
 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;           // Set USART1 Operation Mode

 USART_Init(USART1, &USART_InitStructure);                // Configure UART
 USART_Cmd(USART1, ENABLE);                    // Enable UART

}

void USART_puts(USART_TypeDef* USARTx, volatile char *s){

        while(*s){
                // wait until data register is empty
                while( !(USARTx->SR & 0x00000040) );
                USART_SendData(USARTx, *s);
                *s++;
        }
}

void Delay(__IO uint32_t nCount)
{
  while(nCount--) {
  }
}

int main()
{
        uint32_t brrrr=650;
        //uint32_t brrrr=0;
        uint32_t baud=1600;
        //printf("4");
        init_GPIO();
        initUART (1600);
        //USART_Configuration();
        USART_puts(USART1, "Garlir");
        while(1)
        {
                //GPIOD->BSRRL = 0xF000; // set PD12 thru PD15
                        //GPIOD->ODR=1<<13;
//              if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))
//              {
//
//              }
                GPIO_SetBits(GPIOD, GPIO_Pin_13);
                //USART_puts(USART1, "Garlir");
                USART_SendData(USART1, 'P');
                Delay(38000);
                                GPIO_ResetBits(GPIOD, GPIO_Pin_13);
                //USART_puts(USART1, "Ga");
                USART_SendData(USART1, 'L');
                Delay(38000);
                //USART1->BRR=52.125;

                //baud=baud+300;
                //initUART (baud);
                brrrr=brrrr+1;
                USART1->BRR=brrrr;
                Delay(38000);

        }
}



0