Electronic Dice Using Arduino

Description

It is Electronic Dice Game Using Arduino. It is basic arduino learning project that shows how we access arduino pins for writing. Like in basic arduino LED flasher

Circuit Diagram

In circuit diagram 7 LEDs are connected each with 220 Ohm resistor in series to arduino.

Detail

It is a game in which we press button connected to pin 6 of arduino. When button is pressed different patterns of dice game are produces in fast speed. And when we release this button a random pattern is selected and shown on LEDs. This is simple example in which we generate random number from 1 to 6 range and display this number on LEDs.

Software

Arduino program is simple but lengthy. It first set pin mode for button/key as input with internal pullup resistor enabled and for LEDs as outputs. Initially displays 1 on LEDs. Calls srand() function for random number generation. This function is called once. Then in loop() function when key is pressed it generates random numbers from 1 to 6 range and display on LEDs. When key is released random number generation is stopped and final number is displayed on LEDs. Here is program


#include <stdlib.h>

// Define macro or formulae for random number
// generation from 1 to 6.
#define GetRandomNumber() ((rand() % 6) + 1)

void Display(char Value);
void turnOffAllLeds();

const int Switch = 6;

const int Led1 = 7;
const int Led2 = 8;
const int Led3 = 9;
const int Led4 = 10;
const int Led5 = 11;
const int Led6 = 12;
const int Led7 = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(Switch, INPUT_PULLUP);
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  pinMode(Led4, OUTPUT);
  pinMode(Led5, OUTPUT);
  pinMode(Led6, OUTPUT);
  pinMode(Led7, OUTPUT);

  // Turn off all Leds.
  //turnOffAllLeds();
  Display(1);
  srand(50);
}

void loop() {
  char randomNumber;
  // put your main code here, to run repeatedly:
  while(digitalRead(Switch));
  while(!digitalRead(Switch))
  {
    // Generate random number.
    randomNumber = GetRandomNumber();

    // Display this random number
    // on Leds.
    Display(randomNumber);
    // Give some delay.
    delay(10);
  }
    // Generate random number last time.
    randomNumber = GetRandomNumber();
    // Finally display this number on LEDs.
    Display(randomNumber);
}

void turnOffAllLeds()
{
  digitalWrite(Led1, HIGH);
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, HIGH);
  digitalWrite(Led4, HIGH);
  digitalWrite(Led5, HIGH);
  digitalWrite(Led6, HIGH);
  digitalWrite(Led7, HIGH);
}

// This function displays an integer value from
// 0 to 6 on LEDs.
void Display(char Value)
{
  // Switch off all LEDs.
  //Led1 = Led2 = Led3 = Led4 = Led5 = Led6 = Led7 = 1;
  turnOffAllLeds();
  switch(Value)
  {
  case 1:
    digitalWrite(Led4, LOW);
    break;
  case 2:
    digitalWrite(Led1, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 3:
    digitalWrite(Led1, LOW);
    digitalWrite(Led4, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 4:
    digitalWrite(Led1, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 5:
    digitalWrite(Led1, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led4, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 6:
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led6, LOW);
    digitalWrite(Led7, LOW);
    break;
  }
}


Thermometer Using LM35D Arduino And LCD

Description

It is a LCD Thermometer Using Arduino and LM35D temperature sensor. It shows temperature on 16×2 LCD in degree centigrade.

Circuit Diagram

In circuit diagram 16×2 LCD is connected to arduino. LM35D is connected to analog channel input of arduino.

Detail

LCD is connected in conventional 4 bit mode and LM35D temperature sensor is connected to first analog input channel of arduino. LM35D gives analog output corresponding to temperature. When temperature of environment changes it’s output voltages also changes. It gives 10mv per degree centigrade, means if output of LM35D is 10mv then surrounding temperature will be 1 degree centigrade. If output changes to 100mv then surrounding temperature will be 10 degree centigrade. Our arduino program reads this voltage level and converts it into corresponding temperature value. And then displays this value on 16×2 LCD in centigrade.

Software

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

// Define analog pin where
// LM35D is connected.
const int LM35DPin = A0;

void setup() {
  // put your setup code here, to run once:

  // Setup LCD
  lcd.begin(16, 2);

  lcd.print("Temperature");

}

void loop() {
  // put your main code here, to run repeatedly:
  float temperature = analogRead(LM35DPin);
  temperature = temperature * 5000;
  temperature /= 10240;
  lcd.setCursor(0,1);
  lcd.print(temperature);
  lcd.print((char) 223);
  lcd.print('C');
  delay(300);
}

PC Serial RS232 Interface With Arduino

Description

It is serial RS232 Port interface example with Arduino. Serial Port is a channel for communication with microcontroller/arduino.

Circuit Diagram

Circuit diagram is simple and there is no need of extra component. All hardware interface between arduino microcontroller and PC is already provided in arduino board. Here is simple diagram in proteus to use with serial terminal in proteus.

Detail

Serial port is used to communicate with other devices like microcontrollers and PCs. It is most commonly used method for communication between arduino and computer. In serial RS232 interface all data bits are transmitted and received one bit at a time in a stream. So there is series of bits on single wire/pin. In arduino serial port is implemented over USB. There is USB to serial converter device required between arduino microcontroller and PC that is already implemented in arduino board. So, there is no need to add it additionally. This project is the base for so many other projects.

Software


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.write("Started...");

}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available())
  {
    Serial.write(Serial.read() + 2);
  }

}

