RGB Led Cubo Natalizio Fai-da-te con il PICmicro

rgb led cubo

Interessante progetto realizzato con il PICmicro. Un cubo formato da led RGB, trasformabile , in questo periodo, in un CUBO NATALIZIO.

Il PIC usato, è un PIC18F2525 e sono disponibili sia il codice sorgente che lo schema elettrico.

Cubo RGB LED


/*
(C) 2008 Justin Rajewski Coilgunpower.com
This code is for use with the 3x3x3 RGB LED
Cube sold by Justin Rajewski. This code may 
not be used without permission. Please 
contact Justin at justin@rajewski.com
if you wish to use this code or any
part of this code for something other then
the 3x3x3 RGB LED Cube.
*/
#include 
#include 
#pragma config OSC = HS, IESO = OFF
#pragma config WDT = OFF, WDTPS = 32768 
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = OFF 
#pragma config STVREN = ON, LVP = OFF, XINST = OFF, DEBUG = OFF
#define setBit(var, bitnum)   (var)|=(1<<(bitnum))    //used for bit control
#define resBit(var, bitnum)   (var)&=~(1<<(bitnum))
#define clearBit(var, bitnum) (var)&=~(1<<(bitnum))
#define testBit(var, bitnum)  (var)&(1<<(bitnum))
unsigned short
red [27]={1,5,30,0,0,0,0,0,0,1,5,30,0,0,0,0,0,0,1,5,30,0,0,0,0,0,0},
green [27]={0,0,0,1,5,30,0,0,0,0,0,0,1,5,30,0,0,0,0,0,0,1,5,30,0,0,0},
blue [27]={0,0,0,0,0,0,1,5,30,0,0,0,0,0,0,1,5,30,0,0,0,0,0,0,1,5,30},
stat=0,col=0,bporta=0,bportb=0,bportc=0;
void main (void);
void cube_interrupt (void);//main interrupt
void InterruptVectorHigh (void);//interrupt high priority vector
void setled (unsigned short start);//sets pins acording to rgb settings
void init (void);
void delay_ms (unsigned int ms);//calibrated delay 
void clear_cube (void);//clears the entire cube

void init (void){
ADCON1=0x0F;
TRISA=0;
TRISB=0;
TRISC=0;
PORTA=0xff;
PORTB=0x00;
PORTC=0x07;
T0CON=0xc1;
INTCON=0xE0;
INTCON2=0x84;
INTCON3=0;

}
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto cube_interrupt //jump to interrupt routine
  _endasm
}

#pragma code
#pragma interrupt cube_interrupt
void cube_interrupt (void){//main interrupt
unsigned short ct=0;
resBit (INTCON,2);//reset interrupt
TMR0L=110;
  ++stat;     //stat is used for color fading
if (stat > 30)
 stat=0;
++col;
if (col>8)col=0;//keep col < 9 as we only have col 0-8
//reset cube port
bporta=0xff;//use buffers so there is less flicker
bportb=0x00;
bportc=0x07;
switch (col){
case 0:
setled(col);
bportc|=8;
break;
case 1:
setled(col);
bportc|=16;
break;
case 2:
setled(col);
bportc|=32;
break;
case 3:
setled(col);
bportb=1;
break;
case 4:
setled(col);
bportb=2;
break;
case 5:
setled(col);
bportb=4;
break;
case 6:
setled(col);
bportb=8;
break;
case 7:
setled(col);
bportb=16;
break;
case 8:
setled(col);
bportb=32;
break;
}
PORTA=0xff;//reset the real pins to 			
PORTB=0x00;//keep the color from showing on the pervious LED
PORTC=0x07;
PORTA=bporta;//update the pins
PORTB=bportb;
PORTC=bportc;
}

void setled (unsigned short start){
unsigned short x=0;
if (red[start*3]>stat){
resBit(bporta,0);
}
if (green[start*3]>stat){
resBit(bporta,1);
}
if (blue[start*3]>stat){
resBit(bporta,2);
}
if (red[1+(start*3)]>stat){
resBit(bporta,3);
}
if (green[1+(start*3)]>stat){
resBit(bporta,4);
}
if (blue[1+(start*3)]>stat){
resBit(bporta,5);
}
if (red[2+(start*3)]>stat){
resBit(bportc,0);
}
if (green[2+(start*3)]>stat){
resBit(bportc,1);
}
if (blue[2+(start*3)]>stat){
resBit(bportc,2);
}
}

void delay_ms (unsigned int ms)
{
unsigned int ct=0;
for (ct=0;ct
                            
                            							
							
							
STAMPA     Tags:, ,

Leave a Reply