CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Driving a load with 2 pins
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
blowtorch



Joined: 11 Jun 2013
Posts: 35
Location: Cape Town

View user's profile Send private message

Driving a load with 2 pins
PostPosted: Mon Sep 17, 2018 12:48 am     Reply with quote

I am trying to work out the best way, and indeed if it is feasible / recommended to drive a load using 2 IO pins.

PIC is a 16LF18345. Datasheet (F) on page 417 specifies the output high voltage as VDD-0.7 at 6ma. Now lets say I wanted to source 11ma, and connect the load to 2 port pins (I have a spare pin).

If they both off, or both on, I can't see a problem. But I do see a problem during turn on or turn off. Lets say they both driven low, and I wish to drive them both high.

Should I do this:
Code:
output_float(PIN2);
output_high(PIN1);
output_high(PIN2);
output_drive(PIN2);

or is the compiler going to set TRIS by itself on the second output_high instruction, ending up with one port driven high, the other driven low, basically "shorting" the 2 pins?

Put another way, will I have to use fast io to take control of the tris and will the approach above work, or is there some other way of doing it?

I know I can drive the load by using e.g. a fet or additional circuitry, but I am both interested from an academic / learning perspective, as well as wishing to save a component or 2...

The load is a magnetic indicator (i.e. self driven).
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 2:04 am     Reply with quote

Use portwide I/O.

You are going to have to, since even with fast_io, if you operate one pin after the other there is still a delay.

So for pin 0 & 1 on port B for example.

output_b(3); //turns these two pins both on.
output_b(0); //turns both pins off.

On a PIC18, and some PIC16's, you can access the port latch directly. Then if you have other pins on the same port used, you can cheat and directly change the latch. So:
Code:

#byte LATB=getenv("SFR:LATB")

    //then to set these pins only
    LATB|=3; //requires TRIS already set to output on the pins

    //or to turn then off
    LATB&=0b11111100;


