Thread: Having problem with ADC display on PIC18F4520 LCD

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    5

    Having problem with ADC display on PIC18F4520 LCD

    Hi, I'm having some problems on interfacing ADC with LCD display. The main purpose of using ADC is because I need to read the voltage of the batteries and transfer it, and display to LCD. I'm using MCC18 compiler. Please guide or help me!

    Code:
    include <p18f4520.h>
    #include <delays.h>
    #include <stdlib.h> 
    
    
    void Init_LCD(void);    //Initialise the LCD
    void W_ctr_8bit(char);  //8-bit Control word for LCD
    void W_data_8bit(char); //8-bit Text Data for LCD
    
    
    ADCON0=0b00000001;    
    ADCON0bits.GO = 1;        // Start ADC 
    Delay10TCYx(5); //give channel time to initiliase 
    while(ADCON0bits.DONE); // Wait for conversion to finish. 
    counts=ADRESH*255 +ADRESL; 
    //for 3.62 volts, expect a count of 741 
    millivolts = (counts * 1.367)/100; //gives a result of 3630.9 
        Init_LCD();
        W_ctr_8bit(0x80);
        for(i=0;i<8;i++)
        W_data_8bit(MESS3[i]);
        W_ctr_8bit(0x8C);
        W_data_8bit('V');
    
    
    itoa(millivolts,Distance1); //(int value, char * str, int base); 
    
    
    W_ctr_8bit(0x89);        
    while(Distance1[i])
        {
            W_data_8bit(Distance1[i]);
            i++;
        }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Are you trying to measure the voltage of the batteries that are running the PIC?

    If so, that could be a problem.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    5
    Quote Originally Posted by Click_here View Post
    Are you trying to measure the voltage of the batteries that are running the PIC?

    If so, that could be a problem.
    nope. i'm making a batteries charging system. so measuring the voltage of the rechargeable batteries is the big problem i'm facing

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > itoa(millivolts,Distance1); //(int value, char * str, int base);
    Are you suggesting you have some hacked version of itoa() which doesn't have a base parameter?
    Or are you just ignoring warnings, and feeding itoa a garbage base value?

    How is Distance1 declared?
    char *Distance1; // wrong!

    char Distance1[20]; // Right!

    > W_data_8bit(MESS3[i]);
    Does this work? Do you see the message?
    If not, then we need to get your printing a simple string working.

    > millivolts = (counts * 1.367)/100; //gives a result of 3630.9
    What type is millivolts?
    Your odd comment at the end suggests that it may not be an integer (see your use of itoa later on).

    Try say something like
    millivolts = 1000; // millivolts = (counts * 1.367)/100; //gives a result of 3630.9
    Do you see 1000 converted and printed OK?
    Yes - the problem is in the ADC or the maths
    No - the problem is in the printing / conversion.

    Work through these steps and try and come up with a decent analysis of what works and what doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    5
    Hi Salem. Thanks for the guide, I will work on it. if i declare char MESS[]="welcome"; and display it with W_data_8bit(MESS[i]); . the LCD is able to display. but when i tried using it on itoa, it is not able to display anything.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    > itoa(millivolts,Distance1); //(int value, char * str, int base);
    Are you suggesting you have some hacked version of itoa() which doesn't have a base parameter?
    Or are you just ignoring warnings, and feeding itoa a garbage base value?
    From my own MPLAB C18 stdio.h file.

    Code:
    char *itoa (auto int value, auto char *s);
    So, Microchip uses a non-standard prototype for itoa that does only base 10 conversion.

    What type is millivolts?
    The above is what I would like answered in Salem's list of question; since if it is wrong. I would suspect the approx. results being reported.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PIC18F4520 interfacing with hitachi 44780
    By InNeedOfHelp in forum Tech Board
    Replies: 18
    Last Post: 12-14-2012, 09:58 AM
  2. Replies: 8
    Last Post: 08-02-2012, 06:15 PM
  3. Display problem
    By Creatlv3 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2010, 12:28 AM
  4. Tic Tac Toe display problem
    By Munkey01 in forum Game Programming
    Replies: 9
    Last Post: 03-01-2003, 06:35 PM
  5. Display problem
    By RoD in forum C++ Programming
    Replies: 11
    Last Post: 10-09-2002, 05:25 PM