STC8H Development (I): Configuring and using FwLib_STC8 package library in Keil5

Publisher:760802csyLatest update time:2022-06-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduce

FwLib_STC8 is a C language wrapper library for STC8G, STC8H series MCUs, suitable for rapid prototyping verification based on these MCUs.


Project address: Gitee FwLib_STC8

Mirror address: GitHub FwLib_STC8

To avoid the problems existing in the previous STC12 package library, this package library has been improved in the following aspects


Compatible with SDCC and Keil C51

The FwLib_STC12 package library only supports SDCC compilation, and the register naming is different from the official one, which makes the project code based on FwLib_STC12 incompatible with the project code developed by other Keil. In the development of FwLib_STC8, the naming is consistent with the official library file, and the adaptation to Keil C51 and SDCC is added. Projects based on FwLib_STC8 can be compiled using SDCC under Linux, or using Keil C51 environment under Windows.


Reduce code size

The FwLib_STC12 encapsulation library mostly uses functions to encapsulate registers, and the hierarchical calls between functions will increase the stack overhead, which will make the limited memory of 8-bit MCU even more stretched, and increase the size of the generated firmware. In the FwLib_STC8 encapsulation library, a large number of macro statements are used to encapsulate register operations, and the hierarchical call depth between methods is controlled, which effectively saves the firmware volume and stack occupancy.

The following describes the construction of the Keil55 development environment under Windows


Configure the Keil5 development environment to run the demonstration use case

premise

Keil5 has been installed and the STC plug-in has been configured. If not, please search for tutorials and instructions on the Internet.

(Optional) Git is installed on this machine and you can use git commands on the command line


1. Create a project directory

In the file browser, create a project directory. It is recommended that the directory path should not contain Chinese characters or spaces. For example, this example uses the path

2. Create a Keil5 project

Open Keil5, click Project -> New uVision Project, in the pop-up dialog box, select the directory you just created, fill in the project name, and click Save

3. Select MCU model

After clicking Save, the MCU selection dialog box will pop up. Select STC MCU Database.

Select your model in the list below and click OK to save. If there is no exact matching model, select a model with the same memory quantity in the same series. For example, I use STC8H3K32S2. If there is no such item in the list, I will choose STC8H3K64S4 which is also STC8H series 3K memory.

A dialog box will pop up asking if you want to copy STARTUP.A51, click Yes

4. Keil5 project structure

The Keil5 project has been created. At this time, you can actually add the official header file and develop directly using registers.

The directory structure of the project is


stc8h_demo/

|

│ STARTUP.A51

│ stc8h_demo.uvproj

├─Listings

└─Objects


5. Download FwLib_STC8

You can export the project code through git clone, or directly download the zip package and unzip it


Method 1: Export via git

Execute from the command line


#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

Method 2: Download the zip package and unzip it

Visit the project link at the top of this article, on the page, click "Clone/Download" to download the ZIP, and unzip it into a directory



6. Put FwLib_STC8 into the project directory

Copy the directory obtained in the previous step to the directory of the Keil5 project. After the copy is completed, the project directory structure is as follows


stc8h_demo/

|

│ STARTUP.A51

│ stc8h_test003.uvproj

├─FwLib_STC8

│ ├─demo

│ ├─include

│ └─src

├─Listings

└─Objects


Listing of directories in the file system

7. Add package library in Keil5

Go to Project -> Manage -> Project Items from the menu, or click the corresponding icon from the icon bar.


Open the file management dialog box, add an item in Groups, name it "FwLib_STC8", you can change it to another name as needed, click the newly added Group, click Add Files in the right column, and a file selection dialog box will pop up

In the file selection dialog, browse to FwLib_STC8src, select all c files (you can use Ctrl+A), click Add, then click Close,

You can see that the files have been added.

8. Add demonstration cases in Keil5

Keep the previous window open, click to select Source Group 1, and then click Add Files in the right column. A file selection dialog box will pop up.

Browse to FwLib_STC8demouart, select uart1_timer1_tx.c, this is a test example of serial port 1 using timer 1 as baud rate clock source

After adding, you can see the corresponding files added, click OK to save


You can see that the files you just added have already appeared in the project tree structure.

9. Configure the project in Keil5

It is not possible to compile the project structure directly. You also need to configure the environment variables. Click Project -> Options for Target... from the menu, or click the project options icon directly from the icon bar.

Open the Options dialog box


As shown above, select the C51 tab, and in the input box marked with "2", enter


__CX51__, __CONF_MCU_MODEL=MCU_MODEL_STC8H3K32S2,__CONF_FOSC=36864000UL


Explanation of the above items


__CX51__ is to configure the current compilation environment as C51

__CONF_MCU_MODEL, here set the corresponding MCU model, the current model is MCU_MODEL_STC8H3K32S2, if your chip is not this, you need to change it to the corresponding model, the specific model can be found in FwLib_STC8/include/fw_conf.h. Note: STC8G only lists the large model, just fill in the large model.

__CONF_FOSC is the oscillator frequency that this program will use when running. It depends on what frequency you will use when burning with STC-ISP. Fill in the corresponding number here. The last UL means that this number is an unsigned long integer.

Then click "3", in the pop-up dialog box, select the FwLib_STC8/include directory, which is the header file path for configuring the package library.


After configuration is complete,

Select the Output tab and check Create HEX File. This step is to generate a hex file for burning after compiling.

After completing the above, click OK to save


10. Adjust the code and compile

Because the clock frequency can be set directly using STC-ISP under Windows, there is no need to adjust the MCU clock in the code. Here you need to open the sample code, as shown in the figure below, comment out or delete the line SYS_SetClock();, and then save the file.

Click the button marked with "3" to compile the project. If you want to compile the whole project, press the button on the right. You can usually use the button on the left, or use the shortcut key F7.

Because many methods in the package library are not used, the compiler will prompt a warning, which can be ignored as long as the number of Errors is 0


11. Burn with STC-ISP

Select the corresponding chip signal in STC-ISP, click "Open Program File", select the hex file just generated, the file location is in the Objects directory of the project. Then click "Download/Program", after re-powering the chip, it will be downloaded and run.



12. Observe the serial port output

Use any serial port tool, connect to the corresponding serial port at a baud rate of 115200, and you can see the "T40UString" output every 1 second.



13. Run other use cases

If you need to run other use cases, remove the previous use case file from the project tree, and then follow the previous process to add other use case files again.

You can also create your own files and write code


Questions and Notes

Warning "WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS"

This error occurs because of unused functions in the encapsulation library, which can be ignored. If you do not want to display such warnings, you can use the following configuration:


From the menu, go to Project -> Options for Target…

Switch to the BL51 Misc tab

Enter 16 in Warnings, Disable Warning Numbers

Click OK to save.

If you run it again, there will be no L16 warning.


Error "ERROR L107: ADDRESS SPACE OVERFLOW"

This error occurs because the memory space is insufficient. You need to set it to PDATA or XDATA in Keil. Configure it as follows:


From the menu, go to Project -> Options for Target…

Switch to the Target tab

In Memory Model, select Compact: variables in PDATA or Large: variables in XDATA

Click OK to save.

PDATA is 256 bytes, XDATA has the largest space. For example, STC8H3K32S2 has 3K memory, and XDATA has 3K bytes. However, accessing XDATA is slower than PDATA.


Reasons for slow access to XDATA: 1) It needs to be accessed via MOVX, 2) The address is 16 bits so one more clock is needed to prepare the address


Finish

The above is the description of using FwLib_STC8 package library to configure the environment and run the demonstration case for Keil5 environment.

Reference address:STC8H Development (I): Configuring and using FwLib_STC8 package library in Keil5

Previous article:STC8H Development (Part 2): Configuring and using the FwLib_STC8 package library in Linux VSCode
Next article:STC8A, STC8G, STC8H series IRC internal oscillator frequency adjustment

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号