cancel
Showing results for 
Search instead for 
Did you mean: 

Question on UART and HSE setting on Keli

jjyy1991
Associate II
Posted on January 12, 2013 at 08:08

I am writing the program on Usart but the program is not function well. 

Here is the program:

&sharpinclude <STM32F4xx.h>

&sharpinclude <stdio.h>

&sharpinclude ''stm32f4xx_gpio.h''

&sharpinclude ''stm32f4xx_rcc.h''

&sharpinclude ''stm32f4xx_usart.h''

USART_InitTypeDef UART1_InitS; 

GPIO_InitTypeDef  GPIO_UART1A; 

GPIO_InitTypeDef  GPIO_UART1B; 

uint16_t Uart_ReadByte(void);

void GPIO_Configuration(void);

void Uart_Configuration(void);

void Uart_WriteByte(uint8_t writeByte);

void RCC_Configuration(void); 

void ledon (int i) {

GPIOD->ODR |= ((1UL << (i+12)));

}

void RCC_Configuration(void)

// RCC->AHB1ENR  |= ((1UL <<  3) );         /* Enable GPIOD clock                */

//   GPIOD->MODER = 0x55000000;

// GPIOD->OTYPER==0x0000;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Check the port condition

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,  ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_UART1A.GPIO_Pin =  GPIO_Pin_9;

  GPIO_UART1A.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_UART1A.GPIO_Mode = GPIO_Mode_AF;

GPIO_UART1A.GPIO_OType = GPIO_OType_PP;

GPIO_UART1A.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors

  GPIO_Init(GPIOA,&GPIO_UART1A);

GPIO_UART1B.GPIO_Pin =  GPIO_Pin_10;

  GPIO_UART1B.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_UART1B.GPIO_Mode = GPIO_Mode_IN;

GPIO_UART1B.GPIO_PuPd =  GPIO_PuPd_NOPULL; // enable pull up resistors

  GPIO_Init(GPIOA, &GPIO_UART1B);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1) ;

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1) ;

}

void UART_Configuration(void)

UART1_InitS.USART_BaudRate = 9600;

UART1_InitS.USART_WordLength = USART_WordLength_8b;

UART1_InitS.USART_StopBits = USART_StopBits_1;

  UART1_InitS.USART_Parity = USART_Parity_No;

  UART1_InitS.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

UART1_InitS.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  

USART_Init(USART1, &UART1_InitS);

USART_Cmd(USART1, ENABLE);

}

void Delay(void )

{int i ;

for(i =0; i!= 100000; i++);

}

void Uart_WriteByte(uint8_t writeByte)

{

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

USART_SendData(USART1, (uint8_t)writeByte);

}

uint16_t UART_ReadByte()

{

uint16_t temp =0 ;

while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)

temp =  USART_ReceiveData(USART1);

return (uint16_t) temp & 0x00FF;

}

int main (void) {

  

int loop = 0;

RCC_Configuration();

GPIO_Configuration();

UART_Configuration();

for(loop = 'a'; loop != 'z' +1; loop++)

{Uart_WriteByte(loop); Delay();}

ledon(3);

  

}

Here is the char I have recieved :  ��������

I have read some posts and some of them point out that HSE must be set correctly.

So I have already change the HSE 

In stm32f4xx.h ,

&sharpif !defined  (HSE_VALUE) 

  &sharpdefine HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */

&sharpendif /* HSE_VALUE */

In stm32f4xxconf.h ,

&sharpif defined  (HSE_VALUE)

/* Redefine the HSE value; it's equal to 8 MHz on the STM32F4-DISCOVERY Kit */

 &sharpundef HSE_VALUE

 &sharpdefine HSE_VALUE    ((uint32_t)8000000) 

And the ex

So, my questions are 

1. Are the setting of the USART1 wrong???

2. Is that the problem on setting the external clock??

3.  Are the any setting I have to modify in the Keli project setting???

#don't-just-sit-there---debug!
6 REPLIES 6
Posted on January 12, 2013 at 12:42

''the program is not function well''

That's like saying, ''my car is not running well - what's wrong with it?'' !

In what way(s), exactly, does your program ''not function well''?

What do you expect it to do?

What does it actually do?

What debugging have you done to account for the difference?

Here's some debugging tips:

http://www.8052.com/faqs/120313

http://www.eetimes.com/discussion/break-point/4027526/Developing-a-good-bedside-manner?pageNumber=0

http://bit.ly/VpLzB1

Posted on January 12, 2013 at 15:02

Doesn't look unreasonable, I'd perhaps not waste global resources on initialization structures.

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/DispForm.aspx?ID=1741&RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/F4Discovery%20using%20UART1

You might want to put a scope on the TX pin and confirm the bit timing.

If the baud rate is correct then consider how you have this connected. The CMOS outputs from the STM32F4 are NOT compatible with RS232 levels. You would need a MAX232 level converter or similar part to connect to a PC serial port, or USB-to-Serial dongle. There are adapters which interface at CMOS levels.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jjyy1991
Associate II
Posted on January 12, 2013 at 16:37

Thanks for replying.

1. I have used the 3V Uart to USB converter(5V). So, the voltage level is not a problem.

2. I have used the scope and the waveform looks very unreasonable. 

     It keeps oscillate above 2V and without sending a stop bit or any start bit. 

if you guys really need the photo from scope, I can give you one in next week.

Posted on January 12, 2013 at 18:42

Doesn't the PA9 pin on the STM32F4-Discovery have a really huge bulk capacitor on it? I would suggest you remove that, or consider using a different USART

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jjyy1991
Associate II
Posted on January 13, 2013 at 09:34

Thank you for the suggestion. I have used another Uart4 rather than USART1.

But I have problem on receive uart char.

uint16_t UART_ReadByte()

{

uint16_t temp =0 ;

while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)

temp =  USART_ReceiveData(USART1);

return (uint16_t) temp & 0x00FF;

}

Am I writing the correct thing???

Which should I use the Transmit data register empty or the 

USART_FLAG_RXNE on the receiver sides??

Posted on January 13, 2013 at 14:27

uint16_t UART_ReadByte()

{

uint16_t temp =0 ;

while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)

; // << NOTE SEMICOLON HERE

temp =  USART_ReceiveData(USART1);

return (uint16_t) temp & 0x00FF;

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..