You are on page 1of 17

APPLICATIONS OF 8051

Analog to Digital Conversion (ADC 0804) DC-Voltmeter

by. Engr. Khurram Hashmi

Introduction

One of the applications that an 8051 micro-Controller might provide is in cohesion with Analog to Digital Conversion ICs ADC 0804 or ADC 0808

ADC

01101011

Flow Representation

ADC converts Analog input to Digital 8 bit output 8051 handles this data Algorithms implemented to convert hex standard format to ASCII and displayed on LCD

LCD Display

8051 voltage signal ADC 01101011 8 bit

Circuit Diagram

Voltage Divider Input

Voltage Input taken across voltage Divider. Max of 5volts appears at Input pins 8bit DB0-DB7 to 8051

LCD Display Output

Output read at Port 1 and intrinsically processed as Hex values

Algorithm converts hex valued to ASCII equivalents and Displays on LCD

Algorithm

Lcd.h
A

Library file that defines function prototypes used in files

Lcd.c
Contains

functions involved in coveting hex data to ASCII and writing it to LCD functions for reading ADC output and passes this to LCD write functions in Lcd.c

Voltmeter.c
Contains

Lcd.h
#ifndef __LCD_H__ #define __LCD_H__ #include <REGX51.H>

a header file

#define #define #define #define #define

lcd_port rs rw en flag

P2 P3_0 P3_1 P3_2 P2_7

// Where the LCD is attached and output to LCD shall be sent


// READ // WRITE // ENABLE // D7 of LCD the Busy Flag

LCD

void wrt_cmd(unsigned char); void wrt_data(unsigned char); void wrt_string(unsigned char*); void LCD_INI(void);

Function prototypes

void busy(void);
void hex2lcd(unsigned char);

#endif

Voltmeter.c
#include <REGX51.H> #include "lcd.h" #define #define #define #define adc_port P1 rd wr cs P3_7 P3_6 P3_5 // Port where ADC output is attached //Assigning Read signal P3.7 //Assigning Write signal P3.6 //Assigning Chip Select P3.5

ADC

#define

intr

P3_4

//Assigning INTR signal P3.4


// Start of conversion function called //Read ADC function called

void conv(); void read(); unsigned int

adc_avg , adc;

//defining two integers

ADC 0804 Timing diagram

voltmeter.c
void conv( ) { cs = 0; wr = 0; wr = 1; cs = 1; while(intr); } void read( ) { cs = 0; rd = 0; adc = adc_port; rd = 1; cs = 1; }

continued

//Make CS low //Make WR low //Make WR high //Make CS high //Wait for INTR to go low

When CS=0 Low to High transition of WR Starts data conversion Analog to BCD at the end of which INTR pin goes low as an indication to the CPU

//Make CS low //Make RD low //Read ADC port //Make RD high //Make CS high

Low Active pins on ADC Activates ADC output Low Active pins on ADC restored to high for next reading

void main() { char i; //a counter variable i

Input
//Initiate LCD
//Forever loop

LCD_INI();
while(1){ adc_avg = 0; for(i=0;i<10;i++){ conv(); read();

//Start conversion //Read ADC

adc_avg += adc; //increments adc_avg and keeps on reading } adc_avg = adc_avg/10; wrt_cmd(0x80); wrt_string("V(DC): "); adc = adc_avg * 59; //average of 10 values //cursor line1 pos 0 //string text written before actual reading

Takes 10 values from ADC adc Computes average

Whole number
//place decimal dot . in between // separating post decimal pint vlaues //passed to lcd function //writing V as ASCII

hex2lcd((unsigned char)(adc/1000)); //hex value passed to function wrt_data('.'); adc = adc%1000; hex2lcd((unsigned char)(adc/10));

fraction

wrt_data('V'); } }

LCD

Lcd.c
void busy() { flag=1; rs=0; rw=1; while(flag!=0) { en=0; en=1; } } void wrt_cmd(unsigned char val_lcd) { busy(); // check if LCD is NOT BUSY //high to low //Low to high

//Set flag
//RS low select instruction command register to send command //RW high reading informtion / /untill LCD hasnt completed its operation

Check to see if LCD is Busy

lcd_port=val_lcd;
rs=0; rw=0; en=1; en=0; }

// send data to LCD port


/Instruction Register Selected //for writing information //high to low latch for enable

For writing a Command to the LCD Such as clear screen, cursor shift EN high to Low transition Latches in Information

Lcd.c continued.
void wrt_data(unsigned char dat) For Writing Data to LCD { As A or 55.3 etc busy(); lcd_port=dat; EN high to Low transition Latches rs=1; in Information rw=0; en=1; //high to low en=0; //to latch the data present on the data pins } void wrt_string(unsigned char *string) { while(*string) wrt_data(*string++); }

For Writing String Data to LCD As V(DC): Achieved through incrementing gradually and passing to LCD

Lcd.c continued.
void LCD_INI(void) {

wrt_cmd(0X30);
wrt_cmd(0X0C); wrt_cmd(0X01); wrt_cmd(0X06); }

//initiate LCD

Initiates the LCD


//clear LCD //shift cursor right

Converts HEX values to ASCII and Sends to LCD Port


void hex2lcd ( unsigned char hex)
{ //converts hex to AASCII and displays on LCD char temp1, temp2; temp1 = hex; temp2=0; do{ temp1 = temp1-100; if(temp1>=0) temp2++; } while(temp1>=0); //temporary variables

hex

Hex 30 31 .

ASCII 0 1 .

Hundreds digit

Tens digit

Units digit

if(temp2>0) wrt_data(temp2+0x30); temp2=0; temp1 = temp1+100; do{ temp1 = temp1-10; if(temp1>=0) temp2++; } while(temp1>=0); wrt_data(temp2+0x30); temp2 = temp1+10; wrt_data(temp2+0x30); }

+30 H

+30 H

+30 H

57

9
Tens digit Unit Dgit Fractional digit

LCD

by Engr. Khurram Hashmi Faculty of Engineering University of Central Punjab

You might also like