Why use pinMode() instead of digitalWrite() to bit bang TM1637

I found a TM1637 4 digit 7 segment LED library in the library manager. The library bit bangs the DATA and CLK lines. This is probably because the communication protocol is similar, but not quite I2C, so the AVR I2C hardware peripheral might not work with this device. That part seems to make sense. What is confusing me is that instead of using digitalWrite() the code is changing the pinMode() from input to output for the bit banging!?

What is the reason to do this? I intended on rewriting the code to excise all the Arduino-ness out of it and write to the direction registers and output registers directly. Changing from input to put put is done with the DDRx register (on a AVR 328p), which is different from changing the PORTx register for making the pin high or low. Why is the library changing from input to output?

TM1637 datasheet

Library

Going from input to output is operating as an open Drain driver.

Thanks for the quick response

Okay, I think I get it. The TM1637 is using the typical I2C bus with pullup resistors so that you must switch to an input to pull the line low.

So I would use the direction registers and set the pin to an input to pull the line low. Set the direction register to output to allow the pullup resister to pull the line high.

I should be able to use this method on any mcu (I also play around with Parallax Propellers and others)

No, that is backwards. You set the pin to OUTPUT, and DRIVE it LOW. You flip it to INPUT, to let the pullups PULL it HIGH.

You are correct. I double checked with a logic analyzer and a bit of test code (trust, but verify).
I am going to have to wrap my head around that a bit more. I am mixed up somewhere. I thought that when a pin is set as an input the pin would be sinking current, so that anything connected to that bus would see ground.

Thanks again for the help!

No.


An input pin can be considered as an open circuit (almost), maybe 100 Meg Ohms.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.