74HC165 and 75HC565

I hope I put this under the right topic.

I am automating a frog tank and am wanting to use 2 74HC165's for the sensor input and 2 74HC565 for the motor and light control. So what I have is this...

// inslude the SPI library:
#include <SPI.h>
/* digital pin 04 (SD Card SS pin)
 * digital pin 08 (Input SS pin) 74HC165
 * digital pin 09 (Output SS pin) 74HC595
 * digital pin 10 (Ethernet SS pin)
 * digital pin 11 (MOSI pin) 74HC595 Pin14
 * digital pin 12 (MISO pin) 74HC165 Pin9 (Q7)
 * digital pin 13 (SCK pin)
 */
//                                       __
const int InPutCE = 7; // 74HC165 Pin15 (CE)
//                                          __
const int InPutSelect = 8; // 74HC165 Pin1 (PL)
const int OutPutSelect = 9;// 74HC595 Pin12
unsigned int DataInput;
unsigned int OldDataInput;
int a = 0;
int i = 0;

void setup()
{
pinMode (OutPutSelect, OUTPUT);
pinMode (InPutSelect, OUTPUT);
pinMode (InPutCE, OUTPUT);
digitalWrite(InPutCE,HIGH);
Serial.begin(9600);
SPI.begin();
}

void loop()
{
  int delayTime = 50;

  //The i in the if statement is used to show a steady output to the 595 and reset when it reaches 0xFFFF
  if (i==65536){ //65536 = 2 bytes
    i==0;
  }
  else{
    i=i+1;
  }
  
  
// This Do section is a "Test Script" for getting the 165 to talk to the 595 VIA hardware SPI.
    do
    {
     digitalWrite(InPutSelect,LOW);//Load inputs into the register
     delay(10);
     digitalWrite(InPutSelect,HIGH);//Causes data to transfer serially at the DS with each clock pulse
     delay(10);
     digitalWrite(InPutCE,LOW);//enables the clock
     delay(10);
    // Transfer MSByte than LSByte
     DataInput = SPI.transfer(0x00);//transfers the MSB
     DataInput = (DataInput<<8)&SPI.transfer(0x00);//moves MSB to left byte than transfers the LSB to the right byte
     digitalWrite(InPutCE,HIGH);//Disables the clock
      Serial.print("Datainput = ");
      Serial.println(DataInput,BIN);
      Serial.print("OldDataInput = ");
      Serial.println(OldDataInput,BIN);
     delay(delayTime);
  //displays the swith status if new and old data are different
     if (DataInput != OldDataInput)
       {
        digitalWrite(OutPutSelect,LOW);
        // Transfer MSByte than LSByte
        SPI.transfer((DataInput>>8)&B11111111);
        SPI.transfer(DataInput&B11111111); //When testing Change "i" for "DataInput"
        // take the SS pin high to de-select the chip:
        digitalWrite(OutPutSelect,HIGH);
        delay(10000);
        a = a + 1;
        OldDataInput == DataInput;//makes the Data the new new "old data"
       }
     else
       {
         a = 0;
       }
    } while (a != 0); //basicaly continues untill the new data equals the old data.
    
    //Continues the original if statment for displaying the value of i in binary with leds...This part works with out a problem.
   digitalWrite(OutPutSelect,LOW);
   SPI.transfer((i >> 8) & B11111111);
   SPI.transfer(i & B11111111);
   digitalWrite(OutPutSelect,HIGH);
   delay(delayTime);
  }

I do not have an "O"scope so I put an LED and 330ohm between Q7 and ground and it is flashing indicating the transfer of data, but the data is not being saved to the variable.
I am trying to use as much hardware as possible and all I have found here has been software solutions to this.

The Attached PDF is the hardware schematic.

IO Test VIV.pdf (51.4 KB)

Serial Output.png

Nice work - good looking schematic!

OldDataInput == DataInput;//makes the Data the new new "old data"

Maybe try just 1 = here, for an assignment vs a comparison?

if (i==65536){ //65536 = 2 bytes
    i==0;
  }

Shouldn't that second '==' be a '='?

if (i==65536){ //65536 = 2 bytes
    i=0;
  }

Assignment, not comparison.

CrossRoads:
Nice work - good looking schematic!

OldDataInput == DataInput;//makes the Data the new new "old data"

Maybe try just 1 = here, for an assignment vs a comparison?

I'm fairly new to C and I believe that my line would work for my porpose, and I'm not quite sure what you mean by 1=here.

The issue I believe is here

     digitalWrite(InPutSelect,LOW);//Load inputs into the register
     delay(10);
     digitalWrite(InPutSelect,HIGH);//Causes data to transfer serially at the DS with each clock pulse
     delay(10);
     digitalWrite(InPutCE,LOW);//enables the clock
     delay(10);
    // Transfer MSByte than LSByte
     DataInput = SPI.transfer(0x00);//transfers the MSB
     DataInput = (DataInput<<8)&SPI.transfer(0x00);//moves MSB to left byte than transfers the LSB to the right byte
     digitalWrite(InPutCE,HIGH);//Disables the clock
      Serial.print("Datainput = ");
      Serial.println(DataInput,BIN);
      Serial.print("OldDataInput = ");
      Serial.println(OldDataInput,BIN);
     delay(delayTime);

DataInput is not being populated even when the switches are being toggled to lets say 0011100100000111. DataInput still reads 0x0000.

CrossRoads... I understand now. duh "i" will equal not does it equal.

I am trying to use as much hardware as possible

Funny most people try to minimize it :slight_smile:

  //The i in the if statement is used to show a steady output to the 595 and reset when it reaches 0xFFFF
  if (i==65536){ //65536 = 2 bytes
    i==0;
  }
  else{
    i=i+1;
  }

i is an int, its range is from -32768 to 32767 It will never be 65536, even an unsigned int will never reach that. So the else part is executed allways, resulting in negative valuies for i when overfloow occurs. From the code I assume you want to avoid that.

Easiest solution is to make i an unsigned int and you can do just

i = i+ 1; // or i++; in short

It will automatically start again with zero when overflow occurs

unsigned int 0xFFFF = 65535, so reset there if you need to, or let it roll to 0 on the next count.

same thing here with = vs ==
i==0

That still catches me too on occasion.

Try to learn youself to write the constant first

if (5 = i) gives a compile error where if (i=5) wont

if (5==i) will not give an error too

catches some of the glitches :wink:

OK...it works a bunch of == instead of = was a big coulperate and the final test code is

// inslude the SPI library:
#include <SPI.h>
/* digital pin 04 (SD Card SS pin)
 * digital pin 08 (Input SS pin) 74HC165
 * digital pin 09 (Output SS pin) 74HC595
 * digital pin 10 (Ethernet SS pin)
 * digital pin 11 (MOSI pin) 74HC595 Pin14
 * digital pin 12 (MISO pin) 74HC165 Pin9 (Q7)
 * digital pin 13 (SCK pin)
 */
//                                       __
const int InPutCE = 7; // 74HC165 Pin15 (CE)
//                                          __
const int InPutSelect = 8; // 74HC165 Pin1 (PL)
const int OutPutSelect = 9;// 74HC595 Pin12
unsigned int DataInput;
unsigned int OldDataInput;
int a = 0;
int i = 0;

void setup()
{
pinMode (OutPutSelect, OUTPUT);
pinMode (InPutSelect, OUTPUT);
pinMode (InPutCE, OUTPUT);
digitalWrite(InPutCE,HIGH);

SPI.begin();
}

void loop()
{
  int delayTime = 50;

    i=i+1;

    do
    {
     digitalWrite(InPutSelect,LOW);//Load inputs into the register
     digitalWrite(InPutSelect,HIGH);//Causes data to transfer serially at the DS with each clock pulse
     digitalWrite(InPutCE,LOW);//enables the clock
    // Transfer MSByte than LSByte
     DataInput = SPI.transfer(0x00);//transfers the MSB
     DataInput = (DataInput<<8)+SPI.transfer(0x00) ;//moves MSB to left byte than transfers the LSB to the right byte
     digitalWrite(InPutCE,HIGH);//Disables the clock
     delay(delayTime);
  //displays the swith status if new and old data are different
     if (DataInput != OldDataInput)
       {
        digitalWrite(OutPutSelect,LOW);
        // Transfer MSByte than LSByte
        SPI.transfer((DataInput>>8)&B11111111);
        SPI.transfer(DataInput&B11111111);
        // take the SS pin high to de-select the chip:
        digitalWrite(OutPutSelect,HIGH);
        delay(3000);
        a = a + 1;
        OldDataInput = DataInput;//makes the Data the new new "old data"
        }
     else
       {
         a = 0;
       }
    } while (a != 0); //basicaly continues untill the new data equals the old data.
    
    //Continues the original if statment for displaying the value of i in binary with leds...This part works with out a problem.
   digitalWrite(OutPutSelect,LOW);
   SPI.transfer((i >> 8) & B11111111);
   SPI.transfer(i & B11111111);
   digitalWrite(OutPutSelect,HIGH);
   delay(delayTime);
  }

