Dörrkransbelysning

Submitted by patrik on Mon, 2006-12-04 12:02

No idea about the english name of this...
The goal is to light up a decoration with 6 leds. It has to be light, powered by batteries and last as long as possible without recharging. A PIC is used for PWM and to automaticly turn the light on when it gets dark.

Code:

/*Led PWM
PHermansson 2006
For mikroC compiler for PIC, www.mikroelektronika.co.yu
*/

char counter;

void interrupt(){
counter ++; // increment counter
TMR0 = 254;
INTCON = 0x20; // set T0IE, clear T0IF . h20=100000
}

void main() {
OPTION_REG = 0x84; // TMR0 prescaler
TRISIO = 0b00010000; //GP4 in, GP5 out.
GPIO = 0xFF; // initialize gpio
counter = 0; // set counter
TMR0 = 254; // set timer0
INTCON = 0xA0; //TMRO interrupt enabled 80=10000000-> T0IE off
ANSEL=0;

do{ // Loop
if (counter > 200) // Counts from 0 to 255 and then flips to 0
// Counter > x sets pwm period, lower value = brighter led.
{
if ( GPIO.GPIO4 == 1 )
{
GPIO.GPIO5 = 1;
}
else
{
GPIO.GPIO5 = 0;
}
}
else
{
GPIO.GPIO5 = 0;
}
} while(1);

}

Some images: