0

I'm beginer in STM32, i have a project and need to receive data from another device like arduino, and now I try transmit data from UART 3 and I receive data with UART 1. but I can't get any data. I connect TX uart 3 to RX uart 1 and TX uart 1 to RX uart 3.

/* USER CODE BEGIN PV */
int i = 0;
char bufferReceive[6], bufferTransmit[10];

/* USER CODE END PV */

/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)  //current UART
        {
        HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);   //activate UART receive interrupt every time
        }
}

/* USER CODE END 0 */

int main(void)
{
 HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
 while (1)
  {
    /* USER CODE END WHILE */`enter code here`


     sprintf(bufferTransmit,"%d\n",i);
      HAL_UART_Transmit(&huart3, (uint8_t*)bufferTransmit, sizeof(bufferTransmit), 1000);
                    HAL_Delay(500);

                    i++;

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
Gilang
  • 1
  • 1

2 Answers2

0

First thing your code is messy and hard to read.

Second, you are missing your entire initialization region before while(1) so your system shouldn't be doing anything.

/* USER CODE BEGIN PV */
int i = 0;
char bufferReceive[6], bufferTransmit[10];
/* USER CODE END PV */

/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)
    {
        HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
    }
}
/* USER CODE END 0 */

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_USART3_UART_Init();
    MX_USART1_UART_Init();
    /* USER CODE BEGIN 2 */
    HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        sprintf(bufferTransmit,"%d\n",i);
        HAL_UART_Transmit(&huart3, (uint8_t*)bufferTransmit, sizeof(bufferTransmit), 1000);
        HAL_Delay(500);
        i++;
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}

You are receiving one byte at HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);.

Which does not wait it simply moves onward.

Next, you transmit bufferTransmit which then causes UART1 to receive one byte and continues to receive all bytes one by one in the same memory location.

It seems you are not using i properly as you probably want something like HAL_UART_Receive_IT(&huart1, (uint8_t*)&bufferReceive[i], 1);

studentbrad
  • 113
  • 7
0

You do not initialize anything. Peripherals, pins, clocks etc have to be set up before they can work.

0___________
  • 60,014
  • 4
  • 34
  • 74