STC8H Development (Part 2): Configuring and using the FwLib_STC8 package library in Linux VSCode

Publisher:breakthrough3Latest update time:2022-06-09 Source: eefocusKeywords:Linux  VSCode Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The previous section introduces the use of FwLib_STC8 in the Keil5 environment of Windows. The following describes the environment setup under VSCode in Linux (Ubuntu 20.04 is used in this article)


Configure the VSCode development environment to run the demonstration case

premise

VSCode + PlatformIO environment has been installed, and MCS-51 Platform has been configured. If not, please search for tutorials and instructions online.

Git is already installed on this machine

Frequency setting and parameter preparation

Because the chip's built-in RC clock frequency cannot be adjusted through the burning tool under Linux, the chip clock adjustment must be done by other methods.


The simplest and most direct way is to use STC-ISP under Windows to set the chip to the target frequency. This way, when you want to change the frequency in the future, you need to use STC-ISP again to modify it.

If you want to adjust the frequency under Linux in the future without using STC-ISP, then when burning STC-ISP, check the last item "Add important test parameters at the end of the program area". These parameters record the calibration of these frequencies of the chip at the factory. Read these parameters in the program, and you can directly set the corresponding frequency to be used in the future.

Code reference demo/tim/timer0_print_cpuid.c

For chips with version 7.4.4U or above, the frequency parameters have been fixed to the CHIPID read-only address and can be read directly

Code reference demo/mem/mem_read_chipid.c

If there is no Windows environment, you can adjust the calibration dynamically through code (less accurate) or calibrate through an oscilloscope (accurate)

For the second, third, and fourth cases, you need to get the following four values, which will be used when configuring the project below:


IRCBAND: frequency band number, the value range is [0x00, 0x11]. For STC8G and STC8H1K, since there are only two frequency bands, the value is [0x00, 0x01]

VRTRIM: voltage calibration value corresponding to the frequency band

ITRIM: corresponds to the frequency band and the trimming value of the target frequency. The above three parameters can be used together to determine a clock frequency.

LITRIM: Fine-tune the clock frequency generated by the above three parameters

Add STC8G, STC8H corresponding boards in PlatformIO

Because PlatformIO's MCS-51 only contains a few boards, they need to be added in advance. The board configurations for STC8G and STC8H can be downloaded from platform-intel_mcs51/tree/contrib-snovotill/boards.


Put all these json files into the boards directory of your local PlatformIO corresponding to MCS-51. The default path is the .platformio/platforms/intel_mcs51/boards directory under the user's home directory, as shown in the figure

1. Create a project in VSCode

Open VSCode, click the PlatformIO icon at the bottom to open the PlatformIO homepage

Click New Project to open the New Project dialog box.

Fill in the dialog box in order


Name: Enter the project name

Board: Select chip model

Framework: For MCS-51, this field is blank.

Location: Select the project directory. If you do not use the default directory, uncheck it and choose your own path.

Click Finish to create the project

During this time, a dialog box will pop up asking whether to trust the author of all files in this directory, select "Yes"

After the project is created, you can see the directory structure of the project


.

├── include # The project's header file path

├── lib # Library files used by the project, FwLib_STC8 will be placed in this directory

├── src # Source files of the project, all project codes are placed here

└── test # Test file of the project


On the right is the configuration information corresponding to the current environment

2. Import FwLib_STC8 package library

Right click on lib, click Open Containing Folder, open the folder

In the lib folder, make sure the path is correct. Right-click Open In Terminal, or press Ctrl + Alt + T to open the command line. You can also cd directly to this path.

Execute the git command to clone the project to the current path


#github

git clone https://github.com/IOsetting/FwLib_STC8.git FwLib_STC8

# or giteee

git clone https://gitee.com/iosetting/fw-lib_-stc8.git FwLib_STC8

Back to VSCode, you can see that there is a new directory FwLib_STC8 under the lib directory, and you can see the .h and .c files in it. PlatformIO will automatically configure the package library to the project according to the library.json in the package library

3. Compile the demo application

In the project tree, browse to the path FwLib_STC8/demo/uart, select uart1_timer1_tx.c, which is the demonstration code for using Timer1 as the baud rate generator for serial port 1. Right-click Copy.


Then right-click on src and click Paste to copy the code to the project code directory


Configure the compilation parameters below, open paltformio.ini, and add the parameters below,

The content after adding is


[env:stc8h3k32s2]

platform = intel_mcs51

board = stc8h3k32s2

build_flags =

    -D__CONF_FOSC=36864000UL

    -D__CONF_MCU_MODEL=MCU_MODEL_STC8H3K32S2

    -D__CONF_CLKDIV=0x02

    -D__CONF_IRCBAND=0x03

    -D__CONF_VRTRIM=0x19

    -D__CONF_IRTRIM=0x28

    -D__CONF_LIRTRIM=0x00

upload_protocol = custom

upload_port = /dev/ttyUSB0

upload_speed = 1152000

