go_away

Author Topic: PIC18f4550 Analog Inputs  (Read 718 times)

0 Members and 1 Guest are viewing this topic.

Offline extreme.alyTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
PIC18f4550 Analog Inputs
« on: June 08, 2013, 11:36:35 AM »
Hello,

I'm trying to use AN3 and AN4, but I dont know why the value that the microcontroller (PIC18F4550) is receiving 0-3 (out of 1000)..

Here are the configs I'm using, please help:

Code: [Select]
#pragma config FOSC = INTOSCIO_EC //internal oscillator
//#pragma config PLLDIV = 1 //divide by 5 to give 4MHz to PLL
//#pragma config FCMEN = OFF //no fail safe clock monitor
//#pragma config IESO = OFF //oscillator switchover disabled
//#pragma config PWRT = ON //oscillator power up timer enabled (release version only)
//#pragma config BOR = OFF //hardware brown out reset
//#pragma config WDT = OFF //watchdog timer disabled
#pragma config MCLRE = OFF //MCLR pin enabled
#pragma config LPT1OSC = ON //timer1 low power operation
#pragma config PBADEN = OFF //portB 0to 4 digital - not analogue
#pragma config LVP = OFF //low voltage programming disabled
#pragma config CCP2MX = ON //portc1 = CCP2
#pragma config XINST = OFF //do not allow PIC18 extended instructions
#pragma config STVREN = ON //stack overflow will cause reset
//#pragma config CP0 = OFF //code protection block 0
//#pragma config CP1 = OFF
//#pragma config CP2 = OFF
//#pragma config CP3 = OFF
//#pragma config CPB = OFF //code protection boot block
//#pragma config CPD = OFF //code protection data EEPROM
/*#pragma config WRT0 = OFF //write protection block 0
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTB = OFF //write protection boot block
#pragma config WRTC = OFF //write protection configuration register
#pragma config WRTD = OFF //write protection data EEPROM
#pragma config EBTR0 = OFF //table read protection block 0
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF //table read protection boot block
*/

Note the comment box too, At first I had these configs too but then to debug, I removed them, but the result is the same..

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,929
  • Helpful? 98
Re: PIC18f4550 Analog Inputs
« Reply #1 on: June 08, 2013, 05:06:37 PM »
Need to see all the code to setup and read the ADC.

Are you allowing enough settling time after selecting the ADC input?
Are the Tad time correct?
How you you seeing the ADC value?
Which compiler are you using?
Have you studied the PIC data sheet on using the ADC?
Have you read and studied the 18C reference Manual (Doc #DS39500A) om how to use the ADC?
Are you using function that came which the compiler for the ADC or are you writing/reading the PIC ADC registers directly?

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: PIC18f4550 Analog Inputs
« Reply #2 on: June 08, 2013, 09:23:50 PM »
I've never used a PIC, but if the value is exactly 0, 1, 2 or 3, then perhaps the problem is related to that those are the highest 2 bits out of a 10-bit value between 0 and 1023. I e 1023 == 0x3ff. So, if you were only reading the highest byte of a two-byte value, that's what you would get.

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,929
  • Helpful? 98
Re: PIC18f4550 Analog Inputs
« Reply #3 on: June 09, 2013, 08:39:05 AM »
Good point jwatt.
The PIC has a 10 bit ADC and the ADC result is put into two 8 bit registers.
If the ADC result is Right Justified (a setting in the ADC registers) and only the high byte is read then you effectively have a 2 bit ADC.

 


Get Your Ad Here

data_list