2017-01-26 01:49 AM
Greetings,
I have been testing a new board, and have a strange behaviour on these pins. I have them set as inputs (And have pushbuttons there) but I am always reading 0. The hardware is ok (There is a physical pull up resistor [10k], and the traces areok, I have +Vcc at the mcu pins when pushbuttons are not pressed, and zero when pressed), but the software is not working. It is probably some silly thing that I forgot, but can't find it...
I had been away from stm for months, and last time was not long or deep enough, so this time tried to do it using HAL (and code generated by cube). It did not work, so, as the first time I got the project running using std peripheral library, I went again to that option. Same result...
The code for the GPIO set is: (for the std periph variant)
// Definition of the pins
#define PIN_SW3_Pin GPIO_Pin_13
#define PIN_SW3_GPIO_Port GPIOC
#define PIN_SW2_Pin GPIO_Pin_14
#define PIN_SW2_GPIO_Port GPIOC
#define PIN_SW1_Pin GPIO_Pin_15
#define PIN_SW1_GPIO_Port GPIOC
// Disable LSE (As documentation says it has precedence over GPIO for those pins)
RCC_LSEConfig(RCC_LSE_OFF);
//enable clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
//Set pins as input
GPIO_InitStruct.GPIO_Pin = PIN_SW3_Pin|PIN_SW2_Pin|PIN_SW1_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStruct);�?�?�?
//But this is always zero (same for the other two)
if(GPIO_ReadInputDataBit(PIN_SW1_GPIO_Port, PIN_SW1_Pin))
{
GPIO_WriteBit(PIN_LED3_GPIO_Port, PIN_LED3_Pin, 1);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
else
{
GPIO_WriteBit(PIN_LED3_GPIO_Port, PIN_LED3_Pin, 0);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
The code does some more (At the moment, basically it has Timer 2 generating a PWM output in other led) but I think this is all the relevant stuff. Anyway, if someone thinks it's important, here is the complete initialization:
// RCC init function
FLASH_SetLatency(FLASH_Latency_1);
RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_6);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
// NVIC init function
NVIC_InitTypeDef nvicStructure;
nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
nvicStructure.NVIC_IRQChannelSubPriority = 1;
nvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure);
//Systick init function
RCC_ClocksTypeDef rcc_clocks;
RCC_GetClocksFreq(&rcc_clocks);
SysTick_Config(rcc_clocks.SYSCLK_Frequency / 1000);
// GPIO init function
// Port C inputs
GPIO_InitStruct.GPIO_Pin = PIN_SW3_Pin|PIN_SW2_Pin|PIN_SW1_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStruct);
// Port B outputs
GPIO_InitStruct.GPIO_Pin = PIN_LED1_Pin|PIN_LED2_Pin|PIN_LED3_Pin|PIN_BLE_WS_Pin
|PIN_BLE_MDLP_Pin|PIN_BLE_WU_SW_Pin|PIN_BLE_WU_HW_Pin|PIN_BLE_CMD_Pin
|PIN_EEPROM_CS_Pin|PIN_EEPROM_WP_Pin|PIN_BLE_CONN_ST_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// Port A outputs
GPIO_InitStruct.GPIO_Pin = PIN_EEPROM_HOLD_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(PIN_EEPROM_HOLD_GPIO_Port, &GPIO_InitStruct);
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
Still had not started with ADC or external peripherals. As I said, probably is a silly thing, but can't figure why it does not work...
Thanks in advance.
#pc14 #inputs #pc15 #pc13 #stm32f100c8t7b #stm32f1 #gpio2017-01-26 05:07 AM
Make sure you set all the fields in the GPIO initialization structure or clear it. These pins are also in the low power domain.
2018-02-21 12:45 AM
Hi;
I have a problem.
I want to be output pins PC14 and PC 15, so make the registers PWR -> CR = 0x00000100, RCC-> BDCR = 0x00000000.
probably LSE ocillator isn't off.
How can do this ?
Thanks,