Close

Here be code

A project log for Global View

Persistence of vision is awesome

jarrettJarrett 11/04/2017 at 02:330 Comments

If you don't need or want a simple C driver for this chipset, I'd skip this post!

#define OE          LATBbits.LATB5
#define XLAT        LATCbits.LATC2

//12 bits per channel = 1.5 bytes * 24 channels
#define TABLESIZE 36
#define CHANNELS  24

void main(void)
{
    unsigned int i;
    uint8_t mapInc = 0;
    uint8_t blob[TABLESIZE];

    while(1)
    {
        //Zero out buffer
        for(i = 0; i < TABLESIZE; i++) {
            blob[i] = 0x00;
        }
        
        //Colourspace is 0xFFF levels
        //Dividing/multiplying by four to speed up the fade
        for(i = 0; i < 0x1000 / 4; i++) {
            setChannel(blob, mapInc, i * 4);
            LEDMap(blob);
        }
        
        mapInc = (mapInc + 1) % 24;
    }
}

void setChannel(uint8_t *blob, uint8_t channel, uint16_t value) {
    uint8_t lvalue;
    uint8_t rvalue;
    uint8_t newVal;
    uint8_t byteAddr;
    
    if(channel % 2 == 0) {
        byteAddr = (channel * 3) / 2;
        lvalue = (uint8_t)(value & 0xFF);
        rvalue = (uint8_t)(value >> 8);
        
        *(blob + byteAddr) = lvalue;
        newVal = (*(blob + byteAddr + 1)) & 0xF0;
        newVal = rvalue | newVal;
        
        *(blob + byteAddr + 1) = newVal;
    } else {
        byteAddr = (((channel - 1) * 3) / 2) + 1;
        
        lvalue = (uint8_t)(value << 4) & 0xF0;
        rvalue = (uint8_t)(value >> 4);
        
        newVal = (*(blob + byteAddr)) & 0x0F;
        newVal = lvalue | newVal;
        
        *(blob + byteAddr) = newVal;
        *(blob + byteAddr + 1) = rvalue;
    }
}

void LEDMap(uint8_t *blob)
{
    uint8_t data = 0;
    
    XLAT = 0;
    
    for(int i = TABLESIZE - 1; i >= 0; i--) {
        data = *(blob + i);
        SPIWrite(data);
    }
    
    XLAT = 1;
    __delay_ms(1);
    XLAT = 0;
}

I cut out all the PIC initialisation stuff, but the only gotcha there is that the SPI must transmit on the active to idle clock transition.

Discussions