//Main.c
// INTEGER
#include <P18f4520.h>
#include<stdio.h>
#include<stdlib.h>
#include "lcd.h"
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF
unsigned char text1[]={" ADC Expt. "};
unsigned char text2[]={"ADC IN="};
unsigned char L,H,RD,reference[20];
unsigned long adcout;
void main()
{
TRISD = 0x00;
TRISB=0x00;
TRISAbits.TRISA0=1; //ADC channel 0
CMCON=0x07;
ADCON1 = 0X0C; //AN0 as analog iput
ADCON2=0x86; //Right justified,Fosc/64
lcdinit();
lcddisplay(1,text1);
lcddisplay(2,text2);
while(1)
{
ADCON0 = 0X01; //Channel 0 selected
ADCON0bits.GO = 1; //start convergen
while(ADCON0bits.DONE ==1); //wait for convergen end
L=ADRESL;
H=ADRESH;
L>>=2;
L &=0x3F;
H<<=6;
H &=0xC0;
adcout=L |H;
RD=adcout*5/254;
sprintf(reference,"%d Volt",RD);
lcddisplayvalue(1,reference);
}
}
//---------------------------------------------------------------------------------------
//lcd.h
void delay(unsigned int value);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdinit(void);
void lcddisplayvalue(int row,unsigned char *str);
void lcddisplay(int row, unsigned char *str);
#define ldata PORTD
#define rs PORTBbits.RB3
#define rw PORTBbits.RB4
#define en PORTBbits.RB5
void lcddisplayvalue(int row,unsigned char *str)
{
int k;
if (row==1)
lcdcmd(0xC7);
else lcdcmd(0xC9);
for(k=0;k<16;k++)
{
if(str[k] !=0)
lcddata(str[k]);
else break;
}
while((k<16)){
lcddata(' ');
k++;
}
}
void lcddisplay(int row,unsigned char *str)
{
int k;
if (row==1)
lcdcmd(0x80);
else lcdcmd(0xC0);
for(k=0;k<16;k++)
{
if(str[k] !=0)
lcddata(str[k]);
else break;
}
while((k<16)){
lcddata(' ');
k++;
}
}
void delay(unsigned int value)
{
int i,j;
for(i=0;i<=value;i++)
for(j=0;j<=5;j++);
}
void lcdcmd (unsigned char value)
{
ldata=value;
rs=0;
rw=0;
en=1;
delay(1);
en=0;
}
void lcddata (unsigned char value)
{
ldata=value;
rs=1;
rw=0;
en=1;
delay(1);
en=0;
}
void lcdinit(void)
{
lcdcmd(0x38);
// delay(1);
lcdcmd(0x0E);
// delay(1);
lcdcmd(0x01);
// delay(1);
lcdcmd(0x06);
// delay(1);
}
Excellent sir. The code works perfectly fine.
ReplyDelete