I am trying to make a small project with an STM32 which uses serial commands to control the rotation direction of a motor. I used UART interrupts and callbacks to try to achieve that.
I am using an L298N and PWM signals to run the motor. I plan on toggling the IN2 and IN3 states of the L298N to change the direction.
I looked in the documentation and few other sources and wrote a program for that. When I run debug, I am not able to receive UART messages.
Here's the main and the callback function parts of the program:
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
HAL_UART_Transmit_IT(&huart1, "trial", sizeof("trial"));
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
This the infinite while loop of the main (I use this part to stop the motor if either stop buttons are pressed):
while(1)
{
if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_2) == 1) || (HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_7) == 1) )
{
pulseWidth = 0;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
}
}
Here is the interrupt callback function:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart -> Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, &rData, 1);
receiveBuffer[pos++] = rData;
if (rData == "\n")
{
pos = 0;
if (strstr(receiveBuffer, "OPEN"))
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
}
else if (strstr(receiveBuffer, "CLOSE"))
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
}
menset(receiveBuffer, 0, 8);
}
}
}
When I start debug and use Hercules to send serial data, the MCU does not read it. Can anyone help me to understand what is wrong?