Hello, I'm currently learning to program STM32 microcontroller and for the first project, I chose to do blinking LED. I wrote short code and the on-board LED connected to pc13 was blinking. But then I wanted to add another LED to it(same pc13 pin) and then I noticed that both LEDs take turns when blinking. It goes like this: on_board LED ON - external LED OFF, on-board LED OFF - external LED ON and so on.
Then I noticed that when I set pc13 bit ('1'), on-board LED is OFF and external LED is ON and vice-versa. I then unplug external LED and observe the same results.
My question is why when I set pc13 bit ON, on-board LED stays LOW, but external LED lights up?? It seems like the pc13 on-board LED is inverted or something like that.
Then I noticed that when I set pc13 bit ('1'), on-board LED is OFF and external LED is ON and vice-versa. I then unplug external LED and observe the same results.
My question is why when I set pc13 bit ON, on-board LED stays LOW, but external LED lights up?? It seems like the pc13 on-board LED is inverted or something like that.
Code:
#include "stm32f1xx.h" // main library for MCU
void delay_ms(int ms);
int main(void)
{
//Enable the GPIO(PortC)
RCC ->APB2ENR |= RCC_APB2ENR_IOPCEN;
// Set any Control Registers for Port C Pin 13
GPIOC ->CRH &= ~(GPIO_CRH_CNF13); // making CNF13 '00'
GPIOC ->CRH &= ~(GPIO_CRH_MODE13_1); // making Mode13 as '01' - output mode, max speed 10 MHz
GPIOC ->CRH |= GPIO_CRH_MODE13_0; // making Mode13 as '01' - output mode, max speed 10 MHz
//Make default state of port C pin 13 = '0'
GPIOC ->BSRR = GPIO_BSRR_BR13;
while(1)
{
//Turn on LED (BSRR)
GPIOC ->BSRR |= GPIO_BSRR_BS13;
//Wait
delay_ms(200000);
//Turn off LED(BRR)
GPIOC ->BSRR |= GPIO_BSRR_BR13;
//Wait
delay_ms(200000);
}
}
void delay_ms(int ms) // this will wait x milliseconds - x * 1000 ticks
{
for (int i = 0; i < ms; ++i) { }
}