// LucidTronix 7 Segment LED and 74HC595 shift register Tutorial
http://www.lucidtronix.com/tutorials/41
//! +---+-------+-------------------------------+
//! |No.|PinName|Pin Description |
//! |---|-------|-------------------------------|
//! | 1 | E |Digital Pin input |
//! | 2 | D |Digital Pin input |
//! | 3 | COM |Commont pin input(VCC or GND) |
//! | 4 | C |Digital Pin input |
//! | 5 | DP |Digital Pin input |
//! | 6 | B |Digital Pin input |
//! | 7 | A |Digital Pin input |
//! | 8 | COM |Commont pin input(VCC or GND) |
//! | 9 | F |Digital Pin input |
//! |10 | G |Digital Pin input |
//! +---+-------+-------------------------------+
int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
byte dec_digits[] = {0b11000000,0b11111001,0b10100100,0b10110000,
0b10011001,0b10010010,0b10000011,0b11111000,0b10000000,0b10011000 };
void setup()
{
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
for (int numberToDisplay = 0; numberToDisplay < 10; numberToDisplay++)
{
// take the latchPin low so
// the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, dec_digits[numberToDisplay]);
//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
// pause before next value:
delay(300);
}
}
.END
Arduino shiftOut() function
http://arduino.cc/en/Reference/ShiftOut
Description
Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available.
Note: if you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW).
This is a software implementation; see also the SPI library, which provides a hardware implementation that is faster but works only on specific pins.
Syntax
shiftOut(dataPin, clockPin, bitOrder, value)
Parameters
dataPin: the pin on which to output each bit (int)
clockPin: the pin to toggle once the dataPin has been set to the correct value (int)
bitOrder: which order to shift out the bits; either MSBFIRST or LSBFIRST.
(Most Significant Bit First, or, Least Significant Bit First)
value: the data to shift out. (byte)
Returns
None
Note
The dataPin and clockPin must already be configured as outputs by a call to pinMode().
shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255.
...
Example
For accompanying circuit, see the tutorial on controlling a 74HC595 shift register.
//**************************************************************//
// Name : shiftOutCode, Hello World //
// Author : Carlyn Maw,Tom Igoe //
// Date : 25 Oct, 2006 //
// Version : 1.0 //
// Notes : Code for using a 74HC595 Shift Register //
// : to count from 0 to 255 //
//****************************************************************
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//count up routine
for (int j = 0; j < 256; j++) {
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, j);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);
delay(1000);
}
}
.END2
No comments:
Post a Comment