Connect with us

Strange behaviour on LCD Display

Discussion in 'Microcontrollers, Programming and IoT' started by rob_croxford, Mar 31, 2011.

Scroll to continue with content
  1. rob_croxford


    Aug 3, 2010
    Hi guys, I have a problem with a bit of code i have been writing. The code executes correctly however it shows some strange behaviour. The issue is with the Do-While loops within the If statements.. The first Do-while loop reads from a designated ADC pin for a variable 0-5V signal which denotes the control percentage of the output and displays on an LCD - this works fine. At the press of a button (the next if statement) the current should be displayed. This works to an extent however will only display the value seen for the proportional control and will not take its own reading from the designated pin. The same happens for the power section (to show power dissipitated at the load). The buttons work and display either control, current or power however the value for the proportional control section is the only value displayed for either case.

    Can anyone help me with this problem?? Please see the code bellow - this is the main chunk of the program, i have tried to set it out and lable it so it is clear what is happening.
    Bein relativly new to programing PIC's i am finding it hard to troubleshoot this particular problem.

    The calculations may seem a bit odd however this is simply a test program using various Pots to act as the varible inputs so precise calculations are not an issue at this stage.

    Any help would be greatly appreciated.



    void main(){

    PORTC = 0xFF;
    TRISA = 0xFF;
    TRISC = 0xFF;

    ANSEL = 0x13, 0x12; // Configure AN pins as digital I/O not 18,19
    ANSELH = 0;
    ADC_Init(); // Initialize ADC

    Lcd_Init(); // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    //***************************** Menu 1 ****************************************

    Lcd_Out(1,1,txt); // Write text in first row

    //*********************** Proportional Control ********************************

    if (Button(&PORTA,2,10,1)) {
    Lcd_Out(1,5,"Control:"); // Write text in first row
    percentage = Adc_Read(0); // convert 1024 steps to a percentage
    Data1 = percentage*j;
    Data2 = Data1/102;
    VAL = Data2;

    WordToStr(VAL,chVAL); // convert int - char
    Lcd_Out(2,5,chVAL); // Display percentage on LCD
    }while (RA2 == 1);

    //****************************** Current **************************************

    } else if (Button(&PORTB,4,10,1)){
    Lcd_Out(1,5,"Current:"); // Write text in first row
    current1 = Adc_Read(1); // convert 1024 steps to a percentage
    current2 = current1*49;
    VAL2 = current2;

    WordToStr(VAL2,chVAL2); // convert int - char
    Lcd_Out(2,5,chVAL); // Display percentage on LCD
    }while (RB4 == 1);

    //******************************* Power ***************************************

    } else if (Button(&PORTB,5,10,1)) {
    Lcd_Out(1,6,"Power:"); // Write text in first row
    power1 = (VAL2*230);
    VAL3 = (power1/1000);

    WordToStr(VAL,chVAL3); // convert int - char
    Lcd_Out(2,3,chVAL3); // Display percentage on LCD
    }while (RB5 == 1);

    //******************************** END ****************************************

    }while (1);
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    Jan 21, 2010
    OK, the buttons are on RA2, RB4, and RB5?

    When pressed they pull the pin high?

    Are they momentary contact?

    It seems to me that if all this is true, that the various while loops only execute while the button is pressed. When they are released, the circuit goes back to displaying txt and txt2.

    I don't see where you set the TRISB or PORTA or PORTB -- are you relying on the default values?

    edit: Also in the current section, you calculate cvVal2 but display chVal

    edit2: Also in the Power section, you continue to use the last value for current. You do not re-read it.

    edit3: Lastly? in the Power section you calculate VAL3, but convert Val to character form.

    Several of those errors contribute to only the proportional control value being displayed.
    Last edited: Mar 31, 2011
  3. rob_croxford


    Aug 3, 2010
    Hi Steve,

    Thanks i just needed someone with a kene eye to look over it! It was the issue with the conversions of value to character form and writing the wrong variables.

    All sorted now :)

    Thanks alot
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
Electronics Point Logo
Continue to site
Quote of the day