TRIS As floating output

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
Hi,
Working on something,
Can i use this sample to avoid sending 5v to Outputport
Mean i need to change between GND/0V and NC / Floating,
Code:
{
    TRISC1=0;
    RC1=0;
    for (y=0;y<2000;y++)
{__delay_ms(1);
}
    TRISC1=1;
 // Output/Input "floating"
}
 

sagor

Joined Mar 10, 2019
903
I would use a "standard" logic level MOSFET switch on the output pin of your MCU. A output high will make the MOSFET conduct (closed switch), an output low will make the MOSFET "open", not conducting. Only unknown is at what level of NC (closed) that device needs in terms of voltage/current or "resistance". If it triggers with 5 or 10 ohms resistance (detects as closed), then just about any logic level MOSFET will do, such as the 2N7000 series. If you need a lower "NC" resistance, you may have to look for a MOSFET with a lower RDSon.
 

BobTPH

Joined Jun 5, 2013
8,812
I would use a "standard" logic level MOSFET switch on the output pin of your MCU. A output high will make the MOSFET conduct (closed switch), an output low will make the MOSFET "open", not conducting. Only unknown is at what level of NC (closed) that device needs in terms of voltage/current or "resistance". If it triggers with 5 or 10 ohms resistance (detects as closed), then just about any logic level MOSFET will do, such as the 2N7000 series. If you need a lower "NC" resistance, you may have to look for a MOSFET with a lower RDSon.
Which is exactly what the micro is doing. No need for an external transistor unless you need to sink more current than the micro can.
 

sagor

Joined Mar 10, 2019
903
That is the one thing we don't know, how much current is drawn when the contact is "closed". Also, the vendor recommends running the device at 12V, not 5V for some reason, so I suspect there is +12V on the contacts to be "closed". Too high for the micro. Thus, unless the user measures the open voltage and then the current in a closed state, we are all guessing if the TRIS trick will work or not.
 

JohnInTX

Joined Jun 26, 2012
4,787
If you never want to send 5V to the output then these two lines should be the other way round.
Agreed. Use that sequence in power up initialization too.

Which is exactly what the micro is doing. No need for an external transistor unless you need to sink more current than the micro can.
Agreed. We need to know the specs.

A big thing that jumps out at me is potential r-m-w issues. Going from active low to pulled up open-drain works fine in principle but the slower rise time of a pulled up output should be considered. More importantly, if TS is using any peripheral on PORTC (I2C slave particularly) he should not use TRISC1 = x. He should define two TRISC values that have the other 7 bits constant and the switched bit different i.e.

#define TRISC_OUT1 0b00000010 // TRISC1 = 1
#define TRISC_OUT0 0b00000000 //TRISC1 = 0
then use
TRISC = TRISC_OUT1; // RC1 = pulled up
TRISC = TRISC_OUT1 // RC1 = active low (assuming RC1 is 0

There are solid reasons why this must be so but I don't have time to describe it all in detail at the moment. If TS wants more info I have a writeup somewhere that I'd be happy to post.

TL;DR is
Don't flip individual bit on TRIS
All outputs go to LATx registers
If your chip does not have LATx registers, maintain a copy of the port in RAM e.g. PORTCimage, flip bits on that then write the byte to the port using PORTC = PORTCimage.

Doing that will avoid some real issues that I personally have experienced first hand back in the early days. I found them difficult to sort out. My client found them expensive.

Good luck!

EDIT: r-m-w is Read-Modify-Write, it's how the PIC manipulates its registers including the IO ports.
 

geekoftheweek

Joined Oct 6, 2013
1,201
Which PIC are you using? I am assuming an early 10/12/16f based on the original question. If something more recent there may be other registers to consider
 
Top