FwLib_STC8 Firmware Library

repository·master·Indexed 41 days ago

https://github.com/iosetting/fwlib_stc8

A lightweight firmware library for STC8G and STC8H series MCUs providing optimized access to on-chip resources. It is compatible with both SDCC and Keil C51 compilers and includes cross-compiler compatibility macros for memory space qualifiers, register access, and interrupts. The library includes documentation for ADC channel mapping, resolution references, and wiring guides for DS18B20 sensors in Normal, Parasite, and Simulated Parasite power modes.

Tokens
6.6K
Snippets
13
Records
15
Agent score
49%

What's inside FwLib_STC8

  1. Use Compatibility Macros for Keil and SDCC

    master

    FwLib_STC8 provides a set of macros to ensure code compatibility between Keil C51 and SDCC. To write portable code, use these macros instead of compiler-specific keywords.

    Key macros include:

    • Memory space qualifiers: __BIT, __DATA, __IDATA, __PDATA, __XDATA, __CODE.
    • Register access: SBIT(name, addr, bit), SFR(name, addr), SFRX(addr), SFR16X(addr).
    • Interrupts: INTERRUPT(name, vector), INTERRUPT_USING(name, vector, regnum).
    • Assembly: NOP().

    Detailed definitions can be found in include/fw_reg_base.h.

    |
    | ----------- | ---------------- | ---------------- |
    | __BIT | bit | __bit |
    | __DATA  | data  | __data |
    | __IDATA | idata | __idata |
    | __PDATA | pdata | __pdata |
    | __XDATA | xdata | __xdata |
    | __CODE | code | __code |
    | SBIT(name, addr, bit) | sbit name = addr^bit | __sbit __at(addr+bit) name |
    | SFR(name, addr) | sfr name = addr | __sfr __at(addr) name |
    | SFRX(addr) | (*(unsigned char volatile xdata *)(addr)) | (*(unsigned char volatile __xdata *)(addr)) |
    | SFR16X(addr) | (*(unsigned int  volatile xdata *)(addr)) | (*(unsigned int  volatile __xdata *)(addr)) |
    | INTERRUPT(name, vector) | void name (void) interrupt vector | void name (void) __interrupt (vector) |
    | INTERRUPT_USING(name, vector, regnum) | void name (void) interrupt vector using regnum | void name (void) __interrupt (vector) __using (regnum) |
    | NOP() | _nop_() | __asm NOP __endasm |
    |
  2. Wiring the DS18B20 Temperature Sensor

    master

    The DS18B20 is a digital thermometer that can be connected using three-wire or two-wire configurations. It supports long-distance connections for multiple chips on a single-wire bus.

    Normal Power Mode (Three-wire)

    Uses three connections. Voltage can be 3.3V or 5V.

    • P35 $\rightarrow$ DQ
    • GND $\rightarrow$ GND
    • 5V/3.3V $\rightarrow$ VDD

    Parasite Power Mode (Two-wire)

    Uses two connections where the DQ pin acts as both the data communication and power pin. In this mode, both the DS18B20's GND and VDD must be connected to GND. The host (STC8) must provide a pull-up resistor on the DQ pin to VCC; for STC8 microcontrollers, this can be configured via the PxPU register.

    Warning: Not all DS18B20 chips support parasite power mode. Some may only output 0 in this configuration. If your chip fails, use the Simulated Parasite Power Mode instead.

    • P35 $\rightarrow$ DQ
    • GND $\rightarrow$ GND and VDD
    • 5V (connected to DQ via pull-up)
  3. Wire a DS18B20 in Parasite Power Mode

    master

    In Parasite Power Mode, the DS18B20 uses a 2-wire connection where the DQ line also provides power.

    Requirements:

    • Both DS18B20 GND and VDD must be connected to system ground.
    • The DQ pin acts as the data/parasite power line and requires a pull-up resistor. You can enable this using the PxPU command in the library.

    Warning: Some inexpensive DS18B20 units may not function correctly in parasite mode (they may only output 0). If your sensor fails in this mode, use the 'Parasite Power Mode Emulation' method instead.

    /* Parasite Power Mode Wiring */
    // P35   -> DQ
    // GND   -> GND -> VDD
    // 5V    -> (Not connected to sensor VDD/GND directly, used for pull-up source)
  4. Emulate Normal Power Mode for unreliable Parasite Mode sensors

    master

    If your DS18B20 sensor does not work in native parasite mode, you can use a hardware circuit to force it to operate in Normal Power Mode while maintaining a 2-wire physical connection. This requires adding a diode and a capacitor.

    Components needed:

    • One 1N4148 diode.
    • One 0.1uF capacitor.

    Circuit Layout:

    1. Connect the DQ line to the anode of the 1N4148 diode.
    2. Connect the cathode of the diode to the DS18B20 VCC pin.
    3. Connect the 0.1uF capacitor between the DS18B20 VCC and GND pins.
    4. Connect the DS18B20 GND to system ground.
    +-----1N4148-|>|-----+
          |                    |
          |     |DS18B20|-VCC--+
          |     |       |      |
    P35 --+-DQ--|DS18B20|     0.1uF
                |       |      |
    GND ----GND-|DS18B20|-GND--+
  5. Wire a DS18B20 in Normal Power Mode

    master

    To use the DS18B20 in a standard 3-wire connection (Normal Power Mode), connect the pins as follows:

    • DQ (Data): Connect to a GPIO pin (e.g., P35).
    • GND: Connect to system ground.
    • VDD: Connect to 5V or 3.3V.

    This mode provides a stable power supply to the sensor via a dedicated VDD line.

    /* Example Pin Mapping */
    // P35 -> DQ
    // GND -> GND
    // 5V/3.3V -> VDD
  6. Quick Start with SDCC Makefile

    master

    To use FwLib_STC8 with the SDCC compiler via Makefile, follow these steps:

    1. Clone the repository:
      git clone https://github.com/IOsetting/FwLib_STC8.git FwLib_STC8
    2. Configure the Makefile: Edit the Makefile to match your specific MCU hardware and toolchain:
      • Set MCU (e.g., STC8H1K08).
      • Set MCU_IRAM, MCU_XRAM, and MCU_CODE_SIZE based on your chip's memory layout.
      • Set TOOCHAIN_PREFIX to your SDCC binary path.
      • Configure LIB_FLAGS with hardware-specific constants like __CONF_FOSC, __CONF_MCU_MODEL, __CONF_CLKDIV, __CONF_IRCBAND, __CONF_VRTRIM, __CONF_IRTRIM, and __CONF_LIRTRIM.
    3. Compile:
      make clean; make
    MCU                 ?= STC8H1K08
    MCU_IRAM            ?= 256
    MCU_XRAM            ?= 1024
    MCU_CODE_SIZE       ?= 8192
    
    TOOCHAIN_PREFIX     ?= /opt/sdcc/sdcc-4.3.0/bin/
    
    LIB_FLAGS           := __CONF_FOSC=36864000UL \
                        __CONF_MCU_MODEL=MCU_MODEL_STC8H1K08 \
                        __CONF_CLKDIV=0x00 \
                        __CONF_IRCBAND=0x01 \
                        __CONF_VRTRIM=0x1F \
                        __CONF_IRTRIM=0xB5 \
                        __CONF_LIRTRIM=0x00 
  7. Quick Start with Keil C51

    master

    To integrate FwLib_STC8 into a Keil C51 project:

    1. Clone the repository and copy/move the files into your Keil project directory.
    2. Add to Project: Use Project -> Manage -> Project Items to add the library files to your project.
    3. Configure Preprocessor Symbols:
      • Go to Project -> Options for Target ....
      • Switch to the C51 tab.
      • In the Preprocessor Symbols -> Define field, add: __CX51__, __CONF_MCU_MODEL=MCU_MODEL_STC8H3K32S2,__CONF_FOSC=36864000UL
      • Note: __CX51__ enables Keil-specific keyword macros. __CONF_MCU_MODEL should be set to your specific MCU type (refer to FwLib_STC8/include/fw_conf.h for available types). __CONF_FOSC should match the frequency your MCU is trimmed to in STC-ISP.
    4. Include Header: Add #include "fw_hal.h" to your source code.
    #include "fw_hal.h"
  8. Simulated Parasite Power Mode for DS18B20

    master

    If your DS18B20 chip does not work in standard Parasite Power Mode, you can use a 0.1uF capacitor and a 1N4148 diode to implement a two-wire connection. In this setup, the DS18B20 actually operates in Normal Power Mode.

    Circuit Diagram:

          +-----1N4148-|>|-----+
          |                    |
          |     |DS18B20|-VCC--+
          |     |       |      |
    P35 --+-DQ--|DS18B20|     0.1uF
                |       |      |
    GND ----GND-|DS18B20|-GND--+
    +-----1N4148-|>|-----+
          |                    |
          |     |DS18B20|-VCC--+
          |     |       |      |
    P35 --+-DQ--|DS18B20|     0.1uF
                |       |      |
    GND ----GND-|DS18B20|-GND--+
  9. Fix VS Code IntelliSense errors for SDCC

    master

    VS Code does not fully support SDCC's Intel 8051 syntax, which may cause error highlighting in your editor. To fix this, update your .vscode/c_cpp_properties.json file:

    1. Add "__SDCC_SYNTAX_FIX" to the defines array.
    2. Add the SDCC include path to the includePath array (e.g., "[sdcc path]/share/sdcc/include/**").
    {
        "configurations": [
            {
                "defines": [
                    "__SDCC_SYNTAX_FIX"
                ],
                "includePath": [
                    "...",
                    "/opt/sdcc/sdcc-4.3.0/share/sdcc/include/**"
                ]
            }
        ]
    }
  10. Fix VS Code Syntax Errors for SDCC

    master

    VS Code's C++ extension may report syntax errors when using SDCC-specific Intel 8051 syntax. To resolve this, update your .vscode/c_cpp_properties.json file:

    1. Add "__SDCC_SYNTAX_FIX" to the defines array.
    2. Add the SDCC include path to the includePath array (e.g., "[sdcc path]/share/sdcc/include/**").
    {
        "configurations": [
            {
                "defines": [
                    "__SDCC_SYNTAX_FIX"
                ],
                "includePath": [
                    "...",
                    "/opt/sdcc/sdcc-4.3.0/share/sdcc/include/**"
                ]
            }
        ]
    }
  11. Check ADC resolution and channel counts for STC8H series

    master

    The ADC resolution and the number of available channels vary depending on the specific STC8H series being used. Use this reference to determine the precision (bit-depth) and capacity of your hardware before configuring ADC software.

    | 系列              |   精度 |  通道数      |
    |  ---------------- | ---------- | -------------- |
    | STC8H1K08 series     | 10-bit     |    9 channels  |
    | STC8H1K28 series     | 10-bit     |    12 channels |
    | STC8H3K64S4 series   | 12-bit     |    12 channels |
    | STC8H3K64S2 series   | 12-bit     |    12 channels |
    | STC8H8K64U series    | 12-bit     |    15 channels |
    | STC8H2K64T series    | 12-bit     |    15 channels |
    | STC8H4K64TLR series  | 12-bit     |    15 channels |
    | STC8H4K64TLCD series | 12-bit     |    15 channels |
    | STC8H4K64LCD series  | 12-bit     |    15 channels |
  12. ADC Channel and Pin Mapping for STC8G/STC8H

    master

    This table maps the ADC channel index (binary representation) to the physical I/O pin for various STC8G and STC8H microcontroller series. Use this to identify which GPIO pins to connect to your analog signals based on the ADC channel you wish to sample.

    |       |   STC8H1K28  | STC8H1K08    | STC8H3K64S4<br>STC8H3K64S2    | STC8H8K64U<br>STC8H2K64T<br>STC8H4K64TLR    | STC8H4K64TLCD<br>STC8H4K64LCD   | STC8G1K08A | STC8G1K08<br>STC8G1K08T   | STC8G2K64S4<br>STC8G2K64S2 |     
    |  ---- | ------------ | ------------ | -------------- | --------------- | -------------- | ---------- | -----------  | ----------- |             
    |  0000 | P1.0/ADC0    |  P1.0/ADC0   |  P1.0/ADC0     |  P1.0/ADC0      |  P1.0/ADC0     |  P3.0/ADC0 |  P1.0/ADC0   |  P1.0/ADC0  |                    
    |  0001 | P1.1/ADC1    |  P1.1/ADC1   |  P1.1/ADC1     |  P1.1/ADC1      |  P1.1/ADC1     |  P3.1/ADC1 |  P1.1/ADC1   |  P1.1/ADC1  |                    
    |  0010 | P1.2/ADC2    |  N/A         |  P1.2/ADC2     |  P5.4/ADC2      |  P5.4/ADC2     |  P3.2/ADC2 |  P1.2/ADC2   |  P1.2/ADC2  |              
    |  0011 | P1.3/ADC3    |  N/A         |  N/A           |  P1.3/ADC3      |  P1.3/ADC3     |  P3.3/ADC3 |  P1.3/ADC3   |  P1.3/ADC3  |                
    |  0100 | P1.4/ADC4    |  N/A         |  N/A           |  P1.4/ADC4      |  P1.4/ADC4     |  P5.4/ADC4 |  P1.4/ADC4   |  P1.4/ADC4  |                 
    |  0101 | P1.5/ADC5    |  N/A         |  N/A           |  P1.5/ADC5      |  P1.5/ADC5     |  P5.5/ADC5 |  P1.5/ADC5   |  P1.5/ADC5  |                   
    |  0110 | P1.6/ADC6    |  N/A         |  P1.2/ADC6     |  P1.6/ADC6     |  P6.2/ADC6     |  N/A       |  P1.6/ADC6   |  P1.6/ADC6  |        
    |  0111 | P1.7/ADC7    |  N/A         |  N/A           |  P1.7/ADC7      |  P6.3/ADC7     |  N/A       |  P1.7/ADC7   |  P1.7/ADC7  |        
    |  1000 | P0.0/ADC8    |  P3.0/ADC8   |  P0.0/ADC8     |  P0.0/ADC8      |  P0.0/ADC8     |  N/A       |  P3.0/ADC8   |  P0.0/ADC8  |              
    |  1001 | P0.1/ADC9    |  P3.1/ADC9   |  P0.1/ADC9     |  P0.1/ADC9      |  P0.1/ADC9     |  N/A       |  P3.1/ADC9   |  P0.1/ADC9  |              
    |  1010 | P0.2/ADC10   |  P3.2/ADC10  |  P0.2/ADC10    |  P0.2/ADC10     |  P0.2/ADC10    |  N/A       |  P3.2/ADC10  |  P0.2/ADC10 |                     
    |  1011 | P0.3/ADC11   |  P3.3/ADC11  |  P0.3/ADC11    |  P0.3/ADC11     |  P0.3/ADC11    |  N/A       |  P3.3/ADC11  |  P0.3/ADC11 |                     
    |  1100 | N/A          |  P3.4/ADC12  |  P0.4/ADC12    |  P0.4/ADC12     |  P0.4/ADC12    |  N/A       |  P3.4/ADC12  |  P0.4/ADC12 |                     
    |  1101 | N/A          |  P3.5/ADC13  |  P0.5/ADC13    |  P0.5/ADC13     |  P0.5/ADC13    |  N/A       |  P3.5/ADC13  |  P0.5/ADC13 |                     
    |  1110 | N/A          |  P3.6/ADC14  |  P0.6/ADC14    |  P0.6/ADC14     |  P0.6/ADC14    |  N/A       |  P3.6/ADC14  |  P0.6/ADC14 |                     
    |  1111 | 1.19Vref     |  1.19Vref      |  1.19Vref       |  1.19Vref       |  1.19Vref      |  1.19Vref    |  1.19Vref    |  1.19Vref   |