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.

Sleep, wakeup, turn on LED..?

Status
Not open for further replies.

MrNobody

New Member
Hi..
I'm having a tiny bit of problem here.. What I'm tryhing to do is just basic stuff but I couldn't understand why it isn't working.. I'm using PIC12F615.

What I plan to do is these:
1) when GP2 is HIGH, go into sleep mode.
2) when GP4 is pressed, wakeup from sleep (interrupt-on-change) and turn LED at GP1 HIGH/LOW

However, what I found is these:
1) It did not go into SLEEP mode when GP2 is HIGH. Infact, it didn't even recognize that GP2 is HIGH.

2) It also did not recognize that GP4 is HIGH and because of that, GP1 is always LOW.

Below is the code.. I simulate using MPLAB SIM. Did I overlook something..? Please help.. Thanks..

Code:
#include <pic.h>

__CONFIG(INTIO & OSC_8MHZ & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BOREN);

void interrupt isr(void)
{
    if (GPIF == 1)
	GPIF = 0;
}

main()
{
    TRISIO4 = 1;
    TRISIO1 = 0;
    TRISIO2 = 1;
    IOC4 = 1;
    GPIE = 1;
    ei();

    while(1)
    {
        if (GP4==1)
	    GP1 = 1;
	else
	    GP1 = 0;
		
	if (GP2==1)
	    SLEEP();
    }
}
 
You need to read the port in the interrupt in order to clear the mismatch condition.

BTW, how will you know if it's sleeping?

Mike.
 
Last edited:
You need to read the port in the interrupt in order to clear the mismatch condition.

BTW, how will you know if it's sleeping?

Mike.

I monitor the Power-Down bit in STATUS register by using watch window. That bit should be LOW when it is in sleep mode but it remains HIGH.

I also put breakpoint and I step through the code. Even when I set GP2 to HIGH by using MPLAB SIM's stimulus, in the watch window, GP2 is still LOW so it never enter to the sleep mode section of the code.

I suspect that GP4 is also experiencing the same problem because GP1 is always LOW eventhough I set GP4 as HIGH.

Below is the disassembly listing if it helps.

Code:
1:                 #include <pic.h>
2:                 
3:                 __CONFIG(INTIO & OSC_8MHZ & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BOREN);
4:                 
5:                 void interrupt isr(void)
6:                 {
7:                 	if (GPIF == 1)
   021    1283     BCF 0x3, 0x5
   022    1C0B     BTFSS 0xb, 0
   023    2825     GOTO 0x25
   024    2826     GOTO 0x26
   025    282C     GOTO 0x2c
8:                 	{
9:                 		while(GPIF)
   026    2828     GOTO 0x28
   028    180B     BTFSC 0xb, 0
   029    282B     GOTO 0x2b
   02A    282C     GOTO 0x2c
   02B    2827     GOTO 0x27
10:                			GPIF = 0;
   027    100B     BCF 0xb, 0
11:                	}
12:                }
   02C    0871     MOVF 0x71, W
13:                
14:                main()
15:                {
16:                	TRISIO4 = 1;
   00B    1683     BSF 0x3, 0x5
   00C    1605     BSF 0x5, 0x4
17:                	TRISIO1 = 0;
   00D    1085     BCF 0x5, 0x1
18:                	TRISIO2 = 1;
   00E    1505     BSF 0x5, 0x2
19:                	IOC4 = 1;
   00F    1616     BSF 0x16, 0x4
20:                	GPIE = 1;
   010    1283     BCF 0x3, 0x5
   011    158B     BSF 0xb, 0x3
21:                	ei();
   012    178B     BSF 0xb, 0x7
22:                    while(1)
   01F    2813     GOTO 0x13
   020    280A     GOTO 0xa
23:                    {
24:                		if (GP4==1)
   013    1E05     BTFSS 0x5, 0x4
   014    2816     GOTO 0x16
   015    2817     GOTO 0x17
   016    2819     GOTO 0x19
25:                			GP1 = 1;
   017    1485     BSF 0x5, 0x1
   018    281A     GOTO 0x1a
26:                		else
27:                			GP1 = 0;
   019    1085     BCF 0x5, 0x1
28:                		
29:                		if (GP2==1)
   01A    1D05     BTFSS 0x5, 0x2
   01B    281D     GOTO 0x1d
   01C    281E     GOTO 0x1e
   01D    281F     GOTO 0x1f
30:                			SLEEP();
31:                    }
   01E    0063     SLEEP
 
I figured out the reason why the IO input failed.. It is because at startup, the IO are set as ANALOG by default. Its ANSEL bit are all HIGH.. After I clear the ANSEL bits, everything is working.. It also managed to go into SLEEP mode and wake up from sleep mode by using interrupt-on-change.

To clear the GPIF flag, i use the code below.. It seems to work but i need confirmation that that is the right way to do it.. Thanks..

Code:
void interrupt isr(void)
{
	if (GPIF == 1)
	{
		GP4 = GP4;
		GPIF = 0;
	}
}
 
Last edited:
Hi, just a quick question wanna ask u guys..
Lets say in my circuit, I have 1 2-channel op-amp comparator. I didn't use the one in PIC because I'm using PWM with it.

Anyway, currently, the purpose of the comparator is to wake up PIC from sleep (using IOC) when the voltage level change from LOW to HIGH. This means that I can't use the PIC's pull-up resistor to minimize current consumption during sleep mode (by tying the input pin to VDD) because using it would mean that the input pin is at HIGH, so when the comparator change from LOW to HIGH, there won't be any voltage level change experienced by PIC pin. This means that no interrupt.

I was wondering, is it really necessary for me to rewire the comparator so that it wakes up the PIC using a HIGH to LOW voltage level change instead of LOW to HIGH? I mean, will the save in power consumption be so significant if I use the pull-up?

Another thing is, lets say I rewire the comparator. So, in my scenario, the comparator's output will be HIGH for most of the time. Would that increase the power consumption compared to when the comparator being LOW most of the time..?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top