How to interface Serial ADC0831 with 8051 Microcontroller (AT89C51, AT89S51)

Introduction

It is an example of Serial ADC0831 To 8051 Microcontroller (AT89C51/AT89S51) Interface. ADC or Analog To Digital Converter reads analog signal level and convert digital/binary value against this level.

Circuit Diagram

Description

ADC is most widely used and important part in electronics. Most of the time in digital electronics we need to deal or process analog signals. Sensors often present their output in the form of analog signals such as temperature, light, gas and pressure sensors (analog voltage/current). As microcontroller can understand only digital quantities so there is need to convert these analog signal levels to corresponding digital value. This task of analog to digital conversion is performed by Analog To Digital Converter or ADC. Now a days in the market there are so many integrated circuits available in which whole circuit of ADC is integrated. So, ADC is available in a single chip. Some microcontrollers have built-in ADC. But, in case of traditional 8051 microcontroller we need to attach separate ADC chip. Different ADCs have different characteristics. These have different input voltage ranges. Some provide parallel output and some output data in serial stream of bits. These have different resolution say 4, 8, 10, 12 or even 24 bit. High resolution means smaller step and small step means ADC can sense small change in input signal. Let us consider an ADC with 4 bit resolution. It means it’s output may have 2(n) = 2(4) = 16 different levels. consider it’s input voltage range is 0v to 5v. So,

ADC Range = 5v – 0v = 5v

We have used 8 bit, serial output ADC ADC0831 in this project that has only three interface pins to read analog to digital converted value from it. So, less number of pins of microcontroller are used for this purpose and hardware is simple. Software is also simple in which we read ADC result by giving pulses to clock pin of ADC and reading back each bit from data pin of ADC. In parallel output ADC more pins of microcontroller are reserved and hardware becomes complex and so, difficult to troubleshoot.

Remember: Minimum parts used with minimum connections in a circuit make it simple, maintainable and easy to troubleshoot than a circuit with more parts and more connections but sometime it makes the software of microcontroller complex.

As discussed before three pins of ADC are connected to 8051 microcontroller that are

  1. Chip Select (CS)
  2. Clock (CK)
  3. Data (DO)

Remaining pins are

  1. Vref connected to 5v
  2. Vin- connected to ground
  3. Vin+ connected to input analog signal. Here, a wiper pin of variable resistor

Vin- is at 0v and Vref is at 5v so, ADC range is 5v – 0v = 5v. Analog input signal that is to be converted is applied to pin Vin+ of ADC0831. This signal may have level from 0v to 5v range. We have used variable resistor at ADC input just for testing and changing analog input. This change is sensed by microcontroller and is displayed on LCD.

Code Explanation

Here is function to initialize ADC.

1-  void Init_ADC0831()
2-  {
3-     adc_CLK = 0;
4-     adc_CS = 1;
5-     adc_Data = 1;
6-  }

In the above code we have initialized microcontroller pins for ADC, so that ADC is ready for conversion.

Following is the function that reads analog signal level through ADC.

01-  unsigned char ReadADC0831()
02-  {
03-	unsigned char Data;
04-	unsigned char i;
05-	adc_CS = 0;
06-	;;;;;;;
07-	for(i = 0; i < 10; i ++)
08-	{
09-		adc_CLK = 1;;;;;;;;
10-		Data <<= 1;
11-		Data |= adc_DATA;
12-		adc_CLK = 0;;;;;;;;
13-	}
14-	adc_CS = 1;
15-	return Data;
16-  }

At lines 03 we have defined a variable named "Data" in which we will store ADC result coming from ADC. At line 04 a counter variable for "for" loop is defined. At line 05 first we lower down or provide logic 0 to chip select pin of ADC that instruct ADC to start new conversion. After this we delay for some time at line 06 so that ADC complete it's conversion. For loop will be executed 10 times for each bit of ADC result. Actual bits are 8 but according to ADC0831 datasheet we will read 10 bits from ADC and will ignore first 2 bits. At line number 09 we set clock pin of ADC at logic high for bit reading. Line 10 space is made in variable "Data" at least significant bit position. Line 11 reads bit present on data pin of ADC and store this bit in "Data" variable at least significant bit position. After this we lower down ADC clock and cycle for single bit reading is complete. This cycle is repeated 10 times and all ADC result bits are read out of ADC. At line 14 we disable chip select by providing logic high to it and now whole ADC result is present in "Data" variable that we return for further processing.

Following is the main function or entry point of our microcontroller program

