cancel
Showing results for 
Search instead for 
Did you mean: 

USART don't send what I want

manespgav
Associate II
Posted on April 04, 2013 at 22:20

First of all, sorry for my bad english and possibles gramatical mistakes;

I'm working on stm32f100rb (Discovery Board). Well, when I want to sent a Ascii Character, for example, ''A'', I use the next command:USART_SendData(USART1, 0x65); I use PA9 for Tx and PA10 for Rx. To read Tx, I use the Rx channel of a Rs232 to Usb. When I run the code on the stm32f1, I don't Receive the correct char on Tera Term. Here is mine code:

// STM32 USART1 (Tx PA.9, Rx PA.10) - STM32 VLDiscovery - sourcer32@gmail.com
#include ''stm32F10x.h''
#include ''STM32vldiscovery.h''
#include ''stm32f10x_usart.h''
#include ''stm32f10x_rtc.h''
#include ''stm32f10x_bkp.h''
#include <stdio.h>
/********Private Variables ********/
extern
__IO uint32_t TimingDelay;
ErrorStatus HSEStartUpStatus;
__IO uint32_t i=0;
GPIO_InitTypeDef GPIO_InitStructure;
/**************************************************************************************/
void
RCC_Configuration(
void
);
void
RTC_Configuration(
void
);
void
GPIO_Configuration(
void
);
void
USART_Configuration(
void
);
void
SYSCLKConfig_STOP(
void
);
void
EXTI_Configuration(
void
);
void
RTC_Configuration(
void
);
void
NVIC_Configuration(
void
);
void
SysTick_Configuration(
void
);
void
Delay(__IO uint32_t nTime);
/**************************************************************************************/
int
main(
void
)
{
unsigned 
char
text[] = 
''''
;
unsigned 
int
i, len;
RCC_Configuration();
GPIO_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
EXTI_Configuration();
USART_Configuration();
RTC_Configuration();
NVIC_Configuration();
SysTick_Configuration();
while
(1)
{
while
(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
// Wait for Empty
USART_SendData(USART1, 
'U'
);
// len = strlen(text);
//USART_SendData(USART1, 0x1A); // Send 'I'
//USART1->DR = (0x97 & (uint16_t)0x01FF);
Delay(100);
/* for(i=0; i<len; i++)
{
Delay(100);
while (!(USART1->SR & USART_FLAG_TXE));
USART1->DR = text[i];
}
Delay(1000);
*/
}
}
/**************************************************************************************/
void
RCC_Configuration(
void
)
{
// clock for GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
/**************************************************************************************/
void
GPIO_Configuration(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
// PA.09 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_10; 
// PA.10 USART1.RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************************/
void
USART_Configuration(
void
)
{
USART_InitTypeDef USART_InitStructure;
/* USART configured as follow:
- BaudRate = 9600 //115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600; 
//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(USART1, &USART_InitStructure);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
void
SYSCLKConfig_STOP(
void
)
{
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if
(HSEStartUpStatus == SUCCESS)
{
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while
(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while
(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
void
EXTI_Configuration(
void
)
{
EXTI_InitTypeDef EXTI_InitStructure;
/* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void
RTC_Configuration(
void
)
{
// RTC clock source configuration ------------------------------------------
// Allow access to BKP Domain 
PWR_BackupAccessCmd(ENABLE);
//Reset Backup Domain 
BKP_DeInit();
// Enable the LSE OSC 
RCC_LSEConfig(RCC_LSE_ON);
// Wait till LSE is ready 
while
(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
// Select the RTC Clock Source 
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
// Enable the RTC Clock 
RCC_RTCCLKCmd(ENABLE);
// RTC configuration -------------------------------------------------------
// Wait for RTC APB registers synchronisation 
RTC_WaitForSynchro();
// Set the RTC time base to 1s 
RTC_SetPrescaler(32767); 
// Wait until last write operation on RTC registers has finished 
RTC_WaitForLastTask();
// Enable the RTC Alarm interrupt 
RTC_ITConfig(RTC_IT_ALR, ENABLE);
// Wait until last write operation on RTC registers has finished 
RTC_WaitForLastTask();
}
void
NVIC_Configuration(
void
)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 2 bits for Preemption Priority and 2 bits for Sub Priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Configures the SysTick to generate an interrupt each 1 millisecond.
* @param None
* @retval None
*/
void
SysTick_Configuration(
void
)
{
/* Setup SysTick Timer for 1 msec interrupts */
if
(SysTick_Config(SystemCoreClock / 1000))
{ 
/* Capture error */
while
(1);
}
/* Set SysTick Priority to 3 */
NVIC_SetPriority(SysTick_IRQn, 0x0C);
}
void
Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while
(TimingDelay != 0);
}

Somebody knows how to fix this problem? Thanks you in advance. #prolem #think! #tx #rx #usart
8 REPLIES 8
emalund
Associate III
Posted on April 04, 2013 at 22:28

When I run the code on the stm32f1, I don't Receive the correct char on Tera Term.

 

I ALWAYS, when an UART is involved transmit 'U' (capital U (0x55) an put a scope on the TX.

transmitting 'U' produces a nice square wave with a frequency of 1/2 the baudrate. AH with one stop bit, not two.

this, about a year ago led me to discover a bug in the STM32 supplied code the system clock was running half as fast as the parameters specified.

Erik
Posted on April 04, 2013 at 22:32

Did the original example work? What characters did you see?

If you didn't see any, you should perhaps review how you have things connected. Use a scope and check you can see a signal, and that is correctly formed, and the bit times are consistent with the chosen baud rate.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
manespgav
Associate II
Posted on April 04, 2013 at 22:52

I receive chars, but not what i want.

I am sendig 'U' and i will see in the oscilloscope, i will upload a image with the results.

When I send 'U' I receive ''U'' in tera term, but when I send ''A'', I  receive ''_'' in tera term, For 'B' sent, i receive ''/'', etc...

Andrew Neil
Evangelist
Posted on April 05, 2013 at 00:23

''i will see in the oscilloscope, i will upload a image with the results''

 

Do take time to think about what you see before uploading it

''When I send 'U' I receive 'U' in tera term, but when I send 'A', I  receive '_' in tera term, For 'B' sent, i receive '/', etc...''

 

To say ''etc'' implies that there is a regular pattern - so think about what that pattern could be; and think about what might cause it...

Hint: Draw the waveform for the ''sent'' character, and the waveform for the ''received'' character - and compare them...

The most important tool for debugging is the one between your ears...

manespgav
Associate II
Posted on April 05, 2013 at 11:48

Thanks a lot mate! As you just said, I realised what the error is.

Will try using a TTL Usart USB device.

This is my oscilloscope's capture:

0690X00000602lAQAQ.jpg

Posted on April 05, 2013 at 13:11

Use a proper level convertor (MAX3232 and similar).

JW

PS. We see this kind of error quite a lot here (it was quite regular on 8052.com in its heydays, too, IIRC; and I guess this will be quite common in other fora dealing with mcus). A canned answer with links would be nice. http://retired.beyondlogic.org/serial/serial1.htm#40 could be one of them,  http://hw-server.com/rs-232-overview-rs-232-standard is more extensive but also harder to read. There surely are others; please suggest and comment.

manespgav
Associate II
Posted on April 05, 2013 at 13:34

Thanks a lot! Trying with a TTL USB this evening.

SOLVED.

emalund
Associate III
Posted on April 05, 2013 at 23:09

your chip may be blown.  3V3 pins do not like +-12 and the receive pin has been exposed.

Erik