Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I use 74HC595 shift register
10-12-2013, 11:33 PM,
#1
How can I use 74HC595 shift register
I want control 8 relays with shift register. I use 74HC595 and write some code to send binary number to shift register but (first number+1) send and stop working.
Code:
//Pin connected to DS of 74HC595
#define DS 21
//Pin connected to SH_CP of 74HC595
#define SHCP 22
//Pin connected to MR of 74HC595
#define MR 24
//Pin connected to ST_CP of 74HC595
#define STCP 23
//Pin connected to OE of 74HC595
#define OE 25

int i,j,;
          
void setup()
    {
  //set pins to output so you can control the shift register
   pinMode(STCP, OUTPUT);
   pinMode(SHCP, OUTPUT);
   pinMode(DS, OUTPUT);
   pinMode(MR, OUTPUT);
   pinMode(OE, OUTPUT);
    }
      
void loop()
{
    for (i=240 ;i<256 ;i++)  
        {
         digitalWrite(STCP,LOW);                              //low latch clock
           for (j=0;j<8;j++)                                  //loop for send one byte,bit by bit
           {
               if ((i&0b10000000)!=0) digitalWrite(DS ,HIGH); //if bit is 1 data line high
               else digitalWrite(DS ,LOW);                   //if bit is 0 data line is low
                    digitalWrite(SHCP ,LOW);                      //one clock for shift register
                    delayMicroseconds(5);
                    digitalWrite(SHCP ,HIGH);
                    i=i<<1;                                       //shift byte
                }
         digitalWrite(STCP,HIGH);                            //high lach clock
         delay(1000);
    }
}
output is 11110001 Instead 11110000.
can i use spi library?
Reply
12-12-2013, 04:44 AM, (This post was last modified: 12-12-2013, 04:48 AM by iyahdub.)
#2
RE: How can I use 74HC595 shift register
rest of the code would help us to help you Smile
Anyway, SPI should work , dont see why not
Reply
12-12-2013, 09:54 AM,
#3
RE: How can I use 74HC595 shift register
On a first glance, I can spot two issues:

a) The shift destroys your outer loop's counter i. Won't affect first (and only) result, though.
b) Clock might be (too?) assymetric: HI-phase has no well-defined timing (no delay). But MAY be OK.

The assymetric clocking might lead to the clock latch getting set HIGH too early, which would affect only the last bit. Just an idea.
Reply
18-12-2013, 07:49 PM,
#4
RE: How can I use 74HC595 shift register
I use i2c for DS1307 and 24C64 eeprom. can you help me for software spi library?
Reply
19-12-2013, 11:55 AM,
#5
RE: How can I use 74HC595 shift register
(18-12-2013, 07:49 PM)Akhgari Wrote: I use i2c for DS1307 and 24C64 eeprom. can you help me for software spi library?

I'm not sure these 2 i2c devices can also work with SPI protocol ...
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
19-12-2013, 04:46 PM,
#6
RE: How can I use 74HC595 shift register
Hi

Ach, I ported for P32 the code of DS1306 SPI RTC which is available into standard IDE.
I think which the DS1306 and DS1307 are very similar, may be will be necessary change only the communication protocol.


Bye Bye, Moreno
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)