01-  void main()
02-  {
03-	unsigned char i;
04-	unsigned char Msg[20];
05-	unsigned int AverageValue;
06-	unsigned char Sample[5] = {0, 0, 0, 0, 0};

07-	InitSystem();
08-	while(1)
09-	{
10-		AverageValue = 0;
11-		for(i = 4; i > 0; i --)
12-		{
13-			Sample[i] = Sample[i - 1];
14-			AverageValue += Sample[i];
15-		}
16-		Sample[0] = ReadADC0831();
17-		AverageValue += Sample[0];
18-		AverageValue /= 5;
19-		sprintf(Msg, "   ADC = %d      \0", AverageValue);
20-		gotoxy_lcd(1, 2);
21-		print_lcd(Msg);
22-	}
23-  }

In this function we perform main task of our program. From line 03 to 06 different variable are defined that will be used next in the function. At line 07 we initialize our system by calling "InitSystem" function. "InitSystem" function initializes ADC, LCD and prints default message "ADC0831 To 8051 " on first line of 16x2 LCD. After this, while loop is executed. From line 10 to 18 we are reading ADC value and calculating 5 point moving average. From line 19 to 21 we are formatting our result in a string "Msg" and showing result on LCD. Steps from line 10 to 21 are repeated until power to microcontroller is applied.

Free Downloads

Interfacing-Serial-ADC0831-To-8051-AT89C51-1.rar with Proteus Simulation

Related Projects

8051 Basic LED Flasher

8051 To 16x2 LCD Interfaing

8051 Digital Clock On 16x2 LCD

More Projects

Resistor Color Codes

Resistor-Color-Codes
Electrical & electronics engineering students and professionals often need to calculate resistor values from it’s color bands or colors from resistor value. This smart tool in your pocket can help you in finding resistor value from colors or colors from value. Wherever you are, in class room, LAB, field or in the market for purchasing different resistors with different color bands, this handy tool will help you. So, there is no need to remember codes. It supports 4, 5 and 6 bands resistors. It requires no special permissions.

This smart and free app will do this work for you.

How It Works?

Colors To Value Calculation

Tap a color band to change it’s color. In the following figure current value of resistor is 47K Ohm.

Resistor-Color-Codes-Usage-0.png

Suppose we want to change this value by changing it’s colors as

  1. 1st most significant color to brown.
  2. 2nd most significant color to green.
  3. 3rd most significant color to black.
  4. 4th most significant color to orange.
  5. 5th most significant color to brown.
  6. 6th most significant color to blue.

Now we tap 1st most significant color and following color selection list will appear.

Resistor-Color-Codes-Usage-1.png

We select our 1st color brown from list. Now, color list will disappear and main screen will reflect this change and will show 1st color changed to brown as shown in the following figure.

Resistor-Color-Codes-Usage-2.png

Now, tap 2nd most significant color to change it’s color to green. Again color list will appear for 2nd band as shown below

Resistor-Color-Codes-Usage-3.png

We select green color from list and main screen will reflect the change as shown bellow

Resistor-Color-Codes-Usage-4.png

Here, we want 3rd color as black that is already black. So, we just leave it and move to next band that is 4th most significant band.

Resistor-Color-Codes-Usage-5.png

Tap 4th color and select orange color from color list that is shown below

Resistor-Color-Codes-Usage-6.png

Similarly, change next two colors 5th and 6th and finally we will find these results. Results show that this resistor is of 150K Ohm with 1% tolerance and 10ppm/C temperature coefficient.

Resistor-Color-Codes-Usage-10.png

Value To Colors Finding

Now, there is second case where we want to find out colors against a resistor value. Suppose, we want to find out colors against 10000 Ohm or 10K Ohm resistor. Enter value 10000 in the resistor input box as shown below

Resistor-Color-Codes-Usage-11.png

Note: Entered value will be considered in Ohm not K Ohm or M Ohm.

Now, tap/press “Done” button and app will show colors against 10000 Ohm/ 10K Ohm resistor as shown below.

Resistor-Color-Codes-Usage-12.png

Download this app from Google Play. Enjoy! this free app.

Disclaimer

We have designed this app at our best. We have tested it so many times but use it at your own risk. We will not responsible for any kind of loss in any form due to this app. If you find any bug or defect then please inform us at udigital.solutions@gmail.com. If you have any suggestions please, email us. We will be happy to hear from you.

Wire Gauge Calculator

Wire-Gauge-Calculator-5.jpg

