cancel
Showing results for 
Search instead for 
Did you mean: 

Problem receiving data on USART1

johnjohn9104
Associate II
Posted on July 18, 2012 at 12:37

Hello,

I am using the STM32L-Discovery board and try to transmit / receive data via USART1. My problem is that the controller does nor react on data sent from the PC, but if I send one byte from the controller the ''

USART_GetFlagStatus(USART1, USART_FLAG_RXNE)

'' reports new received data. The behaviour of the code below is that the controller sends ''0x41 0x00'' every time the Sys-Timer-Interrupt occurs because it seems to receive every time the 'A' is sent and the (not really received) data is echoed. Can anybody help please ? Regards Tom This is my code:

#include ''stm32l1xx.h''
/* function prototypes */
void
RCC_Configuration(
void
);
void
GPIO_Configuration(
void
);
void
NVIC_Configuration(
void
);
void
USART1_Configuration(
void
);
void
TIM2_Configuration(
void
);
void
SysTick_Handler(
void
);
void
TIM2_IRQHandler(
void
);
void
USART1_Send(
char
cData);
int
iTick = 0;
int
main(
void
)
{
char
data;
SystemInit();
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART1_Configuration();
TIM2_Configuration();
// Init SysTick Timer
SysTick_Config(1000000);
while
(1)
{
if
(iTick != 0)
{
iTick = 0;
USART1_Send(
'A'
);
}
if
(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)!= RESET)
{
data=(uint8_t)USART_ReceiveData(USART1);
//echo
USART1_Send(data);
USART_ClearFlag(USART1, USART_FLAG_RXNE);
GPIOB->ODR ^= GPIO_Pin_7;
}
}
}
void
USART1_Send(
char
cData)
{
USART_SendData(USART1, cData);
while
(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
void
RCC_Configuration(
void
)
{
// Enable GPIOA, GPIOB clock
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
// Enable TIM2 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Enable USART1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
void
NVIC_Configuration(
void
)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Place the vector table into FLASH
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//enable tim2 irq
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void
SysTick_Handler(
void
)
{
iTick = 1;
}
void
GPIO_Configuration(
void
)
{
// Initialize Leds mounted on STM32 board
GPIO_InitTypeDef GPIO_InitStructure;
// Configure the GPIO_LED pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure USART1 Rx (PA10) as input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1 Tx (PA9) as alternate function
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
}
void
USART1_Configuration(
void
)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockStructure;
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
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_Init(USART1, &USART_InitStructure);
USART_ClockStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInit(USART1, &USART_ClockStructure);
USART_Cmd(USART1, ENABLE);
}
void
TIM2_Configuration(
void
)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//setting timer 2 interrupt to ??hz ((??/1000)*1000)s
TIM_TimeBaseStructure.TIM_Prescaler = 1000 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
//timer 2 interrupt
void
TIM2_IRQHandler(
void
)
{
//if interrupt happens the do this
if
(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
//clear interrupt and start counting again to get precise freq
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//toggle led to see some chance in port B pin 6
if
(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_6) == RESET)
GPIO_WriteBit(GPIOB,GPIO_Pin_6,SET);
else
GPIO_WriteBit(GPIOB,GPIO_Pin_6,RESET);
}
}

1 REPLY 1
Posted on July 18, 2012 at 13:42

#include ''stm32l1xx.h''
/* function prototypes */
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART1_Configuration(void);
void TIM2_Configuration(void);
void SysTick_Handler(void);
void TIM2_IRQHandler(void);
void USART1_Send(char cData);
int iTick = 0;
int main(void)
{
char data;
SystemInit();
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART1_Configuration();
TIM2_Configuration();
// Init SysTick Timer
SysTick_Config(1000000);
while(1)
{
if (iTick != 0)
{
iTick = 0;
USART1_Send('A');
}
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE)!= RESET)
{
data=(uint8_t)USART_ReceiveData(USART1);
USART1_Send(data); // echo
GPIOB->ODR ^= GPIO_Pin_7;
}
}
}
void USART1_Send(char cData)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, cData);
}
void RCC_Configuration(void)
{
// Enable GPIOA, GPIOB clock
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
// Enable TIM2 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Enable USART1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Place the vector table into FLASH
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//enable tim2 irq
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void SysTick_Handler(void)
{
iTick = 1;
}
void GPIO_Configuration(void)
{
// Initialize Leds mounted on STM32 board
GPIO_InitTypeDef GPIO_InitStructure;
// Configure the GPIO_LED pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure USART1 Rx (PA10) as input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1 Tx (PA9) as alternate function
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
}
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART1 configured as follow:
- BaudRate = 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 = 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_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//setting timer 2 interrupt to ??hz ((??/1000)*1000)s
TIM_TimeBaseStructure.TIM_Prescaler = 1000-1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}

//timer 2 interrupt
void TIM2_IRQHandler(void)
{
//if interrupt happens the do this
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
//clear interrupt and start counting again to get precise freq
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//toggle led to see some chance in port B pin 6
if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_6) == RESET)
GPIO_WriteBit(GPIOB,GPIO_Pin_6,SET);
else
GPIO_WriteBit(GPIOB,GPIO_Pin_6,RESET);
}
}

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