74HC595 Shift Register

Greeniverse
Posts: 2
Joined: Tue Dec 11, 2018 3:18 am

74HC595 Shift Register

Postby Greeniverse » Tue Dec 11, 2018 3:48 am

Hi All,

I am currently trying to control a TI 74HC595 Shift register using my ESP32 DevKit C. I think my problem seems to do with the data stream or clock rate. I try and turn a single output on and off in a loop and the high bit takes up 3 outputs and rather than turn off, it shifts the bit down a few pins.

Code: Select all

#include <Wire.h>
#include <MultiShiftRegister.h>

const int latchPin     = 12;                                //ESP32 Pin 12
const int clockPin     = 14;                                //ESP32 Pin 14
const int dataPin      = 27;                                //ESP32 Pin 27

MultiShiftRegister msr (1, latchPin, clockPin, dataPin);

void setup() {
  Serial.begin(9600);

  pinMode(latchPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(clockPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(dataPin, OUTPUT);                                 //Setting Pin Orientation
  delay(500);
}
void loop() {
  msr.set_shift(1);
  delay(2000);
  msr.clear_shift(1);
  delay(2000);
So in goes: LEDs 1-3 are on for 2 seconds, then LEDs 3-5 are on, then LEDs 5-7 are on. This code worked fine on an Arduino Uno. Any help would be appreciated! shift register library is included.
Attachments
MultiShiftRegister.h
(540 Bytes) Downloaded 849 times
MultiShiftRegister.cpp
(1.42 KiB) Downloaded 864 times

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: 74HC595 Shift Register

Postby ESP_Sprite » Tue Dec 11, 2018 7:43 am

Moved to the Arduino subforum.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: 74HC595 Shift Register

Postby idahowalker » Tue Dec 11, 2018 4:12 pm

Perhaps you may reconsider the code you are using from the Uno in relation to using an ESP32. For one look at the clock rate of the ESP32, Do you want your dual core ESP32 sleeping for a whole 2 seconds when it can be doing other things?
Any ways,

On the ESP32 you have 4 timers available for use, timers 0-3.

Code: Select all

#include <Wire.h>
#include <MultiShiftRegister.h>
////
#define latchPin     = 12                                //ESP32 Pin 12
#define clockPin     = 14                                //ESP32 Pin 14
#define dataPin      = 27                                //ESP32 Pin 27
#define TIMER_FOUR 3
#define FourK 4000
#define TwoK 2000
////
volatile int iTicCount = 0; //counts milliseconds
////
MultiShiftRegister msr (1, latchPin, clockPin, dataPin);
////
void IRAM_ATTR onTimer()
{
  iTicCount++;
  //
  if ( iTicCount == TwoK )
  {
msr.set_shift(1);
  }
  if ( iTicCount == FourK )
  {
    msr.set_shift(1);
    iTicCount = 0;
  }
  //
}
void setup()
{
//
 /* Use 4th timer of 4.
    1 tick 1/(80MHZ/80) = 1us set divider 80 and count up.
    Attach onTimer function to timer
    Set alarm to call timer ISR, every 10000uS and repeat / reset ISR (true) after each alarm
    Start an timer alarm
  */
  timer = timerBegin( TIMER_FOUR, TimerDivider, true );
  timerAttachInterrupt( timer, &onTimer, true );
  timerAlarmWrite(timer, OneK, true);
  timerAlarmEnable(timer);
  ////
   pinMode(latchPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(clockPin, OUTPUT);                                //Setting Pin Orientation
  pinMode(dataPin, OUTPUT);                                 //Setting Pin Orientation
  //
  }
  void loop() {} 
 
As a possibility.

Greeniverse
Posts: 2
Joined: Tue Dec 11, 2018 3:18 am

Re: 74HC595 Shift Register

Postby Greeniverse » Sat Dec 15, 2018 8:27 am

Hi idohowalker,

Thanks for the reply.

I think I may have worded my problem incorrectly. The looped set_shift and clear_shift turns a pin on the shift register high and low to blink an LED. It's just an example to visually test the shift registers.

My main problem is that when I try and do this, the shift register is only supposed to turn on 1 LED by sending a byte (ie sending B01000000 should turn on the 2nd LED from the left). After i send the byte it turns on 3 LEDs. I think it has something to do with the length of a an up tic is longer than a single clock pulse and so it registers B01110000.

This is all speculation and I don't really know how to change any of the hardware stuff. I didn't do any computer science stuff at all so I don't really know how the clock rates between the shift register and ESP32 relate. If i'm close, is there anything that could help?

Thanks

Who is online

Users browsing this forum: No registered users and 54 guests