This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I cannot print the error value correctly in the LCD received from the motor controller using the UART protocol.

I am writing a code to receive the UART protocol transmitted from the motor controller and I am decoding the accepted protocol as speed, voltage and error using arrays. The received protocol is in continuous mode so, I captured them in another buffer to keep them constant. When I am receiving an error, suppose when error = 11, I am getting 10 or 00 like that. How to get the correct value. Here are some references.

Help me in completing this.

 This is the code I wrote.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART1_UART_Init(void);
void Ringbuf_Init(void);
void Ringbuf_Reset(void);
void lcd_data(char data,int rs);
void lcd_send_cmd (char cmd);
void lcd_send_data (char data);
void lcd_init (void);
void lcd_display(unsigned char *s);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

#define RxBuf_Size   20
#define MainBuf_Size 14

uint8_t RxData[RxBuf_Size];
uint8_t MainData[MainBuf_Size];

uint16_t oldPos = 0;
uint16_t newPos = 0;

uint16_t Head, Tail;
int call=0;
int errorcall=0;
int a,b,c;
int i,j,k,l,m,n,o,p;
int x,y;
int Error,Speed; 
int Key1 = 'A', Key2 = 'B', Key3 = 'C';
int flag1 = 0, flag2 = 0, flag3 = 0;
float Voltage;
float nearest,ans,q;
char buffer[14];


void seperation(uint8_t arr[])
{
	//Decoding the speed value
	a = arr[2] - '0'; b = arr[3] - '0'; c = arr[4] - '0'; 
	Speed   = 100.0*a + 10.0*b + c;
	
	//Decoding the voltage value
	k = arr[6] - '0'; l = arr[7] - '0';  m = arr[9] - '0'; n = arr[10] - '0';  
	p = 10.0*k + l;  
	q = (10.0*m + n)/100.0;
	ans = p + q;
	Voltage= (long int) ((ans)*100+0.5) / 100.0;
	
	//Decoding the Error value
	if(arr[12]==0x00 && arr[13]==0x00)
	{
		Error=0;
	}
	else
	{
		x = arr[12] - '0'; 
		y = arr[13] - '0'; 
		Error   = 10.0*x + y;
  }
}

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
	NVIC_SystemReset();
	HAL_UART_DeInit(&huart1);
	HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t *) RxData, RxBuf_Size);
	__HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
}
	
//UART Reception Callback
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart->Instance == USART1)
	{
		HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t *) RxData, RxBuf_Size);
		__HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
		//memcpy (MainData, RxData, Size);
		seperation(MainData);
		


		for (i=0; i<Size; i++)
		{
			if ((RxData[i] == Key1) && (RxData[i+1] == Key1) &&(RxData[i+6] == Key2) && (RxData[i+12] == Key3))
			{
				MainData[0] = RxData[i];
				MainData[1] = RxData[i+1];
				MainData[2] = RxData[i+2];
				MainData[3] = RxData[i+3];
				MainData[4] = RxData[i+4];
				MainData[5] = RxData[i+6];
				MainData[6] = RxData[i+7];
				MainData[7] = RxData[i+8];
				MainData[8] = RxData[i+9];
				MainData[9] = RxData[i+10];
				MainData[10] = RxData[i+11];
				MainData[11] = RxData[i+12];
				MainData[12] = RxData[i+13];
				MainData[13] = RxData[i+14];
				
				
				break;
				
			}
		}
	}
	else
	{
		Voltage=0;
	}
}

	
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */

void lcd_data(char data,int rs)
	{
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,rs);
		
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, ((data>>3)&0x01));
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, ((data>>2)&0x01));
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, ((data>>1)&0x01));
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, ((data>>0)&0x01));
		
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,1);
		HAL_Delay(5);
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,0);
		HAL_Delay(5);

	}
	
	void lcd_send_cmd (char cmd)
{
    char datatosend;
    datatosend = ((cmd>>4)&0x0f);
    lcd_data(datatosend,0);
    datatosend = ((cmd)&0x0f);
    lcd_data(datatosend, 0);
}
void lcd_send_data (char data)
{
    char datatosend;
    datatosend = ((data>>4)&0x0f);
    lcd_data(datatosend, 1); 
    datatosend = ((data)&0x0f);
    lcd_data(datatosend, 1);
}

