Question
USART don't send what I want
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