1

I am a very newbie in microcontroller and the embedded world, so I started with trying to code a blinking programme in stm32 using basics registers and all, finally, I got it to blink, but then I found that whenever the pin pc13 is high the built-in led is off but on when pc13 is low (exactly reversed!), I don't know why I even added an external led to check whether I am right or not and I am right (in doubting it's working) the external led is on whenever builtin is off and vice versa, please help???? my code is : (my board is STM32F103C8 64KB FLASH, STM32_SMART BOARD)

#include "stm32f10x.h"                  // Device header


void delay(int rep);

int main(void)
{
    RCC->APB2ENR |= 0x10; /// Or  0b10000 --> Anabling Preiph GPIOC
    GPIOC->CRH &= 0xFF0FFFFF;  /// Reset the PORT C PIN 13
    GPIOC->CRH |= 0x00300000;  /// Set Port C PIN 13 as Output 
    GPIOC->ODR  |= 0x2000;     /// Set Port C Pin 13 
    while(1)
    {
        /// Blinking the Port C pin 13 
        GPIOC->ODR  |= 0x2000;
        delay(10000000);
        GPIOC->ODR  &= ~0x2000;
        delay(10000000);
        
    }

}


/// Random time delay Function
void delay(volatile int a) {
    //Added volatile in a and in i
    for (volatile int i = 0; i < a; i++);
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • IS THERE ANY REASON FOR USING CAPS IN TITLE? `please help?` With what exactly? As you explained, the led seems to be working. Did you study the schematic and documentation of the board you are using? Do you understand basic electrical components, like resistors or diodes? Did you study how the diode is connected on your board and how does the current flow through it? – KamilCuk Jan 02 '21 at 13:24
  • do not use "magic numbers". The included header contains human-readable definitions. – 0___________ Jan 02 '21 at 13:38
  • This is why you will see a BAR above a pin name on pin-out tables. It means active-low. – Weather Vane Jan 02 '21 at 13:45
  • What if there is no pinout description? – 0___________ Jan 02 '21 at 13:48

1 Answers1

2

The LED can be connected in two ways. To the pin and to GND or VCC. The first one will turn on the LED when the pin is high. The second one when the pin is low.

enter image description here

From your description, your led is connected to the Vcc.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • That is in fact, how the vast majority of BluePill boards are wired. They've also got the wrong resistor values on the data lines of the usb port. I forget. Think they're meant to be 1.5k but are instead 10k. I read an answer from someone here about _exactly_ the issue. His response was to craft a compiler macro that flipped the bit in the needed direction to get the desired light. `LedOn(pin)` and `LedOff(pin)` are the best my memory serves me right now. – enhzflep Jan 02 '21 at 14:53
  • It is up to the engineer designing the electronics to choose between sourcing current or sinking current, depending on the current constraint and possible other requirements. But in your software you should abstract away the physical pin level and hide it behind some access functions as suggested by @enhzflep above. – koder Jan 02 '21 at 18:52