void lcd_init (void)
{
    HAL_Delay(500);
		lcd_send_cmd(0x02); // Return Home
		lcd_send_cmd(0x28); //4 bit mode
		lcd_send_cmd(0x0C); //Display on/cursor off
		lcd_send_cmd(0x01); // clear display
		HAL_Delay(10);
}


int main(void)

{
  /* USER CODE BEGIN 1 */

  /* 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_DMA_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
		
  HAL_UARTEx_ReceiveToIdle_DMA(&huart1, RxData, RxBuf_Size);
	__HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
  
	/* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	int voltagenew;
	int speed1;
	int error1,voltage1,error2;
	lcd_init();
  while (1)
  {
		if(MainData[4]>47 && MainData[4]<58)
		{
			if(MainData[3]>47 && MainData[3]<58)
			{
				if(MainData[2]>47 && MainData[2]<58)
				{
					if(MainData[6]>47 && MainData[6]<58)
					{
						if(MainData[7]>47 && MainData[7]<58)
						{
							if(MainData[9]>47 && MainData[9]<58)
							{
								if(MainData[10]>47 && MainData[10]<58)
								{
									if(MainData[2]>47 && MainData[2]<58)
									{
										if(MainData[3]>47 && MainData[3]<58)
										{
											if(MainData[0]>64 && MainData[13]<91)
											{
												if(MainData[1]>64 && MainData[13]<91)
												{
													if(MainData[5]>64 && MainData[5]<91)
													{
														if(MainData[11]>64 && MainData[11]<91)
														{
											
																seperation(MainData);
													
																voltagenew=Voltage*100;
																speed1=Speed*1;
																
																error1=Error*1;
																
																lcd_send_cmd(0x80);
																lcd_display("SPD:");
																lcd_send_cmd(0x84);
																lcd_send_data((Speed/100)+48);
																lcd_send_data(((Speed/10)%10)+48);
																lcd_send_data((Speed%10)+48);
																lcd_send_cmd(0x89);
																lcd_display("ERR:");
																lcd_send_cmd(0x8D);
																lcd_send_data((Error/10)+48);
																lcd_send_data((Error%10)+48);
																lcd_send_cmd(0xC0);
																lcd_display("VOLTAGE:");
																lcd_send_cmd(0xC9);
																lcd_send_data((voltagenew/1000)+48);
																lcd_send_data(((voltagenew/100)%10)+48);
																lcd_send_data(0x2E);
																lcd_send_data(((voltagenew/10)%10)+48);
																lcd_send_data((voltagenew%10)+48);

																voltage1=voltagenew;
															
															
																HAL_Delay(10);
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}	
		
		
		
		else
		{
			lcd_send_cmd(0x80);
			lcd_display("SPD:");
			lcd_send_cmd(0x84);
			lcd_send_data((speed1/100)+48);
			lcd_send_data(((speed1/10)%10)+48);
			lcd_send_data((speed1%10)+48);
			lcd_send_cmd(0x89);
			lcd_display("ERR:");
			lcd_send_cmd(0x8D);
			lcd_send_data((error1/10)+48);
			lcd_send_data((error1%10)+48);
			lcd_send_cmd(0xC0);
			lcd_display("VOLTAGE:");
			lcd_send_cmd(0xC9);
			lcd_send_data((voltage1/1000)+48);
			lcd_send_data(((voltage1/100)%10)+48);
			lcd_send_data(0x2E);
			lcd_send_data(((voltage1/10)%10)+48);
			lcd_send_data((voltage1%10)+48);
		}
  }
  /* USER CODE END 3 */
}
void lcd_display(unsigned char *s)
{
	while(*s)
	{
		lcd_send_data(*s++);
		HAL_Delay(50);
	}
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Channel5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
                          |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);

  /*Configure GPIO pins : PA0 PA1 PA2 PA3
                           PA4 PA5 PA6 PA7 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
                          |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */