Programming STM32F103C8T6 in Arduino IDE

Publisher:ohp991养生的香菇Latest update time:2019-03-18 Source: eefocusKeywords:STM32F103C8T6  Arduino  IDE  Programming Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface

As time goes by, the microcontrollers in the hands of geeks have changed from the old 51 and PIC to AVR and STM32. Various convenient development tools have also appeared, such as the once popular Arduino. However, the AVR microcontroller to which Atmega328 belongs is still a bit old after all. It was the mainstream about 20 years ago. Now ARM is popular. Today we will play with the combination of Arduino and STM32.


After half a day's hard work, I finally got Arduino IDE to support my 32-bit board. The cost-effectiveness of STM32 chips is generally higher than that of the AVR microcontroller in Arduino, so the benefits are beyond words. Sharing this interesting gameplay here will make STM32 programming easier, and not as troublesome as in KEIL (no need to understand the underlying operation), making it easier for novices to get started. Without further ado, let's go...


Board + USB to TTL (CH340) + USB cable



 But in fact, the most classic and cheapest one is the following one, about 10 yuan (Taobao)



 The pins are defined as:



 I finally found a picture, and it feels like the functions are clear at a glance, just like uno, but not very clear (just make do with it, I will list them below)



software download

Version Arduino IDE1.8.7 https://www.arduino.cc/en/Main/Software


You can just click next during the installation process, but you need to know the installation directory. The default is C drive, and I installed it on D drive.



Once installed, the interface is very simple.



 Arduino_STM32 Download

Then go to github to download the code


Arduino_STM32:

https://github.com/rogerclarkmelbourne/Arduino_STM32



 Download the compressed file, unzip it, rename it and copy it to the hardware directory in your Arduino IDE installation directory.



 Now you can find the STM32 series board in Tools-Development Board



 Download and install the Arduino SAM development board

Next, open the Arduino IDE and go to Tools > Development Board > Development Board Manager in the Options bar. Download and install the Arduino SAM development board. If you don’t install this development board, arm-none-eabi-g++ errors will appear when you compile (this should be a cross compiler).




 Download and burn Bootloader

First we need to download Bootloader and enter another open source project of the person just mentioned. The address is as follows:

https://github.com/rogerclarkmelbourne/STM32duino-bootloader/tree/master/binaries


Select the file with the interface name corresponding to another LED of your STM32 minimum system board except the power indicator light (be sure to select the corresponding one). My board is PC13, so download this:



Then use a USB to serial cable to connect the development board and computer USB, where the TX RX of the serial cable is distributed to the development board PA10 PA9, then short BOOT0 to 3.3V and BOOT1 to GND to enter the burning state, and the VCC GND of the serial cable is distributed to the 5V of the development board without additional power supply.


Required tools: flash_loader_demo_v2.8.0


ST official download address: http://www.st.com/en/development-tools/flasher-stm32.html


The download was a bit difficult, I don't know if it was a network problem or a computer problem, it took a lot of effort to download it. To avoid trouble, here is a network disk download:


https://pan.baidu.com/s/1SfAn2l6k6tWdIS_jWSzVsg



 Select the serial port number of the USB serial port line. Note that boot0 needs to be 1 and boot1 needs to be 0 (all 0 by default)


 

 

 

 

Add the downloaded bin file in the red box below, click next to complete the download (burning of bootloder)


 


 Driver Installation

Enter the following directory D:\Arduino\hardware\Arduino_STM32\drivers\win, and run install_drivers.bat and install_STM_COM_drivers.bat as an administrator



 Then disconnect the serial port cable of the development board, connect the BOOT0 of the development board to GND, press and hold the reset button, use the mobile phone data cable to connect the MicroUSB of the development board and the USB of the computer to start installing the driver, and you will find a new USB serial device, which is COM11 below



 Download Routine Test

Set the upload mode of Arduino IDE to STM32duino bootloader



Change the port number to the new serial port number



Open a routine program, the LED flashes, the pin in the routine program is PB1, modify PC13 (corresponding to the board, you can also connect an LED without modification)



 The routine is as follows, then compile and upload:



 The Done message indicates that the upload was successful.



 Then you can see the LED flashing, you are done, drink a glass of water and take a rest.



 Finally, let’s look at some commonly used functions.


1. PWM pin

No. Physical Pin

0 PA0

1 PA1

2 PA2

3 PA3

6 PA6

7 PA7

8 PA8

9 PA9

10 PA10

16 PB0

22 PB6

23 PB7

