Skip to content

Instantly share code, notes, and snippets.

@podstawek
Last active April 2, 2022 11:59

Revisions

  1. podstawek revised this gist Apr 2, 2022. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions mos6522_shiftout.ino
    Original file line number Diff line number Diff line change
    @@ -107,17 +107,6 @@ void setup() {
    digitalWrite(D1, LOW);
    digitalWrite(D0, HIGH);

    digitalWrite(CS1, HIGH);
    digitalWrite(CS2, HIGH);

    // per 6522 datasheet:
    // "selected MCS6522 register will be accessed when CS1 is high and CS2 is low"
    digitalWrite(CS1, LOW);
    digitalWrite(CS2, HIGH);
    // RW switch
    delay(1);
    digitalWrite(RW, LOW);

    }

    void loop() {
  2. podstawek created this gist Apr 2, 2022.
    124 changes: 124 additions & 0 deletions mos6522_shiftout.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    // Define Arduino pin constants to correspond with MOS6522 pins
    const byte CS1 = 24; // Chip Select 1
    const byte CS2 = 23; // Chip Select 2
    const byte RS0 = 38; // Register Select 0
    const byte RS1 = 37; // Register Select 1
    const byte RS2 = 36; // Register Select 2
    const byte RS3 = 35; // Register Select 3
    const byte RW = 22; // Read-Write Line
    const byte D0 = 33; // Data Bus 0
    const byte D1 = 32; // Data Bus 1
    const byte D2 = 31; // Data Bus 2
    const byte D3 = 30; // Data Bus 3
    const byte D4 = 29; // Data Bus 4
    const byte D5 = 28; // Data Bus 5
    const byte D6 = 27; // Data Bus 6
    const byte D7 = 26; // Data Bus 7
    const byte CLOCKOUT = 11; // Mega 2560 clock goes to
    const byte ENCDEC = 53;

    void setup() {
    // set all digital ports to output mode
    int inMin = 11; // Lowest input pin
    int inMax = 54; // Highest input pin
    for (int i = inMin; i <= inMax; i++)
    {
    pinMode(i, OUTPUT);
    }
    {
    // set up timer on CLOCKOUT pin
    pinMode (CLOCKOUT, OUTPUT);
    TCCR1A = bit (COM1A0);
    TCCR1B = bit (WGM12) | bit (CS10);
    OCR1A = 7;
    }

    digitalWrite(ENCDEC, LOW);

    // per 6522 datasheet:
    // "selected MCS6522 register will be accessed when CS1 is high and CS2 is low"
    digitalWrite(CS1, HIGH);
    digitalWrite(CS2, LOW);
    // RW switch
    delay(1);
    digitalWrite(RW, LOW);

    // first set Register Select to select ACR (1011):
    delay(1);
    digitalWrite(RS3, HIGH);
    digitalWrite(RS2, LOW);
    digitalWrite(RS1, HIGH);
    digitalWrite(RS0, HIGH);

    // then set the ACR to enable Shift Out
    // Start by zeroing the 3 relevant ACR bits (000):
    delay(1);
    digitalWrite(D7, LOW);
    digitalWrite(D6, LOW);
    digitalWrite(D5, LOW);
    digitalWrite(D4, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D2, LOW);
    digitalWrite(D1, LOW);
    digitalWrite(D0, LOW);

    // then set to Shift Out, free-running mode shift rate controlled by T2 (100):
    delay(1);
    digitalWrite(D7, LOW);
    digitalWrite(D6, LOW);
    digitalWrite(D5, LOW);
    digitalWrite(D4, HIGH);
    digitalWrite(D3, LOW);
    digitalWrite(D2, HIGH);
    digitalWrite(D2, LOW);
    digitalWrite(D1, LOW);
    digitalWrite(D0, LOW);

    // now set Register Select to select T2C-L timer (1000):
    delay(1);
    digitalWrite(RS3, HIGH);
    digitalWrite(RS2, LOW);
    digitalWrite(RS1, LOW);
    digitalWrite(RS0, LOW);

    // set T2C-L timer to a value, say A0 (00100000)
    digitalWrite(D7, LOW);
    digitalWrite(D6, HIGH);
    digitalWrite(D5, HIGH);
    digitalWrite(D4, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D2, LOW);
    digitalWrite(D1, LOW);
    digitalWrite(D0, LOW);

    // now set Register Select to select Shift Register (1010):
    digitalWrite(RS3, HIGH);
    digitalWrite(RS2, LOW);
    digitalWrite(RS1, HIGH);
    digitalWrite(RS0, LOW);

    // send a pattern to Shift Register (e.g. 00011000)
    digitalWrite(D7, LOW);
    digitalWrite(D6, HIGH);
    digitalWrite(D5, HIGH);
    digitalWrite(D4, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D2, HIGH);
    digitalWrite(D1, LOW);
    digitalWrite(D0, HIGH);

    digitalWrite(CS1, HIGH);
    digitalWrite(CS2, HIGH);

    // per 6522 datasheet:
    // "selected MCS6522 register will be accessed when CS1 is high and CS2 is low"
    digitalWrite(CS1, LOW);
    digitalWrite(CS2, HIGH);
    // RW switch
    delay(1);
    digitalWrite(RW, LOW);

    }

    void loop() {
    }