Trying to understand PIC datasheet for the PIC 12F683

microcontrollerpic

I'm new at asking questions on this forum so please forgive me if my format needs to be cleaned up. I have a PIC 12F683 datasheet. I'm trying to understand how, for instance, ANSEL=0 to set I/O to digital is obtained from a datasheet.

I have searched the web for some clues, but all I seem to find is "it just is." That's not helping me. I just need a little help getting past the bridge between the datasheet information and the actual code that manipulates these ports. Page 39 of "Programming PICs in Basic and page 32 (4-2) of the datasheet makes no sense to me… I'm not the first to post this sort of question from what I have found, but they didn't provide specifics. I'm hoping what I have provided will be enough for one of you folks to see what my question is and help.

I can see on page 31 (4.1 of the datasheet) that the TRISIO register determines direction and it seems clear that applying a "1" to that register will make the corresponding GPIO pin an input and so forth. But when it comes to the ANSEL=0 I'm lost- can't seem to make sense of this. Can someone please throw me a bone?

Best Answer

Taking this datasheet for the PIC12F683 (To ensure we're using the same version of datasheet): http://ww1.microchip.com/downloads/en/devicedoc/41211d_.pdf

Section 4.0 "GPIO PORT" offers a complete description on how the IO port works. You should read this On the right hand side of the page it says:

"Note: The ANSEL and CMCON0 registers must be initialized to configure an analog channel as a digital input. Pins configured as analog inputs will read ‘ 0 ’."

This means if ANSEL and CMCON0 are not configured, any reads on a IO pin that doubles as analog input will always return 0.

Further down in that section at 4.2 you get the detailed description about the ANSEL register and it's bit mask on the next page:

Bits 0 to 3
ANS<3:0>: Analog Select bits
Analog select between analog or digital function on pins AN<3:0>, respectively.
1 = Analog input. Pin is assigned as analog input(1).
0 = Digital I/O. Pin is assigned to port or special function.

So, writing XXXX0000 to ANSEL makes all pins digital IO. XXXX0001 would make AN0 analog and the other 3 digital, and so on.

CMCOM0's bitmask is described under section 8.7

<snip>
111 = CIN pins are configured as I/O, COUT pin is configured as I/O, Comparator output disabled, Comparator off.

So you need to write XXXXX111 to CMCON0 to disable the comparator and free the pins for digital IO.