In software setup() function we first initialize serial channel at 9600 bits/sec. Then we send “Started…” string towards PC. You can see how this “Started…” string is received in terminal in proteus shown in above diagram. You will also see next in video that how this string “Started…” is received in PC. In loop() function we check that if any received character is available? then read this character add 2 to it and send back towards PC.
Here is video for further clarification.

20170601_161518 from Rashid Mehmood on Vimeo.

16×2 LCD Interface With Arduino

Today I am going to tell you how to interface 16×2 LCD with Arduino.

Circuit Diagram

LCD

Display has very importance in embedded systems. It is used to interact humans with system. Instructions and device function results are shown on displays. Without display we cannot see what operation is being performed inside the arduino/microcontroller. LCDs are most widely used in embedded systems to provide user interface. In this example I am using HD44780 based 16×2 LCD. This LCD can be interfaced using 8 bit or 4 bit mode. In 8 bit mode all 8 data pins are used while in 4 bit mode only 4 pins of LCD are used. In this project I have used 4 bit mode of operation. 16×2 means it has 16 columns and 2 rows. HD44780 controller is installed on other sizes of LCDs like 16×1, 16×4, 20×2, 20×4 etc.

LCD.jpg
Click to Enlarge

Pin-Out

Pin Symbol Function
1 Vss ground (0 V)
2 Vdd power (4.5 – 5.5 V)
3 Vo contrast adjustment
4 RS H/L register select signal
5 R/W H/L read/write signal
6 E H/L enable signal
7-14 DB0 – DB7 H/L data bus for 4- or 8-bit mode
15 A (LED+) backlight anode
16 K (LED-) backlight cathode

We can read & write data to LCD but to keep things simple we have hardwired R/W line to ground for only writing. It means we can only print on LCD but cannot read back content written in LCD RAM.

Software

In arduino we use builtin library for LCD interfacing “LiquidCrystal.h”. And this makes LCD interfacing with arduino simple and easy.

#include <LiquidCrystal.h>

// Define object of LiquidCrystal class.
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);

  // Print welcome message
  // to LCD.
  lcd.print("Welcome!");
  
  // Set cursor to next line and 
  // first column.
  lcd.setCursor(0,1);
  
  lcd.print("micro-digital.net");

}

void loop() {
  // put your main code here, to run repeatedly:

}

Led Flasher With Arduino

It is simple and basic arduino project. You can say it welcome/hello world project in arduino programming. In this project I will first blink on board LED of arduino and then will use off board LED.

Blinking On Board Arduino LED

Arduino is a prototyping board for embedded systems development. In this example I am using Arduino UNO board. In this board ATMega328 microcontroller is installed. On board arduino LED is connected to pin number 13. Here is the circuit with off board LED.

What Will You Need

