PIC12F683: digital and analog input on same pin

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
User avatar
tHeo
Posts: 3
Joined: Fri Feb 03, 2017 1:49 am

PIC12F683: digital and analog input on same pin

Post by tHeo » Fri Feb 03, 2017 2:03 am

Hello, first topic on here, hope someone will clear out some doubts I have.

I'm designing a tiny motor controller for some micro R/C vehicles, and the board should also manage some LEDs installed on the models.

I need to read two servo PWM signals (1 to 2 ms high time, 20ms period) and since I don't really want to switch to a PIC with more pins (for board size reasons) I planned to use the same pin (GP4/AN3 in my case) to read one of the signals AND the battery voltage.

The battery would be a 2S LiPo, so its voltage will always (theoretically) be between 6.0V and 8.4V.
I setup a voltage divider with 10k resistors to bring half the battery voltage to the analog input. The voltage on the input will always be between 3.0V and 4.2V, so I shouldn't have any issues due to the digital input being fed an analog voltage (datasheet states minimum input-high voltage is 2.0V).

The PWM signal would drive an NPN transistor that pulls the input down to ground, shorting the lower side of the voltage divider; the other resistor would act as a pull-up, bringing the voltage back up to 3...4.2V once the PWM pulse ends.

Plan is to wait for the first PWM pulse and read its duration with the pin configured as a digital input, then immediately switch to analog input and start the ADC conversion.
Once the conversion is done, switch the pin back to digital input and wait for the next PWM pulse.
Given that the down time of the PWM signal is at least 18ms, I should have time to complete the conversion before the next pulse.

Do you see any potential issues or flaws in this solution?

Thanks and sorry for the long post :wink:

User avatar
phalanx
Non-SFE Guru
Posts: 1979
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC12F683: digital and analog input on same pin

Post by phalanx » Fri Feb 03, 2017 7:15 am

Hi Theo,

Since you will have a varying voltage on your I/O pin while in digital mode, the biggest concern when doing something like this is to make sure the voltage range that could be present is enough to register as a HIGH value on the pin. On page 123 of the datasheet, there are details about Vih (Input Hi Voltage) for I/O ports. In this case, there are details about TTL inputs over 2 different voltage ranges and a single line about Schmitt Trigger buffers. If you look on page 8, you will see that your GP4 pin is a TTL input.

Assuming you are running your PIC at 5V, you have a minimum Vih value of 2.0V so your 3.0-4.2V range should work properly in this application. Keep in mind that a 10K pull-up is higher than recommended for the A/D converter so your acquisition time will be a little longer than the ideal value but with 18ms to spare, you have enough time to make multiple samples for the purposes of averaging your result.

Definitely breadboard this solution to make sure it works as expected. In theory it should but test before you commit!

-Bill

User avatar
tHeo
Posts: 3
Joined: Fri Feb 03, 2017 1:49 am

Re: PIC12F683: digital and analog input on same pin

Post by tHeo » Fri Feb 03, 2017 8:13 am

Hi phalanx,

and thank you for your reply.

I already had checked everything you listed but I was worried I was overlooking something important.

Yes the PIC will be run at 5V via a L4941 (great little IC with only 450mV DO @ 1A).

I will breadboard it for sure before implementing a PCB, hoping everything will be fine.

Thanks again!

User avatar
phalanx
Non-SFE Guru
Posts: 1979
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC12F683: digital and analog input on same pin

Post by phalanx » Fri Feb 03, 2017 8:20 am

Let us know how it works once you get a chance to breadboard it. I've never had occasion to try something like this so I'm curious how well it will work.

-Bill

User avatar
tHeo
Posts: 3
Joined: Fri Feb 03, 2017 1:49 am

Re: PIC12F683: digital and analog input on same pin

Post by tHeo » Fri Feb 03, 2017 1:25 pm

I tried it a couple minutes ago and...well, it seems to work :D

I'm having a few glitches, nothing I can level out with some filtering (and I guess the poor connections of the breadboard do not help)

User avatar
phalanx
Non-SFE Guru
Posts: 1979
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC12F683: digital and analog input on same pin

Post by phalanx » Mon Feb 06, 2017 6:58 am

Awesome! Good luck with your project.

-Bill

Post Reply