Precise delay with counter

Posted on September 19, 2014 at 1:47 PM

If you work at high speed, as STM32F4 devices do (84MHz or more) then this tutorial is not right for you. But, you are able to descrease system speed to any frequency basicly you want.

If you want to use delay with systick down timer, look at my library here.

You can use great solution, Systick timer to make an interrups for you. But you can also decrease speed to let’s say 42Mhz, and that systick every 42ticks checks for variable in interrupt handler and increase/decrease it if neccessary, that can take up to 10cycles. In practise, that’s the same if we have 32MHz clock instead of 42MHz.

I was making some experiments with delay functions with only one variable (counter) and delay in simple was like

but this method is not accuracy, because this works with system ticks and depends on core clock. But how we can make a delay like this, to be independent of core clock? Continue reading

Program STM32F4 with UART

Posted on September 17, 2014 at 11:24 PM

STM32F4 devices have great feature. They can be programed with only USB to UART converter. Each device has bootloader inside, which supports UART programming. This is not very great method for Discovery boards, because they have better and faster solution on board, ST-link. But if you are working own board, then this solution can be quite nice.

In this tutorial, I will go through step-by-step how to program device with USB->UART converter. I will use FTDI’s home made converter.

This will work on all STM32F4 boards (Discovery, Nucleo). Also, if you create own board. Continue reading

Library 37- BMP180 pressure sensor for STM32F4

Posted on September 17, 2014 at 4:11 PM

BMP180 is a digital pressure sensor, with builtin temperature sensor. Its range is between 300 and 1100 hPa (0.3 to 1.1Bar). In relating to the sea level, this is +9000 to -500 meters.

Sensor works with I2C communication at 100kHz.

If you want sensor from ebay, here is one link.

Sensor is quite bad created, because if you want to read pressure, you first need to read current temperature for pressure calculation. To read temperature you have to wait at least 4.5ms. After that, you first have to read temperature and then you can start with pressure measurement. Time for pressure measurement depends on mode you select. After pressure is measured, you can read uncompensated data from device. This is because device has one register for control temperature and pressure, and also one register for data. So both, temperature and  pressure can not be measured at same time.

This takes about at least 10ms for every stuff to be done. At the end a lot of calculations are needed to get valid pressure from device.

Because veeery long time for STM32F4, I split things into several functions, to start temp, read temp, start pressure, read pressure, etc.

I have also added possibility, that you calculate pressure right above the sea from known pressure at known altitude.

Sequency for reading useful data is:

  • Start temperature measurement
    • Wait 4.5ms at least
  • Read temperature
  • Start pressure measurement
    • Delay depends on measurement mode you select
  • Read pressure
  • Use pressure data



  • Read BMP180 temperature sensor
  • Read BMP180 pressure sensor
  • Calculate sea level pressure at from given know pressure and altitude
    • If you are at ex 1000meters and there si pressure 98000Pascals, then you can calculate pressure at sea level from this values

Continue reading

Library 36- DAC Signal generator for STM32F4

Posted on September 16, 2014 at 12:42 PM

For FFT project purpose, I needed simple really simple signal generator. I don’t have separate device at home, so I made on with STM32F4.

DAC Signal library uses timer for output generation and DMA for transferring data from memory to DAC peripheral. So this library does not use processor for controlling. Everything is behind the scenes.

You can use 4 different, really simple signal waves:

  1. Square
  2. Sawtooth
  3. Triangle
  4. Sinus

They are really simple, predefined signals. You can adjust signal frequency for specific wave. I tested some frequencies, and sinus worked just well at about 100kHz, but square works great to about 50kHz. Square wave has 50% duty cycle. If you need variable square wave duty cycle, then you should look at my PWM library or PWM tutorial.

This library is not a real signal generator, sinus signal is not going to negative. Signals have fake ground at the middle of DAC max value, so 2047 is fake ground, and sinus is going from 0 to 4095. If you want “negative” effect, then you should add a capacitor in series to the output, this will allow only AC component to go through. Capacitor varies between your signal frequency, but about 680nF should be just OK.



  • Supports 4 different simple predefined waves
  • Supports 2 DAC channels
  • Timer driven DMA
    • TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8 can be used

Continue reading

Library 35- LIS302DL or LIS3DSH accelerometer

Posted on September 15, 2014 at 10:36 PM

Maybe you notice one chip between four leds on STM32F4-Discovery board. This chip is accelerometer. On market, there are 2 versions of STM32F4-Discovery board. First release had LIS302DL (old board) and new release (current) has LIS3DSH device. LIS3DSH has 5 selectables full scales (2/4/6/8/16G), old LIS302DL has only 2 (2.3/9.2G).

I made a simple library which supports both devices. Library automatically recognize connected device, if there is any. You just have to check which is on board, because different devices have different possible settings. Both devices uses the same pinout on board. They use SPI for communication with STM32F4.

I have a new version of STM32F4-Discovery board, so LIS3DSH is in use. In that case i had to find someone who will test this for me. I found one, he is also very good with STM32F4 devices. His website is here.



  • Supports LIS302DL accelerometer
    • old STM32F4-Discovery boards
  • Supports LIS3DSH accelerometer
    • new STM32F4-Discovery boards
  • Supports sensor detection

Continue reading