Recent site activity

RGB LED Knightrider

posted 16 Jul 2011 08:26 by Chris Shucksmith   [ updated 17 Jul 2011 11:00 ]
A little toy completed in under two hours! 


I soldered some headers on the 44-pin PICKIT Demo Board after realising that 0.1" headers would fit, albeit diagonally. If you have four RGB LEDs, a pile of jump leads and a 56R resister, this little bit of C does a nice multiplexed nightrider.

/*
 * File:   main.c
 * Author: Chris
 */
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                       // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                        // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                    // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF  // CONFIG4L

#include <p18cxxx.h>

void main(void) {
    unsigned char colour[8] = {0, 0, 0, 0, 0, 0, 0, 0};
    unsigned char bulb;
    unsigned char colorbulb;

    int changeDelay = 50000;

    LATB = 0x0F; // hold outputs high
    LATC = 0x00; // hold outputs high
    TRISB = 0b11111000; // PORTB bit 0-2 output (RGB)
    TRISC = 0b11110000; // PORTC bit 0-3 output (Anode 1,2,3,4)

    while (1) {
        if (++bulb == 4) bulb = 0;
        LATC = (LATC & 0xF0); // all annodes off
        LATB = (LATB & 0xF8) | (~(1 << (colour[bulb])) & 0x07); // set RGB, 1 bit low
        LATC = (LATC & 0xF0 | (1 << bulb)); // select 1 annodes high

        if (--changeDelay == 0) {
            changeDelay = 800;
            colour[colorbulb] = (colour[colorbulb] + 1) & 0x03; // 0,1,2,3
            if (++colorbulb == 4) colorbulb = 0;
        }
    }
}

Links:

Č
ċ
ď
main.c
(2k)
Chris Shucksmith,
17 Jul 2011 10:15