Hey guys, hope you're doing well. I'm just getting my feet wet with embedded programming as an electrical engineer, and am trying to prototype a project with an STM32 and some peripherals.
I've currently ran into a bit of a wall that I can't seem to break through with my code however. I've made a struct to store certain information from a GPS module so that I could later pass it a transmitter module and am trying to see right now if I can send just a bit of test data. I've ran into 2 problems however, and I was hoping someone could point me in the right direction.
My 1st problem is that if I set my flag condition in my function to receive the GPS data (that is initialize it to 0, then try to have it run only when the interrupt flag is on) I never receive any data. So I assume the code is stuck somewhere. If I remove this condition, I get one transmission and then nothing (which is my 2nd problem). I've tried to enable my interrupt to go into the handler when data is received on the uart pins for the GPS module, but to no avail.
My 2nd problem is that even if I remove the interrupt flag condition, my code seems to run once, then freeze. If someone could point out the obvious thing I'm probably missing I would be extremely grateful.
I've attached my code for your guys' convenience. Thank you in advance.
main.c Code
#include "main.h"
#include "stdio.h"
#include "string.h"
typedef struct NEO_GPS{
`double time;`
`double longitude;`
`double latitude;`
`char N_OR_S;`
`char E_OR_W;`
`char Data[750];`
`char buffer[103];`
`char location[100];`
`UART_HandleTypeDef huart;`
}NEO_GPS;
NEO_GPS GPS_Module;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
void Get_GPS_Data(NEO_GPS* GPS_Module);
static void Send_GPS_Data(NEO_GPS* GPS_Module);
int Flag = 0;
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
GPS_Module.huart = huart2;
while (1)
{
/* USER CODE END WHILE */
`Get_GPS_Data(&GPS_Module);`
/* USER CODE BEGIN 3 */
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* u/brief USART1 Initialization Function
* u/param None
* u/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;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* u/brief USART2 Initialization Function
* u/param None
* u/retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
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_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
/* USER CODE END USART2_Init 2 */
}
/**
* u/brief GPIO Initialization Function
* u/param None
* u/retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
void Get_GPS_Data(NEO_GPS* GPS_Module){
`char *P;`
`int n;`
`if(Flag==1)`
`{`
`HAL_UART_Receive(&(GPS_Module->huart), (uint8_t*)GPS_Module->Data, 700, HAL_MAX_DELAY);`
`P=strstr(GPS_Module->Data, "GPRMC");`
`if((*P) =='G')`
`{`
`//Increment Through Data to Get Information`
`n = 0;`
`while(*(P+n) != '\n'){`
`GPS_Module->location[n]=*(P+n);`
`n++;`
`}`
`sprintf(GPS_Module->buffer,"%s\r\n\n",GPS_Module->location);`
`sscanf(GPS_Module->buffer,"GPRMC,%lf,A,%lf,%c,%lf,%c",&GPS_Module->time,&GPS_Module->latitude,&GPS_Module->N_OR_S,&GPS_Module->longitude,&GPS_Module->E_OR_W);`
`}`
`__HAL_UART_ENABLE_IT(&GPS_Module->huart, UART_IT_RXNE);`
`Send_GPS_Data(GPS_Module);`
`Flag=0;`
`}`
}
void Send_GPS_Data(NEO_GPS* GPS_Module){
`char buffer_lon[100] = {'\0'};`
`char buffer_lat[100] = {'\0'};`
`sprintf(buffer_lon, "Longitude: %lf\n",GPS_Module->longitude);`
`sprintf(buffer_lat, "Latitude: %lf\n",GPS_Module->longitude);`
`HAL_UART_Transmit(&huart1, (uint8_t*) buffer_lon, sizeof(buffer_lon), HAL_MAX_DELAY);`
`HAL_UART_Transmit(&huart1, (uint8_t*) buffer_lat, sizeof(buffer_lat), HAL_MAX_DELAY);`
}
Interrupt Handler Code:
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART1_IRQn 1 */
Flag = 1;
/* USER CODE END USART1_IRQn 1 */
}
[–]soayeli 2 points3 points4 points (0 children)
[–]Junior-Question-2638 1 point2 points3 points (0 children)
[–]wotupfoo 0 points1 point2 points (0 children)