During transformer/ciol design we have to decide how much thick wire will be able to carry required amount of charge through it. Wires are divided according to their thickness/area of cross section. Each wire is assigned a number according to Standard Wire Gauge (SWG). This number is called SWG number of that wire. We know, a thick wire will be able to carry more charge than a thin wire. Here, I have considered circular mils (cmils) as unit of cross sectional area measurement.

Now, there is no need to look up a table/chart against your required value of current for wire selection. This smart and free app will do this work for you.

How It Works?

It takes two values as input. First is Current that has to be passed through wire, so wire should be able to carry this value of current. Second value is Rate circular mils per ampere.
It generates two output values as well. First is SWG number of a wire that is safe for given value of current and it’s actual rate. Second value is the SWG number of next wire that may be or may be not near the required wire with it’s actual rate. You can decide yourself which SWG wire will be better for your current requirement.

Wire-Gauge-Calculator-0.jpg

First enter value of Current.

Wire-Gauge-Calculator-1.jpg

Now, enter value of Rate. In open air wires remain cool and can pass charge at more rate means with less cross sectional area a wire can pass more charge and so more current. But, when we use same wire in transformer/coil winding where wires are wound in layers, one layer over other and there is no air present between these layers but there is paper between them, so wires fail to get cool due to air, their charge passing capability is reduced and so, can pass charge at lower rates. Usually, we double rate (area) in transformers than in open air. We use 500 cmils/Amp value or more for transformers. There are so many other factors that affect this value, some of which are temperature, peak load time, working duration of machine per day etc. So, you have to choose better value of Rate considering all these factors.

Wire-Gauge-Calculator-2.jpg

Now, you press “CALCULATE” button and results will be shown to you as in the following figure.

Wire-Gauge-Calculator-3.jpg

Download this app from Google Play. Enjoy! this free app.

Disclaimer

We have designed this app at our best. We have tested it so many times but use it at your own risk. We will not responsible for any kind of loss in any form due to this app. If you find any bug or defect then please inform us at udigital.solutions@gmail.com. If you have a mind blowing idea of an app you can also share with us at this email.

How To Develop LED Signage

What is visible, sells. Yes, if you want to sell your product it should be visible to others. Otherwise it will be like a waste put in your store or shop for years. If you’re looking for a reliable wholesale backdrops printing company, get in touch with the team at Craftsmen.


How To Develop LED Signage from Rashid Mehmood on Vimeo.

If you have a product/business, peoples should have awareness regarding this. LED sign boards are the one of the best ways to make your product/business visible to your customers. You can buy the tools you will require on Shoppok. These have the following advantages

  1. Get your product/business visible to others.
  2. Get your product/business noticeable.
  3. Helps a product/business inside of a building/plaza to get visible to outside people.
  4. Visible during dark light times, in the evenings, nights and cloudy environment.
  5. More visible than painted or printed sign boards due to light.
  6. More noticeable to people due to blinking/flashing lights with attractive colors.
  7. Lights of different colors make the environment more pleasant.
  8. Your product/business easily gets space in the memories of peoples.
  9. Better return on investment. Initially, it looks costly but it returns your money back usually within 1 to 3 months.

So, it is worthy to make LED sign boards for others to support their business and earn handsome amount of money for you as well. I have written a book How To Develop LED Signage. In this book I have tried to teach you, how we can develop these LED sign boards. You can purchase this book from here. If you find any errors/mistakes or want to give suggestion please inform me at this email id developledsignage@gmail.com. I will be happy to hear from you for the improvement of this book. Thanks…

What is visible, sells.

What is visible, sells. Yes, if you want to sell your product it should be visible to others. Otherwise it will be like a waste put in your store or shop for years. If you have a product/business, peoples should have awareness regarding this. LED sign boards are the one of the best ways to make your product/business visible to your customers. These have the following advantages
1. Get your product/business visible to others.
2. Get your product/business noticeable.
3. Helps a product/business inside of a building/plaza to get visible to outside people.
4. Visible during dark light times, in the evenings, nights and cloudy environment.
5. More visible than painted or printed sign boards due to light.
6. More noticeable to people due to blinking/flashing lights with attractive colors.
7. Lights of different colors make the environment more pleasant.
8. Your product/business easily gets space in the memories of peoples.
9. Better return on investment. Initially, it looks costly but it returns your money back usually within 1 to 3 months.

So, it is worthy to make LED sign boards for others to support their business and earn handsome amount of money for you as well. In this book I have tried to teach you, how we can develop these LED sign boards. If you find any errors/mistakes or want to give suggestion please inform me at this email id developledsignage@gmail.com. I will be happy to hear from you for the improvement of this book. Thanks…

