Author Topic: XC8 help - unexpected interaction of code + ADC  (Read 1291 times)

0 Members and 1 Guest are viewing this topic.

Online DTJTopic starter

  • Frequent Contributor
  • **
  • Posts: 997
  • Country: au
XC8 help - unexpected interaction of code + ADC
« on: August 31, 2016, 07:34:40 am »
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
}
 

Offline leeatljs

  • Contributor
  • Posts: 31
  • Country: gb
Re: XC8 help - unexpected interaction of code + ADC
« Reply #1 on: August 31, 2016, 07:45:38 am »
What is LCD_DATA_PORT defined as?
 

Online DTJTopic starter

  • Frequent Contributor
  • **
  • Posts: 997
  • Country: au
Re: XC8 help - unexpected interaction of code + ADC
« Reply #2 on: August 31, 2016, 07:53:16 am »
Hi leeatljs,

LCD data port is defined as:

#define LCD_DATA_PORT      LATA    //PORTA


My ADC init is:

OpenADC( ADC_FOSC_4 & ADC_RIGHT_JUST & ADC_2_TAD, ADC_CH9 & ADC_REF_VDD_VSS & ADC_INT_OFF, 1 );
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf