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);
}
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);
}