Categories Uncategorized

IO Ports

There are 4 IO ports in standard 8051 microcontroller of 8 pins each. These IO ports are used to communicate to external world (devices). We can read and write on all pins of all ports. Some port pins have more than one function. All ports pins are set for reading upon microcontroller reset. These port names are

  1. Port 0
  2. Port 1
  3. Port 2
  4. Port 3

But, in programming these ports are termed as P0, P1, P2 and P3.

Port 0

This is an 8 pin IO port from pin 32 to 39. It is an open drain port and requires external pullup resistors of usually 10K Ohm. To configure a port pin as output we write 0 to this pin. Below is the C code to configure pin 3 of P0 as output pin.

P0_3 = 0; // Configure pin 3 of P0 as output.

To set it again for reading we write 1 to this pin and C code for doing this for pin 3 will be

P0_3 = 1; // Configure pin 3 of P0 as input.

Alternate Functions of Port 0

P0 pins are also called a AD0-AD7. These are used for address bus and data bus when we connect external memory to 8051 microcontroller. Address and data are multiplexed on this port to save pins for other purposes.

Port 1

This is also 8 pin port from pin 1 to 8. It has internal pullups and there is no need to attach external pullups. We can attach devices like keypad or LCD with it for generic IO. We can configure it’s any pin for input or output in the same way as for P0. The code to configure a pin of P1 for output is

P1_5 = 0; // Configure pin 3 of P1 as output.

Similarly, we can configure P1 pin as input

P0_5 = 1; // Configure pin 3 of P1 as input.

Port 2

Port 2 or P2 has pins from 21-28. This also has internal pullups. This port can also be used for general purpose IO. This port pins are configured in the same way as others.

Alternate Functions of Port 2

This port takes part as address bus high byte when we connect external memory to 8051 microcontroller. It’s alternate pin names are A8-A15. As P0 is used for 8 bit data and low address byte and P2 is used for high address byte so both ports combined become 16 bit wide address bus. In this way 8051 microcontroller accesses 64KB of external memory.

Port 3

Port 3 has pins from 10-17. This port is also internal pullups. This port can also be used for general purpose IO but it has important alternate functions that are discussed next. The method for configuring this port for input/output is same as of other ports discussed above.

Alternate Functions of Port 3

This port has transmit and receive pins for serial port of microcontroller. It has 2 external interrupts and 2 counting inputs for both timers (Timer0 and Timer1). Further it has write enable and read enable pins for external memory (when it is used).

Alternate functions of P3 are

P3 Pin Function Alternate Function Pin Description
P3_0 IO RxD 10 Serial Port Receive
P3_1 IO TxD 11 Serial Port Transmit
P3_2 IO INT0 12 External Interrupt 0
P3_3 IO INT1 13 External Interrupt 1
P3_4 IO T0 14 Timer0 Input
P3_5 IO T1 15 Timer1 Input
P3_6 IO WR 16 Write Enable External RAM
P3_7 IO RD 17 Read Enable External RAM
Categories Uncategorized

Introduction

In 1980 Intel introduced a microcontroller standard MCS-51 also known as 8051. This standard provided a base for microcontrollers and ease of development for microcontroller based applications. This is simplest microcontroller with less but most widely used peripherals that are enough for majority of applications. Less peripherals in a microcontroller make it simple to learn and reduce learning time and efforts while more peripherals and features in a microcontroller make it complex to learn and increase learning time and efforts. Standard 8051 microcontroller has less peripherals and so best suits for newbies and students as compared to other microcontrollers like PIC, AVR or ARM that are complex to understand (due to more features and peripherals). Most of the developers easily learn and develop 8051 based applications. So, it’s simplicity attracts developers. It has not just a microprocessor but has important resources and peripherals on a single chip that makes design simple, low cost in terms of money, time and efforts as compared to microprocessor design that becomes complex and costly. Although external resources and peripheral can also be attached like in microprocessor based design. All peripherals can be mapped in memory address space of 8051 and this feature is also not available in most or all of other microcontrollers like PIC and AVR (however some of them may have this feature but not all). Because of this feature we can design Single Board Computer (SBC) system on 8051 microcontroller. Tools and IDEs are easily available for the development of 8051 microcontroller. Ability to develop 8051 based application is necessary to acquire degrees of various disciplines of engineering. Due to these characteristics 8051 microcontroller is one of the most widely used microncontroller today. After Intel different companies are now producing variants of 8051 standard microcontroller. All of these are based on 8051. Some of these companies are Atmel, Philips, Dallas, Infineon.
8051 based micontroller have the following major parts in it

  1. Memory
    • Program Memory
    • Data Memory
    • Special Function Registers (SFRs)
    • Bit Addressable Memory
  2. IO Ports
  3. Interrupts
  4. Timers
  5. Serial Port

