ADCON1

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
robrlstn
Posts: 4
Joined: Mon Oct 24, 2011 9:08 pm

ADCON1

Post by robrlstn » Tue Oct 25, 2011 3:13 am

ADC Library Module, Example Code snippet:

ADCON1 = $07 // PORTE as digital (LCD)
TRISA.0 = 1 // configure AN0 as an input
ADCON1.7 = 1 // set analogue input on PORTa.0

The TRISA.0 = 1 statement makes sense. But the two ADCON1 statements do not.

For the PIC 18F4620, the ADCON1 register is where port pins can be configured to be either analog or digital. If a $07 is loaded, then the PORTB pins are digital (AN8 - AN12). So I do not understand the comment about PORTE being digital. RE0- RE2 (AN5 - AN7) are configured as analog by loading a hex 7.

Does the following ADCON1.7 = 1 mean that bit 7 of the ADCON1 register is loaded with a 1? It must mean something else since bit 7 of the ADCON1 register is not implemented.

I do in fact need the ADCON1.7 = 1 statement to read an analog input voltage on AN0 so clearly I do not understand these statements.

Also, what would be the appropriate statement to enable, say, AN3 or AN4 with an ADCON1.x = 1 statement?

Thanks very much for your help.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Oct 25, 2011 10:43 am

A few comments.

First, there are almost as many different ADC module types as there are devices. Last count was up to 14.
The ADC.bas module hasn't been updated to reflect this, so there's a good chance that some tweeks will need to be made. Some chips use a completely different set of registers.

Second, the example is for an 18F452, which has different ADCON1 bit assignments than your 4520 (remember point #1?). ADCON1.7 IS used in that chip, but it controls the justification. Setting it to 1 sets right-justified. The comments in that code snippet are a bit misleading.
They should probably read more like this:

Code: Select all

  ADCON1 = $07		// set all ADC pins as digital (LCD is on PORTE)
  TRISA.0 = 1       // configure AN0 as an input 
  ADCON1.7 = 1      // set adc result to right justified
From what I can tell, there's no explicit code to "set AN0 as an analog pin" in that example. I haven't tried it, but it would probably work because of the sneaky fact that analog inputs are never really disconnected. Setting a pin to analog/digital mode using ADCON1 (or whichever register for your chip) has more to do with removing the digital buffer connection than anything else. From the 452 datasheet...
17.3 Configuring Analog Port Pins

The port pins that are desired as analog inputs, must have their corresponding
TRIS bits set (input). If the TRIS bit is cleared (output), the digital output level (VOH or VOL) will be converted.
The A/D operation is independent of the state of the CHS2:CHS0 bits and the TRIS bits.

Note
1: When reading the port register, all pins configured as analog input channels will read
as cleared (a low level). Pins configured as digital inputs will convert an analog input.
Analog levels on a digitally configured input will not affect the conversion accuracy.

2: Analog levels on any pin that is defined as a digital input (including the AN4:AN0
pins) may cause the input buffer to consume current that is out of the device’s specification.
To answer your question, in order to use AN3 or AN4 with the 4620, it's best to set the appropriate PCFG3:PCFG0 bits in the ADCON1 register (ADCON1 = $0A)

robrlstn
Posts: 4
Joined: Mon Oct 24, 2011 9:08 pm

ADCON1

Post by robrlstn » Tue Oct 25, 2011 3:30 pm

Jerry, Thank you for the very informative answer.

Looking at the 18F452, the code now makes sense, although even more sense with your suggested comment change.

That quote was very useful.

I just noticed that the 18F4620.bas device file says that this chip has 9 ADC channels:

// system header ...

#const _adc = $09 // 9 ADC channels available

But it actually has 13 channels.


It must be a thankless job for anyone to try and keep documentation current with changes happening so fast.

Post Reply