Exploring STC 8051 Microcontrollers – Coding


Serial communication is probably the most widely used communication method. It is simple to implement and can be used to communicate with a computer literally directly without needing too many extra external hardware. Serial communication also has the distance advantage over other communication interfaces. Serial communication can be implemented using UART hardware peripherals of STC micros. Many sensors, communication devices like GSM modems and RF devices, RFIDs and other external devices use this simple and time-proven interface for communicating with host micros. UART is also the backbone of other communication methods like MODBUS, IrDA, LIN, etc.

To learn more about UART visit the following link:



 #include "STC8xxx.h"
#include "BSP.h"
#include "LCD.c"
#include "lcd_print.c"
void setup(void);
void main(void)
  unsigned char msg1[10] = {"MicroArena"};
  unsigned char msg2[10] = {"SShahryiar"};
  char i = 0x00;
  char rcv_1 = 0x00;
  char rcv_4 = 0x00;
  LCD_goto(0, 0);
  LCD_putstr("TXD1: ");
  LCD_goto(10, 0);
  LCD_putstr("RXD1: ");
  LCD_goto(0, 1);
  LCD_putstr("TXD4: ");
  LCD_goto(10, 1);
  LCD_putstr("RXD4: ");
    for(i = 0; i < 10; i++)
      LCD_goto(5, 0);
      LCD_goto(5, 1);
      rcv_1 = UART1_read_buffer();  
      rcv_4 = UART4_read_buffer();
      LCD_goto(15, 0);
      LCD_goto(15, 1);
void setup(void)
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
  UART1_init(9600, \
             UART1_baud_source_TMR2, \
             UART1_timer_12T, \
  UART4_init(9600, \
             UART4_baud_source_TMR4, \
             UART4_timer_1T, \



STC8A8K64S4A12 has four hardware UART peripherals. Of these four, UART1 has some advanced features that are rarely needed. The rest are pretty much alike. Like other communication hardware peripherals, pin configuration must be selected as alternative pin arrangements are available.

RXD         TXD         Hex            Option
P0.2        P0.3        0x00           option 1
P5.2        P5.3        0x04           option 2
#define UART4_pin_option(value)        do{P_SW2 |= value;}while(0)

UART, although asynchronous, need to transmit and receive data in timed-frame formats and so when two UART devices need to communicate with each other, they must negotiate a mutually agreed baud rate or else data will not recognized. It is like tuning to the right radio station for listening the music channel that we want to listen. Thus, baud rate generation is a very crucial part of UART peripheral. For baud rate generation, UARTs can either use Timer 2 or other timer having the same number as the UART itself, for example, in the case of UART3, only Timer 2 and Timer 3 can be used as baud rate generator. I personally recommend that we keep Timer 2 reserved for other applications or UART2 and use other timers independently as to avoid conflicts in different UART hardware. Yes, I know it contradicts with my past statement of keeping Timer 2 reserved for UART applications but what else can be done when multiple UARTs are used in an application. BSP functions take care of everything internally and so we can focus on coding.

The system clock is set to 12MHz. This is very important because timers are responsible for baud rate generation and are dependent on system clock.

 CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54); 

To setup UART, we need to specify baud rate, clock source of baud rate generator, i.e., the timer to be used, its prescale factor and system clock speed in hertz. 

 UART4_init(9600, \
           UART4_baud_source_TMR4, \
           UART4_timer_1T, \

UART reading and writing is done as shown below.

rcv_4 = UART4_read_buffer();

The demo here uses two built-in UARTs – UART1 and UART4, crisscrossed among themselves. Each transmitting and receiving the other’s message. Whatever each is sending and receiving is also shown on an LCD display.


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


Leave a Reply

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