Memory

There are 4 types of memory in standard 8051 microcontroller.

Program Memory

In standard 8051 microcontroller there is 4KB of on-chip or internal memory where program to be executed resides. This program is executed when microcontroller power is applied in the target circuit. This program is transferred using a programmer/burner device from PC to 8051 microcontroller.

Data Memory

In standard 8051 based microcontroller there is 128 bytes of data memory. Data memory is the place where our program data or variables reside. Stack also occupies space in this area of memory. This area belongs to address space from 0x00 (0 decimal) to 0x7f (127 decimal).

Special Function Registers

These are the registers that control or store status of specific behavior or data related to a function of 8051 microcontroller. SFRs belong to address space from 0x80 (128 decimal) to 0xff (255 decimal). But there are only 21 SFRs in standard 8051 microcontroller. Remaining 107 locations may be used by other derivatives of 8051 but not in standard 8051 microcontroller.

Bit Addressable Memory

As it’s name shows this is a memory area in which single bit can be addressed or accessed. There is no need of reading or writing whole byte to access a single bit. However we can also access whole byte as well. There are 16 bytes memory for this purpose and belongs to address space from 0x20 (32 decimal) to 0x2f (47 decimal).

IO Ports

Standard 8051 has 4 IO ports of 8 pins each. These ports are Port 0, Port 1, Port 2 and Port 3. These ports are used to communicate with external world. Some pins of ports have more than one function.

Interrupts

Interrupt is the signal to CPU for an event that requires immediate attention of CPU. An interrupt may be a hardware or software generated. Standard 8051 microcontroller has 5 Interrupts sources. These sources are

  1. External Interrupt 0 (INT0)
  2. Timer 0 Overflow Interrupt (TF0)
  3. External Interrupt 1 (INT1)
  4. Timer 1 Overflow Interrupt (TF1)
  5. Serial Port Interrupt
    Serial port interrupt is further divided into

    1. Receive Interrupt (RI)
    2. Transmit Interrupt (TI)

Timers

Timer is resource that is used to count events or to calculate time duration of an event or to simply calculate delay. In standard 8051 microcontroller there are 2 timers called Timer 0 and Timer 1. Both of these timer can be configured and run independently. Timers also generate overflow interrupt during counting.

Serial Port

Serial port is a hardware resource in 8051 microcontroller that convert serial stream of bits on receive pin “RxD” and convert it into parallel data and convert parallel data into serial stream of bits that is transmitted through transmit pin “TxD” (each bit one by one). Serial port is also called UART (Universal Asynchronous Receiver Transmitter). Serial port also uses a timer for calculation of baud rate or duration of a single bit. Serial port has receive and transmit interrupts associated with it. Using this serial port we can connect our 8051 microcontroller with PC, some other microcontroller, mobile or modem.

Resistor Color Code Calculator

Resistor is a basic building block in electronics circuitry. Resistor is used to limit current in a circuit because it presents opposition to charges. Resistors that are reasonably small, their values are shown as color bands on them. These color bands represent the value of that specific resistor.

Here is a simple free software tool “Resistor Color Code Calculator” by Micro Digital to easily calculate a 4 band (some times called 3 band) resistor value without knowing color codes. This is best for hobbies and new student of engineering.

You can select/change color for each of the four bands using combo boxes.

In the following example the resistor value is 4.7 K Ohm. Some times it is also written as 4K7 Ohm.

Downloads

Resistance-Color-Code-Calculator.exe

Visit Projects Page

How To Convert C Code Into Assembly in Keil 8051 Compiler

You can convert C code into assembly by following these steps.

1 – Right Click on Target & select “Options for Target” like in the following diagram

Now the following dialog will appear.

2 – Now select “Listing” Tab and new page will appear like in the following figure

3 – Now tick “Assembly Code” check box and press OK button.

4 – Compile your code.

5 – Check newly generated “lst” extension file like main.LST in this example.

This file will have actual C code and Assembly code generated corresponding to this C code. In this way problem is that compiler generates routines with labels that are not self exploratory. This compiler generated assembly code is no more optimized. But it may help us in so many ways. Some times it may be useful for converting our previously written C code into assembly and save our time to code it again from beginning.

Go to Main Projects Page