AnsweredAssumed Answered

USART2 : Communication RS232

Question asked by edwin marcellin on May 17, 2018
Latest reply on May 18, 2018 by edwin marcellin

Hello,

 

I have a prototype where a sensor send information in RS232 to my STM32152RE board through a MAX232 to convert the signal. I plugged in my board with RX/TX on USART 2 (+5V et GND).

 

I used CUBE MX and i only modify the code as following but i receive no signal.

Is someone can help me, please?

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32l1xx_hal.h"
#include <stdio.h>

 

 

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;

 

/* Private variables ---------------------------------------------------------*/
int i=0;
uint8_t buffer[100];
int len;

uint8_t Rx_indx, Rx_data[2], Rx_Buffer[100], Transfer_cplt;

 

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);

 


/* Private function prototypes -----------------------------------------------*/

 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t i;
if (huart->Instance == USART2) //current UART
{
if (Rx_indx==0) {for (i=0;i<100;i++) Rx_Buffer[i]=0;} //clear Rx_Buffer before receiving new data

if (Rx_data[0]!=13) //if received data different from ascii 13 (enter)
{
Rx_Buffer[Rx_indx++]=Rx_data[0]; //add data to Rx_Buffer
}
else //if received data = 13
{
Rx_indx=0;
Transfer_cplt=1;//transfer complete, data is ready to read
}

HAL_UART_Receive_IT(&huart2, Rx_data, 1); //activate UART receive interrupt every time
}

}

 

int main(void)
{

HAL_Init();


SystemClock_Config();


MX_GPIO_Init();
MX_USART2_UART_Init();


/* USER CODE BEGIN 2 */

HAL_UART_Receive_IT(&huart2,Rx_data,1); //activate uart rx interrupt every time receiving 1 byte

/* USER CODE END 2 */


while (1)
{
if(Transfer_cplt)
{
sprintf(buffer,"%s\r\n",Rx_Buffer);
len=strlen(buffer);
HAL_UART_Transmit(&huart2,buffer, len,1000);
Transfer_cplt=0;
HAL_Delay(500);

i++;
}

}

}


void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

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

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* USART2 init function */
static void MX_USART2_UART_Init(void)
{

huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

}

/** Pinout Configuration
*/
static void MX_GPIO_Init(void)
{

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

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

#endif /* USE_FULL_ASSERT */

 

Outcomes