Thomas Brandon wrote:
I am looking at a project involving 4 MIDI interfaces (both in & out).
I cannot resist...
/*--------------------------cut here  ------------------------------*/
/*
    MIDI SERIAL TO PARALLEL
    Jose L P Souto - souto@cryogen.com
    Compiler: CC5xFree v3.0D from www.bknd.com
    MCU: PIC16C505 @ 4Mhz Internal RC

        .-------  ------.
        |    &nbs p;  \/      |
Vcc     . +5V       GND .   Gnd
        |    &nbs p;          |
OVRUN   < RB5       RB0 >   OUT0
        |    &nbs p;          |
ACK     > RB4       RB1 >   OUT1
        |    &nbs p;          |
MIDI_IN > RB3       RB2 >   READY
        |    &nbs p;          |
OUT7    < RC5       RC0 >   OUT2
        |    &nbs p;          |
OUT6    < RC4       RC1 >   OUT3
        |    &nbs p;          |
OUT5    < RC3       RC2 >   OUT4
        |    &nbs p;          |
        .---------------.

MIDI_IN - input (needs a pull-up)
ACK     - input
READY   - output
OVRUN   - output
OUT0    - output
OUT1    - output
OUT2    - output
OUT3    - output
OUT4    - output
OUT5    - output
OUT6    - output
OUT7    - output

No Watch_dog
*/
#include <16c505.h>
bit ready   @   PORTB.2;
bit midi_in @   PORTB.3;
bit ack     @   PORTB.4;
bit overun  @   PORTB.5;
unsigned char x,rxBuf;
/*---------------------------------------------------------------------- ----*/
void Delay (unsigned char timeout)
/*---------------------------------------------------------------------- ----*/
{    // delay = 3*timeout + 7uS (including call and return)
    while(1)
    {
        timeout--;
        if(timeout==0)
        {
            nop();
            nop();
            return;
        }
    }
}
/*---------------------------------------------------------------------- ----*/
void main (void)
/*---------------------------------------------------------------------- ----*/
{
    OSCCAL = W;                      // OTP part
//  OSCCAL = 0x80;        & nbsp;         // change for windowed part
    PORTB  = 0;
    TRISB  = 0x18;
    PORTC  = 0;
    TRISC  = 0x00;
    OPTION = 0xC3;      &nb sp;           // <- check it for your appl
    while (1)
    {
        while(midi_in)
            if(ack)
                 ready=0;
        Delay(2);   &n bsp;            &nbs p;  // 2*3+7=13us
        if(midi_in)
            continue;
        rxBuf=0x80;
        if(ack)
            ready=0;
        nop();
        do {
            if(ack)
                 ready=0;
            nop();
            Delay(5);             ;   // 5*3+7=22us
            Carry=midi_in;          // sample bit
            rxBuf=rr(rxBuf);        // store and count
        } while(!Carry);
        Delay(5);
        /* no stop-bit check */
        if(!ready)
        {
            x=rxBuf&0x03;
            W=PORTB&0x7C;
            PORTB=W|x;
            x=rr(rxBuf);
            PORTC=rr(x);
            overun=0;
            ready=1;
        }
        else
        {
            ready=0;
            overun=1;
            if(ack)
                 while(ack);
            while(!ack);
            overun=0;
        }
    }
}