cancel
Showing results for 
Search instead for 
Did you mean: 

Input pin stm32g0 always remains low

Ali110
Associate II

I am learning the stm32 using a stm32g070cbt6 micro controller with custom PCB with keil. When I add an external pull-up to the pa4 pin and use it as an input pin. Nevertheless, when I read the pin, the output is low always, even though on the hardware pin is high(3.3v). Yet I couldn't figure out why it happened. Please assist in finding this problem.

Thanks in advance.

#include "main.h"

void SystemClock_Config(void);

uint16_t count =0;

int main(void)

{

HAL_Init();

SystemClock_Config();

RCC->IOPENR |= (1<<0);

GPIOA->MODER &= ~((1<<8) | (1<<9));

while (1)

{

if (!(GPIOA->IDR &(1<<4))) // if the pin is low count should increament

{

count++;

}

}

}

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;

RCC_OscInitStruct.PLL.PLLN = 9;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV3;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

{

Error_Handler();

}

}

4 REPLIES 4
gbm
Lead III

The pin is low - see IDR register. Try to declare count as volatile.

WojtekP1
Associate III

from what i can understand is that you turn on clock for GPIO and then in just next instruction use GPIO registers. I would try adding something in between just to try.

second

GPIOA->MODER &= ~((1<<8) | (1<<9));

this set up A4 to mode 00 which is input. all fine.

second - how do you check that count as it's infinite loop?

It's not set to volatile - so this loop may increment register, without even touching global variable.

Ali110
Associate II

That's problem, i add 1k pull up at PA4 which makes this pin high, but actually it is low.

@Community member I appreciate your reply.

Purpose for count only:I want the count to increase only when a low pulse is detected at a pin.

The issue is with pin PA4 on port A. On the hardware side, I added a 1K pull-up resistor to make the pin high theoretically, but in practice, I get low. could propose anything