cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with UART interrupt

Raf_al
Associate III

Hi,

I have two MCU communicating with each other via RS485. It's a very simple code one MCU send a data to other and the other is trying to receive this.

 

With MCU2 code everything is fine. It sends data like it should be.

Problem is with MCU1 code. When I delete #65 line in code (that one with HAL_UART_Transmit...) it works great. It receive data from MCU2 and starts engine and horn. But when I add this line it stop working. I dont understand why this is happening.

When the MCU2 works as a receiver everything is ok, but when I try to send something from it suddenly the whole uart stops working

 

MCU1 code:

/* USER CODE BEGIN 0 */


uint8_t buffer[2];
uint8_t ZERO[2]={0x00,0x00};
uint8_t AA[2]={0xAA,0xAA};
uint8_t BB[2]={0xBB,0xBB};

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

if(buffer[0]==0xCC && buffer[1]==0xCC)
{
	Engine(ON);
}

if(buffer[0]==0xDD && buffer[1]==0xDD)
{
	Horn(ON);
}


HAL_UART_Receive_IT(&huart2, buffer, 2);
}



/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */


  transceiver(WRITE);
  HAL_UART_Transmit(&huart2, ZERO, 2, 10);      
  transceiver(READ);
  HAL_UART_Receive_IT(&huart2, buffer, 2);




  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {



    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

MCU2 code:

/* USER CODE BEGIN 0 */


uint8_t CC[2]={0xCC,0xCC};
uint8_t DD[2]={0xDD,0xDD};


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */


  HAL_Delay(1000);
  transceiver(WRITE);
  HAL_UART_Transmit(&huart2, CC, 2, 10);
  HAL_Delay(1000);
  HAL_UART_Transmit(&huart2, DD, 2, 10);
  transceiver(READ);


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

13 REPLIES 13
Karl Yamashita
Lead III

Is the Transceiver (READ) switching fast enough before the other MCU replies? Check with oscilloscope.

 

Maybe try this approach

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */

  
  HAL_UART_Receive_IT(&huart2, buffer, 2); // enable first

	transceiver(WRITE);
	if((HAL_UART_Transmit_IT(&huart2, ZERO, 2)) != HAL_OK) // use transsmit interrupt
		LED3(ON);
    

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	transceiver(READ);
}

 

 

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
Raf_al
Associate III

I think that problem is somewhere else. I can press that button (button that sends 0xDD for example) 10 seconds after MCU1 goes to tranceiver read mode.


@Raf_al wrote:

I think ...


It's good to have a theory. :thumbs_up:

Now you need to devise a test to prove whether your theory is correct or not ...

See The Scientific Approach to Debugginghttp://www.8052mcu.com/faqs/120313 

Raf_al
Associate III

Ok, so i tried with HAL_UART_Transmit_IT but it didnt work.

By accident I disconnected my oscilloscope from MCU1 UART lines and now it works.:grinning_face_with_sweat: