Sunday, August 19, 2007

sto4021 mini lib

This mini lib allows one to quickly read data from a 4021 parallel to serial shift register IC. This effectively adds eight digital inputs to the Arduino at a cost of only three digital pins. Additional I/O can be added by assigning additional latch pins to successive 4021's.

URL: http://www.milkcrate.com.au/_other/arduino/sto_4021.zip.

sto_4021_setup(latch, clock, data_in)
returns: void
This function should be called during setup(), and sets up the pins and relevant direction for read operations.

latch (int): sets the latch pin
clock (int): sets the clock pin
data_in (int): sets the data input pin


sto_4021_read(); returns: byte
This function should be used to read the 4021 IC, and returns a byte of data of the current state of the latched input pins on the device.


Example Code:

/*
Reading a NES Controller (using encapsulation)
By Sebastian Tomczak
19 August 2007
Adelaide, Australia
*/


/* INCLUDE */
#include <sto_4021.h>

/* INITIALISATION */
byte controller_data = 0;

/* SETUP */
void setup() {
Serial.begin(9600);
sto_4021_setup(2, 3, 4);
}

/* MAIN PROGRAM */
void loop() {
controller_data = sto_4021_read();
Serial.print(controller_data);
delay(10);
}

0 comments: