cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART receiving sting

Posted on September 29, 2016 at 07:07

I started 2 days ago working on STM32f4 discovery board, and i have basic background in C programming.

I succedded in sending and receiving characters using USART with interrupt, but whenever i try to receive a string nothing happenes. here is my code

#include ''stm32f4xx.h''
#include ''stm32f4xx_usart.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''misc.h''
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
void setup_Pheriph()
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//ENABLE PERIPH CLOCK FOR USART2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//ENABLE GPIOA CLOCK
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//ENABLE GPIOD CLOCK
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//SETUP GPIOA PINS FOR Tx AND Rx
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; //SINCE WE HAVE USED ALTERNATIVE FUNCTION WE SHOULD ASIGN IT DOWN
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //PULL UP RESISTORS
GPIO_Init(GPIOA, &GPIO_InitStruct);
//SETUP GPIOD PINS FOR BLINKING LED
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOD, &GPIO_InitStruct);
//SETTING UP AF FOR UARTS2
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //WE SET PIN2 AND 3 AS AF FOR UART2
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
//SETUP USART CONFIG
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //WE SETUP USART FOR Rx AND Tx
USART_Init(USART2, &USART_InitStruct);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //SETUP INTERRUPT REQUEST FOR USART FOR ONLY RECEIVE
//SETTING UP INTERRUPT REQUEST NVIC
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn; //CHANNEL IS THE INTERRUPT NAME I WANT TO CONFIGURE
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //ENABLE USART GLOBALLY
NVIC_Init(&NVIC_InitStruct);
//ENABLE USART2 PERIPHERAL
USART_Cmd(USART2, ENABLE);
}
void USART_send_char(USART_TypeDef *USARTx, char c)
{
while(!USART_GetITStatus(USARTx,USART_FLAG_TXE));
USART_SendData(USARTx, c);
}
void USART_send_string(USART_TypeDef *USARTx, volatile char *str) //WE PASS ANY USART AND STRING
{
while(*str)
{
/*while(!(USARTx->SR & 0x00000040)); //P.993 SR-> STATUS REGISTER, 0X040 -> TC FLAG
//LOOP TILL TC FLAG = 1, WHICH MEANS DATA IS TRANSFERED
//ANOTHER WAY IS LIKE THIS*/
while(!USART_GetFlagStatus(USARTx, USART_FLAG_TC )); //STAY HERE WHILE THIS FLAG IS ZERO
USART_SendData(USARTx, *str);
*str++;
}
}
uint16_t USART_receive_char(USART_TypeDef *USARTx)
{
//WAIT UNTIL CHAR IS RECEIVED
while(!USART_GetFlagStatus(USARTx, USART_FLAG_RXNE));
//READ RECEIVED CHAR
return USART_ReceiveData(USARTx);
}
uint16_t* USART_receive_string(USART_TypeDef *USARTx)
{
uint16_t string[20],x;
static int cnt = 0;
x=USART_receive_char(USARTx);
while(x!='\n' || x!='\r')
{
//STORE THE RECEIVED CHAR INTO ARRARY
string[cnt++]=x;
}
string[cnt]='\0';
return string;
}
int main(void)
{
setup_Pheriph();
USART_send_string(USART2, ''Welcome Rabah!\r\n'');
while(1)
{
}
}
uint16_t rec_char;
uint16_t *rec_string;
void USART2_IRQHandler()
{
/*rec_char = USART_receive_char(USART2);
if(rec_char == 'o')
GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
if(rec_char == 'f')
GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);*/
if(rec_string == ''red'')
{
GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
}
else
{
USART_send_string(USART2,rec_string);
}
}

3 REPLIES 3
Posted on September 29, 2016 at 15:21

If you are going to use polling mode to read the USART don't enable the RXNE interrupt.

Would suggest you use the TXE to determine if the USART can take another character rather than TC.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 30, 2016 at 03:33

This isn't going to work

if(rec_string == ''red'')

You'd want to compare the content, not the pointers if (strcmp(rec_string, ''red'') == 0)
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 02, 2016 at 04:22

:o

IDK how did i this mistake. i will try it and update you. thanks for your help