Jump to content
 
  • 0

MCC keeps setting my outputs to "Analog", why?


kittysoman2013
 Share

Question

2 answers to this question

Recommended Posts

  • 1

Setting ANSEL just disables the digital input. So with ANSEL set to 1, you'll get back 0 regardless which voltage is applied. If you want to make analog measurements, it's a good idea to disable the digital input. Otherwise it'll begin to toggle between 0 and 1 all the time if the voltage is in the forbidden zone (between low and high level).

Back when PICs didn't have a dedicated LAT (latch) register, leaving the ANSEL at 1 had a serious side effect:

2044955611_anselclassic.png.3eccb6c393e1979327d8f32fb3755117.png

This is from an old PIC16F887. If ANSEL was left at 1, PORT would be always zero. You could still set the output stage to 1 or 0, but you would always read back 0. This leads to a RMW (read-modify-write) problem. To change a particular pin, you had to read out the PORT register for an entire bank, modify the bit for your pin and write it back. If a pin has ANSEL set, it'll return 0, although the output is actually 1. With the write back operation, this pin will be set to 0. So you want to set RA3 to 1, but RA5 (with ANSEL set) gets cleared mysteriously at the same time..

This lead to day-filling debugging sessions and lot of grey hair, so many people still yell "Set ANSEL to zero!!" if you use it as a digital output pin. For modern PICs, this isn't a problem anymore:

2055091349_anselmodern.png.1e450427417db0945b87c212541ca5e0.png

These devices have a latch register, where you set the logic state for the output buffer. The digital read back is separated, its still available in PORT. If you leave ANSEL set to 1, it'll only affect the PORT register now, LAT is independent from the actual voltage at the pin.

 

Long story short: For modern PICs, you can leave ANSEL set to 1 for digital outputs.

 

  • Like 1
Link to comment
Share on other sites

  • 0
  • Member

The ANSEL and the TRIS registers work independently of each other.  The RESET condition for both registers is ANALOG (all bits set) and INPUT (all bits set).  This is not simply because all registers should be set to 1's (no true and not done) but rather, the lowest power/safest state of the device would be analog input followed by digital input because chip cannot know what is attached to each pin.

Because ANSEL and TRIS are independent, you get some interesting side behavior.  i.e. MCC always defaults to the reset state for each register.  ANSEL does not impact the pin's ability to be an output (if you write to the LAT register) so leaving ANSEL set is safe but strange.  In fact, it is possible to make an ADC conversion on an output pin and see how the pin is behaving (is it over loaded or charging a capacitor).

I suppose the short answer is, setting the pin to output did not require disabling the analog feature so MCC did not do that.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

 


×
×
  • Create New...