PIC 18F, comparators and digital outputs - Page 1
Forum section: electronics / general

author: Aethelstan
author_rank: Contributor
timestamp: October 26, 2018, 06:33:32 pm
content: Hi all,First off I must state this is related to a university project I am working on. I don't want to ask how to do something, but I want to understand something in the PIC18F2420 datasheet.I wish to use both comparators within the 18F2420. RA2 and RA5 must remain a digital output as they are hard wired as H bridge enable signal. In the datasheet, the comparator modes on page 234 are all listed, and I believe I must use two common reference comparators and set the CM bits of CMCON to 100. This mode states that RA0, RA1 and RA3 are analogue inputs, whilst RA2 remains digital. That is good, it meets the requirements I want. However, when I come to set the ADCCON1 register to set the digital and analogue status of the pins, page 224 shows that RA2 cannot be digital if RA3 is analogue. To me this seems contradictory, but the people who create the datasheets know infinitely more than I do, so what am I missing?Secondly, I believe the internal reference voltage can only be connected to RA2, meaning I need to create my own reference voltage and apply it to RA3. Would this simply be a case of using RA3 as an analogue input with my reference voltage applied?Thanks allDavid

author: NorthGuy
author_rank: Super Contributor
timestamp: October 26, 2018, 09:16:18 pm
content: This is the price you have to pay for using old and obsolete chips. PIC18s were always contrived because the ill-conceived architecture required the designers to squeeze all the SFRs into as little space as possible. Therefore, they wanted to minimize the number of control bits (such as you see in the ADCON1 register where all the analog/digital selection is done with only 4 bits). This has only been re-paired in recent K42 series, which have much more user-friendly SFRs. Some of the older K series are somewhat better, but these ancient creatures you use are extremely idiosyncratic and illogical.PIC16 architecture is not contrived that much, so they're easier to work with, especially new ones.If your professor doesn't mind, I would pick up a better chip.

author: MarkF
author_rank: Super Contributor
timestamp: October 27, 2018, 12:34:39 am
content: It is not necessary to enable the Comparator Reference out on RA2.You can use mode CM<2:0> = 110 and PCFG<3:0>=1101. This will allow you to use RA0 and RA1 with the internal reference.You will need to use the comparator outputs internally since you are using RA5.

author: MarkF
author_rank: Super Contributor
timestamp: October 27, 2018, 12:59:23 am
content: The above will NOT work since you are using RA5 for your H-bridge.In order to disable C2OUT on RA5 you need to set the TRISA bit as an input.* Setting the TRISA<5:4> bits will disable the comparator outputs by configuring the pins as inputs.20.5 Comparator OutputsThe comparator outputs are read through the CMCONregister. These bits are read-only. The comparatoroutputs may also be directly output to the RA4 and RA5I/O pins. When enabled, multiplexers in the output pathof the RA4 and RA5 pins will switch and the output ofeach pin will be the unsynchronized output of thecomparator.You will need to use external comparators.

author: Aethelstan
author_rank: Contributor
timestamp: October 27, 2018, 03:54:20 pm
content: Hi guys, thanks for the responses.The choice of microcontrollers is already decided for me, and the base board is given to work from. We can’t have new board made, otherwise I would control the H bridge via port C. I think some decisions were made to specifically cause us problems.I will take another look and probably not use the built in comparators, I don’t need to trigger an interrupt with them if the main loop is fast enough so I guess I can simply check the status of RA0 and RA1 in code. It is a shame, as we did have a lecture where the comparators were brought to our attention. Perhaps I will report on why I didn’t use the comparators, explaining how using RA2 and RA5 makes them unavailable.Cheers 🙂