⚠️ This forum has been restored as a read-only archive so the knowledge shared by the community over many years remains available. New registrations and posting are disabled.

All times are UTC + 8 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Jan 11th, '12, 13:33 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
As promised, I'm gonna document the construction of a simple ON/OFF mains timer using a PIC Micro Controller.
I need to make one for my micro system anyway, so will document everything here.
Gonna start with the code first this time.
I want to keep everything as simple as possible in the hope that people who haven't done this kind of thing before might give it a try, it's really not as hard as it seems.
Any micro controller will do the job, but I like the PIC family, and I have some 16F84's in the component drawers.
They are very cheap, around 30 cents iirc, the relay will probably cost more !

Code:
#include <pic.h>


/* Variable declarations */
unsigned char count;
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char pump_state;

/* Forward declaration of local functions */
void init(void);
void interrupt timer(void);
void reset_timers(void);

/* Device specific configuration bits */
#define PWRTE     0x08                 /* PoWeR Up Timer (Enable low) - (include to disable) */
#define WDTEN      0x3FFF               /* enable watchdog timer */
#define XT         0x3FFD               /* crystal/resonator */

__CONFIG(WDTEN & XT & ~PWRTE);

/* Local defines */
#define PUMP_ON_TIME   2               /* Time in minutes to have the pump ON */
#define PUMP_OFF_TIME  3               /* Time in minutes to have the pump OFF */
#define PUMP_RELAY     RA0             /* Relay is on PORT A bit 0 */
#define ON             0               /* Output logic 0 turns the pump ON */
#define OFF            1               /* Output logic 1 turns the pump OFF */

/* Main function */
void main(void)
{
   init();                             /* Initialise the device */
   
   while (1)
   {
      CLRWDT();
      if (pump_state == ON)
      {
         if (minutes == PUMP_ON_TIME)
         {
            PUMP_RELAY = OFF;
            pump_state = OFF;
            reset_timers();
         }
      }
      if (pump_state == OFF)
      {
         if (minutes == PUMP_OFF_TIME)
         {
            PUMP_RELAY = ON;
            pump_state = ON;
            reset_timers();
         }
      }     
   };
}

/******************************************************************/
/* Device specific Initialisation                                 */
/*****************************************************************/
void init(void)
{
   /* Intialise the hardware */
   OPTION_REG = 0x84;                  /* Enable Port B Internal pull ups and set TMR0 prescaler ro 32 */
   INTCON = 0xA0;                      /* Enable interrupts for TMR0 */
   T0CS = 0;                           /* Set TMR0 to Timer Mode */
   TRISA = 0;                          /* Set Port A as all outputs */
   TRISB = 0;                          /* Set Port B as all outputs */
   PUMP_RELAY = OFF;                   /* Turn the pump OFF */
   
   /* Initialise the software */
   reset_timers();
   pump_state = OFF;                   /* Pre-set the variable that reflects the pump state */
}

/* Reset time variables to 0 function */
void reset_timers(void)
{
   count = 0;
   seconds = 0;
   minutes = 0;
   hours = 0;
}

/* Timer interupt function */
void interrupt timer(void)
{
   T0IF = 0;                           /* Clear T0 interrupt flag */

   if (++count == 125)                 /* 125 counts of 8mS = 1S */
   {
      count = 0;
      if (++seconds == 60)
      {
         seconds = 0;
         if (++minutes == 60)
         {
            minutes = 0;
            hours++;
         }
      }
   }

}


The MPLAB Development environment is available free here, click the link for "MPLAB IDE v8.83"
MPLAB IDE

I have also made the project available here
MPLAB Project File

MPLAB has a simulator, so if you want to have a play with it, you don't even need any hardware to get started.


Top
 Profile  
Reply with quote  
    Advertisement
 
PostPosted: Jan 11th, '12, 14:15 
Site Admin
Site Admin
User avatar

Joined: Mar 12th, '06, 07:56
Posts: 17803
Images: 4
Location: Perth
Gender: Male
Blog: View Blog (1)
Yous guys talk funny in this section of the forum..... :geek:


Top
 Profile Personal album  
Reply with quote  
PostPosted: Jan 11th, '12, 14:26 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
earthbound wrote:
Yous guys talk funny in this section of the forum..... :geek:


:)

It did cross my mind though, that if enough people are interested, BYAP could stock various items, perhaps even full kits for different projects.
The initial outlay would not be much.

One of the problems with making and selling a product is it has to go through all sorts of expensive approvals, selling the bits to allow the customer to make it himself is (well in the UK) completely devoid of those regulations.


Top
 Profile  
Reply with quote  
PostPosted: Jan 11th, '12, 20:42 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
And some hardware.
All I need to do now is mount the relay safely inside an old extension lead and job done.
I decided to add an LED, so the code changed very slightly from the above, but the above would work without an LED.

Attachment:
mains.png
mains.png [ 24.2 KiB | Viewed 5421 times ]


Attachment:
DSC_0005-3.JPG
DSC_0005-3.JPG [ 169.02 KiB | Viewed 5421 times ]


Top
 Profile  
Reply with quote  
PostPosted: Jan 11th, '12, 21:06 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
Forgot to draw the protection diode across the relay.
You should add this with the pointy end to +12V straight across the coil pins.
When the relay switches off, it prevents back EMF which would eventually destroy the transistor.


Top
 Profile  
Reply with quote  
PostPosted: Jan 11th, '12, 22:14 
A posting God
A posting God
User avatar

Joined: Dec 10th, '11, 15:03
Posts: 2089
Gender: Male
Are you human?: What is human?
Location: Perth Hills
Awesome work Dui!

Now you just need one to control the world! (Pinky and the brain theme tune starts)


Top
 Profile  
Reply with quote  
PostPosted: Jan 12th, '12, 01:16 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
rsevs3 wrote:
Now you just need one to control the world! (Pinky and the brain theme tune starts)


I'm working on it, but my stack keeps overflowing ;)


Top
 Profile  
Reply with quote  
PostPosted: Jan 12th, '12, 04:07 
Bordering on Legend
Bordering on Legend
User avatar

Joined: Oct 24th, '10, 17:16
Posts: 338
Gender: Male
Are you human?: Some Time
Location: Chonburi Thailand
PIC16F84 is more expensive & less feature.
Why don't you use PIC16f628?

I have pcb for this packaging, my own design, general purpose.
and space for Transfomer, rectifier, regulator, uln2003 driver, relay on board.
I can send a few for you if you are interested.


Top
 Profile  
Reply with quote  
PostPosted: Jan 12th, '12, 09:32 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
I used the 16F84 because I already had some that I bought last year.
Seems the price has gone up a lot since last year, I paid around 150 Baht for 5, now they're 160 Baht each (from RS Thailand)

Thanks for the offer to send some, but I can pick some up at the electronics shop next time I'm in town :)


Top
 Profile  
Reply with quote  
PostPosted: Jan 12th, '12, 15:08 
Bordering on Legend
Bordering on Legend
User avatar

Joined: Oct 24th, '10, 17:16
Posts: 338
Gender: Male
Are you human?: Some Time
Location: Chonburi Thailand
For IC Let's try http://www.es.co.th

What I offer you is PCB not IC.
This can help you reduce time for tedious wiring.
If you interest PM me your address.

BTW my name is Tikamporn as you see on the board, Thai registered name 55

PCB
Image

Image

Image


Top
 Profile  
Reply with quote  
PostPosted: Jan 12th, '12, 20:14 
Almost divorced
Almost divorced
User avatar

Joined: Nov 11th, '09, 03:13
Posts: 1004
Gender: Male
Are you human?: The top half is
Location: Chiang Mai, NW Thailand.
Thank you Ao+ that is very kind.
I'll PM you my GF's address in town, my house is in the jungle and the post office seem to struggle to find it.
A lot of stuff goes missing if sent to this house, the bills always seem to arrive on time though ;)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 8 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group
Portal by phpBB3 Portal © phpBB Türkiye
[ Time : 0.094s | 15 Queries | GZIP : Off ]