stm32plus Examples Online Browser



stm32plus ships with a wealth of example code that exists to demonstrate the capabilities of the library and provide you with easy to reuse templates for your own code.

This is a large page so give it time to load and when it’s complete you can use your browser’s search capability to find what you need.

ads7843

/**
 * ADS7843 touch screen test, implements a basic drawing
 * application using an ILI9325 LCD.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 */

Click here to show the full ads7843 code

blink

button

/**
 * Auto-repeating button demo that doesn't use interrupts
 *
 * This demo assumes that you have a button on PA0 and an
 * LED on PD13. Hold down the button and the LED will
 * flash to indicate the presses. The default
 * configuration of PA0, PD13 and a LOW state when
 * pressed is correct for the STM32F4DISCOVERY board.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full button code

crc

/**
 * CRC demo. This demo will show how to calculate the
 * 32-bit CRC of a stream of bytes using the hardware
 * CRC peripheral on the STM32. The calculated CRC
 * will be output to USART1. USART1 is configured as
 * 57600-8-N-1.
 *
 * Usart1 (non-remapped) is not available on the
 * STM32F4DISCOVERY. If this is your target board then
 * choose a remapped pin set or move to another usart,
 * e.g. Usart2.
 *
 * The STM32 CRC peripheral operates on 32-bit words
 * with considerations for the endian-ness of the data
 * left to the user. stm32plus provides implementations
 * for little and big endian calculations with
 * customisable padding bytes for when your data stream
 * isn't a multiple of 32-bits long.
 *
 * Big endian mode:
 *   The words are bit-reversed before going to the CRC
 *   unit and the result is bit-reversed before
 *   returning to you.
 *
 * Little endian mode:
 *   No transformations at all are performed on the
 *   data.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full crc code

cs43l22_beep

/**
 * This example will sound the CS43L22 internal beep tone
 * twice per second. From experimentation I have
 * determined that the CS43L22 needs an incoming data
 * stream in order to generate the beep - just supplying
 * MCLK is not enough.
 *
 * Therefore for this demo I stream in a continuous
 * stream of nothing (zeros) while sounding the beep.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F407VGT6
 */

Click here to show the full cs43l22_beep code

dac_noise

/**
 * Demonstration of DAC channel 1 producing a pseudo-
 * random noise sequence. The output can be seen on PA4.
 * Attach an oscilloscope to see the noise wave or take
 * a look at waveform_f1.png in the example subdirectory
 * to see how it looks on the STM32F103ZET6.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F407VGT6
 *   STM32F103ZET6
 */

Click here to show the full dac_noise code

dac_triangle

