STM32 Tutorial: #4 Using STM32Duino

Brief

This article is about how to use Arduino IDE and STM32duino to develop software for my STM32F103ZET6 development board (Type: PZ6806L). Arduino helps me out from a lot of hard C code such as GPIOX_xxxx and RCCxxx. Life should be easier and you deserve that.

To make STM32duino work with my board, I bought a ST-Link-v2, which officially supports STM32F10xx. Investment on better hardware can save you a lot of time. I won’t tell you how could I conclude this. According documents, the ST-Link should connect to STM32F10xx board in this way:
ST-Link pins are listed on the left side:
- SWDIO to PA13
- GND to GND
- SWCLK to PA14
- 3.3v to 3.3v
Check my board carefully, I found the PA13 and PA14 pins are on the left side. Also the 3.3v and GND are very close to them, named by Power Module. Connect pins carefully, make sure you won’t miss anything. Here is the
Official page about ST-Link.
ST-Link tools download link.

Install Arduino IDE and STM32duino

Download Arduino IDE and install it.
Note:
You’d better download the zip version and extract to your favorite place such as Desktop. As I know the dry version works much better than installer version.
Download STM32duino and install it.
Instruction of installing STM32duino.
Run Arduino IDE, look up the menu Tools\Board, a long list of STM32 chip set will appear.
Here is my configuration:
- Board -> Generic STM32F103Z series which is under STM32 Boards (STM32duino.com)
- Variant -> STM32F103ZE
- CPU Speed(MHz) -> 72MHz (Normal)
- Upload Method -> STLink I tried other options but no luck
- Optimize -> Size(default) The most important thing we consider
- Port -> COMx Varies, depends on your situation

Connect Dev Board

Shut off the dev board, connect ST-Link pins to board then plug ST-Link to PC’s USB port. Now the board will light up. If you are in Arduino IDE, just compile and upload, be patient. If everything is right, you will see the IDE shows message like this:

Sketch uses 16984 bytes (3%) of program storage space. Maximum is 524288 bytes.
Global variables use 3552 bytes (5%) of dynamic memory, leaving 61984 bytes for local variables. Maximum is 65536 bytes.
STM32 ST-LINK CLI v2.1.0
STM32 ST-LINK Command Line Interface

ST-LINK SN : Old ST-LINK firmware/ST-LINK already used
ST-LINK Firmware version : V2J17S4 (Need Update)
Old ST-LINK firmware detected!
Please upgrade it from ST-LINK->'Firmware update' menu.
Connected via SWD.
Connection mode : Normal.
Device ID:0x414 
Device flash Size : 512 Kbytes
Device family :STM32F10xx High-density

Loading file...
Flash Programming:
  File : C:\Users\igame\AppData\Local\Temp\arduino_build_214956\sketch_aug01a.ino.bin
  Address : 0x08000000
Flash memory programming...
北北北北北北北北北北北北北北北北北北北北北北北北北 0%
 0%圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹圹 100%
Flash memory programmed in 1s and 234ms.
Programming Complete.

MCU Reset.

Application started.

Congratulations! You just made it!

Port Program From CoIDE

Now we could port our bare C program written under CoIDE to Arduino. Here is the code. It’s much simpler.

#define DIGIT_SEGMENTS 8
#define LED_BEGIN 32
#define LED_END (LED_BEGIN + DIGIT_SEGMENTS - 1)
#define MAX_NUM 0x16

char DIGIT_MATRIX[MAX_NUM * DIGIT_SEGMENTS] = {
  1, 1, 1, 1, 1, 1, 0, 0, // 0
  0, 1, 1, 0, 0, 0, 0, 0, // 1,
  1, 1, 0, 1, 1, 0, 1, 0, // 2,
  1, 1, 1, 1, 0, 0, 1, 0, // 3,
  0, 1, 1, 0, 0, 1, 1, 0, // 4,
  1, 0, 1, 1, 0, 1, 1, 0, // 5,
  1, 0, 1, 1, 1, 1, 1, 0, // 6,
  1, 1, 1, 0, 0, 0, 0, 0, // 7,
  1, 1, 1, 1, 1, 1, 1, 0, // 8,
  1, 1, 1, 1, 0, 1, 1, 0, // 9,
  1, 1, 1, 0, 1, 1, 1, 1, // A,
  0, 0, 1, 1, 1, 1, 1, 1, // b,
  1, 0, 0, 1, 1, 1, 0, 1, // c,
  0, 1, 1, 1, 1, 0, 1, 1, // d,
  1, 0, 0, 1, 1, 1, 1, 1, // e,
  1, 0, 0, 0, 1, 1, 1, 1, // f,
  1, 0, 1, 1, 1, 1, 0, 1, // G,
  0, 0, 1, 0, 1, 1, 1, 1, // h,
  0, 1, 1, 0, 0, 0, 0, 1, // I,
  0, 1, 1, 1, 0, 0, 0, 1, // J,
  0, 1, 1, 0, 1, 1, 1, 1, // K,
  0, 0, 0, 1, 1, 1, 0, 1 // L
}; //

int num = 0;

void setup() {
  // put your setup code here, to run once:
  for(int i = LED_BEGIN; i <= LED_END; i++)
    pinMode(i, OUTPUT);
}

void showDigit(int x) {
  if (x >= 0 && x <= MAX_NUM) {
    char* ptr = DIGIT_MATRIX + x * DIGIT_SEGMENTS;

    for(int i = 0; i < DIGIT_SEGMENTS; i++) {
      digitalWrite(LED_BEGIN + i, *ptr == 1 ? LOW : HIGH); // NOTE: LOW to turn LED on!
      ptr++;
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  showDigit(num++);
  if (num > MAX_NUM) num = 0;
  delay(200);
}

NOTE
The Arduino digitalWrite() uses LOW to light LEDs up, not HIGH as I assumed.

Good luck!

Reference

  1. STM32F100 Value Line
  2. STM32F103ZE Introduction

你可能感兴趣的:(stm32)