Second prototype using the 82C54

The second prototype uses a 74HC595 shift register to adress the datalines of the 82C54. It is now possible to send different divider values to the 82C54 and thus creating different pulse waveform frequencies.

The code:

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

int statusled = 13;

int incomingByte = 0;   // for incoming serial data

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(statusled, OUTPUT);
  digitalWrite(statusled,HIGH);

  // init 8254 control ports 2,3,4
  pinMode(2, OUTPUT); // WR
  digitalWrite(2, HIGH); // disabled
  pinMode(3, OUTPUT); // A0
  pinMode(4, OUTPUT); // A1
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);

  Serial.begin(9600);
  //settozero();
}

void loop() {

  if(Serial.available() > 0)
      {
        incomingByte = Serial.read();
        if (incomingByte == 49) { // 1
          firstrun();
        }
      }

}

void firstrun() {

    // control word sequence
    Serial.println("A0A1 -> 1");
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);

    Serial.println("2 low");
    digitalWrite(2, LOW);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, B01101100);
    digitalWrite(latchPin, HIGH);
    Serial.println("595 loaded");

    Serial.println("2 high");
    digitalWrite(2, HIGH);
    // end control word sequence

    // start data transfer, LSB MSB
    // 18181 for 400hz
    data_transfer( 5, 71);
}

void data_transfer(int LSB, int MSB) {

    Serial.println("A0A1 -> 0");
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);

    Serial.println("2 low");
    digitalWrite(2, LOW);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, LSB);
    digitalWrite(latchPin, HIGH);
    Serial.println("595 LSB");

    Serial.println("2 high");
    digitalWrite(2, HIGH);

    Serial.println("2 low");
    digitalWrite(2, LOW);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, MSB);
    digitalWrite(latchPin, HIGH);
    Serial.println("595 MSB");

    Serial.println("2 high");
    digitalWrite(2, HIGH);
}