ADC Interfacing with 8051
Read the Introduction here for the ADC tutorials . For LCD tutorials Click here
Both the above two tutorials combined and tried to read the adc value and Displaying the value in 16x2 LCD only for the learning purpose . If you have any doubt ask me By emailing thnnara123@gmail.com ,or comment below .
Circuit
Program In written Keil with C compiler
**********************************************************************************
#include "lcd.h"
#include"adc.h"
#define test_port P2
#include<stdio.h>
void main(){
unsigned char buffer[8];
int adc_value;
LCDPORT = 0x00;
adc_port = 0xFF;
test_port =0x00;
lcd_init();
string("adc ");
get_adc();
P2 = adc_port;
adc_value = 0x60;//adc_port;
LINE2
sprintf(buffer,"%d",adc_value); // used to convert Binary value from ADC to ASCI value for Lcd
string(buffer);
while(1);
}
**********************************************************************************
**********************************************************************************
Main file // ADC File
***********************************************************************************
#include <REGX51.H> #include "lcd.h"
#include"adc.h"
#define test_port P2
#include<stdio.h>
void main(){
unsigned char buffer[8];
int adc_value;
LCDPORT = 0x00;
adc_port = 0xFF;
test_port =0x00;
lcd_init();
string("adc ");
get_adc();
P2 = adc_port;
adc_value = 0x60;//adc_port;
LINE2
sprintf(buffer,"%d",adc_value); // used to convert Binary value from ADC to ASCI value for Lcd
string(buffer);
while(1);
}
***********************************************************************************
adc.h // ADC Header File
************************************************************************************
#define adc_port P3
sbit RD_adc = P1^0;
sbit WR_adc = P1^1;
sbit INTR = P1^2;
get_adc()
{
WR_adc = 0;
delay(1);
WR_adc =1;
while(INTR==1);
RD_adc = 1;
delay(1);
RD_adc = 0;
}
LCD.h // LCD Header File
***********************************************************************************
#include"delay.h" #define LCDPORT P0 // named the Port2 as LCDPORT sbit RS=LCDPORT^2; // P2.2 named RS sbit E=LCDPORT^3; // P2.3 named as E #define LINE2 lcd_cmd(0xc0); // used to display the second line oxc0 is void latch(void) // used to a high to low pulse the pin E { E = 1; delay(1); E = 0; } void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port { RS = 0; // send a '0' value to select to send command delay(1); LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit latch(); delay(1); LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left latch(); } void lcd_data(unsigned char c) { RS =1; // send 1 to send data delay(1); LCDPORT = c & 0xf0 | 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 . delay(1); latch(); LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left latch(); } void lcd_init() { delay(20); lcd_cmd(0x30); //as per data sheet delay(20); lcd_cmd(0x30); //as per data sheet delay(4); lcd_cmd(0x32); //as per data sheet delay(4); lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels) lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels) lcd_cmd(0x0c); // Make cursorinvisible lcd_cmd(0x6); // Set entry Mode(auto increment of cursor) } void string(const char *q) // used to send single charcter to display the lcd { while (*q) { lcd_data(*q++); } }
***********************************************************************************
To download the Whole Project Click here