Simple test program for PWM pins. There are 12 PWM pins in total. 16bit means 65536-level PWM, which is very precise:


void setup()

{

        pinMode(PB0, PWM);

        pinMode(PA7, PWM);

        pinMode(PA6, PWM);

        pinMode(PA3, PWM);

        pinMode(PA2, PWM);

        pinMode(PA1, PWM);

        pinMode(PA0, PWM);

        pinMode(PB7, PWM);

        pinMode(PB6, PWM);

        pinMode(PA10, PWM);

        pinMode(PA9, PWM);

        pinMode(PA8, PWM);

}

void loop()

{

        for (int i = 0; i < 65536; i++)

        {

                delayMicroseconds(40);

                pwmWrite(PB0, i);

                pwmWrite(PA7, i);

                pwmWrite(PA6, i);

                pwmWrite(PA3, i);

                pwmWrite(PA2, i);

                pwmWrite(PA1, i);

                pwmWrite(PA0, i);

                pwmWrite(PB7, i);

                pwmWrite(PB6, i);

                pwmWrite(PA10, i);

                pwmWrite(PA9, i);

                pwmWrite(PA8, i);

        }

}

It can also be expressed in serial numbers:


int pins[12] = {0, 1, 2, 3, 6, 7, 8, 9, 10, 16, 22, 23};

void setup()

{

  for (int i = 0; i < 12; i++)

  {

    pinMode(pins[i], PWM);

  }

}

void loop()

{

  for (int i = 0; i < 100; i++)

  {

    for (int j = 0; j < 12; j++)

    {

      pwmWrite(pins[j], i * i);

    }

    delay(20);

  }

}

2. ADC pin

Physical Pin ADC Channel

PA0 CH0

PA1 CH1

PA2 CH2

PA3 CH3

PA4 CH4

PA5 CH5

PA6 CH6

PA7 CH7

PB0 cannot be expressed using a sequence number

PB1 cannot be expressed in serial numbers

ADC test procedure:


void setup()

{

  Serial.begin(115200);

  pinMode(PB0, INPUT_ANALOG);

  pinMode(PA7, INPUT_ANALOG);

  pinMode(PA6, INPUT_ANALOG);

  pinMode(PA5, INPUT_ANALOG);

  pinMode(PA4, INPUT_ANALOG);

  pinMode(PA3, INPUT_ANALOG);

  pinMode(PA2, INPUT_ANALOG);

  pinMode(PA1, INPUT_ANALOG);

  pinMode(PA0, INPUT_ANALOG);

  pinMode(PB1, INPUT_ANALOG);

}

void loop()

{

  delay(50);

  Serial.print("\tPB0="); Serial.print(analogRead(PB0));

  Serial.print("\tPA7="); Serial.print(analogRead(PA7));

  Serial.print("\tPA6="); Serial.print(analogRead(PA6));

  Serial.print("\tPA5="); Serial.print(analogRead(PA5));

  Serial.print("\tPA4="); Serial.print(analogRead(PA4));

  Serial.print("\tPA3="); Serial.print(analogRead(PA3));

  Serial.print("\tPA2="); Serial.print(analogRead(PA2));

  Serial.print("\tPA1="); Serial.print(analogRead(PA1));

  Serial.print("\tPA0="); Serial.print(analogRead(PA0));

  Serial.print("\tPB1="); Serial.println(analogRead(PB1));

}

3. Allowed serial ports

There are three hardware external serial ports: Serial1, Serial2, Serial3, and one USB virtual serial port:


void setup()

{

  Serial.begin(115200);

  Serial1.begin(115200);  //TX=PA9,RX=PA10

  Serial2.begin(115200);  //TX=PA2,RX=PA3

  Serial3.begin(115200);  //TX=PB10,RX=PB11

}

void loop()

{

  delay(100);

  Serial.println("Test Serial");

  Serial1.println("Test Serial1");

  Serial2.println("Test Serial2");

  Serial3.println("Test Serial3");

}

There are also I2C, SPI, etc. There are related routines in the downloaded Arduino_STM32.


Conclusion

The main frequency of STM32 is 72M, which is much faster than that of atmega series, and it has more AD, PWM and serial ports (in short, it is much more powerful). I think if its library can be as rich as Arduino, it will basically be eliminated. Survival of the fittest, learning never ends, keep working hard!

Keywords:STM32F103C8T6  Arduino  IDE  Programming Reference address:Programming STM32F103C8T6 in Arduino IDE

Previous article:STM32's understanding of priority setting NVIC_SetPriority()
Next article:About the use of ST-Link to download STM32 programs

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号