/**
 * Demonstration of DAC channel 1 producing a triangular
 * wave superimposed upon a base output of 1/16 Vref.
 * Timer 6 is used as the trigger for each conversion.
 *
 * The output can be seen on PA4. Attach an oscilloscope
 * to see the triangular wave or take a look at
 * waveform_f1.png in the example subdirectory to see
 * how it looks on the STM32F103ZET6.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full dac_triangle code

dma_copy

/**
 * Demo of the DMA peripheral used to copy a block of
 * memory. We repeatedly copy a small 256 byte buffer
 * using the completion interrupt to signal the end
 * of each transfer. If all is well then a LED on PF6
 * will blink once per second.
 *
 * If this example is to be run on the STM32F4DISCOVERY
 * board then change the LED configuration from PF6 to
 * PD13 and invert the set() / reset() logic because
 * that LED is active HIGH.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full dma_copy code

dma_fill

/**
 * Demo of the DMA peripheral used to fill a block of
 * memory with a single byte. We will repeatedly fill a
 * buffer with 0xAA and check that it's done OK. If so
 * then a LED on PF6 will blink once per second.
 *
 * DMA1, channel 4 is used for this demo for F1
 * devices and DMA2, channel3, stream 4 is used for
 * F4 devices.
 *
 * If this example is to be run on the STM32F4DISCOVERY
 * board then change the LED configuration from PF6 to
 * PD13 and invert the set() / reset() logic because
 * that LED is active HIGH.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full dma_fill code

exti

/**
 * Button demo that uses EXTI interrupts to signal that
 * the button is pressed. EXTI allows you to process
 * input from GPIO pins asynchronously.
 *
 * This demo assumes that you have a button on PA8 and
 * an LED on PF6. The LED will light as long as the
 * button is held down.
 *
 * An Exti8 (external interrupt) is attached to PA8 and
 * is configured to trigger on both rising (pressed)
 * and falling (released) edges.
 *
 * To use this demo on the STM32F4DISCOVERY board you
 * will need to make the following changes to target the
 * onboard button and LEDs:
 *
 *  LED_PIN to 13
 * 	BUTTON_PIN to 0
 * 	GpioF... to GpioD...
 * 	Exti8 to Exit0
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full exti code

fatfs_iterate

/**
 * FAT file system iterator demo.
 *
 * This example will recursively iterate over the
 * directories and files on a FAT16/32 file system
 * on an SD card connected via SDIO. The SD card
 * must be inserted and ready when this application
 * runs. This demo was tested on a 4Gb class 10
 * SDHC microSD card.
 *
 * The output of the program is sent to a graphical
 * LCD, in this case an ILI9325 panel connected via
 * the FSMC. Other LCD drivers can be used by
 * changing the device driver name and ensuring that
 * the correct access mode is selected (8 or 16 bit).
 *
 * Because I use the STL string I have added
 * include/stl to the compile-time include path.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full fatfs_iterate code

fatfs_reader

/**
 * FAT file system reader demo.
 *
 * Tihs example will open a text file from a subdirectory
 * of the SD card and write the contents to USART1. The
 * filename is /files/test.txt. To run this demo you must
 * have USART1 wired to a terminal program on the PC that
 * is set up to receive at 57600-8-N-1 mode. I recommend
 * the free "RealTerm" program.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * The SD card must be inserted and ready when this
 * application runs. This demo was tested on a 4Gb class
 * 10 SDHC microSD card.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full fatfs_reader code

fatfs_writer

/**
 * FAT file system writer demo.
 *
 * This example will create a file called "test.txt" in a
 * directory called "output" on the SD card. If the
 * directory does not exist then it will be created. If
 * the file does exist then it will be deleted before we
 * open it for writing.
 *
 * When we're done writing to it we will re-open the
 * file and write the content to USART1.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * The SD card must be inserted and ready when this
 * application runs. This demo was tested on a 4Gb
 * class 10 SDHC microSD card.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full fatfs_writer code

fsmc_sram

/**
 * Demo of an SRAM chip connected to the FSMC on an
 * STM32F103ZET6 (LQFP144). In this case it's an ISSI
 * IS61LV25616 256K x 16 (4Mbit) SRAM on bank #3.
 *
 * This demo will write a repeating pattern to the SRAM
 * and read it back afterwards. If it is successful
 * then a LED attached to PF6 will be flashed for 500ms.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 */

Click here to show the full fsmc_sram code

hd44780

/**
 *
 * HD44780 20x4 character LCD demo. For this demonstration
 * we will attach the character LCD to the following pins:
 *
 * PC[0] => RS
 * PC[1] => ENABLE
 * PC[2] => D0
 * PC[3] => D1
 * PC[4] => D2
 * PC[5] => D3
 *
 * This demonstration attachs a text terminal class to the
 * LCD for convenient text output. We then go into an
 * infinite loop writing out the famous Lorem Ipsum
 * passage, scrolling the display each time the output
 * reaches the end of the display.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full hd44780 code

hx8347a

/**
 * HX8347A LCD test, show a looping graphics demo
 *
 * It's a 16-bit device and we control it in this demo
 * using the FSMC peripheral on bank 1. The wiring
 * that you need to do is as follows:
 *
 * PE1  => RESET
 * PD11 => RS (D/CX)
 * PD7  => CS
 * PD4  => RD
 * PD5  => WR
 * PD14 => D0    PE11 => D8
 * PD15 => D1    PE12 => D9
 * PD0  => D2    PE13 => D10
 * PD1  => D3    PE14 => D11
 * PE7  => D4    PE15 => D12
 * PE8  => D5    PD8  => D13
 * PE9  => D6    PD9  => D14
 * PD10 => D7    PD10 => D15
 * PD13 => Backlight PWM (if variable backlight)
 *
 * The code works without structural change on both
 * the F1 and F4. I had to additional wait states
 * to the data setup time to get my panel to work on
 * the F4. You may also have to adjust.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full hx8347a code

i2c_cs43l22

/**
 * I2C / I2S demonstration using the onboard CS43L22
 * peripheral of the STM32F4Discovery board. This
 * example will play a continuous 1kHz sine wave tone
 * at 44.1kHz. The duration of the sample is 3 seconds
 * and we just go into a loop playing it continuously.
 *
 * To make it interesting we'll use I2S interrupt
 * mode to supply the data. Plug some headphones into
 * the jack on the STM32F4Discovery to hear the output
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F407VGT6
 */

