adc >> show in LCD with pic18f4550

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
User avatar
milad1993
Posts: 2
Joined: Mon Jul 14, 2014 10:43 pm

adc >> show in LCD with pic18f4550

Post by milad1993 » Mon Jul 14, 2014 11:05 pm

Hello there. I'm new to this forum and new to PIC. I have an assesment and I have problem in solving it.. Hope all the friends here can guide me and teach me.

I was required to design a circuit to take in ADC value and then display it on the LCD.

I have no problem in configuring ADC ports, taking ADC values, as well as setting up the LCD Screen.
I want to show real voltage value (0to 5 v),for example voltage is 4.3 volt , I dont know what shoul I do .
I read from adc , now i have adc value(0 to 255).

My problem is, how do I convert the value taken from ADRESH and display it onto LCD screen?

p/s: I am using MPLAB c18 compiler and PIC 18f4550.
thank you very much.
this code shown 0 to 255 (digital value):

Code: Select all

#include <p18f4550.h>
#include <delays.h>
#include "LCD.h"
#include <stdio.h>
#include"stdlib.h"
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
#pragma config WDT = OFF //Disable watchdog timer
unsigned char msg[16];
void main()
{
int adc_in;

        
ADCON1 = 0b00001110;//VSS,VDD ref. AN0 analog only
ADCON0 = 0x00;//clear ADCON0 to select channel 0 (AN0)
ADCON2 = 0b00001000;//ADCON2 setup: Left justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/2)
ADCON0bits.ADON = 0x01;//Enable A/D module
lcdinit();

	while(1)
	{
        ADCON0bits.GO_DONE = 1;//Start A/D Conversion

            while(ADCON0bits.GO_DONE != 0);//Loop here until A/D conversion completes
		   
               
adc_in=ADRESH;//Set the delay to the 8 MSB

locate(1,1);
sprintf(msg,"%d",adc_in);
lcdprint(msg);
          
	}

}



Image
Last edited by milad1993 on Thu Oct 09, 2014 5:06 am, edited 1 time in total.

skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: adc >> show in LCD with pic18f4550

Post by skimask » Tue Jul 15, 2014 7:04 pm

What class/degree is this for?
I ignore "one post wonders".

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: adc >> show in LCD with pic18f4550

Post by waltr » Wed Jul 16, 2014 7:19 am

skimask wrote:What class/degree is this for?
Yep.
You need to study how the value from a ADC is related to the Input Voltage and the Reference Voltage.

skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: adc >> show in LCD with pic18f4550

Post by skimask » Wed Jul 16, 2014 8:21 pm

....another drive by one time poster looking for the quick answer to finish a nonexistant project for some unknown class at some overrated learning institution (not a typo, 'institution' is my new word for B.S. college or school setting that doesn't teach squat about anything)
I ignore "one post wonders".

okayid
Posts: 6
Joined: Mon Sep 15, 2014 2:24 pm

Re: adc >> show in LCD with pic18f4550

Post by okayid » Thu Sep 18, 2014 10:13 pm

Hi,
Try this:

#include <stdio.h>
float adc_in; //declare adc_in as a float variable.
int *volts_val; //declare a pointer volts_val.
char volts_str [15]; //declare string array.

adc_in = (ADRESH / 51);// acd_in is brought into 0.000 - 5.000 range by div by 51.
volts_val=&adc_in; //volts_val points to address of adc_in.
sprintf (volts_str , "Volts = %2f " , volts_val); //converts "Volts=" + volts_val(2 dec places) to string and puts it in volts_str.
//NB. volts_str + volts_val are pointers to volts_str[10] + adc_in.
lcdprint(volts_str);

User avatar
milad1993
Posts: 2
Joined: Mon Jul 14, 2014 10:43 pm

Re: adc >> show in LCD with pic18f4550

Post by milad1993 » Thu Oct 09, 2014 5:05 am

thanks guys

I got it , finally just with a simple code ....!

Code: Select all

#include <p18f4550.h>
#include <delays.h>
#include "lcd.h"
#include <stdio.h>
#include"stdlib.h"
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
#pragma config WDT = OFF //Disable watchdog timer

void main()
{
int adc_in=0;
float volt=0;
  
unsigned char msg[20];

ADCON1 = 0b00001110;//VSS,VDD ref. AN0 analog only
ADCON0 = 0x00;//clear ADCON0 to select channel 0 (AN0)
ADCON2 = 0b00001000;//ADCON2 setup: Left justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/2)
ADCON0bits.ADON = 0x01;//Enable A/D module
lcdinit();
while(1)
{
 ADCON0bits.GO_DONE = 1;//Start A/D Conversion

 while(ADCON0bits.GO_DONE != 0);//Loop here until A/D conversion completes
		   

adc_in = ADRESH;

volt=adc_in*0.0196078431372549;

locate(1,1);

sprintf(msg,"%d.%02d   ", (int)volt,(int)((volt-(int)(volt))*1000));

printlcd(msg);
  //Delay10KTCYx(100);
    }
}
IRAN <3

Post Reply