kittysoman2013 0 Posted May 27, 2020 Ok, so every time I set up a pin as an output MCC insists on making it "Analog". It looks like this setting has something to do with the ANSEL register, but surely an output is not Analog so why do they do this? Quote Share this post Link to post Share on other sites
1 holdmybeer 9 Posted June 4, 2020 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: 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: 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. 1 Quote Share this post Link to post Share on other sites
0 N9WXU 55 Posted May 28, 2020 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. Quote Share this post Link to post Share on other sites
Ok, so every time I set up a pin as an output MCC insists on making it "Analog". It looks like this setting has something to do with the ANSEL register, but surely an output is not Analog so why do they do this?
Share this post
Link to post
Share on other sites