2021-06-22 05:36 AM
Currently, it seems that the IDR of any port updates only when the IO is configured as an output and does not record the state when the IO is configured as an input.
Here is two examples of simple code for each situation
1) Both PC2 and PC15 is set as output. LED(PC15) does light up if PC2 is set high
int main(void){
RCC->AHB2ENR |= BIT2; // Enable clock for the PORTC
GPIOC->MODER &= ~BIT31; // Configure the PC15 as an output (LED pin)
GPIOC->MODER &= ~BIT5;
GPIOC->ODR |= BIT2;
// PC2 is an input, which should be recorded as high in the IDR register
while(1){
if(GPIOC->IDR & BIT2) GPIOC->ODR |= BIT15;
}
return 0;
}
2) PC2 is set as input, PC15 set as output. LED(PC15) does not light up even if the level on the PC2 is high (hooked wire to between supply voltage and the test point connected to the PC2).
int main(void){
RCC->AHB2ENR |= BIT2; // Enable clock for the PORTC
GPIOC->MODER &= ~BIT31; // Configure the PC15 as an output (LED pin)
// PC2 is an input, which should be recorded as high in the IDR register
while(1){
if(GPIOC->IDR & BIT2) GPIOC->ODR |= BIT15;
}
return 0;
}
Is there anything else necessary to set up the IDR for detecting the state of input pin???
2021-06-22 05:46 AM
IDR should always reflect the state of the input, unless you're in analog mode. It's possible your PC2 pad isn't connected to what you think it is.
2021-06-22 06:04 AM
Okay... yeah, the device was in the analog mode and not the input... For further reference, the fix is changing MODER from 11 to 00, which I did not do...
Thanks!