Click here to show the full i2c_cs43l22 code

ili9325

/**
 * ILI9325 LCD test, show a looping graphics demo
 *
 * It's a 16-bit device and we control it in this demo
 * using the FSMC peripheral on bank 1. The wiring that
 * you need to do is as follows:
 *
 * PE1  => RESET
 * PD11 => RS (D/CX)
 * PD7  => CS
 * PD4  => RD
 * PD5  => WR
 * PD14 => D0    PE11 => D8
 * PD15 => D1    PE12 => D9
 * PD0  => D2    PE13 => D10
 * PD1  => D3    PE14 => D11
 * PE7  => D4    PE15 => D12
 * PE8  => D5    PD8  => D13
 * PE9  => D6    PD9  => D14
 * PD10 => D7    PD10 => D15
 * PD13 => Backlight PWM (if variable backlight)
 *
 * The code works without structural change on both the
 * F1 and F4. You will most likely have to change the
 * timing  configuration to suit your panel and FSMC
 * bus speed.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full ili9325 code

ili9327

/**
 * ILI9327 LCD test, show a looping graphics demo
 *
 * It's a 16-bit device and we control it in this demo
 * using the FSMC peripheral on bank 1. The wiring
 * that you need to do is as follows:
 *
 * PE1  => RESET
 * PD11 => RS (D/CX)
 * PD7  => CS
 * PD4  => RD
 * PD5  => WR
 * PD14 => D0    PE11 => D8
 * PD15 => D1    PE12 => D9
 * PD0  => D2    PE13 => D10
 * PD1  => D3    PE14 => D11
 * PE7  => D4    PE15 => D12
 * PE8  => D5    PD8  => D13
 * PE9  => D6    PD9  => D14
 * PD10 => D7    PD10 => D15
 * PD13 => Backlight PWM (if variable backlight)
 *
 * The code works without structural change on both
 * the F1 and F4. You will most likely have to change
 * the timing configuration to suit your panel and FSMC
 * bus speed. I include working timings for the F1 and
 * F4 for the ILI9327 development board that I own.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full ili9327 code

ili9481

/**
 * ILI9481 HVGA LCD test, show a looping graphics demo
 *
 * It's a 16-bit device and we control it in this demo
 * using the FSMC peripheral on bank 1. The wiring that
 * you need to do is as follows:
 *
 * PE1  => RESET
 * PD11 => RS (D/CX)
 * PD7  => CS
 * PD4  => RD
 * PD5  => WR
 * PD14 => D0    PE11 => D8
 * PD15 => D1    PE12 => D9
 * PD0  => D2    PE13 => D10
 * PD1  => D3    PE14 => D11
 * PE7  => D4    PE15 => D12
 * PE8  => D5    PD8  => D13
 * PE9  => D6    PD9  => D14
 * PD10 => D7    PD10 => D15
 * PD13 => Backlight PWM (if variable backlight required)
 *
 * The code works without structural change on both the
 * F1 and F4. You will most likely have to change the
 * timing configuration to suit your panel and FSMC bus
 * speed. I include working timings for the F1 and F4
 * for the ILI9481 panel board that I own.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full ili9481 code

lds285

/**
 * MC2PA8201 LCD test, show a looping graphics demo
 *
 * The MC2PA8201 driver is used by many of the Nokia QVGA
 * cellphone LCDs including the N95 8Gb.
 *
 * It's an 8-bit device and we control it in this demo
 * using the FSMC peripheral on bank 1. The wiring that
 * you need to do is as follows:
 *
 * PE1  => RESET
 * PD11 => RS (D/CX)
 * PD7  => CS
 * PD4  => RD
 * PD5  => WR
 * PD14 => D0
 * PD15 => D1
 * PD0  => D2
 * PD1  => D3
 * PE7  => D4
 * PE8  => D5
 * PE9  => D6
 * PD10 => D7
 * PD13 => Backlight PWM (if variable backlight)
 *
 * There are no special considerations for the F1 versus
 * the F4. The code works on both without any change.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full lds285 code

mc2pa8201

/**
 * MC2PA8201 LCD test, show a looping graphics demo
 *
 * The MC2PA8201 driver is used by many of the Nokia QVGA
 * cellphone LCDs including 2730, 6300, N82 and N93.
 *
 * It's an 8-bit device and we control it in this demo
 * using the FSMC peripheral on bank 1. The wiring that
 * you need to do is as follows:
 *
 * PE1  => RESET
 * PD11 => RS (D/CX)
 * PD7  => CS
 * PD4  => RD
 * PD5  => WR
 * PD14 => D0
 * PD15 => D1
 * PD0  => D2
 * PD1  => D3
 * PE7  => D4
 * PE8  => D5
 * PE9  => D6
 * PD10 => D7
 * PD13 => Backlight PWM (if variable backlight)
 *
 * There are no special considerations for the F1 versus
 * the F4. The code works on both without any change.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full mc2pa8201 code

power

/**
 * Low power modes test. This example requires a
 * pushbutton wired to PA8 and an indicator LED wired
 * to PF6.
 *
 * The LED will continually flash as 250ms intervals
 * until the button is pressed at which point the MCU
 * will go into STOP mode pending an interrupt to wake
 * it up. Press the button again will cause the EXTI
 * interrupt that will wake it up.
 *
 * The commented out line _lpm.standby() can be
 * substituted for the stopInterruptWakeup() call to
 * cause the MCU to enter STANDBY mode. STANDBY can be
 * exited by WAKEUP, IWDG, RTC alarm and of course
 * external reset. STANDBY mode clears SRAM therefore
 * execution after wakeup is back at the start of your
 * program (the reset handler).
 *
 * To run this example on the STM32F4DISCOVERY board
 * change the LED pin to PD13 to use the onboard LED
 * and change the button pin to PA0 to use the onboard
 * user button. Also change Exti8 to Exti0 to match
 * the button pin number.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full power code

sdio

/**
 * SD card demonstration using the onboard SDIO peripheral
 * in high-speed 4-bit mode. This example will erase 100
 * blocks from 20000 to 20099, then write a bit-pattern to
 * each of these blocks and finally read back each block to
 * verify the bit-pattern. Blocks are always 512 bytes in
 * size, even if your card is a non-SDHC card that has
 * physical block sizes that are not 512 bytes.
 *
 * If all is OK then a LED on PF[6] will be flashed for
 * 1-second at the end of each of the erase, write, read
 * sequences. If anything goes wrong then the LED will
 * flash rapidly at 5Hz.
 *
 * The SdioDmaSdCard class encapsulates access to the SDIO
 * peripheral classes and uses DMA and interrupts to
 * transfer data. The default constructor will
 * automatically detect appropriate SDIO initialisation
 * and transfer frequencies based on targets of 200kHz and
 * 24MHz, respectively. You can tune these dividers by
 * using an SdioDmaSdCard constructor parameter of
 * 'false' (see the code comments below).
 *
 * Please note that this demo is destructive to any files
 * that happen to be on your SD card and are unfortunate
 * enough to be located on the blocks we target. You
 * should reformat the card after you're done.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F407VGT6
 *   STM32F103VET6
 *   STM32F103ZET6
 */

