Pages: [1]   Go Down
Author Topic: 74HC165 and 75HC565  (Read 2107 times)
0 Members and 1 Guest are viewing this topic.
Detroit
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Code:
// 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.35 KB - downloaded 49 times.)

* Serial Output.png (29.52 KB, 411x284 - viewed 46 times.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Assignment, not comparison.
Logged

Detroit
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
    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.
« Last Edit: August 15, 2011, 12:07:58 pm by erich_7719 » Logged

Detroit
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am trying to use as much hardware as possible
Funny most people try to minimize it smiley

Code:
  //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



Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27386
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Detroit
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK...it works a bunch of == instead of = was a big coulperate and the final test code is
Code:
// 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...
Quote
I am trying to use as much hardware as possible
Funny most people try to minimize it smiley

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 smiley-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).




Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Detroit
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Detroit
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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);
    }
Logged

Seattle, WA
Offline Offline
Full Member
***
Karma: 1
Posts: 174
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

(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 smiley-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...

Logged

Pages: [1]   Go Up
Jump to: