You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.3 KiB

4 years ago
  1. //Tut from sparkfun, edited for multiple shift ins. This one works
  2. //see 74165 data sheets for more details on how it works
  3. /* IO Definitions ProCAT Flash*/
  4. //#define DATA_IN 4
  5. //#define CLK 5
  6. //#define LATCH_EN 6
  7. // HARDWARE CONNECTIONS
  8. // Connect the following pins between your Arduino and the 74HC165 Breakout Board
  9. // Connect pins A-H to 5V or GND or switches or whatever
  10. const int data_pin = 4; // Connect Pin 11 to SER_OUT (serial data out)
  11. const int clk_pin = 5; // Connect Pin 12 to CLK (the clock that times the shifting)
  12. const int shld_pin = 6; // Connect Pin 8 to SH/!LD (shift or active low load)
  13. const int ce_pin = 9; // Connect Pin 9 to !CE (clock enable, active low)
  14. byte incoming; // Variable to store the 8 values loaded from the shift register
  15. byte incoming2; // Variable to store the 8 values loaded from the shift register
  16. byte incoming3; // Variable to store the 8 values loaded from the shift register
  17. // The part that runs once
  18. void setup()
  19. {
  20. // Initialize serial to gain the power to obtain relevant information, 9600 baud
  21. Serial.begin(9600);
  22. // Initialize each digital pin to either output or input
  23. // We are commanding the shift register with each pin with the exception of the serial
  24. // data we get back on the data_pin line.
  25. pinMode(shld_pin, OUTPUT);
  26. pinMode(ce_pin, OUTPUT);
  27. pinMode(clk_pin, OUTPUT);
  28. pinMode(data_pin, INPUT);
  29. // Required initial states of these two pins according to the datasheet timing diagram
  30. digitalWrite(clk_pin, HIGH);
  31. digitalWrite(shld_pin, HIGH);
  32. }
  33. // The part that runs to infinity and beyond
  34. void loop() {
  35. incoming = read_shift_regs(); // Read the shift register, it likes that
  36. // Print out the values being read from the shift register
  37. Serial.println("\nThe incoming values of the shift register are: ");
  38. Serial.print("ABCDEFGH : ");
  39. print_byte(incoming); // Print every 1 and 0 that correlates with A through H
  40. //Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros
  41. delay(2000); // Wait for some arbitrary amount of time
  42. }
  43. // This code is intended to trigger the shift register to grab values from it's A-H inputs
  44. byte read_shift_regs()
  45. {
  46. byte the_shifted = 0; // An 8 bit number to carry each bit value of A-H
  47. // Trigger loading the state of the A-H data lines into the shift register
  48. digitalWrite(shld_pin, LOW);
  49. delayMicroseconds(5); // Requires a delay here according to the datasheet timing diagram
  50. digitalWrite(shld_pin, HIGH);
  51. delayMicroseconds(5);
  52. // Required initial states of these two pins according to the datasheet timing diagram
  53. pinMode(clk_pin, OUTPUT);
  54. pinMode(data_pin, INPUT);
  55. digitalWrite(clk_pin, HIGH);
  56. digitalWrite(ce_pin, LOW); // Enable the clock
  57. // Get the A-H values
  58. result1 = shiftIn(data_pin, clk_pin, MSBFIRST);
  59. result2 = shiftIn(data_pin, clk_pin, MSBFIRST);
  60. result3 = shiftIn(data_pin, clk_pin, MSBFIRST);
  61. digitalWrite(ce_pin, HIGH); // Disable the clock
  62. //return the_shifted;
  63. }
  64. // A function that prints all the 1's and 0's of a byte, so 8 bits +or- 2
  65. void print_byte(byte val)
  66. {
  67. byte i;
  68. for(byte i=0; i<=7; i++)
  69. {
  70. Serial.print(val >> i & 1, BIN); // Magic bit shift, if you care look up the <<, >>, and & operators
  71. }
  72. Serial.print("\n"); // Go to the next line, do not collect $200
  73. }