help with stm32f103 led code - Page 1
Forum section: electronics / general

author: jeet55
author_rank: Contributor
timestamp: July 09, 2020, 07:38:47 am
content: Code:int main(){RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;GPIOC->CRH |= GPIO_CRH_MODE13_1;GPIOC->CRH &= ~GPIO_CRH_MODE13_0;GPIOC->CRH &= ~(GPIO_CRH_CNF13_0 | GPIO_CRH_CNF13_1);GPIOC->ODR |= GPIO_ODR_ODR13;//GPIOC->BSRR |= GPIO_BSRR_BS13;while(1){}return 0;}this is the code for turning on pc13 led on stm32f103 but it doesn't work any guess why?

author: ozcar
author_rank: Frequent Contributor
timestamp: July 09, 2020, 08:47:38 am
content: What is the other end of the LED connected to? Maybe you have to drive PC13 low to turn it on.

author: jeet55
author_rank: Contributor
timestamp: July 09, 2020, 08:54:39 am
content: tried driving it low doesn't workedit: sorry you wr correct led needs to be driven low thank you

author: Doctorandus_P
author_rank: Super Contributor
timestamp: July 12, 2020, 12:02:19 am
content: I recommend to start with a working example project.I'm also foolin' around with the Blue Pills & STM32, and have gotten some projects to work, but it's not an easy task.There are different clones of the STM32F103 around, there are different toolchains, different flash programs and bootloaders, different frameworks (CMSIS, Mbed, arduino, opencm3, etc)The uC also has to initialize a bunch of "stuff" before main() is even called, and if you make some error in the startup code, there is no hope of your thing ever working properly.Also:Instead of just setting or resetting the LED pin, try toggling it. That works with either polarity, and you can also see the uC is "doing something".The blinking led project is always one of the most difficult. There is a lot that can go wrong, and few hints of what is going wrong.What is the output from your programmer? Can you verify the chip can at least be programmed?

author: Doctorandus_P
author_rank: Super Contributor
timestamp: July 12, 2020, 09:14:08 am
content: Today I've had success again.I had some very weird problems (as usual).One of the problems was that my latest batch of "Blue Pills" had some "id code" 0x2ba01477For older Pills, the ID code was: 0x1ba01477This had some problem with the toolchain I used "some time ago".Yesterday & today I got a blinking LED working again on my new batch of Blue Pills.I could not install stlink-tools with apt (Still have to figure out why that does not work), So I compiled from source (github.com/texane)I had a look at the first tutorial from Matej Bagsic:His blinking led project (github) does not "blink" though, and is dependent on an I/O pin, and it's also for an STM32F40x.So, after a quick search:https://www.startpage.com/row/search?q="blue+pill"+blinking+led+example+programI found Satoshim (again):https://github.com/satoshinm/pill_blinkI compiled his "bare-metal/" project with the provided makefile.There is no programmer specified in that makefile, so I added a target for st-flash:flash: pill_blink.binst-flash write pill_blink.bin 0x8000000After that I have changed the blink rate multiple times, and verified the change in the blink rate on the Blue Pill after flashing.There were 2 other weird problems I had to overcome:I have another Blue Pill, which I flashed with a GRBL variant some time ago (years), and this was still attached to an USB port and somehow this showed up as an STM32F40x and gave an incromprehensible error with libusb, but after detaching the plug the error went away.Some other things to keep track of:The first time you flash with st-flash, you overwrite the bootloader (which also has a blinking led). And then the led stopped blinking, it just went off. After a power cycle it started flashing at the new flashrate, and consecutive programming cycles did not need a power cycle, nor reset button or BOOT jumper changes. Both BOOT jumpers are "0", that is, on the side of the USB connector.In the old programmer software I used I had to "unlock" the flash of each new blue pill before it could be written. With st-flash this is apparently not necessary anymore.Satoshinm also has blink projects for CubeMX and LibOpenCM3, so pick your poison.