2020-11-23 10:11 AM
I am getting no output with the code below
Below is my main.c UART.c and UART.h file
main.c
<int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t buf []= " List Avialable \n\r"
" 1. LED RED ON \n\r"
" 2. LED GREEN ON \n\r";
uint8_t buf1 [20]={0};
uint8_t i=0;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Transmit(&huart3,buf,sizeof(buf),100 );
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Receive(&huart3,buf1,sizeof(buf1),1000);
UART_read1(buf1);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}>
UART.c
<#include "uartcheck.h"
#include "string.h"
#include "stm32f2xx_hal_uart.h"
uint8_t buffer1 [20];
uint8_t buffer_index1=0;
uint8_t _uart_flag;
uint8_t buffer2[20]="INVALID INPUT ";
uint8_t REDLED[15]= "LED RED ON";
uint8_t GREENLED[15]= "LED GREEN ON";
uint8_t tempbuf[15];
uint8_t j=0;
void UART_read1(uint8_t *buff1)
{
buffer1[buffer_index1]= *buff1;
tempbuf[j]=buffer1[buffer_index1];
if (j==15)
{
UART_buffer_check();
j=0;
}
j++;
buffer_index1++;
}
void UART_buffer_check()
{
if (memcmp(tempbuf,REDLED,15)==0)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
buffer_index1=0;
}
if (memcmp(tempbuf,REDLED,15)==0)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
buffer_index1=0;
}
}>
UART.h
<#include "main.h"
#include "stm32f2xx_hal.h"
extern void UART_read1(uint8_t *buff1);
extern void UART_bufcheck();
>
Solved! Go to Solution.
2020-11-23 12:11 PM
Thank you so much for youre reply. I guess i got where i went wrong. Will increasing the time in recieve function help?
2020-11-23 11:51 AM
With:
HAL_UART_Receive(&huart3,buf1,sizeof(buf1),1000);
You wait until you receive 100 char (in les than 1 second!)
But in:
_read1()
You manage only the 1st char of buf1...
You compare 15 char for REDLED, but its content is only 10.
Use the debugger and a breakpoint at the beginning of read1 to understand what appens.
It's more efficient to use const qualifier for buffer2, REDLED, GREENLED. Const strings...
2020-11-23 12:11 PM
Thank you so much for youre reply. I guess i got where i went wrong. Will increasing the time in recieve function help?