Advertisement
prjbrook

eeprom programmer0 vimMCP93.ino

Jan 16th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. //Now working on vimMCP92.ino 12/01/2023 11:10:39 AM. doWritePulse() seems OK with empty zif socket.
  2. //11/01/2023 12:12:37 PM testZif0 working ok. Next add CE,OE and WE into method.
  3. //11/01/2023 11:32:35 AM11/01/2023 11:32:35 AM
  4. //vimMPC91
  5. //Trying nvim editing...
  6. //...3 working on getting 4040 to be controlled by pins 0,1 on mcp2
  7. //....1.ino working now with both mcps, mcp and mcp2,outputting hex ok.Next, vim..2.ino
  8. //vimMPV1.ino is next vim effort. This will try to talk to TWO MPCs on I2C line.
  9. //Now doing ...05. Think DIR ok if a bit clumsy.
  10. //Tidying up ..Uno04
  11. //Tue Dec 13 11:36:37 NZDT 2022 ...Uno3 worked. Output 0xfa in two nibbles. Still need to tidy up.
  12. //..mcp...01 worked for outputted nibbles Sat Dec 10 13:12:21 NZDT 2022
  13. //Works PBWed Dec 7 11:48:39 NZDT 2022
  14. //416
  15. #include <Adafruit_MCP23X08.h>
  16. #include <Adafruit_MCP23X17.h>
  17.  
  18. #include <ezLED.h> // ezLED library
  19.  
  20. #define LED_PIN 0 // MCP23XXX pin LED is attached to
  21. #define CLOCK_PIN 0
  22. #define RESET_PIN 1 //for 4040
  23.  
  24. #define CE_PIN 2
  25. #define OE_PIN 3
  26. #define WE_PIN 4
  27.  
  28. byte byteOut;
  29. ezLED led(13); // create a LED object that attach to pin 9
  30. // uncomment appropriate line
  31. Adafruit_MCP23X08 mcp;
  32.  
  33. Adafruit_MCP23X08 mcp2;;
  34. //Adafruit_MCP23X17 mcp;
  35.  
  36. void setup() {
  37. Serial.begin(9600);
  38. if (!mcp.begin_I2C()) {
  39. Serial.println("Error.");
  40. while (1);
  41. }
  42.  
  43. if (!mcp2.begin_I2C(0x21)) {
  44. Serial.println("Error 2.");
  45. while (1);
  46. }
  47. //mcp.pinMode(LED_PIN, OUTPUT);
  48. //mcp.pinMode(1, OUTPUT);2022-12-17
  49. //17/12/2022 11:54:34 AM
  50. allGPIOsOutputs();
  51.  
  52. allGPIOsOutputs2();
  53. Serial.println("Reset 4040 then ...Looping...");
  54. CD4040ResetHi(); //a high on 4040 reset pin will set counter to 0
  55. CD4040ResetLo(); //low allows counting
  56. }
  57.  
  58. void loop() {
  59. /* mcp.digitalWrite(LED_PIN, HIGH);
  60. delay(500);
  61. mcp.digitalWrite(LED_PIN, LOW);
  62. delay(500); */
  63. //uint8_t byte0 = mcp.readGPIO();
  64. // Serial.println(byte0,HEX);
  65. //mcp.writeGPIO(0x8b);
  66. // mcp.writeGPIO(0x6f); //SEnds chars ok to zif socket Tue Jan 3 11:48:39 NZDT 2023
  67. //mcp2.writeGPIO(0x7a);
  68. // Serial.println(PINC,HEX);
  69. /* Serial.println(PIND,HEX); //hi Nib
  70. Serial.println(PINC,HEX);
  71.  
  72. Serial.println(getHiNib(),HEX ) ;
  73. Serial.println(getLoNib(),HEX ) ;
  74. // Serial.println(getByte(),HEX);
  75. doWritePulse(); delay(500);
  76. led.toggle();
  77. testBlip();
  78. initZif(); */
  79. // testZifWrite0();
  80. //testWriteThenRead();
  81. writeAdr0to5();
  82. readAdr0to5();
  83. while(1) {
  84. led.toggle();
  85. delay(400);
  86. }
  87. CEHi();
  88.  
  89. OEHi();
  90. WEHi();
  91. delay(500);
  92. CELo();
  93. OELo();
  94. WELo();
  95. delay(500);
  96. // CD4040ResetHi(); //a high on 4040 reset pin will set counter to 0
  97. CD4040ClockHi();
  98. delay(500);
  99. CD4040ClockLo();
  100. // CD4040ResetLo(); //a high on 4040 reset pin will set counter to 0
  101. //Taking out the resets will cause loop to advance count normally .0,1,2,3, ..
  102.  
  103. }
  104.  
  105. void someDataFromArd() {
  106. pinMode(7,OUTPUT);
  107. pinMode(6,OUTPUT);
  108. pinMode(5,OUTPUT);
  109. pinMode(4,OUTPUT);
  110. digitalWrite(7,HIGH);
  111. digitalWrite(6,LOW);
  112. digitalWrite(5,HIGH);
  113. digitalWrite(4,LOW);
  114. }
  115.  
  116. void sendHiNib() { //experiment. Send half a port
  117. DDRD = DDRD | B11111100; //"safe". Hi nib is all outputs
  118. PORTD = 0xb5;
  119. }
  120. void sendHLoNib() { //experiment. Send half a port
  121. DDRC = DDRC | B00001111; //"safe". Lo nib is all outputs
  122. PORTC = 0xc3;
  123. }
  124. void sendHiNib2( byte byteOut) { //send the Hi nib of byteH
  125. DDRD = DDRD | B11111100; //"safe". Hi nib is all outputs
  126. PORTD = (PORTD & 0x0f) | (byteOut & 0xf0); // byteH; //0xb5;
  127. }
  128. void sendLoNib2( byte byteOut) { //send the Lo nib of byteH
  129. DDRC = DDRC | B00001111;
  130. PORTC = (PORTC & 0xf0) | (byteOut & 0x0f); //byteH; //0xb5;
  131. }
  132. void sendBothNibbles(byte byteOut) { //
  133. sendHiNib2(byteOut);
  134. sendLoNib2(byteOut);
  135. }
  136. byte getLoNib(){ // Lower part of port C is read
  137. DDRC = DDRC & B11110000;
  138. byte loNib = PINC & B00001111;
  139. return loNib;
  140. }
  141. byte getHiNib(){ // Upper part of port D is read
  142. DDRD = DDRD & B00001111; // B11110000;
  143. byte hiNib = PIND & B11110000; //B00001111;
  144. return hiNib;
  145. }
  146. byte getByte() { //Get 2 nibbles from Ports C,D and combine
  147. byte byte1=getHiNib() | getLoNib();
  148. return byte1;
  149. }
  150. void allGPIOsOutputs() { //mcp output only port.NB not the mcp2 port
  151. // mcp.pinMode(LED_PIN, OUTPUT);
  152. //mcp.pinMode(1, OUTPUT);
  153. for (int i = 0;i<8;i++) {
  154. mcp.pinMode(i,OUTPUT);
  155. }
  156. Serial.println("All GPIOs are outputs.");
  157. }
  158. void allGPIOsInputs() { //mcp unput only port.
  159. for (int i = 0;i<8;i++) {
  160. mcp.pinMode(i,INPUT);
  161. }
  162. Serial.println("All GPIOs are inputs.");
  163. }
  164. void allGPIOsOutputs2() { //mcp2 output only port.
  165. for (int i = 0;i<8;i++) {
  166. mcp2.pinMode(i,OUTPUT);
  167. }
  168. Serial.println("All GPIOs in second MCP are outputs.");
  169. }
  170. void CD4040ResetHi() { //a high on 4040 reset pin will set counter to 0
  171. mcp2.digitalWrite(RESET_PIN,HIGH);
  172. }
  173. void CD4040ResetLo() { //a Lo on 4040 reset pin will just prepare for next high going reset
  174. mcp2.digitalWrite(RESET_PIN,LOW);
  175.  
  176. }
  177. void CD4040ClockHi() { //a high on 4040 for clock pin
  178. mcp2.digitalWrite(CLOCK_PIN,HIGH);
  179. }
  180. void CD4040ClockLo() { //a Lo going edge on Clock Pin = 0 of mcp2 with bump counter
  181. mcp2.digitalWrite(CLOCK_PIN,LOW);
  182. }
  183. void reset4040() { // Low going pulse on mcp2's pin 1 sends reset pin on 4040 pin 11
  184. CD4040ResetHi();
  185. CD4040ResetLo();
  186. }
  187. void blipClock() { ///toggle CLK on 4040 to advance count
  188. CD4040ClockLo();
  189. CD4040ClockHi();
  190. }
  191. void testBlip() {///send n blips to 4040 after rese.t
  192. reset4040();
  193. for(int i = 0;i<0xa5a;i++) {
  194. blipClock();
  195. }
  196. }
  197.  
  198. void CEHi() { ///disable 28C64
  199. mcp2.digitalWrite(CE_PIN,HIGH);
  200. }
  201. void CELo() { //enable 28c64 chip
  202. mcp2.digitalWrite(CE_PIN,LOW);
  203. }
  204.  
  205. void OEHi() { ///disable 28C64 output
  206. mcp2.digitalWrite(OE_PIN,HIGH);
  207. }
  208. void OELo() { //enable 28c64 chip output
  209. mcp2.digitalWrite(OE_PIN,LOW);
  210. }
  211.  
  212. void WEHi() { ///disable 28C64 writing
  213. mcp2.digitalWrite(WE_PIN,HIGH);
  214. }
  215. void WELo() { //enable 28c64 chip writing
  216. mcp2.digitalWrite(WE_PIN,LOW);
  217. }
  218. void initZif() { ///set up eeprom to be in default initial postion
  219. CEHi(); OEHi(); WEHi(); //Thats the three controls all off for the start
  220. reset4040(); //All adr pins now zeros.
  221. allGPIOsInputs(); //So there's no danger of writing mistakenly
  222. }
  223. void testZifWrite0() { ///print 0x02 to Adr 03
  224. allGPIOsOutputs(); //I/O port now ready
  225. allGPIOsOutputs2(); //mcp2 controls 4040 adr and OE,CE,WE pins
  226. blipClock();
  227. blipClock();
  228. blipClock(); //4040 adr now 03
  229. mcp.writeGPIO(0x12); //test value
  230. }
  231. void doWritePulse() { //send CE,WE blip for 10ms to write byte to 28C64
  232. allGPIOsOutputs(); //just to be sure. Assume adr is OK
  233. allGPIOsOutputs2();
  234. CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
  235. CELo(); WELo(); //Now programming
  236. delay(10); //10ms delay required for 28C64
  237. WEHi();CEHi(); //Pulse finished
  238. }
  239. void testWriteThenRead() { //send arbitrary byte to 28C64 and read and display it
  240. initZif();
  241. testZifWrite0(); //setup
  242. CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
  243. CELo(); WELo(); //Now programming
  244. delay(10); //10ms delay required for 28C64
  245. WEHi();CEHi(); //Pulse finished
  246. //now read back
  247. allGPIOsInputs(); //set up I/O port for reading
  248. CELo(); OELo();
  249. byte readByte=mcp.readGPIO();
  250. OEHi(); CEHi();
  251. Serial.println(readByte);
  252. }
  253. void readAdr0to5() { //read in bytes 0,1...5
  254. allGPIOsInputs();
  255. allGPIOsOutputs2(); //for 4040 at ctrls like OE*
  256.  
  257. CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
  258. reset4040(); //Adr starts at 0
  259.  
  260. CELo(); OELo(); //Start reading
  261. for(int i =0;i<6;i++) {
  262. byte readByte=mcp.readGPIO();
  263. Serial.println(readByte);
  264. blipClock();
  265. }
  266. CEHi();OEHi();
  267. }
  268. void writeAdr0to5() { //send 5 bytes to 28C64
  269. allGPIOsOutputs();
  270. allGPIOsOutputs2();
  271.  
  272.  
  273. CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
  274. reset4040();
  275. CELo();
  276. for(int i=0;i<6;i++) {
  277. byte writeByte = 0x41+i;
  278. mcp.writeGPIO(writeByte);
  279. WELo();
  280. delay(10); //10 ms for write blip
  281. WEHi();
  282. blipClock();
  283. }
  284. Serial.println("Wrote five bytes.");
  285. CEHi();
  286. }
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement