Guest User

Untitled

a guest
Jul 20th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. int aanstaanden = 0;
  2. bool firstLoop = true;
  3. long time = 0;
  4. long prevTime = 0;
  5.  
  6. // pinout:
  7. // D6 through D13 -- data lines
  8. // A0 through A2 -- WE, OE, CE respectively
  9. // D2 up to D6 -- control pins for the shift registers
  10.  
  11. // Main function
  12. void loop()
  13. {
  14. time = millis();
  15. if(firstLoop)
  16. {
  17. write_a_byte(1, 1);
  18. firstLoop = false;
  19. }
  20. else if(time - prevTime > 5000)
  21. {
  22. Serial.print("The following EEPROM value has been found: ");
  23. Serial.print(read_a_byte(1));
  24. prevTime = time;
  25. }
  26.  
  27. }
  28.  
  29.  
  30. uint8_t read_a_byte(uint16_t address) {
  31.  
  32.  
  33. io_input();
  34. turn_on(OE);
  35. turn_off(CE);
  36. turn_on(WE);
  37.  
  38. put_adresses(address);
  39. turn_off(OE);
  40. delayMicroseconds(10);
  41. byte data = read_io();
  42. turn_on(OE);
  43. turn_on(CE);
  44. }
  45.  
  46. void write_a_byte(uint8_t data, uint16_t address) {
  47.  
  48. turn_on(CE);
  49. turn_on(OE);
  50. turn_on(WE);
  51. delay(100);
  52.  
  53. put_adresses(address);
  54. put_io(data);
  55. turn_off(CE);
  56. turn_off(WE);
  57. // Time to latch address
  58. delayMicroseconds(15);
  59. turn_on(WE);
  60. // Time to latch data
  61. delayMicroseconds(15);
  62. turn_on(CE);
  63. delay(1);
  64. // NOTE this bit is commented out in my code, as this will result in an infinite wait since the write function
  65. // isn't working, so it always reads back 255
  66. uint8_t done = 0;
  67. while(done != 1)
  68. {
  69. if(read_a_byte(address) == data)
  70. {
  71. done = 1;
  72. }
  73. else
  74. {
  75. delay(1);
  76. }
  77. }
  78. turn_adresses_off();
  79. }
  80.  
  81.  
  82. void turn_on(uint8_t port)
  83. {
  84. digitalWrite(port, HIGH);
  85. }
  86. void turn_off(uint8_t port)
  87. {
  88. digitalWrite(port, LOW);
  89. }
  90.  
  91.  
  92. void put_adresses(uint16_t address)
  93. {
  94.  
  95. for(uint16_t i = 0; i < 16; i++)
  96. {
  97. if((1 << i) & address)
  98. {
  99. aanstaanden |= (1 << i);
  100. }
  101. else
  102. {
  103. aanstaanden &= ~(1 << i);
  104. }
  105. }
  106. shift_a_byte(aanstaanden);
  107.  
  108.  
  109. }
  110.  
  111. void turn_addresses_off()
  112. {
  113. for(uint16_t i = 0; i < 16; i++)
  114. {
  115. aanstaanden &= ~(1 << i);
  116. }
  117.  
  118. shift_a_byte(aanstaanden);
  119. }
  120.  
  121. void put_io(int val)
  122. {
  123.  
  124. for(int i = 0; i < 8; i++)
  125. {
  126. if((1 << i) & val)
  127. {
  128. // The io-lines start at digital pin 6 and run up to and including digital pin 13
  129. digitalWrite(6 + i, HIGH);
  130. }
  131. else
  132. {
  133. digitalWrite(6 + i, LOW);
  134. }
  135. }
  136.  
  137.  
  138. }
  139.  
  140. byte read_io()
  141. {
  142. byte b = 0;
  143. for(int i = 0; i < 8; i++)
  144. {
  145. if(digitalRead(6 + i) == HIGH)
  146. {
  147. b |= (1 << i);
  148. }
  149. }
  150. return b;
  151. }
  152.  
  153. void io_input()
  154. {
  155.  
  156. pinMode(io_1, INPUT);
  157. pinMode(io_2, INPUT);
  158. pinMode(io_3, INPUT);
  159. pinMode(io_4, INPUT);
  160. pinMode(io_5, INPUT);
  161. pinMode(io_6, INPUT);
  162. pinMode(io_7, INPUT);
  163. pinMode(io_8, INPUT);
  164.  
  165. }
  166.  
  167. // Yes, these pins are all mapped correctly and tested with a volt meter!
  168. void shift_a_byte(int b)
  169. {
  170. digitalWrite(clk, LOW);
  171. digitalWrite(srclk, LOW);
  172. for(int i = 16; i >= 0; i--)
  173. {
  174. if((1 << i) & b)
  175. {
  176. digitalWrite(serial_out, HIGH);
  177. }
  178. else
  179. {
  180. digitalWrite(serial_out, LOW);
  181. }
  182.  
  183. digitalWrite(srclk, HIGH);
  184. digitalWrite(srclk, LOW);
  185. }
  186.  
  187. digitalWrite(clk, HIGH);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment