Hack-a-tree

The 3D DIY Christmas tree soldering set only has a few preset patterns with the pre-flashed microcontroller.
In this blog you will learn to program the microcontroller yourself so that the individual LEDs can be controlled to display your own patterns on the tree.

 

The blog refers exclusively to that 3D DIY Christmas tree soldering kit.

1 Background information processor

The processor used is an STC15W408 from the Chinese manufacturer STCmicro.
This is based on an (8bit) 8051 CISC (Complex Iinstruction Set Ccomputing) architecture. This was introduced by Intel in 1980. For comparison, an ATmega328 is considered. This is based on the ARM RISC (Reducated Iinstruction Set Ccomputing) architecture. The concrete difference is the number of arithmetic operations that can be carried out in one cycle. The 8051 processors typically require multiple clock cycles for an arithmetic operation, whereas the ARM counterpart only requires a single clock for an operation. However, the STC15W408 is designed in such a way that there is no significant difference in performance between the two processors.

 

As already mentioned above, the STC15W408 is an 8 bit processor, which means that a single arithmetic operation is only possible with values ​​from 0-255. This would be a problem for complex projects, as the speed would be drastically reduced for higher numbers. Modern 32-bit processors should be used here, such as the ESP32.

You may be wondering why an 8 bit processor like the STC15W408 is used in this kit. This is because only individual IO pins are controlled here and no complex calculations are necessary.

This processor is therefore ideal for such an application as it is easy to program.

 

Pinout

Figure 1: Pinout of the chip

 

When looking at the pinout of the circuit, you will notice that the IO pins are labeled P0.X to P5.X. This is because the processor has 5 IO registers P0 to P5. The number after the register number represents the bit number in the register.

Figure 2: Pin register P1 (extract from the data sheet chapter 4.7)

 

2 Connection of the LED

There are 37 RGB effect LEDs on the boards, but not each LED can be switched on individually. In the image below, the connected groups are marked with numbers for easier assignment.

Figure 3: Side view with numbered LEDs

 

Since this numbering of the LED is the same on each side of the tree, the four sides are labeled with letters in order to be able to distinguish between them during pin mapping.

Figure 4: Top view with labeled pages

 

The following table shows all IO pins with the name of the connected LED. The missing IO 1.0 to 1.5 are not connected to the LEDs.

1.6

A2

3.0

B1

1.7

A1

3.1

B2

2.0

D3

3.2

B3

2.1

D4

3.3

B4

2.2

D5 + above

3.4

B5

2.3

A5

3.5

C5

2.4

A4

3.6

C4

2.5

A3

3.7

C3

2.6

D2

5.4

C2

2.7

D1

5.5

C1

 

 

Reset circuit

In order for the chip to be flashed, it must be briefly disconnected from the power and then reconnected. Therefore, a small transistor circuit must be built for the FT232, which is connected to the DTR Pin can control the power supply of the chip.

 

To build the programmer you will need:

FT232 Serial Converter
Hole grid board min. 8x13 (e.g. from the PCB assortment)

Socket strips

Cable (e.g. Silicone cable assortment)

Resistors 470Ω, 10kΩ, 100kΩ (e.g. from the Resistance assortment)

LED yellow (e.g. from the LED range)

NPN transistor BC547 (e.g. ->Amazon)

 

(optional: Pogopins or F2M jumper for the connection to the UART pins of the tree)

 

Figure 5: Programmer circuit on breadboard board

 

Build the circuit as shown above. It is recommended not to solder the right socket strip directly onto the PCB if you are not using pogo pins, as the programmer cannot be plugged in this way. Alternatively, we recommend connecting the socket strip to the PCB via cable.

 

If the programmer is set up correctly according to the circuit diagram, you can connect the kit to the kit via pogo pins or a pin strip. To do this, connect the circuit board to the four connections under the chip as follows.

 

P31 (TX) = RX (FT232)

P30 (RX) = TX (FT232)

3V3          = VCC (FT232)

GND        = emitter transistor

 

Make sure the FT232 jumper is set to 3V3, as too high a logic level could damage the chip.

Software

The STC15W408 is not compatible in the Arduino IDE. The PlatformIO plugin for VS Code is a good alternative to the usual Arduino IDE. Over 1000 different boards are already included here, including the STC chips.

 

First, install the Visual Studio Code IDE. You can download the installation file here download.

After the IDE is successfully installed and opened, go to Extensions. You can open this either via the left sidebar (see red marking in the figure) or using the keyboard shortcut Ctrl+Shift+X.

Figure 6: Extensions menu in the left sidebar

 

Search for the Platform IO plugin here and install it.

 

