cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART PROBLEM

doganayserhatt
Associate II
Posted on October 02, 2015 at 23:18

Hi guys, I have a problem. I have a USB-RS232 converter and i'll do usart tutorial with STM32F4. Codes are working, but meaningless character viewing at the serial Hyper terminal program.

Baudrate 9600.

Clock Configuration: HSE_VALUE=8000000 and PLL_M=8 as defined. But don't working. There are still meaningless characters.

my problem is here :

http://s23.postimg.org/mbz2h0xm3/2015_10_03_00_05_57_Ekran_g_r_nt_s.png

 I would be glad if you can help..

My code is here:

/*

USART2 example, with IRQ

I am using a CP2102 USB-to-USART converter.

Wiring connections:

STM32F4 CP2102

PA2 (USART2 Tx) -> Rx

PA3 (USART2 Rx) -> Tx

*/

#include <stdio.h>

#include ''stm32f4xx.h''

  char str[50];

volatile uint32_t msTicks; /* counts 1ms timeTicks       */

void SysTick_Handler(void) {

msTicks++;

}

//  Delays number of Systicks (happens every 1 ms)

static void Delay(__IO uint32_t dlyTicks){                                              

  uint32_t curTicks = msTicks;

  while ((msTicks - curTicks) < dlyTicks);

}

void setSysTick(){

// ---------- SysTick timer (1ms) -------- //

if (SysTick_Config(SystemCoreClock / 1000)) {

// Capture error

while (1){};

}

}

void setup_Periph(){

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

// Enable the APB1 periph clock for USART2

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

// Enable the GPIOA clock, used by pins PA2, PA3

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

// Setup the GPIO pins for Tx and Rx

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

// Connect PA2 and PA3 with the USART2 Alternate Function

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);

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_Tx | USART_Mode_Rx;

USART_Init(USART2, &USART_InitStructure);

/* Enable the USART2 receive interrupt and configure

the interrupt controller to jump to USART2_IRQHandler()

if the USART2 receive interrupt occurs

*/

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);

// Finally enable the USART2 peripheral

USART_Cmd(USART2, ENABLE);

}

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

while(*str){

// Wait for the TC (Transmission Complete) Flag to be set

// while(!(USARTx->SR & 0x040));

while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);

USART_SendData(USARTx, *str);

*str++;

}

}

int main(void) {

setSysTick();

setup_Periph();

  sprintf(str,''MAHMUT'');    

USART_puts(USART2, str);

while(1){

// Nothing done here since we are using interrupts

}

return 0;

}

4 REPLIES 4
Posted on October 03, 2015 at 00:05

And does your board, whatever it is, actually have an 8 MHz clock source?

Perhaps you can configure it using the HSI+PLL, as the HSI has a known 16 MHz value.

Have you put a scope on the pins and confirmed the bit timings?

FYI: You should wait for TXE, not TC, and it's str++ not *str++

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
doganayserhatt
Associate II
Posted on October 03, 2015 at 09:52

I have STM32F4 Discovery board. It has a 8 mhz external clock.

I defined (HSE_VALUE=800000) it inside Options for target->C++.

If clock is using 16 mHz, How do I know? 

I changed it, as TXE and str++ but, still not working. The same results.

I looked Keil Debug Logic Analyzer but, I didn't creat it a new signal. I wrote there PORTA.2 or PA.2, but i took unknown signal error in Logic Analyzer.

Posted on October 03, 2015 at 20:18

If your USB-converter has RS232 levels, you need a level converter such as MAX3232 or similar at the mcu too.

JW
doganayserhatt
Associate II
Posted on October 03, 2015 at 21:21

Thank you for answer waclawek.jan. I will take a new USB-TTL converter.. I thought like you.