Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Analog input not showing on LCD in PIC 16f877 using hi-tech compiler

Status
Not open for further replies.

ecaits

Member level 4
Member level 4
Joined
Jan 16, 2014
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Visit site
Activity points
579
On above subject, I can successfully compile the code but cannot getting output in real pic simulator.
I am taking input in A port and wanted output in LED as well as LCD.

Code is given below.

Plz suggest me which is the missing portion???

#include<htc.h>
#include<pic.h>
#include<math.h>
#include<stdio.h>

#define RS RE0 //LCD Register Select
#define RW RE1 //LCD Read/Write Pin
#define EN RE2 //LCD Enable Pin
#define DATA PORTD //LCD Data Port

void linit(void); //LCD initialization Function
void lclr(void); //LCD Clear Function
void lcomd(unsigned char); //LCD Command Declaring Fucntion
void ldata(char); //LCD Data Display Fucntion
// void DelayMs(unsigned int);

#define _XTAL_FREQ 4000000
unsigned int ADC_Read(unsigned char);
void ADC_Init();


//unsigned int GO_nDONE;

void ADC_Init()
{
ADCON0 = 0x41;
ADCON1 = 0xC0; //right justified
}

unsigned int ADC_Read(unsigned char channel)
{
int i;
char a,b,c,d;
if(channel > 7)
return 0;

ADCON0 &= 0xC5;
ADCON0 |= channel<<3;
__delay_ms(2);
GODONE = 1;
while(GODONE);
i = ((ADRESH<<8)+ADRESL);
a=i/1000;
b=i%1000;
b=b/100;
c=b%10;
c=c/10;
d=c%10;

__delay_ms(20);
return (a);
__delay_ms(20);
return (b);
__delay_ms(20);
return (c);
__delay_ms(20);
return (d);
__delay_ms(20);
ldata(a);
__delay_ms(20);
ldata(b);
__delay_ms(20);
ldata(c);
__delay_ms(20);
ldata(d);
__delay_ms(20);
}

void linit(void)
{
int x;
unsigned char command[]={0x38,0x0c,0x06,0x01};
//LCD Command set for 8 bit Interface, 2 Lines, 5x7 Dots
//38 = 2 lines and 5x7 matrix
//0c = force cursor to begining of 2nd line
//06 = shift cursor to right
//01 = clear display screen

ADCON1 = 0x07; //Make PORTE Pin as Digital

TRISD = 0x00; //Make LCD control port (PORTE) as output
TRISE = 0x00;

__delay_ms(50);
// DelayMs(50); //Make LCD Data Port (PORTD) as output port

for(x=0;x<4;x++)
{
lcomd(command[x]); //Send the Initialisation Commands
__delay_ms(50);
// DelayMs(5);
}
__delay_ms(50);
// DelayMs(50);
}

void lclr(void) // 01 = LCD clear command
{
lcomd(0x01);

__delay_ms(20);
// DelayMs(20);
}

void lcomd(unsigned char cmd)
{
RS=0; RW=0; //Select Command Register, R/W--write enabled

//RS = 0 means data treated as command, 1 means data treated as text
//RW = 0 means write mode, 1 means read mode

EN=1; //Enable LCD to accept commands

DATA=cmd;

EN=0;
// DelayMs(50); //Give a Pulse via Enable pin
__delay_ms(50);
}

void ldata(char byte)
{
RS=1; RW=0; //Select Data register, R/W--write enabled

EN=1;

DATA=byte; //Give a Pulse via Enable pin

EN=0;
// DelayMs(5);
__delay_ms(5);

}

/* void DelayMs(unsigned int Ms)
{
int delay_cnst;

while(Ms>0)
{
Ms--;
for(delay_cnst = 0;delay_cnst <220;delay_cnst++);
}
}
*/

void main()
{
unsigned int a,i;
double x;
TRISB = 0x00;
TRISC = 0x00;
ADC_Init();

// unsigned char first[]={"Hello"};
// DelayMs(50);
__delay_ms(5);
linit();
// DelayMs(50);
__delay_ms(5);

while(1)
{
lclr();
lcomd(0x80); // 80 = force cursor at begining of 1st line

/* for(i=0;i<10;i++) //Display the Message
{
ldata(first);
DelayMs(50);
} */
}
do
{
a = ADC_Read(0);
PORTB = a;
PORTC = a>>8;
__delay_ms(100);
}while(1);

}
 
Last edited:

Try using this Function of "read_adc" it is tested in C18 Compiler just change the syntaz according to hit tech Compiler and use this also Check your LCD working Like printing some welcome message before printing ADC output value
HTML:
unsigned int read_adc()
{

//ADCON0 = 0x00;
delay(100);

// A/D Input Channel //
//Channel Selections///
ADCON0bits.CHS3 = 0 ; //CHS3:CHS0 ~ Analog Channel select bits 
ADCON0bits.CHS2 = 0 ;   // 0001 = Channel 1 (AN1)
ADCON0bits.CHS1 = 0 ;
ADCON0bits.CHS0 = 1 ;
/////////////////////

ADCON0bits.ADON = 1 ; // A/D Module Turn ON ~ enabled
delay(2000);
ADCON0bits.GO= 1;    //start ADC conversion
while(ADCON0bits.GO);
ADCON0bits.ADON = 0 ; // A/D Module Turn off ~ enabled
//return((ADRESH<<8) + ADRESL); //its a 10 bit data stored in 16 bit adc_data;
return ADRES ;
}

- - - Updated - - -

Declare the pin as Input by setting the TRIS bits of the respective pins
 

hello,

What are you doing with this ?

Code:
unsigned int ADC_Read(unsigned char channel)
{ 
int i;
char a,b,c,d;
if(channel > 7)
return 0; 

ADCON0 &= 0xC5;
ADCON0 |= channel<<3;
__delay_ms(2);
GODONE = 1;
while(GODONE);
i = ((ADRESH<<8)+ADRESL);
a=i/1000;
b=i%1000;
b=b/100;
c=b%10;
c=c/10;
d=c%10;

__delay_ms(20);
return (a);
__delay_ms(20);
return (b);
__delay_ms(20);
return (c);
__delay_ms(20);
return (d);
__delay_ms(20);
ldata(a);
__delay_ms(20);
ldata(b);
__delay_ms(20);
ldata(c);
__delay_ms(20);
ldata(d);
__delay_ms(20);
}

you never get the result of ADC , only ADC/1000 !
put the variables char a,b,c,d; outside of this function, to declare them as global

Code:
char a,b,c,d;


unsigned int ADC_Read(unsigned char channel)
{ 
int i;
// char a,b,c,d;   // must be global 
if(channel > 7)
return 0; 
ADCON0 &= 0xC5;
ADCON0 |= channel<<3;
__delay_ms(2);
GODONE = 1;
while(GODONE);
i = ((ADRESH<<8)+ADRESL);
a=i/1000;
b=i%1000;
b=b/100;
c=b%10;
c=c/10;
d=c%10;
return(i);
}


void Display_ADC()
{
lcomd(0x80); // 80 = force cursor at begining of 1st line
ldata(a);
__delay_ms(20); // maybe not usefull !!!
ldata(b);
__delay_ms(20); // maybe not usefull !!!
ldata(c);
__delay_ms(20); // maybe not usefull !!!
ldata(d);
__delay_ms(20); // maybe not usefull !!!
}

So, ADC-Read will return I as the result
and initialise the global variable a,b,c,d
ready to use with Display_ADC();
Instert it in the main loop do while for displayng on LCD on the 1rst line..
or decide another position on LCD..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top