On a side note...

robtillaart:

I am trying to use as much hardware as possible

Funny most people try to minimize it :slight_smile:

If hardware is faster and takes up less memory space? than, why would most people minimize it?

On a side note...
Quote from: robtillaart on Today at 05:35:44 PM
Quote
I am trying to use as much hardware as possible
Funny most people try to minimize it

If hardware is faster and takes up less memory space? than, why would most people minimize it?

(based upon my project experiences)

  • When software is fast enough and can do the job in time why use (additional) hardware?
  • Additional hardware adds to the costs, additional software too but to a lesser extent
  • Hardware is not as flexible as software (OK there are very smart reconfigurable FPGA's)
  • Hardware needs physical space, soldering and adds weight...
  • Memory in which software resides is of course also hardware :wink:

So, in the end the choice between HW and SW often depends upon the requirements of the project. Cost driven projects tend to SW , performance driven project tend to hardware (rule of thumb).

Cool...

My final project will consist of

14 optical water sensors
2 sets of lights (SSR for one and a transistor for the other)
5 pumps (SSR's for these)
4 fans (transistor)
1 temperature/humidity sensor
2 solenoids (SSR's for these)
these give me 14 inputs and 14 outputs so that leaves a small room for improvement/add ons

1 Flow sensor
1 LDR Circuit

along with Data Logging over the internet.

Hoping to put it all on the Arduino Ethernet board with only the transistors, shift registers, sensors and SSR's as added hardware.

Final thought,
Thank you all for helping. I will most likely be back a little further into the project.

hese give me 14 inputs and 14 outputs so that leaves a small room for improvement/add ons

These can't be replaced by software (I think) only by multiplexing hardware :wink:

sorry, what I meant by hardware was using the SPI library to use the built in SPI function of the Atmega verses using 4 other pins and using software like

BYTES_VAL_T read_shift_regs()
{
    byte bitVal;
    BYTES_VAL_T bytesVal = 0;

    /* Trigger a parallel Load to latch the state of the data lines,
    */
    digitalWrite(clockEnablePin, HIGH);
    digitalWrite(ploadPin, LOW);
    delayMicroseconds(PULSE_WIDTH_USEC);
    digitalWrite(ploadPin, HIGH);
    digitalWrite(clockEnablePin, LOW);

    /* Loop to read each bit value from the serial out line
     * of the SN74HC165N.
    */
    for(int i = 0; i < DATA_WIDTH; i++)
    {
        bitVal = digitalRead(dataPin);

        /* Set the corresponding bit in bytesVal.
        */
        bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

        /* Pulse the Clock (rising edge shifts the next bit).
        */
        digitalWrite(clockPin, HIGH);
        delayMicroseconds(PULSE_WIDTH_USEC);
        digitalWrite(clockPin, LOW);
    }

robtillaart:
(based upon my project experiences)

  • When software is fast enough and can do the job in time why use (additional) hardware?
  • Additional hardware adds to the costs, additional software too but to a lesser extent
  • Hardware is not as flexible as software (OK there are very smart reconfigurable FPGA's)
  • Hardware needs physical space, soldering and adds weight...
  • Memory in which software resides is of course also hardware :wink:

So, in the end the choice between HW and SW often depends upon the requirements of the project. Cost driven projects tend to SW , performance driven project tend to hardware (rule of thumb).

A couple of counter points:

  • Using hardware devices, especially well-established ICs (eg. 7400 series) provides the benefit of (quite literally) decades of real-world application and testing, massive amounts of research and development, millions of real-world installations, and so on.
  • Software solutions often consume large amounts of precious memory (for example, see 1-Wire)
  • Software is expensive to develop and maintain. Time is a valuable commodity. Point to consider: Can you really develop a custom solution in software for less than the comparable hardware solution?

Just food for thought...