I am really having a hard time understand how to code the shift register. I know I can control multiple outputs while only using 3 pins on the Arduino UNO board and I followed the tutorial for the 8 LEDS. Can anyone help me understand how to change the outputs.
Here is a tutorial for the 74HC595 shift register. The principles are the same for most all shift registers. https://lastminuteengineers.com/74hc595-shift-register-arduino-tutorial/
The shiftOut() function reference. shiftOut() - Arduino Reference
Which shift register do you have?
HI,
Can you tell us;
What model Arduino you are using?
What part number shift register?
A circuit diagram showing how you are connecting to it.
Thanks.. Tom...
what have you tried, what does it do and what did you expect it to do?
here's some code i wrote to control the 7-segment displays on a Multifunction shield
// drive 7-seg displays on MultiFuction boardj
#include <TimerOne.h>
#include "seg7disp.h"
// -------------------------------------
// constants for multiplexed 7-segment display
#define Latch 4
#define Clock 7
#define Data 8
#define LED0 10
#define LED1 11
byte pins [] = { Latch, Clock, Data, LED0, LED1 };
// a d
// f b c e
// g g
// e c b f
// d h a
//
// h g f e d c b a
const byte SEGMENT_MAP_DIGIT[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0X80, 0X90
};
const byte SEGMENT_MAP_DIGIT_F [] = {
0xC0, 0xCF, 0xA4, 0x86, 0x8B, 0x92, 0x90, 0xC7, 0x80, 0x82
};
const byte DISP_MAP [] = { 1, 2, 4, 8 };
#define N_DISP sizeof(DISP_MAP)
byte disp [N_DISP] = {SEGMENT_OFF, SEGMENT_OFF, SEGMENT_OFF, SEGMENT_OFF};
int flag;
// -----------------------------------------------------------------------------
// shift 16-bits from data into the shift register
void output (
uint16_t data)
{
digitalWrite (Latch, LOW);
for (unsigned i = 0; i < 16; i++, data <<= 1) {
digitalWrite (Data, 0 != (data & 0x8000));
digitalWrite (Clock, HIGH);
digitalWrite (Clock, LOW);
}
digitalWrite (Latch, HIGH);
}
// -----------------------------------------------------------------------------
// repeatedly display one digit
// lower 8-bits selects the digit
// upper 8-bits species the segments
void isr (void)
{
static byte idx = 0;
uint16_t val = (disp [idx] << 8) + DISP_MAP [idx];
output (val);
if (N_DISP <= ++idx)
idx = 0;
}
// -----------------------------------------------------------------------------
// update the value of each digit
void seg7segs (
int val,
byte segs )
{
for (int i = N_DISP-1; i >= 0; i--, val /= 2)
disp [i] = val & 1 ? segs : SEGMENT_OFF;
}
// -----------------------------------------------------------------------------
// update the value of each digit
void seg7disp (
int valX10,
int flip )
{
Serial.println (__func__);
int i;
if (flip) {
for (i = 0; i < (int)N_DISP; i++, valX10 /= 10)
disp [i] = SEGMENT_MAP_DIGIT_F [valX10 % 10];
// blank leading zeros
i = N_DISP-1;
while (SEGMENT_MAP_DIGIT [0] == disp [i])
disp [i--] = SEGMENT_OFF;
}
else {
for (i = N_DISP-1; i >= 0; i--, valX10 /= 10)
disp [i] = SEGMENT_MAP_DIGIT [valX10 % 10];
disp [N_DISP-2] &= SEGMENT_DEC; // decimal pt
// blank leading zeros
i = 0;
while (SEGMENT_MAP_DIGIT [0] == disp [i])
disp [i++] = SEGMENT_OFF;
}
}
// -----------------------------------------------------------------------------
// update the value of each digit
void seg7off (void)
{
Serial.println (__func__);
for (int i = N_DISP-1; i >= 0; i--)
disp [i] = SEGMENT_OFF;
}
void seg7on (void)
{
Serial.println (__func__);
for (int i = N_DISP-1; i >= 0; i--)
disp [i] = 0;
}
// -----------------------------------------------------------------------------
void seg7init (void)
{
Serial.println (__func__);
for (unsigned i = 0; i < sizeof(pins); i++) {
digitalWrite (pins [i], HIGH);
pinMode (pins [i], OUTPUT);
}
Timer1.initialize(5000);
Timer1.attachInterrupt (isr); // blinkLED to run every 0.15 seconds
}
Where is "seg7disp.h" ?
I made a start to put the multifunction shield in Wokwi: Multi Function Shield - Wokwi ESP32, STM32, Arduino Simulator
const byte SEGMENT_OFF = 0xFF;
const byte SEGMENT_DEC = 0x7F;
void seg7disp (int val, int flip);
void seg7init (void);
void seg7off (void);
void seg7on (void);
void seg7segs (int val, byte segs);
It is doing something, but I don't know what: Multi Function Shield - Wokwi ESP32, STM32, Arduino Simulator
The examples ( @groundFungus @gcjr ) will have three events occur using the 595 latch pin, clock pin and data pin.
When you want to write a value to the shift register, you will:
- set the shift register latch pin LOW.
- use shiftOut(dataPIN, clockPIN, BITORDER, data); to move your data to the shift register
- set shift register latch pin HIGH.
The output from this will show on the output legs of the shift register.
{late edit] Here's my mega-7-sega(ment) that lights the LEDs in binary. If you re-arrange the sequence, you can make it show a digit.
74hc595 8-Bit Shift Registers code for your reference,
Click here -> Wokwi Simulator
#define SRCLK 13 // SHCP => SRCLK = Shif Register Clock
#define SER 11 // DS => SER = Serial Input
#define RCLK 9 // STCP => RCLK = Storage Register Clock
void setup() {
ShiftRegisterInit();
}
void loop() {
// TODO: SOMETHING
ShiftRegisterWrite(B10010011); // Write data to shift register;
}
void ShiftRegisterInit() {
pinMode(RCLK, OUTPUT); // Output Pin Mode
pinMode(SER, OUTPUT); // Output Pin Mode
pinMode(SRCLK, OUTPUT); // Output Pin Mode
digitalWrite(SRCLK, HIGH); // Initial with HIGH state
digitalWrite(RCLK, HIGH); // Initial with HIGH state
}
void ShiftRegisterWrite(uint8_t data) {
digitalWrite(RCLK, LOW);
for (uint8_t index = 0; index < 8; index++) {
digitalWrite(SRCLK, LOW);
digitalWrite(SER, bitRead(data, index));
digitalWrite(SRCLK, HIGH);
}
digitalWrite(RCLK, HIGH);
}
SNx4HC595 8-Bit Shift Registers With 3-State Output Registers datasheet (Rev. J)
To all who responded thank you. I found each reply to be very helpful and I think I have a rudimentary understanding of shift registers now. A question I have now is this. I got the photoresistor to work with the shift register but the code turns the led lights on when light is on which is the code example given in the tutorial. How do I change the code such that the lights come on when it is dark?
Change which code? I don't see any.
Nice, THX.
I did something after consuklting with my friends at WIkipedia:
void loop() {
// SOMETHING
for (int tt = 0; tt < 256; tt++) {
ShiftRegisterWrite(tt ^ (tt >> 1)); // Write data to the shift register;
delay(333);
}
}
Points off for use of delay().
a7
I don't see an LDR in the tutorial. Which tutorial are you refering too.
Hi, @n545061
What tutorial?
Can you please post a link?
What type/part number of shift register are you using?
Thanks.. Tom..
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.