For on board LED you need only

  1. 1 x Arduino Board
  2. 1 x USB Cable
  3. 1 x Computer with Arduino IDE installed

For off board LED you need some more parts

  1. 1 x Bread Board
  2. 1 x LED
  3. 1 x 150 Ω Resistor
  4. 3 x Jumper Wires

Software


// Define pin 13 as LED pin
const int LED = 13;

void setup() {
  // put your setup code here, to run once:

  // Set LED pin as output.
  pinMode(LED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  // Set LED pin at high/5V level.
  digitalWrite(LED, HIGH);
  // Delay for 500 ms.
  delay(500);
  // Set LED pin at low/0V level.
  digitalWrite(LED, LOW);
  // Delay for 500 ms.
  delay(500);
}

Transformer Designer


Transformer-Designer
This app is for transformer designing calculations. Transformers are widely used in our electrical applications to step down or step up AC voltage. These are used in power supplies, stabilizers, inverters, converters, UPS and so many other applications. This app is complete solution especially for 2 winding transformer. This app helps transformer designers to find out different transformer parameters. These parameters are

  1. Turn/Volt
  2. Turns for each coil
  3. SWG/AWG Wire for each coil with actual rate (cmils/A)
  4. Core Area for bobbin selection

During transformer design we need to know core area of transformer for the selection of bobbin. We want to calculate turns/volt so that we can calculate total turns for each coil. For each different coil different SWG/AWG wire is needed. 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) or American Wire Gauge (AWG). This number is called SWG or AWG 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.

This app is helpful in transformer winding, power supplies, inverters, converter and UPS development and sales business. Now, if you are a transformer designer/winder this app is for you. You can install it from here.

Transformer-Designer

How It Works?

Let us consider we want a transformer for our LED sign board supply. LED sign board needs almost 12V and 4A supply. Considering other factors like temperature, we decide to design a transformer for 12V and 5A. Now, run this app and first enter 1 number of output coils like shown in the following figure and press “Next”.

transformer-designer-0.png

Now, enter VA, Voltage and/or Current values as shown in the following figure. Here you have to enter first and the only output coil’s power requirement. Enter any two of the values and leave single value empty. If you entered all three values then app will calculate third according to left to right order means it will calculate “Current” value by dividing “VA” by “Voltage”. After this press “Next”.

transformer-designer-1.png

Now, enter number of input coils. Here, in our example this is 1.

transformer-designer-2.png

Here, enter “Voltage” or “Current” value of input coil but not both as total VA of transformer are calculated according to output coils and is shown in the following figure.

transformer-designer-3.png

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/530 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. Here, we enter 530 cmils/A. Enter flux density of core used in weber per square inch. Default value for silicon core is 0.0006 weber per square inch. This app is optimized for silicon core. You can use other cores and do experiment. Finally, enter frequency of input voltage. In my country it is 50 Hz. Press “Next” to continue

transformer-designer-4.png

Now, here results are calculated.

transformer-designer-5.png

Here for each output and input coil following parameters are calculated

  1. VA and/or Voltage and/or Current
  2. Turns for coil
  3. 2 SWG numbers with actual rate (in cmils/A)
  4. 2 AWG numbers with actual rate (in cmils/A)

First SWG is safe SWG and second is calculated for your ease if it is with acceptable rate for you. Because this value also depend some other factors like how long transformer will be operated without resting and surrounding temperature etc. Usually we choose between 500 cmils/A and 530 cmils/A. Similarly, 2 AWG numbers are calculated. First is safe and second may or may not be safe for you. Now, scroll and see other parameters

transformer-designer-6.png

At the end there are some other parameters of transformer like

  1. Total VA of transformer
  2. Turn/Volt
  3. Core Area
  4. Selected Rate value
  5. Frequency and
  6. Flux Density of core
transformer-designer-7.png

Install this app from Google Play. Enjoy! this app.

Transformer-Designer

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.

Transformer Winding Calculator

Transformer-Winding-Calculator-00.jpg

This app can calculate SWG/AWG wire number for your transformer coil and turns per volt to calculate total turns of coil.

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) or American Wire Gauge (AWG). This number is called SWG or AWG 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 app will do this work for you.