Now if you set the TRIS, by simply driving the pins both 'off' at boot (so assuming that current is only drawn when they go high, output_low on both pins), then when you want to drive the pair use the above code, both latches will be operated at once, and TRIS will not change. Since you directly change the latch, every other pin on the port is left unchanged.
Now fortunately, your PIC16, is one of the later ones that gives you access to the LAT register (earlier chips don't), so this option is available to you. Smile
blowtorch



Joined: 11 Jun 2013
Posts: 35
Location: Cape Town

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 3:07 am     Reply with quote

Ttelmah you a star as always - thank you. Easy to put in a macro as well for readability. I am now 100% ok with what you have explained using multiple pins on a single port.

Taking this concept further, lets say the 2 pins were on different ports. Could one still achieve a similar result but with additional steps?

I have read one of your previous posts on the relationship with TRIS & port io, and the datasheet section on the io ports but I am still somewhat unclear.

The datasheet says:
Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input (i.e., disable the output driver). Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output (i.e., enables output driver and puts the contents of the output latch on the selected pin).

So would it be possible to then use a modified approach similar to the one I suggested in the first post...something like this:
Assume using normal IO, and output_low on both pins at start.

To turn on both:
Code:
output_float(pin2); // set pin2 as high impedance
output_high(pin1); // any port, normal io, turn pin on
// we now have vdd present on the pin1, driving the buzzer but also have vdd being present on pin2, which is high impedance therefore we are not shoting pin1 to ground via pin2
LATB|=1; // assuming pin2 is B0, we now set the output latch to high, but no external change occurs in the circuit because TRIS still has the pin as an input
output_drive(pin2); // now change pin2 to output, output latch is already set high, so the pin will now source current
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 3:28 am     Reply with quote

Problem is that if they are on separate ports, they are not going to operate at exactly the same time. Now the difference 'may not matter', but will be there...
blowtorch



Joined: 11 Jun 2013
Posts: 35
Location: Cape Town

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 4:22 am     Reply with quote

Yes agreed. In this case from a load perspective it does not matter.

But from from a PIC perspective it does, I would not want to destroy the port pin by driving it high, when it is connected to another pin which is being driven low, effectively "shorting" the 2.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 4:52 am     Reply with quote

re:
Quote:
The load is a magnetic indicator (i.e. self driven).

Have to ask what VDD does the unit require? Since you're using an LF PIC, assuming a 3 volt battery, Vout is at best say 2.5 volts ?
IF the device can run on say 1.5 volts, you could add a diode between each PIC I/O pin to the device. This would prevent damage to the PIC.

Another point to consider is the device's startup time. If it has say 1ms warmup or startup time. That is snail slow compared to a PIC setting two I/O pins, even on two ports

Also as you're powereng the device from I/I pins, I'd add a cap. Maybe 10-100mfd? Something to act as a small reservoir and smooth out the power to the device.

just food for thought...a lot can be learned 'on the bench' !

Jay
blowtorch



Joined: 11 Jun 2013
Posts: 35
Location: Cape Town

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 5:55 am     Reply with quote

The device is nominal 3V, specified voltage range 2-5V. Circuit voltage 3.3V.

Testing on the bench, the device will start up and work ok from about 1.8V, drawing 7ma. At 2.5V 10.3ma. I have not put it on a scope yet to check current requirement on startup or statup time - the circuit clocked at 4Mhz, so 2 successive calls to output high only a couple microseconds.

Nice on the diodes, but if I have to make a circuit change I would drive it via a transistor (which is what we should have done in the first place).

The whole rationale behind this is a bit of a band aid approach. There are 2 adjacent testpoints on the pcb which are no longer needed, one to a spare pin, the other on the intended driving io pin line.

Out of a number of buzzers, one or 2 will not start, due to the voltage dropping on the output pin when its driven high. Most work fine, and I thought a quick temporary fix could be to simply bridge the 2 together, and drive it using 2 pins, thereby doubling the available current before a voltage drop occurs...
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:02 am     Reply with quote

Could you make the pins inputs when not driving the load.
Make them outputs in the right direction when needed.

Most PIC I/O pins are inputs on start up!
So should not be a problem

This way you don't get the contention problem.
Also avoiding the diodes and their voltage drop.

Should work with one or two ports.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:10 am     Reply with quote

If you look, this was what he was originally posting.
It is fine, provided the target device does not 'care' how the power actually comes 'on'. Problem is (of course), that the power will come on 50% at a time as you turn the pins on, which will result in a slower rise time, and a potential 'step' in the supply appearing. Some devices may not like this. During the phase when one PIC pin is on and the other not driving, the pin will (of course) be well overdriven. Now PIC pins are generally quite forgiving of overload, but it could cause a long term problem.
Two pins on one port switched together avoids all these potential issues. Or adding a driver transistor!... Smile
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:21 am     Reply with quote

Sorry.

I was watching the way this thread was developing with the later posts.
Totally ignored the original post.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:25 am     Reply with quote

I must admit my comment would be that it is going to be well worth adding diodes to trap any overshoot created by a device like a buzzer anyway. These can produce nasty overshoot, which could be potentially damaging to PIC pins... :(
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:33 am     Reply with quote

hmm.. now I'm curious as to what the 'magnetic indicator' really is !
I originally thought some new fangled N-S-E-W device but Mr. T wrote the dreaded 'buzzer' word. What the device really is does dictate how to properly power it from the PIC.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:36 am     Reply with quote

I said buzzer, because of this a couple of posts ago:
Quote:

Out of a number of buzzers, one or 2 will not start, due to the voltage dropping on the output pin when its driven high. Most work fine, and I thought a quick temporary fix could be to simply bridge the 2 together, and drive it using 2 pins, thereby doubling the available current before a voltage drop occurs...


Eeek!...
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 11:49 am     Reply with quote

hmm wonder if it's a peizo style 'buzzer'? they don't draw a lot of current but I've seen some electromechanical ones in a very similar package and they are GREAT EMI generators !
blowtorch



Joined: 11 Jun 2013
Posts: 35
Location: Cape Town

View user's profile Send private message

PostPosted: Mon Sep 17, 2018 1:56 pm     Reply with quote

Sorry to clarify: device is an audio magnetic indicator, self driven. Just apply DC to hear the beep...

Max consumption on bench power supply just over 14ma, the PIC can handle up to 50ma on any pin, so not worried about overdriving the pin via the load.

I have tested the code posted earlier, the beeper or buzzer (whatever its commonly called) works fine - I just wanted advice on how to avoid setting a pin as output and driving it high, into another pin set as output but being driven low. Hence the fiddling with TRIS to stop this from happening.

The DS for the PIC16LF18345 specifies an output voltage of VDD - 0.7 at 6.9mA, with VDD=3V. I am running at 3.3, so pin output voltage at load of 6mA of less should be 2.6V. Sadly there is no graph giving the relationship between current sourced from a pin and the resultant voltage.

Ironically the %$$% beeper is not critical to the circuit, but seeing that it's there, I would like it to work!

(edited to add DS info)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group