# Globlal Variable Problem: Pic18f4550 and Pickit3 Debugging

I'm working on a project which includes the usage of the A/D converter module in the micro-controller (pic18f4550). I haven't had any problems in the past with the micro-controller using a pickit3 programmer for the debugging process.

The problem comes when I'm in the debugging process since I know that the value of the ADC converter can the result be storage in ADRESH in my case (Left Justify), after that I want to save this result in a simple variable,when I'm doing this process the result of ADC converter isn't saved properly giving a rare result.

The following code was implemented, also I've done a video to reproduce the problem.

#include "fuses.h"
#include <stdio.h>
#include <p18f4550.h>
#include <delays.h>

int Canal0,dato1,dato2;
#define  Time Delay10KTCYx(60)

void main(void)

{

TRISA=0XFF;             //PORTA INPUTS
TRISB=0xFF;             //PORTB OUTPUTS
TRISC=0X00;
TRISD=0x00;             //PORTB OUTPUTS
PORTC=0x00;
PORTD=0x00;
PORTB=0x00;             //CLEAN PORTB

//  VSS,VDD 5v / 2^8 = 20mV por numero osea que para 100 = 20mV*1.953125~1.96
/*  . . .ADRESH . . : . . ADRESL. . .
7 6 5 4 3 2 1 0 : 7 6 5 4 3 2 1 0
X X X X X X X X . X X . . . . . . <-Left Justified
. . . . . . X X . X X X X X X X X <-Right Justified
*/
//when adon=1 , go_done 1 = A/D conversion in progress 0 = A/D Idle

while(1)
{
Canal0=0;
Delay10TCYx(1);
Delay10TCYx(1);

Delay10TCYx(1);
Delay10TCYx(1);

Delay10TCYx(1);
Delay10TCYx(1);

}

}


What could be the problem ? if the adc converter module is configured ok, and the reading process is ok, maybe the problem is the data type.

    SetChanADC(ADC_CH0);  //Channel
Delay10TCYx(1);
Delay10TCYx(1);


The video is : here

Of course. Do notice that I don't use an ADC-library. Maybe you should try to read the ADC-value without using the library and see if that works.

    //confifure A/D-module
ADCON0bits.CHS = 0b01010;       //analog channel select

ADCON1bits.ADFM = 1;            //2's complement format



}

unsigned int read_analog_value(unsigned char channel) {

ADRESH = 0x00;

__delay_us(10);                     //acquisition time

PIR1bits.ADIF = 0;                  //clear interrupt flag



}

I'm not quite familiar with the ADC-library but have you tried using an unsigned integer? I'm currently also working on a project which uses the A/D converter and I'have no troubles at all (pic16f1788).

• Can you show me a snippet code of your implementation ? Sep 7 '14 at 15:57

Hi I have read and understand your code,it is the same approach like my code ,however I noticed that you use : adc_result = ADRESH; to save the result.

I test it and it works like charm and I'm wondering why this adc_result2=ReadADC(); doesn't work if the function is documented in the Microchip Libraries documentation int ReadADC(void) it should be work O_ó,it seems like a bug of the adc.h library is present here.

The following code works successfully with the debugger :

 while(1)

{

Canal0=0x000;