Exploring STC 8051 Microcontrollers – Coding

Analogue-to-Digital Converter (ADC)

STC8A8K64S4A12’s 12-bit analogue-to-digital converter is an attractive feature. In the market, there are many enhanced 8051-core microcontrollers but most don’t have any built-in ADC while some have low resolution ones. Like the analogue comparator, the ADC of STC8A8K64S4A12 is very simple and have limited basic options. The speed of this ADC can reach up to 800ksps. However, some care needs to be taken in order to maintain accuracy and consistency in measurements. For better results it is better to use well-calibrated external voltage reference source and additional filtering. These are well documented in STC8A8K64S4A12 reference manual and a sample diagram is shown below.

Code

 #include "STC8xxx.h"
#include "BSP.h"
#include "LCD.c"
#include "lcd_print.c"
 
void setup(void);
 
void main(void)
{
  unsigned int ADC_count = 0x0000;
  float voltage = 0.0;
 
  setup();
 
  LCD_goto(0, 0);
  LCD_putstr("CH0/V:");
 
  LCD_goto(0, 1);
  LCD_putstr("CH1/V:");
 
  while(1)
  {
    ADC_count = ADC_get_result(CH0);
    voltage = (((float)ADC_count * 5.0) / 4095.0);
    print_F(10, 0, voltage, 3);
    
    ADC_count = ADC_get_result(CH1);
    voltage = (((float)ADC_count * 5.0) / 4095.0);
    print_F(10, 1, voltage, 3);
    
    delay_ms(400);
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 4, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  P10_input_mode;
  P11_input_mode;
 
  ADC_enable;
  ADC_result_format_right_aligned;
  ADC_set_conversion_speed(ADC_conv_256_CLKs);
 
  LCD_init();
  LCD_clear_home();
}

Schematic

Explanation

This ADC example utilizes polling method to get voltage reading from two ADC channels associated with pins P1.0 and P1.1.

System clock setting is very important and this is so because AD conversion speed is dependent of this clock.

where

 CLK_set_sys_clk(IRC_24M, 4, MCLK_SYSCLK_no_output, MCLK_out_P54); 

In our case, the system clock is 6MHz and so FADC is 732Hz. Though the conversion speed is not pretty impressive, it is good enough for this example as sensing voltage variations are not rapid. Nyquist criterion should always be kept in mind while sampling analogue signals.

ADC Input pins need to declared as inputs.

 P10_input_mode;
P11_input_mode;

ADC setup is pretty straight. All we need to do is to enable the ADC, select data output alignment and ADC clock speed prescalar.

 ADC_enable;
ADC_result_format_right_aligned;
ADC_set_conversion_speed(ADC_conv_256_CLKs);

Data output can be either left-aligned or right-aligned. Right-aligned data is easy to read and that is why it my choice.

The ADC reading function reveals this fact.

 unsigned int ADC_get_result(unsigned char channel)
{
    register unsigned int value = 0x0000;
       
    ADC_set_channel(channel);
    delay_ms(1);
 
    ADC_start_conversion;
    while(!check_ADC_flag);
    clear_ADC_flag;  
  
    value = ((ADC_RES << 8) | ADC_RESL);
  
    return value;
}

In the main, ADC channels 0 and 1 are read and their readings are converted to voltage. The voltages are shown on an LCD.

 ADC_count = ADC_get_result(CH0);
voltage = (((float)ADC_count * 5.0) / 4095.0);
print_F(10, 0, voltage, 3);
    
ADC_count = ADC_get_result(CH1);
voltage = (((float)ADC_count * 5.0) / 4095.0);
print_F(10, 1, voltage, 3);
    
delay_ms(400);

ADC reading can be improved by applying several techniques like using filters, averaging technique and so on. Special attention is needed while designing PCBs in order to minimize cross-talk between channels and noise from onboard digital circuitry.

Generally, AD conversion should be fast, ADC I/O pins must be set in high impedance mode and AVCC to VCC voltage difference should not be more than 0.3V.

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 *