cancel
Showing results for 
Search instead for 
Did you mean: 

UART interrupt recieve Turning LED ON and OFF from terminal by using "Command+Enter"

MAlam.2
Associate II

I want to Turn On and OFF LED by Command "LED ON" or "LED OFF". When i write command "LED ON" in the command terminal like Tera Term the LED should be On. 

I used here string comparision to compare if the given command is same as "LED ON" or "LED OFF". But i am not getting the expected output. the Led is not turned on or off according to the command rather it gives command invalid.

UART_HandleTypeDef huart2;

char *user_data = "The program is running\r\n";

uint8_t recvd_data;

uint8_t data_buffer[100];

uint32_t count=0;

uint8_t reception_complete = FALSE;

int main(void)

{

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 uint16_t len_of_data = strlen(user_data);

 HAL_UART_Transmit(&huart2,(uint8_t*)user_data,len_of_data,HAL_MAX_DELAY);

    while(reception_complete != TRUE)

    {

    HAL_UART_Receive_IT(&huart2,&recvd_data,1);

    }

    while (1)

    {

    }

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

if(recvd_data == '\r')

{

reception_complete = TRUE;

data_buffer[count++]='\r';

//int result1,result2;

char str1[]="LED ON";

char str2[]="LED OFF";

 char *error_buffer = "Command is Invalid\r\n";

//result1=strcmp(str1,(char*)data_buffer);

//result2=strcmp(str2,(char*)data_buffer);

if(strcmp(str1,(char*)data_buffer)==0)

{

HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);

}

else if(strcmp(str2,(char*)data_buffer)==0)

{

HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);

}

else

{

HAL_UART_Transmit(huart,(uint8_t*)error_buffer,strlen(error_buffer),HAL_MAX_DELAY);

}

}

else

{

data_buffer[count++] = recvd_data;

}

}

2 REPLIES 2
TDK
Guru

If the string your receive isn't "LED ON", then what is it?

All variables that are modified within the ISR should be marked volatile.

If you feel a post has answered your question, please click "Accept as Solution".

it is command invalid if it doesnot matches. Anyway i have already solved the problem.