p18f25k22 + XC8 on MPLABX + novice user
I've got a situation where if I call a function it knobbles the operation of the ADC even though from what I can tell the function called has nothing to do with the ADC SFRs or port pin used by the ADC.
It will read the ADC correctly first time through the loop. But the second (and subsequent) times through the loop the read ADC value will be zero.
If I cut the code from the called function, LCD_char4(), and just stick it in-line the ADC read works ok and reads the ADC correctly each time through the loop. Why? Some depth of stack thing?
The LCD functions work and correctly write to the LCD as intended.
The called function is used to put 4 bits on PORTA twiddle some pins. The ADC pin read is on PORTB
while (1) //do this forever
{
// read ADC & update LCD
ConvertADC(); // Start conversion
while (BusyADC()); //wait while it converts...
ReadADC(); //then read it - result will be in ADRESH & ADRESL
ADC_result = ADRESH*256 + ADRESL; //combine ADC result into one reg
// LCD_char4("Z"); //write the character 'Z' to the LCD << FAILS ADC FAILS ON 2nd LOOP IF THIS FUNCTION IS CALLED
//this is the code contained in LCD_char4() function <<WORKS OK IF THIS CODE IS COPIED FROM THE FUNTION AND PLACED INLINE AND FUNCTION NOT CALLED
RS_PIN = 1;
RW_PIN = 0; //R/W=LOW : Write
LCD_Nibble(i >> 4); //Send upper 4 bits
LCD_Nibble(i); //Send lower 4 bits
__delay_us(50); //must wait 50uS for slow LCD module
//end of code in LCD_char4() function
} // end of ADC read forever loop
//this is the function called
void LCD_char4(char i) //write char data to lcd
{
RS_PIN = 1;
RW_PIN = 0; //R/W=LOW : Write
LCD_Nibble(i >> 4); //Send upper 4 bits to LCD
LCD_Nibble(i); //Send lower 4 bits to LCD
__delay_us(50); //must wait 50uS for slow LCD
}
//and this is the nibble function called from the LCD_char4() function.
void LCD_Nibble(unsigned char dat) {
dat &= 0x0f; //clear top 4 bits of dat
LCD_DATA_PORT &= 0xf0; //clear lower 4 bits of port. interested only in DB7-DB4
LCD_DATA_PORT |= dat; //or the two and store at port
E_PIN = 1;
Delay10TCYx(1); //enable pulse width >= 300ns
E_PIN = 0; //Clock enable: falling edge
}