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.

110 lines
3.5 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. byte results1;
  18. byte results2;
  19. byte results3;
  20. // The part that runs once
  21. void setup()
  22. {
  23. // Initialize serial to gain the power to obtain relevant information, 9600 baud
  24. Serial.begin(9600);
  25. // Initialize each digital pin to either output or input
  26. // We are commanding the shift register with each pin with the exception of the serial
  27. // data we get back on the data_pin line.
  28. pinMode(shld_pin, OUTPUT);
  29. pinMode(ce_pin, OUTPUT);
  30. pinMode(clk_pin, OUTPUT);
  31. pinMode(data_pin, INPUT);
  32. // Required initial states of these two pins according to the datasheet timing diagram
  33. digitalWrite(clk_pin, HIGH);
  34. digitalWrite(shld_pin, HIGH);
  35. }
  36. // The part that runs to infinity and beyond
  37. void loop() {
  38. read_shift_regs(); // Read the shift register, it likes that
  39. // Print out the values being read from the shift register
  40. Serial.println("\nThe incoming values of the shift register are: ");
  41. Serial.print("ABCDEFGH : ");
  42. print_byte(results1); // Print every 1 and 0 that correlates with A through H
  43. //Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros
  44. Serial.print("IJKLMNOP : ");
  45. print_byte(results2);
  46. Serial.print("QRSTUVWX : ");
  47. print_byte(results3);
  48. //Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros
  49. delay(2000); // Wait for some arbitrary amount of time
  50. }
  51. // This code is intended to trigger the shift register to grab values from it's A-H inputs
  52. byte read_shift_regs()
  53. {
  54. byte the_shifted = 0; // An 8 bit number to carry each bit value of A-H
  55. // Trigger loading the state of the A-H data lines into the shift register
  56. digitalWrite(shld_pin, LOW);
  57. delayMicroseconds(5); // Requires a delay here according to the datasheet timing diagram
  58. digitalWrite(shld_pin, HIGH);
  59. delayMicroseconds(5);
  60. // Required initial states of these two pins according to the datasheet timing diagram
  61. pinMode(clk_pin, OUTPUT);
  62. pinMode(data_pin, INPUT);
  63. digitalWrite(clk_pin, HIGH);
  64. digitalWrite(ce_pin, LOW); // Enable the clock
  65. // Get the A-H values
  66. results1 = shiftIn(data_pin, clk_pin, MSBFIRST);
  67. results2 = shiftIn(data_pin, clk_pin, MSBFIRST);
  68. results3 = shiftIn(data_pin, clk_pin, MSBFIRST);
  69. digitalWrite(ce_pin, HIGH); // Disable the clock
  70. //return the_shifted;
  71. }
  72. // A function that prints all the 1's and 0's of a byte, so 8 bits +or- 2
  73. void print_byte(byte val)
  74. {
  75. byte i;
  76. for(byte i=0; i<=7; i++)
  77. {
  78. Serial.print(val >> i & 1, BIN); // Magic bit shift, if you care look up the <<, >>, and & operators
  79. }
  80. Serial.print("\n"); // Go to the next line, do not collect $200
  81. }