cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F10x UART tx hyperterminal

nick2
Associate II
Posted on March 24, 2015 at 14:45

Hello,

I want to sent some characters to a hyperterminal program. But my program wont work..

I dont receive any characters in my hyperterminal. 

I am using the STM32-P107 development kit. The controller on it is the STM32F107VC

I really need help here. I hope someone here see what i am doing wrong. I would really appreciate it. 

This is my code:

/* Includes ------------------------------------------------------------------*/

#include ''stm32f10x.h''

#include ''stm32f10x_gpio.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_usart.h''

#include <stdio.h>

void USART_Print_Nick(void)

{

 int i;

//**************************************RCC Configuration*************************************\

 /* Enable GPIO clock */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC, ENABLE);

 /* Enable UART clock */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

//**************************************GPIO Configuration*************************************\

 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure USART Tx as alternate function push-pull */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;   // PA.02 USART1.TX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Configure USART Rx as input floating */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.03 USART1.RX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 //LED TOGGLE.. initialized // PA.08 = output port for LED

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOC, &GPIO_InitStructure);

//**************************************USART Configuration*************************************\

 USART_InitTypeDef USART_InitStructure;

 USART_InitStructure.USART_BaudRate = 115200;

 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 configuration */

 USART_Init(USART2, &USART_InitStructure);

 /* Enable the USART2 */

 USART_Cmd(USART2, ENABLE);

//******************************************Program**********************************************\

 while(1)

   {

    // 1010.1010

    USART_SendData(USART2, 0xAA);

    //LED toggle

    GPIO_WriteBit(GPIOC, GPIO_Pin_8, 1);

       for(i=0;i<0x100000;i++);

    GPIO_WriteBit(GPIOC, GPIO_Pin_8, 0);

       for(i=0;i<0x100000;i++);

   }

}

15 REPLIES 15
Posted on March 24, 2015 at 15:54

The USART is not compatible with RS232 levels, needs a converter.

You need to wait for TXE to assert before stuffing characters out.

while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, 0x55); // Send Char

while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART2); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, Data); // Echo Char
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
nick2
Associate II
Posted on March 25, 2015 at 08:43

It still dont work here. I really dont know what i am doing wrong. Could you look to my software one more time?

//********************************************************************************************\

//*****************************************Includes*******************************************\

//********************************************************************************************\

 #include ''stm32f10x.h''

 #include ''stm32f10x_gpio.h''

   #include ''stm32f10x_rcc.h''

 #include ''stm32f10x_usart.h''

 #include <stdio.h>

void USART_Print_Nick(void)

{

//********************************************************************************************\

//*****************************************Variabelen*****************************************\

//********************************************************************************************\

 int i;

//********************************************************************************************\

//**************************************RCC Configuration*************************************\

//********************************************************************************************\

 /* Enable GPIO clock */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC, ENABLE);

 /* Enable UART clock */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

//********************************************************************************************\

//**************************************GPIO Configuration************************************\

//********************************************************************************************\

 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure USART Tx as alternate function push-pull */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;   // PA.02 USART1.TX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Configure USART Rx as input floating */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.03 USART1.RX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 //LED TOGGLE.. initialized // PC.08 = output port for LED

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOC, &GPIO_InitStructure);

//  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); //nodig?

//********************************************************************************************\

//**************************************USART Configuration***********************************\

//********************************************************************************************\

 USART_InitTypeDef USART_InitStructure;

 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 configuration */

 USART_Init(USART2, &USART_InitStructure);

 /* Enable the USART2 */

 USART_Cmd(USART2, ENABLE);

//********************************************************************************************\

//******************************************PROGRAM*******************************************\

//********************************************************************************************\

 while(1) // Don't want to exit

 {

   while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty

   USART_SendData(USART2, 0x55); // Send char

       for(i=0;i<0x100000;i++);

 }

}

francescatodiego
Associate II
Posted on March 25, 2015 at 09:25

Add


 USART_SendData(USART2, 0x55); // Send char
 while (1) ;

tx only one 0x55 char _______________________________________


 /* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.03 USART1.RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

floating ? The Rx line is fixed externally ?
nick2
Associate II
Posted on March 25, 2015 at 10:02

Thanks for answer man!

But still dont work :(

Or can you show me the complete software? I added the 

while (1) ;

I just want to put every 50ms (or something) a char to my hyperterminal program to start. After that i will receive character etc. But for now just send every 50 ms a byte (in this case 0x55)

The software is now like this:

//********************************************************************************************\

//*****************************************Includes*******************************************\

//********************************************************************************************\

#include ''stm32f10x.h''

#include ''stm32f10x_gpio.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_usart.h''

#include <stdio.h>

void USART_Print_Nick(void)

{

//********************************************************************************************\

//*****************************************Variabelen*****************************************\

//********************************************************************************************\

 int i;

//********************************************************************************************\

//**************************************RCC Configuration*************************************\

//********************************************************************************************\

 /* Enable GPIO clock */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    //RCC_APB2Periph_AFIO

 /* Enable UART clock */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

