Hello All,
FIXED AND EDITED TO SHOW THE FIX....
Background:
Pin B10 is configured to turn an LED on and A8 is configured to receive input when the button/switch is pressed.
The following C code does not work:
The following code does work:
The APB2ENR need "C" hex (=1100) passed into it and GPIOA->CRH needs "8" hex (=1000) passed into it. This will turn LED to ON when button is pressed.
Device is STM32F103
FIXED AND EDITED TO SHOW THE FIX....
Background:
Pin B10 is configured to turn an LED on and A8 is configured to receive input when the button/switch is pressed.
The following C code does not work:
Code:
int main(void) {
//Setting Ports A and B as active:
RCC->APB2ENR = (0x3<<0);
Defining GPIOA as input on pin 8 by passing 1 into CNF8(2):
GPIOA->CRH = (0x1<<2);
//Defining GPIOB as an output on pin 10 by passing 10 into MODE10(8):
GPIOB->CRH = (0x2<<8);
while(1){
// Using IDR to read when the button is pressed from A8:
if(GPIOA->IDR & (1<<8))
// resetting pin B10:
GPIOB->BSRR = (1<<26);
else
// setting 1 into pin B10:
GPIOB->BSRR = (1<<10);
}
}
Code:
int main(void) {
RCC->APB2ENR = (0xC<<0);
GPIOA->CRH = (0x8<<0);
GPIOB->CRH = (0x2<<8);
while(1){
if(GPIOA->IDR & (1<<8))
GPIOB->BSRR = (1<<26);
else
GPIOB->BSRR = (1<<10);
}
}
Device is STM32F103
Last edited: