This article is simple demonstration, how to use popular shift register 74HC595 as output port expander for IQRF TR-52D module. I use It as simple remote switch unit for my workshop at oposite side of garden. Simply, from serial terminal or single purpose software written in C#.
Hardware is briefly depicted by following block diagram. The colors indicate different voltage domains. Full schematic and layout are linked at the end of the article.
74HC595 DRIVER
The most interesting parts of our code are SHift Redister (SHR) initialisation and update routines. For SHR control we need these five lines:
- DATA – input data line (SHR: pin 14; TR-52D: pin 8)
- CLK – clock input line (SHR: pin 11; TR-52D: pin 5)
- STROBE – strobe input line (SHR: pin 12; TR-52D: pin 6)
- OE – output enable input line (SHR: pin 13; TR-52D: pin 7)
- MR – master reset input line (SHR: pin 10; TR-52D: pin 1)
// Port initialisation and SHR setup
void SR74HC595_Init()
{
TRISA.0 = 0; // Pin 1 = MR (output)
TRISC.6 = 0; // Pin 5 = CLK (output)
TRISB.4 = 1; // paralel pin (input)
TRISA.5 = 1; // paralel pin (input)
TRISC.3 = 0; // Pin 6 = STROBE (output)
TRISC.4 = 0; // Pin 7 = OE (output)
TRISC.5 = 0; // Pin 8 = DATA (output)
TRISC.7 = 1; // paralel pin (input)
OE_SET; // output at 3rd state
CLK_CLR; // data at zero
DATA_CLR; // clk at zero
STROBE_CLR; // strobe at zero
MR_CLR; // clear data register
waitDelay(1);
MR_SET;
waitDelay(1);
STROBE_SET; // storbe it out
waitDelay(1);
STROBE_CLR;
waitDelay(1);
OE_CLR; // enable outputs
}
This simple part of code set PIC ports direction and state and set these ports to initial state. Next, we will clear SHR, strobe it out and enable output pins. The following routine ensure SHR updates against the arguments
// fill 74HC595 and strobe it out
void SR74HC595_SendByte(uns8 data)
{
uns8 i;
uns8 mask = 1;
waitDelay(1);
for (i=0; i<8; i++)
{
if (data & mask) // set data against the mask
DATA_SET;
else
DATA_CLR;
waitDelay(1); // shift it
CLK_SET;
waitDelay(1);
CLK_CLR;
waitDelay(1);
mask = mask << 1;
}
STROBE_SET; // strobe it out
waitDelay(1);
STROBE_CLR;
waitDelay(1);
}
Rest of code is linked at the end of this article.
PC control unit
Remote switch unit is done. Now is right time to do something for comfortable communication with this unit. I don’t like time-consuming solutions, so I chose USB-UART data converter at my favourite china reseler and made simple adapter for it.
Of course, you need some application for IQRF module. In this case, You do not need to write any line of code, because You can use complete example from startup-package without any changes. This example has name UART_LINK.c. I prepared single purpose software written in C# for easier using of remote switch unit. Executable file is linked below.