upload_flags =

    -p$UPLOAD_PORT

    -s$UPLOAD_SPEED

    -e

upload_command = ${platformio.packages_dir}/tool-stc8prog/stc8prog $UPLOAD_FLAGS -f $SOURCE


Explanation of the above compilation parameters


build_flags is the compilation parameter passed in during compilation

__CONF_FOSC is the clock frequency of the chip when it is running. 36864000UL represents 36.864MHz. If STC-ISP is used, it must be consistent with the set value.

__CONF_MCU_MODEL is the chip model. If it is not STC8H3K32S2, find the corresponding model in include/conf.h

__CONF_CLKDIV frequency division coefficient, the system clock is the result of FOSC divided by the frequency division coefficient, the default value is 0, which means no frequency division

__CONF_IRCBAND=0x03 The following four parameters are the frequency setting values ​​that need to be prepared in the previous preparation stage. If STC-ISP is used for setting, these four parameters can be omitted.

__CONF_VRTRIM=0x19

__CONF_IRTRIM=0x28

__CONF_LIRTRIM=0x00

Explanation of the above burning parameters


upload_protocol burning protocol, because stcgal can not burn stc8g, stc8h, here is set to custom, which means using a custom burning tool

upload_port port, connected to the serial port corresponding to the USB2TTL of the chip

upload_speed baud rate used for downloading

upload_flags The parameter used when downloading -e means erase

upload_command The complete command used for downloading

If the frequency is set by STC-ISP, the frequency setting statement in the sample code needs to be commented out.


void main(void)

{

    SYS_SetClock(); // <--- This line is used to directly set the RC clock according to the compilation parameters. If you use the clock set by STC-ISP, comment out this line

    // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt

    UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);


Now you can compile, click the PlatformIO icon on the left, then click PROJECT TASKS -> stc8h3k32s2 in the navigation bar (the name here may be different depending on your environment) -> General -> Build


If SUCCESS is displayed at the end, it means the compilation is successful.


4. Burn the firmware to the chip via stc8prog

Go to the stc8prog release page stc8prog/releases to download the latest version and extract the stc8prog executable file. Browse to your own PlatformIO packages installation directory, which defaults to the .platformio/packages directory in the user's home directory.

Create a directory tool-stc8prog below, and copy the stc8prog executable file just obtained to this directory


Go back to VSCode and you can execute the burning task.


Connect the USB2TTL and chip, click the PlatformIO icon on the left, click PROJECT TASKS -> stc8h3k32s2 in the navigation bar (the name may be different depending on your environment) -> General -> Upload, wait for the wait prompt to appear below, then power on the chip again and the download will start.

After the download is complete, it will automatically run. Through the serial port tool CuteCOM, or the command line screen /dev/ttyUSB0 115200, use the 8-bit baud rate 115200 connection, and you can observe the "T41U string" output every 1 second.



5. Run other demo cases

If you want to run other demo cases, delete the files in the include and src directories, copy the demo case files you want to run to the src directory, and compile and burn them according to the above steps.


Multi-environment configuration of PlatformIO

PlatformIO supports configuring multiple environments at the same time. Each environment uses different chips and parameters, and sets one of them as the default environment. You can use shortcut keys to directly compile and burn the default environment, which is much more convenient than Keil5.


The following is an example of multi-environment configuration of platformio.ini. Two environments are configured here, one is stc8h3k32s2_36m840hz for stc8h3k32s2 chip, the other is stc8h1k08_36m840hz for stc8h1k08, and stc8h3k32s2_36m840hz is set as the default environment. In development, you can directly use the shortcut keys Ctrl + F9 to invoke compilation, and use the shortcut keys Ctrl + Alt + U to invoke burning.


[platformio]

default_envs = stc8h3k32s2_36m840hz


[env:stc8h3k32s2_36m840hz]

platform = intel_mcs51

board = stc8h3k32s2

build_flags =

    ;--opt-code-size

    -D__CONF_FOSC=36864000UL

    -D__CONF_MCU_MODEL=MCU_MODEL_STC8H3K32S2

    -D__CONF_CLKDIV=0x02

    -D__CONF_IRCBAND=0x03

    -D__CONF_VRTRIM=0x19

    -D__CONF_IRTRIM=0x28

    -D__CONF_LIRTRIM=0x00

upload_protocol = custom

upload_port = /dev/ttyUSB0

upload_speed = 1152000

upload_flags =

    -p$UPLOAD_PORT

    -s$UPLOAD_SPEED

    -e

upload_command = ${platformio.packages_dir}/tool-stc8prog/stc8prog $UPLOAD_FLAGS -f $SOURCE

[1] [2]
Keywords:Linux  VSCode Reference address:STC8H Development (Part 2): Configuring and using the FwLib_STC8 package library in Linux VSCode

Previous article:STC8H Development (Part 3): Introduction and demonstration of analog-to-digital conversion ADC based on FwLib_STC8
Next article:STC8H Development (I): Configuring and using FwLib_STC8 package library in Keil5

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号