Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Automatic PIC Switch

Status
Not open for further replies.

Andraho

New Member
Hello!

I've had some experience in PIC programming, but want to do more complex stuff (take over the world! :D). I took some classes in Boulder, CO with SparkFun's summer school but I wanted to get started in PICs now (they did arduino). It took me more than a couple years to understand everything, when I had time, but now I think things are starting to click. This is intense stuff, and very complex. I've been messing around with LEDs and stuff, which has been very fun, and now I'm working on an automatic PIC switch. I found this tutorial on Gooligum which worked for me!

https://www.electro-tech-online.com/custompdfs/2010/11/PIC_Mid_C_3.pdf

I'm doing a mobile project, which will basically turn on a device (turn on a relay to turn on device). Since I found that tutorial, I've bought the 12F629, because that's what they use. But I want to have the PIC sleeping while it's waiting for the pushbutton switch to be pressed. I'm using Hi Tech compiler:

Code:
#include <htc.h>
#define _XTAL_FREQ 4000000

/***** CONFIGURATION *****/
// ext reset, no code or data protect, no brownout detect,
// no watchdog, power-up timer enabled, 4MHz int clock
__CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);

#define nB_LED 1 		// indicator LED on GP1
#define nBUTTON 2		// switch on GP2

unsigned char sGPIO;	// shadow copy of GPIO

void main()
{
	TRISIO = ~(1<<nB_LED);		// configure LED pin (only) as an output
	IOCB |= 1<<nBUTTON;			// Enable IOC nBUTTON intput
	GPIO = 0;			// start with LED off	
	sGPIO = 0;		// update shadow
	
	GPIE = 1;		// enable port change interrupt
	ei();		// enable global interrupts
	
	while(1)
	{
		GPIO = sGPIO;
		SLEEP();
	}
}

void interrupt isr(void)
{
	GPIO;
	GPIF = 0;
	
	if(GPIO & 1<<nBUTTON)
	{
		sGPIO = 0x02;
		_delay(100000);
		sGPIO = 0x00;
		_delay(100000);
	}
}

That doesn't work (go figure....) so could anyone PLEASE help me? :(:(
 
Oh awesome man, this looks great!!

I'm kind of having problems still though....why isn't the LED test working?? Can I only control the GP port through main?? This makes things hard :( I sorta thought that my ISR would act like a sub-routine thingy?
 
Hi,

It looks like you are trying to do a similar concept; a concept I was just recently working on myself. I read through that tutorial and defined the same identification parameters (LED, BUTTON, etc). Try seeing if the code below works for you; be careful that you set up your hardware to the appropriate pins. I have an LED flash for 4 times just as a test, but you can change that part of the code for your application. I haven’t measured the current consumption to vindicate the fact the PIC is sleeping, but I’ll try to do that soon.

Code:
//
//		IOCSwitchTest2_12F629.C
//

#include <htc.h>
#include <pic12f6x.h>
#define _XTAL_FREQ 4000000

/***** CONFIGURATION *****/
// External reset, no code or data protect, No Brownout Detect,
// No watchdog, Power-Up Timer Enabled, 4MHz Int Clock
__CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);

#define LED GPIO1		// Indicator LED on GP1
#define nLED 1			// LED on pin #1
#define BUTTON GPIO2	// Pushbutton on GP3 (active low)
#define nBUTTON 2

void main()
{
	int i;		// Counter variable
	TRISIO = ~(1<<nLED);		// configure LED pin (only) as output
	IOCB|=1<<nBUTTON;		// Set IOC with GP2
	GPIE = 1;		// Enable GPIO Interrupts

	while(1)
	{		
		__delay_ms(10);
		if(!GPIO2)
		{
			for(i=0;i<4;i++)		// Blink 4 times
			{
				LED = 1;
				_delay(100000);
				LED = 0;
				_delay(100000);
			}
		}
		GPIF = 0;		// Clear GPIO OVF
		SLEEP();		// Sleep until IOC occurs
			
	}
}
 
I measured the consumption current; it averages about 100nA (100E-9) at 4MHz and 5V for the PIC12F629. For my application I'm using 2xAA batteries (~2500mAH) so it should last me approximately 2,854 years if I continuously sleep, that's just an example for you.
 
Last edited:
Is it possible to make this into a time interrupt?

What exactly do you mean by "Time interrupt"? Are you referring to a periodic wake-up and sleep? This is possible with an asynchronous oscillator configured with Timer1, since it is external.
 
Have the PIC sleep for x amount of time (selectable) and then wake to trigger a LED (or something like that) and go back to sleep
 
Have the PIC sleep for x amount of time (selectable) and then wake to trigger a LED (or something like that) and go back to sleep

Yes, that's where you periodically wake-up and sleep again after a certain amount of time. The time will be determined by your external oscillator, when it triggers an interrupt. If you read through the datasheet on sleeping parameters, it should tell you what conditions are required to make it possible, but it is possible.
 
An "Automatic PIC Switch," as the OP implies, is actually a very useful device. You could use it for a wide number of applications, from lighting in your house or anything involved with a means of switching. The fact that the PIC can sleep at such a low consumption current is remarkable, making it very advantageous. You could use a small coin cell battery, solar cell and super-cap, or energy harvesting IC and super-cap to power it, and it could last for a long time. I think many people have wanted a digital switch, where you press it once (ON) and again (OFF); I know I have before. Also, an RTC could be used to turn all the light off at your house at 10 PM or so.

Just to note how useful this device can be!
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top