output open drain

Status
Not open for further replies.

stephanschulz

Well-known member
hi

i am reworking code that i wrote for the maple mini (also arm) to work with the teensy.
what's the equivalent of pinMode(ledPin,OUTPUT_OPEN_DRAIN); in the teensy world?

my led driver needs to receive a pwm in open drain mode.

thanks,
stephan.
 
Clear the port's bit in its PORT register, then output the inverted level to the pin's bit in the DDR register. This way when you write a 0 to the DDR register, the pin becomes an input, and thus high-impedance. When you write a 1 to the DDR register, it becomes an output of a low level.

Example: PB2
Code:
PORTB &= ~(1<<2); // only needs to be done once
DDRB  |=   1<<2; // pull line low
DDRB  &= ~(1<<2); // let line float

There's probably some Arduino equivalent to this. Maybe just using the normal "output a 0 on this pin", then "make this pin an input" and "make this pin an output" functions.

If you're using hardware-based PWM, then I don't think the above can work, though I've never used PWM on an AVR before. All I'd know to do in that case is put a diode between the pin and the line, with its cathode towards the AVR. This way it lets a low level pull the line low, but blocks a high level from the AVR.

EDIT: if you're adding an external component, I guess you might as well just use a mosfet instead of a diode, to do it by the book.
 
Last edited:
Status
Not open for further replies.
Back
Top