Tuesday, March 03, 2009

PIC Relay – 230v

This simple PIC circuit can be used to pre-program the on/off time of 230v electrical appliances. The PIC is programmed as a clock and at specified times, it can turn on/off a relay to control the appliance. I found a broken power guard (which protects electrical equipments from over and under voltage) in my old parts ‘yard’ at home. It was good source for the relay, diods,  step down transformer, voltage regulator, the plastic container and even the LEDs. This relay is having good characteristics since these power guards were produced mainly to protect refrigerators. Only drawback in this design is that it doesn’t have a user interface. If you want to change the on/off time, you have to do it using the program. (I was too lazy to add a display unit for the device) It is being tested with our refridegerator and is working fine. I put the fridge to turn off at midnight for about four hours. 








/*
* Project name:
PIC Relay
* Copyright:
Chandana Inc.
* Description:
PIC Relay - 230v
* Test configuration:
MCU: PIC16F88
Oscillator: XT, 04.0000 MHz
SW: mikroC v8.0
* NOTES:
Time might not be accurate.
*/

unsigned int countdown ; // interrupt counter
unsigned char update ; // update output
unsigned char secs, mins, hrs ; // tally variables

unsigned char ticks, correction,mode;
char oldstate_min, oldstate_hr, PowerOK, Powerchange, OutputStatus;

void interrupt()
{
if (--countdown == 0)
{
countdown = 8000; // 8000 x 125us = 1 second
update = 1; // flag to update clock output
}
PIR1.TMR2IF = 0; // clear interrupt flag

}

void Clock24()
{
if (++secs == 60) // check each tally for rollover
{ secs = 0;
if (++mins == 60)
{ mins = 0;
if (++hrs == 24)
hrs = 0 ;
}
}
update = 0;
}

void Initialize()
{

CMCON=7;
secs = 255;
mins = 23;
hrs = 15;
update = 1;
countdown = 8000;

INTCON.GIE = 1; // enable GIE
INTCON.PEIE = 1; // enable PEIE
T2CON = 0; // prescaler=0; postscaler=0; Timer2=off
TMR2 = 0; // clear timer2
PR2 = 124; // TMR2 resets when matching PR2
PIE1.TMR2IE = 1 ; // interrupt enabled
PIR1.TMR2IF = 0 ; // interrupt flag cleared
T2CON.TMR2ON = 1 ; // start Timer0
}

void main() {
Initialize();

PORTA = 0;
TRISA =0b00010;
PORTB = 0;
TRISB = 0;

oldstate_min=0;
oldstate_hr=0;
mode=0;
PowerOK=0;
Powerchange=0;
OutputStatus=1;

do
{
if (update == 1) // advance seconds?
Clock24(); // yes...then output


if (Button(&PORTA,1,1,1))
{
PowerOK=1;
//mode=0;
}

if (PowerOK && Button(&PORTA,1,1,0))
{
PowerOK=0;
PowerChange=1;
mode=1;
}

if (Powerchange && PowerOK) //power restore
{
Powerchange=0;
mode=0; //back to normal
}

if (mode==0)
{

if (OutputStatus==0) //turn off
{PORTA.F0=1;
PORTA.F2=1;
PORTA.F3=0;}
else
{PORTA.F0=0; //Turn ON
PORTA.F2=0;
PORTA.F3=1;}

}
else
{
PORTA.F0=0;
PORTA.F2=0;
PORTA.F3=0;


}



} while(1); // endless loop
}//~

No comments: