Exploring STC 8051 Microcontrollers – Coding

General Purpose Input-Output (GPIO)

The very first thing to do with a new microcontroller is to play with its GPIOs and this is what we will begin with. STC micros are based on 8051 architecture and so it is no surprise that the GPIOs will have similarities with typical 8051s. GPIOs of STC micros are essentially same as those of Nuvoton N76E003. Those who have seen my past Nuvoton tutorials will find similarities.

BSP

//P00
#define P00_quasi_bidirectional_mode    do{bit_clr(P0M1, 0); bit_clr(P0M0, 0);}while(0) 
#define P00_push_pull_mode              do{P00_quasi_bidirectional_mode; bit_clr(P0M1, 0); bit_set(P0M0, 0);}while(0)
#define P00_input_mode                  do{P00_quasi_bidirectional_mode; bit_set(P0M1, 0); bit_clr(P0M0, 0);}while(0)
#define P00_open_drain_mode             do{P00_quasi_bidirectional_mode; bit_set(P0M1, 0); bit_set(P0M0, 0);}while(0)
 
#define P00_pull_up_enable              do{bit_set(P_SW2, 7); bit_set(P0PU, 0); bit_clr(P_SW2, 7);}while(0) 
#define P00_pull_up_disable             do{bit_set(P_SW2, 7); bit_clr(P0PU, 0); bit_clr(P_SW2, 7);}while(0) 
 
#define P00_schmitt_trigger_enable      do{bit_set(P_SW2, 7); bit_set(P0NCS, 0); bit_clr(P_SW2, 7);}while(0) 
#define P00_schmitt_trigger_disable     do{bit_set(P_SW2, 7); bit_clr(P0NCS, 0); bit_clr(P_SW2, 7);}while(0) 
 
#define P00_high                        bit_set(P0, 0)
#define P00_low                         bit_clr(P0, 0)
#define P00_toggle                      bit_tgl(P0, 0)
#define P00_get_input                   get_bit(P0, 0)

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
void setup(void);
 
void main(void)
{   
    setup();
    
    while(1)
    {
        P55_toggle;
        
        if(P52_get_input == 0)
        {
            delay_ms(400);
        }
        
        delay_ms(200);
    };
}
 
void setup(void)
{
    P55_open_drain_mode;
    
    P52_input_mode;
    P52_pull_up_enable;
}

Schematic

Explanation

Like always this first example is a simple variable flash rate LED flasher. Onboard LED connected to P5.5 and onboard push button connected to P5.2 are used. The board’s schematic shows us that P5.2’s push button must have an internal pull-up to properly function because it is not tied to any external pull-up resistor and P5.5’s LED must be configured as an open-drain output. These are configured so in the setup function.

 void setup(void)
{
    P55_open_drain_mode;    
    P52_input_mode;
    P52_pull_up_enable;
}

In the main loop, P5.5’s LED state is toggled every 200ms. If P5.2’s push button is pressed, P5.2’s state changes and so additional 400ms delay is added, making the total toggling delay 600ms. 

 P55_toggle;        
if(P52_get_input == 0)
{
    delay_ms(400);
}        
delay_ms(200);

Note no clock settings are applied and the micro is running at default clock frequency of 24MHz.

Demo

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Related Posts

2 comments

  • i always get excited when you release new tutorials ,you are really doing a great job i wish i could write code and develop libraries like you.

  • Well, this is very nice and thorough tutorial indeed, many thanks!
    Unfortunately I doubt there is good any reason to learn the STC platform beyond curiosity.
    The STC 8051, although pretty evolved from the original 8051 ISA, does not offer anything crucial to justify the relatively high price of these micros and development tools along with certain cumbersomeness of this ancient platform.
    They simply can not compete even with the legacy Cortex M0 in any way. I am even not aware about any affordable debugger/emulator for them.
    All in all, I would never recommend anybody to start learning/using any 8051 without some very good reason to do so.

Leave a Reply

Your email address will not be published. Required fields are marked *