Click here to show the full sdio code

spi_send_dma

/**
 * This demo illustrates sending and receiving using the
 * SPI peripherals. A block of test data is sent from
 * SPI1 to SPI2 and, if successfully received, then a
 * LED on PF6 is flashed for 1 second. The test repeats
 * continuously.
 *
 * The DMA channels assigned to SPI1 and SPI2 are used
 * to perform the transfer.
 *
 * If you intend to run this example on the
 * STM23F4DISCOVERY board then replace PF6 with PD13 to
 * use the onboard LED.
 *
 * For this demo I'm going to need you to do a little
 * wiring to hook up SPI1 to SPI2 so that we can
 * exchange data over the MOSI pin. Here's the
 * connections that you need to make.
 *
 * 1MOSI/2MOSI: PA7  => PB15
 * NSS:         PA4 <=> PB12
 * SCLK:        PA5 <=> PB13
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full spi_send_dma code

spi_send_interrupts

/**
 * This demo illustrates sending and receiving using the
 * SPI peripherals. A block of test data is sent from
 * SPI1 to SPI2 and, if successfully received, then a LED
 * on PF6 is flashed for 1 second. The test repeats
 * continuously.
 *
 * The SPI interrupts are used to send and receive the
 * data. Note the code that adjusts the relative
 * interrupt priorities.
 *
 * If you intend to run this example on the
 * STM23F4DISCOVERY board then replace PF6 with PD13 to
 * use the onboard LED.
 *
 * For this demo I'm going to need you to do a little
 * wiring to hook up SPI1 to SPI2 so that we can
 * exchange data over the MOSI pin. Here's the
 * connections that you need to make.
 *
 * 1MOSI/2MOSI: PA7  => PB15
 * NSS:         PA4 <=> PB12
 * SCLK:        PA5 <=> PB13
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full spi_send_interrupts code

spi_send_sync

/**
 * This demo illustrates sending and receiving using the
 * SPI peripherals. A block of test data is sent from
 * SPI1 to SPI2 and, if successfully received, then a
 * LED on PF6 is flashed for 1 second. The test repeats
 * continuously.
 *
 * For this demo I'm going to need you to do a little
 * wiring to hook up SPI1 to SPI2 so that we can
 * exchange data over the MOSI pin. Here's the
 * connections that you need to make.
 *
 * If you intend to run this example on the
 * STM23F4DISCOVERY board then replace PF6 with PD13
 * to use the onboard LED.
 *
 * 1MOSI/2MOSI: PA7  => PB15
 * NSS:         PA4 <=> PB12
 * SCLK:        PA5 <=> PB13
 * *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full spi_send_sync code

timer_dma_pwm

/**
 * Timer demo that illustrates how to use a DMA channel
 * to automatically reload the PWM duty cycle.
 *
 * In this example timer 1 is configured to output a
 * PWM signal on channel 1. The timer's DMA channel for
 * update events is used to automatically reload the
 * PWM duty cycle from a sequence that we supply each
 * time the timer gets an update event.
 *
 * The PWM signal is output on PA[8]. Connect this to
 * a LED to see the fade feature.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_dma_pwm code

timer_dma_usart

/**
 * This example shows how to use a timer linked to a DMA
 * channel to transmit data automatically
 * to the USART peripheral.
 *
 * Timer1's update event is configured to fire once per
 * second. That update event is linked to a DMA channel
 * that targets the USART1 peripheral's TX register. For
 * our demo purposes we transmit a sample text string
 * continuously at a rate of 1 character per second.
 *
 * USART1 is used in default (unremapped) configuration:
 * 4800-8-N-1
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_dma_usart code

timer_dual_gpio_out

/**
 * Timer demonstration: Output a 1Hz toggle on/off signal
 * on PA0 and another 1Hz signal 500ms ahead of the first
 * one, output on PA1.
 *
 * The timer is configured to tick at 4000Hz with an auto-
 * reload value of 3999. Channel 1 is configured to output
 * an alternating signal on PA0 when the counter reaches
 * 3999. Similarly, channel 2 is configured to output an
 * alternating signal on PA1 when the counter reaches 1000.
 *
 * We configure the channel 1 compare value to be 3999
 * with an action of "toggle" and we enable GPIO output
 * for channel 1 on its default port of PA0.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_dual_gpio_out code

timer_dual_pwm_gpio_out

/**
 * Timer demonstration: Use timer 2 to output a 1Mhz PWM
 * wave on channel 1 (PA0) and channel 2 (PA1). The demo
 * will repeatedly increment the duty cycle on each
 * channel from 0 to 100% and back down to 0 over 800ms.
 * Channel 1 fades up while channel 2 fades down.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_dual_pwm_gpio_out code

timer_gpio_out

/**
 * Timer demonstration: Output a 1Hz toggle on/off signal
 * on PA0.
 *
 * To achieve the alternating 1Hz signal on PA0 (TIM2_CH1)
 * we first set up Timer2 to tick at 10kHz with its auto-
 * reload register set to 9999. It will take 2 seconds
 * for the timer to tick up to 9999 at which point it
 * will reset back to zero.
 *
 * We configure the channel 1 compare value to be 9999
 * with an action of "toggle" and we enable GPIO output
 * for channel 1 on its default port of PA0.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_gpio_out code

timer_input_capture

/**
 * Timer input capture demo.
 *
 * This demonstration will calculate the frequency of a
 * PWM signal and write it out to USART1 every 3 seconds.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Uart4.
 *
 * The USART protocol is 57600/8/N/1
 *
 * Timer5 channel 4 is used to generate a 100KHz PWM
 * signal. This signal is fed to Timer3 channel 3. Each
 * rising edge of the signal causes an interrupt to fire.
 * When two successive edges have been captured we
 * calculate and display the result.
 *
 * You will need to wire PA3 to PB0 to test this demo.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_input_capture code

timer_interrupts

/**
 * Timer demonstration: This demo uses the advanced
 * control timer Timer1 to do something not very
 * advanced at all. We set the timer to count up and
 * down, basically it oscillates between 0 and 5000 at
 * 1Khz. Each time one of the limits is hit the system
 * raises an 'Update' interrupt. We handle this
 * interrupt by toggling a LED on PF6. The net result
 * of this is that the LED flashes on and off for a
 * second in each of those states.
 *
 * If you are running this on the STM32F4DISCOVERY
 * board then you can replace all reference to PF6 with
 * PD13 to use the onboard LED on that board.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_interrupts code

timer_master_slave

/**
 * Timer master/slave demonstration. Use one timer to
 * switch another one on and off at predefined intervals.
 *
 * Timer2 is configured as the master timer with a
 * frequency of 2000Hz and a reload value of 8000.
 * Channel 1 is configured to generate a PWM wave with a
 * 25% duty cycle. That means 1 seconds on and 3 seconds
 * off.
 *
 * Timer3 is configured as the slave timer with a
 * frequency of 2000Hz and a reload value of 200 with
 * a GPIO output toggle feature so it flashes on/off
 * 5 times per second.
 *
 * If left to run freely timer3 would flash continually
 * at 5Hz. However, attaching it as a slave to the PWM
 * wave generated by timer2 means that it will flash for
 * 1 second then switch off for 3 seconds.
 *
 * The output is generated on PA6. You can wire that
 * directly to a LED or a logic analyser to see the
 * results. My STM32F103ZET6 development board has a LED
 * on PF6 so I've wired PA6 to PF6 and enabled PF6 for
 * output in this demo code.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_master_slave code

timer_pwm_gpio_out

/**
 * Timer demonstration: Use timer 2 to output a 1MHz PWM
 * wave on channel 1 (PA0). The demo will repeatedly
 * increment the duty cycle from 0 to 100% and back down
 * to 0 over 800ms.
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103ZET6
 *   STM32F407VGT6
 */