How It Works?

SWG/AWG Calculation

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 and third are SWG and AWG number of wires that are safe for given value of current and it’s actual rate. Second and forth values are the SWG and AWG numbers of next wires that may be or may be not near the required wire with it’s actual rate. You can decide yourself which SWG/AWG wire will be better for your current requirement.

transformer-winding-calculator-0.png

First enter value of Current.

transformer-winding-calculator-1.png

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/530 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.

transformer-winding-calculator-2.png

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

transformer-winding-calculator-3.png

Turn Per Volt Calculation

First enter area of core in square inches.

transformer-winding-calculator-4.png

Now, enter flux density of core used in weber per square inch. Default value for silicon core is 0.0006 weber per square inch. This app is optimized for silicon core. You can use other cores and do experiment.

transformer-winding-calculator-5.png

Enter value of frequency in hertz. Default value is 50 Hz.

transformer-winding-calculator-6.png

Now, press calculate button below frequency field and app will show result as shown in the following figure.

transformer-winding-calculator-8.png

Download this app from Google Play. Enjoy! this 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.

Wire Gauge Calculator Pro

Wire-Gauge-Calculator-Pro.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) or American Wire Gauge (AWG). This number is called SWG or AWG 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 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 and third are SWG and AWG number of wires that are safe for given value of current and it’s actual rate. Second and forth values are the SWG and AWG numbers of next wires that may be or may be not near the required wire with it’s actual rate. You can decide yourself which SWG/AWG wire will be better for your current requirement.

wire-gauge-calculator-pro-1.png

First enter value of Current.

wire-gauge-calculator-pro-2.png

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/530 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-pro-3.png

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

wire-gauge-calculator-pro-4.png

Download this app from Google Play. Enjoy! this 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.

Home Device Control Over Bluetooth Using Android

Home-Device-Control-Over-Bluetooth-01.jpg

Now, control of your appliances will be at your finger tips. Using this app you can control your appliances (lights, fans, heaters etc) from your android phone remotely over bluetooth connection. If your are a professional or a student of electrical/electronics engineering and can develop arduino based circuits then you can use this free app.

You can download this free app from here.

Home Device Control Over Bluetooth-0 from Rashid Mehmood on Vimeo.

Development of Hardware

A hardware developed on arduino open source platform is connected over bluetooth with this app. Hardware has the functionality of switching high voltage devices like fans, energy savers, tube lights and heaters etc. You can change hardware according to your device power consumption requirements. Large size relays will be required for heavy loads. As there are high voltages involved so be careful during development and use of hardware. We will not be responsible for any kind of damage/loss in any form by using this app and hardware.

Hardware is no more complex and you can develop it easily if you have developed basic electronics circuits and can understand electronic schematic diagrams. Here is the schematic diagram of hardware.

Arduino Code (Free Download)

Here is the arduino source for above hardware.

Home_Device_Control_Over_Bluetooth.rar

How It Works?

Here is the main screen of app showing paired devices. Before you connect to remote hardware you have to perform pairing. In this project HC-05 bluetooth module is used. Default pin code for HC-05 bluetooth module is “1234”. This pin will be required during pairing process. Paired devices will be shown in this screen. And then select your hardware from the list. Here “HC-05” is representing our hardware.

Home-Device-Control-Over-Bluetooth-0.png

Now, our app will try to connect to remote arduino hardware and will show connection status and will also show appliances current status (on/off) as in the following figure.

Home-Device-Control-Over-Bluetooth-1.png

Now, we can tap to turn on/off a device. In the following figure we have turned off 2 lights.

Home-Device-Control-Over-Bluetooth-2.png

Following figure is showing status of fans that we can also switch on/off.

Home-Device-Control-Over-Bluetooth-3.png

In the following figure, status of on and off lights is shown.

Home-Device-Control-Over-Bluetooth-4.png

In the following figure, status of on and off fans is shown.

Home-Device-Control-Over-Bluetooth-5.png

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

More Videos

VID-20160224-WA0002 from Rashid Mehmood on Vimeo.

VID-20160224-WA0007 from Rashid Mehmood on Vimeo.

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 be 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.

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