Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Now working on vimMCP92.ino 12/01/2023 11:10:39 AM. doWritePulse() seems OK with empty zif socket.
- //11/01/2023 12:12:37 PM testZif0 working ok. Next add CE,OE and WE into method.
- //11/01/2023 11:32:35 AM11/01/2023 11:32:35 AM
- //vimMPC91
- //Trying nvim editing...
- //...3 working on getting 4040 to be controlled by pins 0,1 on mcp2
- //....1.ino working now with both mcps, mcp and mcp2,outputting hex ok.Next, vim..2.ino
- //vimMPV1.ino is next vim effort. This will try to talk to TWO MPCs on I2C line.
- //Now doing ...05. Think DIR ok if a bit clumsy.
- //Tidying up ..Uno04
- //Tue Dec 13 11:36:37 NZDT 2022 ...Uno3 worked. Output 0xfa in two nibbles. Still need to tidy up.
- //..mcp...01 worked for outputted nibbles Sat Dec 10 13:12:21 NZDT 2022
- //Works PBWed Dec 7 11:48:39 NZDT 2022
- //416
- #include <Adafruit_MCP23X08.h>
- #include <Adafruit_MCP23X17.h>
- #include <ezLED.h> // ezLED library
- #define LED_PIN 0 // MCP23XXX pin LED is attached to
- #define CLOCK_PIN 0
- #define RESET_PIN 1 //for 4040
- #define CE_PIN 2
- #define OE_PIN 3
- #define WE_PIN 4
- byte byteOut;
- ezLED led(13); // create a LED object that attach to pin 9
- // uncomment appropriate line
- Adafruit_MCP23X08 mcp;
- Adafruit_MCP23X08 mcp2;;
- //Adafruit_MCP23X17 mcp;
- void setup() {
- Serial.begin(9600);
- if (!mcp.begin_I2C()) {
- Serial.println("Error.");
- while (1);
- }
- if (!mcp2.begin_I2C(0x21)) {
- Serial.println("Error 2.");
- while (1);
- }
- //mcp.pinMode(LED_PIN, OUTPUT);
- //mcp.pinMode(1, OUTPUT);2022-12-17
- //17/12/2022 11:54:34 AM
- allGPIOsOutputs();
- allGPIOsOutputs2();
- Serial.println("Reset 4040 then ...Looping...");
- CD4040ResetHi(); //a high on 4040 reset pin will set counter to 0
- CD4040ResetLo(); //low allows counting
- }
- void loop() {
- /* mcp.digitalWrite(LED_PIN, HIGH);
- delay(500);
- mcp.digitalWrite(LED_PIN, LOW);
- delay(500); */
- //uint8_t byte0 = mcp.readGPIO();
- // Serial.println(byte0,HEX);
- //mcp.writeGPIO(0x8b);
- // mcp.writeGPIO(0x6f); //SEnds chars ok to zif socket Tue Jan 3 11:48:39 NZDT 2023
- //mcp2.writeGPIO(0x7a);
- // Serial.println(PINC,HEX);
- /* Serial.println(PIND,HEX); //hi Nib
- Serial.println(PINC,HEX);
- Serial.println(getHiNib(),HEX ) ;
- Serial.println(getLoNib(),HEX ) ;
- // Serial.println(getByte(),HEX);
- doWritePulse(); delay(500);
- led.toggle();
- testBlip();
- initZif(); */
- // testZifWrite0();
- //testWriteThenRead();
- writeAdr0to5();
- readAdr0to5();
- while(1) {
- led.toggle();
- delay(400);
- }
- CEHi();
- OEHi();
- WEHi();
- delay(500);
- CELo();
- OELo();
- WELo();
- delay(500);
- // CD4040ResetHi(); //a high on 4040 reset pin will set counter to 0
- CD4040ClockHi();
- delay(500);
- CD4040ClockLo();
- // CD4040ResetLo(); //a high on 4040 reset pin will set counter to 0
- //Taking out the resets will cause loop to advance count normally .0,1,2,3, ..
- }
- void someDataFromArd() {
- pinMode(7,OUTPUT);
- pinMode(6,OUTPUT);
- pinMode(5,OUTPUT);
- pinMode(4,OUTPUT);
- digitalWrite(7,HIGH);
- digitalWrite(6,LOW);
- digitalWrite(5,HIGH);
- digitalWrite(4,LOW);
- }
- void sendHiNib() { //experiment. Send half a port
- DDRD = DDRD | B11111100; //"safe". Hi nib is all outputs
- PORTD = 0xb5;
- }
- void sendHLoNib() { //experiment. Send half a port
- DDRC = DDRC | B00001111; //"safe". Lo nib is all outputs
- PORTC = 0xc3;
- }
- void sendHiNib2( byte byteOut) { //send the Hi nib of byteH
- DDRD = DDRD | B11111100; //"safe". Hi nib is all outputs
- PORTD = (PORTD & 0x0f) | (byteOut & 0xf0); // byteH; //0xb5;
- }
- void sendLoNib2( byte byteOut) { //send the Lo nib of byteH
- DDRC = DDRC | B00001111;
- PORTC = (PORTC & 0xf0) | (byteOut & 0x0f); //byteH; //0xb5;
- }
- void sendBothNibbles(byte byteOut) { //
- sendHiNib2(byteOut);
- sendLoNib2(byteOut);
- }
- byte getLoNib(){ // Lower part of port C is read
- DDRC = DDRC & B11110000;
- byte loNib = PINC & B00001111;
- return loNib;
- }
- byte getHiNib(){ // Upper part of port D is read
- DDRD = DDRD & B00001111; // B11110000;
- byte hiNib = PIND & B11110000; //B00001111;
- return hiNib;
- }
- byte getByte() { //Get 2 nibbles from Ports C,D and combine
- byte byte1=getHiNib() | getLoNib();
- return byte1;
- }
- void allGPIOsOutputs() { //mcp output only port.NB not the mcp2 port
- // mcp.pinMode(LED_PIN, OUTPUT);
- //mcp.pinMode(1, OUTPUT);
- for (int i = 0;i<8;i++) {
- mcp.pinMode(i,OUTPUT);
- }
- Serial.println("All GPIOs are outputs.");
- }
- void allGPIOsInputs() { //mcp unput only port.
- for (int i = 0;i<8;i++) {
- mcp.pinMode(i,INPUT);
- }
- Serial.println("All GPIOs are inputs.");
- }
- void allGPIOsOutputs2() { //mcp2 output only port.
- for (int i = 0;i<8;i++) {
- mcp2.pinMode(i,OUTPUT);
- }
- Serial.println("All GPIOs in second MCP are outputs.");
- }
- void CD4040ResetHi() { //a high on 4040 reset pin will set counter to 0
- mcp2.digitalWrite(RESET_PIN,HIGH);
- }
- void CD4040ResetLo() { //a Lo on 4040 reset pin will just prepare for next high going reset
- mcp2.digitalWrite(RESET_PIN,LOW);
- }
- void CD4040ClockHi() { //a high on 4040 for clock pin
- mcp2.digitalWrite(CLOCK_PIN,HIGH);
- }
- void CD4040ClockLo() { //a Lo going edge on Clock Pin = 0 of mcp2 with bump counter
- mcp2.digitalWrite(CLOCK_PIN,LOW);
- }
- void reset4040() { // Low going pulse on mcp2's pin 1 sends reset pin on 4040 pin 11
- CD4040ResetHi();
- CD4040ResetLo();
- }
- void blipClock() { ///toggle CLK on 4040 to advance count
- CD4040ClockLo();
- CD4040ClockHi();
- }
- void testBlip() {///send n blips to 4040 after rese.t
- reset4040();
- for(int i = 0;i<0xa5a;i++) {
- blipClock();
- }
- }
- void CEHi() { ///disable 28C64
- mcp2.digitalWrite(CE_PIN,HIGH);
- }
- void CELo() { //enable 28c64 chip
- mcp2.digitalWrite(CE_PIN,LOW);
- }
- void OEHi() { ///disable 28C64 output
- mcp2.digitalWrite(OE_PIN,HIGH);
- }
- void OELo() { //enable 28c64 chip output
- mcp2.digitalWrite(OE_PIN,LOW);
- }
- void WEHi() { ///disable 28C64 writing
- mcp2.digitalWrite(WE_PIN,HIGH);
- }
- void WELo() { //enable 28c64 chip writing
- mcp2.digitalWrite(WE_PIN,LOW);
- }
- void initZif() { ///set up eeprom to be in default initial postion
- CEHi(); OEHi(); WEHi(); //Thats the three controls all off for the start
- reset4040(); //All adr pins now zeros.
- allGPIOsInputs(); //So there's no danger of writing mistakenly
- }
- void testZifWrite0() { ///print 0x02 to Adr 03
- allGPIOsOutputs(); //I/O port now ready
- allGPIOsOutputs2(); //mcp2 controls 4040 adr and OE,CE,WE pins
- blipClock();
- blipClock();
- blipClock(); //4040 adr now 03
- mcp.writeGPIO(0x12); //test value
- }
- void doWritePulse() { //send CE,WE blip for 10ms to write byte to 28C64
- allGPIOsOutputs(); //just to be sure. Assume adr is OK
- allGPIOsOutputs2();
- CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
- CELo(); WELo(); //Now programming
- delay(10); //10ms delay required for 28C64
- WEHi();CEHi(); //Pulse finished
- }
- void testWriteThenRead() { //send arbitrary byte to 28C64 and read and display it
- initZif();
- testZifWrite0(); //setup
- CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
- CELo(); WELo(); //Now programming
- delay(10); //10ms delay required for 28C64
- WEHi();CEHi(); //Pulse finished
- //now read back
- allGPIOsInputs(); //set up I/O port for reading
- CELo(); OELo();
- byte readByte=mcp.readGPIO();
- OEHi(); CEHi();
- Serial.println(readByte);
- }
- void readAdr0to5() { //read in bytes 0,1...5
- allGPIOsInputs();
- allGPIOsOutputs2(); //for 4040 at ctrls like OE*
- CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
- reset4040(); //Adr starts at 0
- CELo(); OELo(); //Start reading
- for(int i =0;i<6;i++) {
- byte readByte=mcp.readGPIO();
- Serial.println(readByte);
- blipClock();
- }
- CEHi();OEHi();
- }
- void writeAdr0to5() { //send 5 bytes to 28C64
- allGPIOsOutputs();
- allGPIOsOutputs2();
- CEHi(); OEHi(); WEHi(); //That's the three controls all off for the start
- reset4040();
- CELo();
- for(int i=0;i<6;i++) {
- byte writeByte = 0x41+i;
- mcp.writeGPIO(writeByte);
- WELo();
- delay(10); //10 ms for write blip
- WEHi();
- blipClock();
- }
- Serial.println("Wrote five bytes.");
- CEHi();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement