Professional Documents
Culture Documents
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
Circuit Diagram
Voltage Input taken across voltage Divider. Max of 5volts appears at Input pins 8bit DB0-DB7 to 8051
Algorithm
Lcd.h
A
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
lcd_port rs rw en 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
adc_avg , adc;
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
Input
//Initiate LCD
//Forever loop
LCD_INI();
while(1){ adc_avg = 0; for(i=0;i<10;i++){ conv(); read();
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
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
lcd_port=val_lcd;
rs=0; rw=0; en=1; en=0; }
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
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