Click here to show the full timer_pwm_gpio_out code

usart_receive_dma

/**
 * USART test: synchronous receiving data
 *
 * This example will receive 5 characters from the PC
 * over USART1 using the DMA channel and then
 * immediately echo them back, also using the DMA
 * channel.
 *
 * Connect an RS232 cable from USART1 to your PC and
 * run a terminal program (there are many free terminal
 * programs) and type 5 characters into it. Those 5
 * characters will be echo'd back to the terminal.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_receive_dma code

usart_receive_interrupts

/**
 * USART test: asynchronous sending and receiving data
 * using interrupts
 *
 * This example will receive 5 characters on USART1 and
 * then immediately echo them back. Connect an RS232
 * cable from USART1 to your PC and run a terminal
 * program (there are many free terminal programs) to
 * see the data. The default (unremapped) USART1 pins
 * are used.
 *
 * We use interrupts to send and receive the data. The
 * hardware raises a TXE interrupt when it's ready to
 * send a character and an RXNE interrupt when data is
 * ready to receive.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_receive_interrupts code

usart_receive_sync

/**
 * USART test: synchronous receiving data
 *
 * This example will receive 5 characters from the PC
 * over USART1 and then immediately echo them back.
 * Connect an RS232 cable from USART1 to your PC and
 * run a terminal program (there are many free terminal
 * programs) and type 5 characters into it. Those
 * 5 characters will be echo'd back to the terminal.
 *
 * Note that if you are using the STM32F4DISCOVERY
 * board then you cannot use Usart1 since the pins
 * clash with onboard peripherals. I have tested
 * this code on that board using Usart2 and Uart4.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_receive_sync code

usart_send_dma

/**
 * USART test: sending data using the DMA peripheral
 *
 * This example will send the string "Hello World"
 * using the DMA peripheral assigned to USART1. USART1
 * is using standard, unremapped pins.
 *
 * Note that if you are using the STM32F4DISCOVERY
 * board then you cannot use Usart1 since the pins
 * clash with onboard peripherals. I have tested this
 * code on that board using Usart2.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_send_dma code

usart_send_dma_interrupts

/**
 * USART test: asynchronous sending multiple sequences of
 * data using DMA to transfer it and using DMA interrupts
 * to move through the sequence of data items to transfer
 *
 * This example will send the first few words from the
 * famous Lorem Ipsum quotation using USART1. Connect an
 * RS232 cable from USART1 to your PC and run a terminal
 * program (there are many free terminal programs) to see
 * the data. The default (unremapped) USART1 pins are used.
 *
 * The DMA channel associated with USART1 Tx is set up and
 * triggered. When the first transfer is complete an
 * interrupt fires and we use to initiate the next
 * transfer. When all transfers are complete we lock up.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_send_dma_interrupts code

usart_send_interrupts

/**
 * USART test: asynchronous sending data using interrupts
 *
 * This example will send the string "Hello World" using
 * USART1. Connect an RS232 cable from USART1 to your PC
 * and run a terminal program (there are many free
 * terminal programs) to see the data. The default
 * (unremapped) USART1 pins are used.
 *
 * We use interrupts to send the data. The hardware
 * raises a TXE interrupt when it's ready to send a
 * character and we do just that. When we get to the end
 * of the string we disable the TXE interrupt so it
 * doesn't fire again.
 *
 * Note that if you are using the STM32F4DISCOVERY
 * board then you cannot use Usart1 since the pins clash
 * with onboard peripherals. I have tested this code on
 * that board using Usart2.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_send_interrupts code

usart_send_sync

/**
 * USART test: synchronous sending data
 *
 * This example will send the string "Hello World" using
 * USART1. Connect an RS232 cable from USART1 to your PC
 * and run a terminal program (there are many free
 * terminal programs) to see the data. The default
 * (unremapped) USART1 pins are used.
 *
 * Note that if you are using the STM32F4DISCOVERY board
 * then you cannot use Usart1 since the pins clash with
 * onboard peripherals. I have tested this code on that
 * board using Usart2.
 *
 * The protocol is 57600/8/N/1
 *
 * Compatible MCU:
 * 	 STM32F1
 * 	 STM32F4
 *
 * Tested on devices:
 *   STM32F103VET6
 *   STM32F407VGT6
 */

Click here to show the full usart_send_sync code