Figure 7: View in the extension management

 

Next, open the PlatformIO home page (Sidebar > Platform IO > Quick Access > PIO Home > Open) and click “New Project”.

 

Figure 8: Project configuration

 

In this window you can now select a name for the project and the board. Here you have to do as shown above STC15W408AS select. Finally, confirm the project configuration via the finish button.

 

After you have created the project, all that remains is to add the code. In order to keep the file with the relevant code as clear as possible, an external header file (.h) with all of the chip's required register declarations is used.

You can create a file suitable for the chip here by Vincent Defert on GitHub.

 

Once the file has been downloaded, you can place it in the directory in VSCode src copy.  Create a new file there with the name main.c, which will later contain the program to be executed.

 

Test code

As a first test, all LEDs should be turned on and then turned off again after a delay.
Now load the following program onto the microcontroller:

#include //(1)

#define ON
0x00 //(2)
#define OFF
0xff

void delay(unsigned int mils){
//(3)
    unsigned char i, j;

    while (mils--){
//(4)
        i=
16;
        j=
40;
        do { while (--j); } while(--i);
       
    }
}

void main(void) {
   
//(5)

    P5M1 =
0x00;      //set port 5 settings register 2 to 0
    P5M0 =
0xff;      //set port 5 settings register 1 to 1
                     
    P3M1 =
0x00;
    P3M0 =
0xff;

    P2M1 =
0x00;
    P2M0 =
0xff;

    P1M1 =
0x00;
    P1M0 =
0xff;

    P5 = OFF; //(6)      
    P3 = OFF;
    P2 = OFF;
    P1 = OFF;

    while(
1) //(7)
    {
        P1_6 = ON; //(8)
        P1_7 = ON;
        P2 = ON;
        P3 = ON;
        P5 = ON;
        delay(
2000);
        P1_6 = OFF;
        P1_7 = OFF;
        P2 = OFF;
        P3 = OFF;
        P5 = OFF;
        delay(
2000);
    }
}

 

Alternatively, the file is available here available for download.

 

Explanation:
(1) At the beginning the header file with the register definitions is integrated.

(2) Macros are then defined for the two LED states. This improves clarity in the routine part. Normally, an IO in the HIGH state switches on a connected LED. This is because the anode (+) of the LED is usually connected to the IO. On the PCB of the circuit board, however, the cathode (-) of the LED is connected to the IO, so it is only switched on in a LOW state.
Since an LED (light emitting diode) is, as the name suggests, a diode, it is blocked when the IO is in a HIGH state and therefore does not light up.

(3) Since no Arduino standard methods such as delay() are automatically integrated into the STC15W408, the delay() function is implemented here.

(4) The inner counting loop takes approximately one millisecond to complete. This loop is then executed with exactly the number of milliseconds that were passed as a parameter to the function. A simple delay can be implemented in this way, but this depends on the frequency of the processor clock and can also show deviations with longer delays.

(5) With the following register assignment, the IO pins are set to push-pull mode. This enables operation with high current on the IO pin. Further information can be found in the chip data sheet in chapters 4.9.2 & 4.7.

(6) Here the IO registers are set to the value HIGH so that all LEDs are switched off at the beginning.

(7) In the loop, the IO pins/LED are switched on and off with a pause of two seconds.

(8) In addition to setting an entire IO register, individual bits can also be set in the register; these correspond to the individual IO pins.

 

Uploading is done simply by clicking on the arrow in the bottom bar.

 

After the upload is successful, the following should appear in the terminal:

 

Figure 9: Output in VS Code Terminal

 

Note: When uploading for the first time, this is the first thing stcgal tool installed automatically, which is required for flashing the chip.

 

Conclusion

After this blog post, you can now program the STC15W408, which is used in the 3D Christmas Tree kit, yourself and display your own patterns on it.

Feel free to experiment with the countless patterns that are now possible with the kit.

Have fun replicating it :)

The following relevant sources were used to create the blog:
https://www.stcmicro.com/datasheet/STC15F2K60S2-en.pdf

https://de.wikipedia.org/wiki/Intel_MCS-51

https://de.wikipedia.org/wiki/Reduced_Instruction_Set_Computer

https://de.wikipedia.org/wiki/Complex_Instruction_Set_Computer

 

If you are interested, you can read these to deepen the basics. The GitHub repositories of the two open source projects stcgal and stc-mcu-open-source are also interesting.

Grundlagen elektronikGrundlagen softwareProjekte für anfängerSpecials

Laisser un commentaire

Tous les commentaires sont modérés avant d'être publiés

Articles de blog recommandés

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery