[Arduino] Using 74HC595 8-bit serial-in/serial or parallel-out shift register
2017. 1. 12. 08:49 |
The 74HC595; 74HCT595 is an 8-bit serial-in/serial or parallel-out shift register with a storage register and 3-state outputs. Both th e shift and storage register have separate clocks. The device features a serial input (DS) and a serial output (Q7S) to enable cascading and an asynchronous reset MR input. A LOW on MR will reset the shift register.
Data is shifted on the LOW-to-HIGH transitions of the SHCP input. The data in the shift register is transferred to the storage register on a LOW-to-HIGH transition of the STCP input. If both clocks are connected together, the shift register will always be one clock pulse ahead of the storage register. Data in the storage register appears at the output whenever the output enable input (OE) is LOW. A HIGH on OE causes the outputs to assume a high-impedance OFF-state. Operation of the OE input does not affect the state of the registers. Inputs include clamp diodes. This enables the use of current limiting resistors to interface inputs to voltages in excess of VCC
※ Display 0~9 and A~E on a 7-segment
Circuit
Source Code 1
const int clockPin = 8; //SH
const int latchPin = 9; //ST
const int dataPin = 10; // DS
const byte seg[10] = {0b00000000, 0b00000001,0b00000010,0b00000100,
0b00001000,0b00010000,0b00100000,0b01000000,
0b10000000,0b1111111};
const byte digits[17] = {
0b01110111, //0
0b00000110, //1
0b10110011, //2
0b10010111, //3
0b11000110, //4
0b11010101, //5
0b11110101, //6
0b01000111, //7
0b11110111, //8
0b11010111, //9
0b11100111, //A
0b11110100, //B
0b01110001, //C
0b10110110, //D
0b11110001, //E
0b11110001, //F
0b11111111 //ALL
};
void setup() {
// put your setup code here, to run once:
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void LEDs(byte a) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, a);
digitalWrite(latchPin, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0; i<10; i++) {
LEDs(seg[i]);
delay(500);
}
for(int i=0; i<18; i++) {
LEDs(digits[i]);
delay(500);
}
}
Source Code 2
//www.elegoo.com
//2016.06.13
// Lab13 Using a 74hc595 and seven segment display to make reciprocal function
// define the LED digit patterns, from 0 - 9
// 1 = LED on, 0 = LED off, in this order:
// 74HC595 pin Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7
// Mapping to a,b,c,d,e,f,g of Seven-Segment LED
byte seven_seg_digits[10] = { B11111100, // = 0
B01100000, // = 1
B11011010, // = 2
B11110010, // = 3
B01100110, // = 4
B10110110, // = 5
B10111110, // = 6
B11100000, // = 7
B11111110, // = 8
B11100110 // = 9
};
// connect to the ST_CP of 74HC595 (pin 9,latch pin)
int latchPin = 9;
// connect to the SH_CP of 74HC595 (pin 8, clock pin)
int clockPin = 8;
// connect to the DS of 74HC595 (pin 10)
int dataPin = 10;
void setup() {
// Set latchPin, clockPin, dataPin as output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
// display a number on the digital segment display
void sevenSegWrite(byte digit) {
// set the latchPin to low potential, before sending data
digitalWrite(latchPin, LOW);
// the original data (bit pattern)
shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);
// set the latchPin to high potential, after sending data
digitalWrite(latchPin, HIGH);
}
void loop() {
// count from 9 to 0
for (byte digit = 10; digit > 0; --digit) {
delay(1000);
sevenSegWrite(digit - 1);
}
// suspend 4 seconds
delay(4000);
}
※ 74HC595 with 8 LEDs
Circuit
in my case, I used Arduino Uno and changed the pin numbers.
//by Simon Monk
//www.elegoo.com
//2016.06.13
int latchPin = 9;
int clockPin = 8;
int dataPin = 10;
byte leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(500);
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
Use Serial Monitor
Same Circuit as above
Source Code
//by Simon Monk
//www.elegoo.com
//2016.06.13
int latchPin = 9;
int clockPin = 8;
int dataPin = 10;
byte leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
updateShiftRegister();
Serial.begin(9600);
while (! Serial); // Wait untilSerial is ready - Leonardo
Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch >= '0' && ch <= '7')
{
int led = ch - '0';
bitSet(leds, led);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
if (ch == 'x')
{
leds = 0;
updateShiftRegister();
Serial.println("Cleared");
}
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
==> Type number on the Serial Monitor -> light on the LED of typed number
'IoT > Arduino' 카테고리의 다른 글
Arduino Project 1 - Automated Mung Bean Sprouts Growing Machine (0) | 2017.01.23 |
---|---|
[Arduino] Motion Sensor and Water Level Sensor (0) | 2017.01.17 |
[Arduino] Joystick, LED Dot Matrix and ADXL335 Module (0) | 2017.01.17 |
[Arduino] Ultrasonic, Keypad and Temperature and Humidity Sensor (0) | 2017.01.15 |
[Arduino] 7 Segment, Servo, LCD and Thermometer (0) | 2017.01.14 |
[Arduino] Library and RGB LED (0) | 2017.01.11 |
[Arduino] Servo Motor (0) | 2017.01.10 |
[Arduino] LCD and 7 Segment (0) | 2017.01.10 |
[Arduino] Illuminance (Photocell) and Ultrasonic Sensor (0) | 2017.01.09 |
[Arduino] Tri LED and Buzzer (0) | 2017.01.09 |