Maker Pro
Maker Pro

Strange behaviour on LCD Display

rob_croxford

Aug 3, 2010
262
Joined
Aug 3, 2010
Messages
262
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.

Thanks,

Rob


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 ****************************************

do{
Lcd_Out(1,1,txt); // Write text in first row
LCD_Out(2,3,txt2);

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

if (Button(&PORTA,2,10,1)) {
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"Control:"); // Write text in first row
do{
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
Lcd_Out(2,11,txt1);
Delay_ms(500);
}while (RA2 == 1);

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

} else if (Button(&PORTB,4,10,1)){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,5,"Current:"); // Write text in first row
do{
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
Lcd_Out(2,11,txt1);
Delay_ms(500);
}while (RB4 == 1);

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

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

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

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

}while (1);
}
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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:

rob_croxford

Aug 3, 2010
262
Joined
Aug 3, 2010
Messages
262
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
 
Top