Wednesday, July 30, 2008

LED (OIL) LAMP

This time I am using another PIC, 16F628, which is better than 16F84 in number of ways. It has internal Oscillator, more IO pins, PWM, more space (twice) etc. As my first program with 16F628, I wrote this quick and dirty code segment to simulate an oil lamp. It uses the built in PWM to change the duty cycle of the LED.

Here is the video



Here is the code in MikroC

unsigned short j, oj,i;

void InitMain() {


PORTB = 0x0; // set PORTB to 0
TRISB = 0; // designate PORTBpins as output
PWM_Init(5000); // initialize PWM module
}//~

void main() {
CMCON=7;

initMain();

j = 127; // initial value for j
oj = 0; // oj will keep the 'old j' value
PWM_Start(); // start PWM

while (1) { // endless loop

j=rand(); // Generate Random Number
if (j>=255)
j=254;

if (j==0)
j=1;

if (j>oj)
{
for (i=oj;i<=j;i++)
PWM_Change_Duty(i); //Smoothen the level change
}
else
if (j {
for (i=oj;i>=j;i--)
PWM_Change_Duty(i);
}
else
PWM_Change_Duty(j); // set new duty ratio,

oj = j; // memorize it
PORTB = oj; // and display on PORTB

Vdelay_ms(200+j); // slow down change pace a little
}
}//~!

PIC Clock - Final Version

My PIC clock is almost done. Only thing remaining is that to give it a nice look. (at the moment, it is inside an ice cream box and it deserves a better place than that). It has following functionalities;
• 12H/24H mode; when in 12H mode, a PM indicator is used.
• Battery backup; when the mains power is not available, it automatically gets power from the batteries and keeps the clock ticking.
• Ability to work totally on batteries. i.e. When the mains power is not available, it automatically switches into battery mode and the display goes blank. But if you want to see the time, you can get back the normal display manually.

Here are some pics

24H mode

12H mode

Inside view





(Video quality is not that great but clear enough to see that the clock is running :-) )