STM32 blink LED – first programming steps in Keil

STM32 first steps – blink LED

On board of STM32 (some people call it “blue pill”) there is green LED, or at other boards there is blue LED. First step is to get familiar with Keil environment, set everything properly and start programming. It is not difficult, but require some time to get used to this ‘new way’ aside ‘Arduino way’ or making ‘sketch’-es. This is true C programming, which is maybe less popular than Arduino, but Keil has some import functions so that you can import .ino file and convert it to new way. I found it works faster and has many cool functions. The problem is that there is almost no ‘helper’ libraries, or those are hidden or not available in free version of the Keil IDE.

Use ST-Link V2 programmer, support debug and goes up to 4 MHz uploat to the board with MCU. Do not forget ST-Link driver.

Before everything, here are links to reference manual and two MCUs (both STM32, different model) showed on the video below.

STM32F103C8  and

STM32F103VET6

Main reference manual for STM32F101 to STM32f107.

First blink program:

#include "stm32f10x.h"

void delay(long cycles)
{
  while(cycles >0)
  cycles--; // Some stupid delay, it is not in milliseconds or microseconds, but rather in some 'wasted clock cycles'
}

void gpio_ports_enable(void)
{
  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN |     RCC_APB2ENR_IOPBEN|RCC_APB2ENR_IOPCEN; //ports A & B clock enabled
  GPIOC->CRH = 0x44344444; //Pin C13 enable. If you don't like number 13, use pin C_1_P.M. LOL
}

int main(void)
{
	gpio_ports_enable();

	for(;;)  //main loop - read "forever", or you may use 'while(1)'
	{
		  GPIOC->BSRR = GPIO_BSRR_BS13; 
		  delay(5000000);
		  GPIOC->BSRR = GPIO_BSRR_BR13; 
		  delay(5000000); 
		}	
	}
}

Thee thing with enabling port, if you remember Arduino way it is ‘PinMode(13, OUTPUT);’, here is not the case, we can write in few ways, for example long one:

GPIOC->CRH |= GPIO_CRH_MODE13_0|GPIO_CRH_MODE13_1;
GPIOC->CRH &= ~(GPIO_CRH_CNF13_0|GPIO_CRH_CNF13_1);

Which will ‘yield’ the same effect as simple expression:

GPIOC->CRH = 0x44344444;

Where numbers 4 are default or reset value which make pins input, floating – or least amount of current consumption. To make your task easier, use calculator in “Programmers” mode (at leas Windows has it). First type in the calculator HEX value you found in the datasheet for the register of interest, then click on icon next to DWORD (initially there is QWORD – click until DWORD appears), and it will show you map with groups of four bits – ‘nibbles’. One byte contains two nibbles. Click on that table to change values and compare with values that is needed for your application and compare it with the datasheet.  Here is how it looks after setting pin PC13 (or just C13) in the calculator:

STM32

To get this calculator in Windows, pres Windows key and type ‘calc’, or if it does not works, press and hold windows key and key ‘R’ to get command prompt, then type ‘calc’. On calculator, there is option ‘standard’, ‘scientific’, but we need ‘programmers’.

Another PWM blink program:

#include "stm32f10x.h"

void delay(long cycles)
{
  while(cycles >0)
  cycles--; // Some stupid delay, it is not in milliseconds or microseconds, but rather in some 'wasted clock cycles'
}

void gpio_ports_enable(void)
{
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN|RCC_APB2ENR_IOPCEN; //ports A & B clock enabled
	GPIOC->CRH = 0x44344444; //Pin C13 enable. If you don't like number 13, use pin C_1_P.M. LOL
}

int main(void)
{
	gpio_ports_enable();

	for(;;)  //main loop - read "forever"
	{
		
		for (int i=0;i<3000;i++)
		{
		  GPIOC->BSRR = GPIO_BSRR_BS13; 
		  delay(i);
		  GPIOC->BSRR = GPIO_BSRR_BR13; 
		  delay(3000-i); 
		}
				for (int i=0;i<3000;i++)
		{
		  GPIOC->BSRR = GPIO_BSRR_BS13; 
		  delay(3000-i);
		  GPIOC->BSRR = GPIO_BSRR_BR13; 
		  delay(i); 
		}
	}
}

This time the same LED on board slowly going bright, then dim at the same rate on STM32 boards. So far, I have only two versions: STM32F103C8 and STM32F103VET6. Firs one is cheapo popular “Blue Pill”, while second one is some advanced version with more pins, more memory and what not. Also more expensive. And I did not found pinout of this board. For this purpose, you may wish to install CubeMX program. This program generates file for starting, but that is some weird “HAL” structure, which I am not familiar, nor want to mess with that, since it is so confusing. But, at least it has MCU with designations on each pin after you click on it. Example:

 STM32Note that this is not programming environment, just “initialization code generator” for STM32.

Be sure to use Notepad++ for saving copy of the codes. In this type of Notepad, if you save program as for example ‘.c’ extension, it will gives you ‘colored’ nice looking text. I usually print from this thing and also saving this code to another place on the disk in the case that I lost trace of the original program which is somewhere in Keil directory.

Continue reading STM32 blink LED – first programming steps in Keil

STM32 programming in Keil

STM32 MCUs programming in Keil

Not so long time ago, I decided to play with micro controllers. My first MCU was PIC and my first programs were in assembler code. That is because low memory space and need for fast execution. Later someone said that I should try to program it in C language. And that solved many problems, and things going faster. Then ‘hit the wall’ because of lack of information and examples how to do something. After that, some time spent in Arduino environment, which is fine for beginners and for really fast results. Just… after a while, there arrive need for something faster, and instead Atmel chips, I decided to use STM32 series. Luckily there are libraries and everything in Arduino IDE thing. But, some of the libraries does not behave as I wish. So, next logical choice is to use something that resembles ‘true C’ rather than ‘sketch’ things. Start is really difficult. For that reason, I want to share with you some experience in programming STM32 MCU family and Keil environment. Keil is free for use, but with limited options and with limited code use – maximum 32 kByte. For beginner like me, that is more than enough. Later will decide what will do – or buy (well, it is expensive), or switch to something else if exist at all.

Later will include some YouTube videos.

 

This page is still under construction…