//********************************************************************************************\

//**************************************GPIO Configuration************************************\

//********************************************************************************************\

 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure USART Tx as alternate function push-pull */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;   // PA.02 USART2.TX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Configure USART Rx as input floating */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.03 USART2.RX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

//********************************************************************************************\

//**************************************USART Configuration***********************************\

//********************************************************************************************\

 USART_InitTypeDef USART_InitStructure;

 USART_InitStructure.USART_BaudRate = 115200;

 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 configuration */

 USART_Init(USART2, &USART_InitStructure);

 /* Enable the USART2 */

 USART_Cmd(USART2, ENABLE);

//********************************************************************************************\

//******************************************PROGRAM*******************************************\

//********************************************************************************************\

 while(1) // Don't want to exit

 {

   while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty

   USART_SendData(USART2, 0x55); // Send char

 }

 while (1);

}

nick2
Associate II
Posted on March 25, 2015 at 10:06

francescatodiego
Associate II
Posted on March 25, 2015 at 10:20

No !

powerup - config uart - send 0x55 - loop if nothing happen check cable connections Hyperterminal port and config if all ok Can you check the microcontroller tx pin signal ? Oscilloscope ? Logic analyzer ? As says clive have you used the level translator like MAX3232 (for 3.3Vdevice ) or MAX232 (for 5V device) ?

//********************************************************************************************\
//*****************************************Includes*******************************************\
//********************************************************************************************\
#include ''stm32f10x.h''
#include ''stm32f10x_gpio.h''
#include ''stm32f10x_rcc.h''
#include ''stm32f10x_usart.h''
#include <
stdio.h
>
void USART_Print_Nick(void)
{
//********************************************************************************************\
//*****************************************Variabelen*****************************************\
//********************************************************************************************\
int i;
//********************************************************************************************\
//**************************************RCC Configuration*************************************\
//********************************************************************************************\
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //RCC_APB2Periph_AFIO
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//********************************************************************************************\
//**************************************GPIO Configuration************************************\
//********************************************************************************************\
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PA.02 USART2.TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.03 USART2.RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//********************************************************************************************\
//**************************************USART Configuration***********************************\
//********************************************************************************************\
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable the USART2 */
USART_Cmd(USART2, ENABLE);
USART_SendData(USART2, 0x55); // Send char

while (1);
}

nick2
Associate II
Posted on March 25, 2015 at 10:47

Still nothing happens.

I got a other program from a coocox example and that works on USART 2, But now i want make my own USART program with the stm32f10x libary's but i cant get it work.

So the hardware is fine. but there is something in the software what is wrong.

So i hope you or someone else can see what i am doing wrong. ?

Thanks again for answer!

Posted on March 25, 2015 at 11:30

Do you have a debugger or scope, can you use those?

How is this function called? Is it called?

Where is main(), and does it call SystemInit() first?

Can you see any signal on the USART2_TX pin with a scope? Is it at the right baud rate or something else? If so what?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
nick2
Associate II
Posted on March 25, 2015 at 11:46

Hello thanks for answer!

Yes i have a debugger and scope. I measured at the Tx pin. I didn't see any signal on the scope (but i did with the coocox libray uart example. So hardware is tested well. With that program is also received characters at my hyperterminal program).

But now i want do it with the stm32f10x library's but i can't get it work. 

I will show you my main.c and the function

void USART_Print(void);

int main(void)

{

SystemInit();

    //automatically added by CoIDE

USART_Print_Nick();

}

//********************************************************************************************\

//*****************************************Includes*******************************************\

//********************************************************************************************\

#include ''stm32f10x.h''

#include ''stm32f10x_gpio.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_usart.h''

#include <stdio.h>

void USART_Print_Nick(void)

{

//********************************************************************************************\

//*****************************************Variabelen*****************************************\

//********************************************************************************************\

 int i;

 int y = 0xAA;

//********************************************************************************************\

//**************************************RCC Configuration*************************************\

//********************************************************************************************\

 /* Enable GPIO clock */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    //RCC_APB2Periph_AFIO

 /* Enable UART clock */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

//********************************************************************************************\

//**************************************GPIO Configuration************************************\

//********************************************************************************************\

 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure USART Tx as alternate function push-pull */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;      // PA.02 USART2.TX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Configure USART Rx as input floating */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // PA.03 USART2.RX

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

//********************************************************************************************\

//**************************************USART Configuration***********************************\

//********************************************************************************************\

 USART_InitTypeDef USART_InitStructure;

 USART_InitStructure.USART_BaudRate = 115200;

 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 configuration */

 USART_Init(USART2, &USART_InitStructure);

 /* Enable the USART2 */

 USART_Cmd(USART2, ENABLE);

 while (1)

 {

while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty

USART_SendData(USART2, y); // Send char

 }

}