• AVR Freaks

Hot!How to wake up from sleep using Wake-up on pin Change?

Author
mplabuser10
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2018/09/04 14:50:10
  • Location: 0
  • Status: offline
2019/04/01 12:00:08 (permalink)
0

How to wake up from sleep using Wake-up on pin Change?

How to make chip wake up on keypress?
 
Our device: pic 16f570
 
I go to sleep like:
    TRISA = 1;
    TRISB = 1;
    TRISC = 1;
 
    CM1CON0bits.C1ON = 0; // disable comparator
    CM2CON0bits.C2ON = 0; // disable comparator
 
RBIE = 1;
      CM1CON0bits.nC1WU = 1;
      CM2CON0bits.nC2WU = 1;
      INTCON1bits.WUR = 1;
      INTIE_REGbits.WUR = 1;
      // go to sleep
      SLEEP();
 
 
I would like the chip to wake up when one of the  RB pins gets pressed (there is a button pad attached to the chip) I want the chip to wake up when one of those buttons gets pressed.
 
 
Now the device just remains asleep
#1

17 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 13:24:10 (permalink)
    0
    Clear the !RBWU bit in the OPTION register.
    Then the RBIF flag in the INTCON0 register gets set on change and the device will wake from sleep.

    That’s how I read the datersheet, anyway.
    See 8.14 Register Definitions — Interrupt Control

    Edit: You May also need to enable the interrupt RBIE: PORTB on Pin Change Interrupt Enable bit in INTCON1.
    If you selected a more recent device, you’d also have control over which pins were enabled for interrupt, and if they did that for positive, negative, or both edges.
    post edited by pcbbc - 2019/04/01 13:32:30
    #2
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 13:44:19 (permalink)
    0
    pcbbc
    Clear the !RBWU bit in the OPTION register.
    Then the RBIF flag in the INTCON0 register gets set on change and the device will wake from sleep.

    That’s how I read the datersheet, anyway.
    See 8.14 Register Definitions — Interrupt Control

    Edit: You May also need to enable the interrupt RBIE: PORTB on Pin Change Interrupt Enable bit in INTCON1.
    If you selected a more recent device, you’d also have control over which pins were enabled for interrupt, and if they did that for positive, negative, or both edges.

    This is how I set Option:
     
    OPTION = 0x95;
     
    i see this is chip header:
     
    #define                                 nRBWU               0x80
     
    I'm not sure what you mean.
     
     
    #3
    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 13:53:18 (permalink)
    0
    That option setting has nRBWU set ON. nRBWU 0x80 means it is in the top bit (bit 7).
    The n prefix, or a ! in front f the name, or a line over the top of RBWU mean it is an inverse logic flag.
    So 1=OFF and 0=ON.

    OPTION = 0x15;

    Assuming of course you have set all the other bits correctly.

    Please do not make vague statements like “I'm not sure what you mean.” Not sure about what? State what exactly you are unsure about.
    Have you not read the datasheet? If you have a problem, post a quote from it and explain your problem. I took the effort to post all the relevent registers and flags. Just saying you don’t understand doesn’t help me clarify.
    #4
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 14:02:04 (permalink)
    0
    pcbbc - I meant I'm not Sure what you mean "Clear the !RBWU bit in the OPTION register." because I'm not that good at understanding how options/bits are packed into a byte and how to clear bits.
     
    Your example is exactly what I wanted. Thanks. I'll try that.
    #5
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 14:15:31 (permalink)
    0
    pcbbc, it's not working.
    In init code I have this:
        OPTION = 0x15;
        
        // 1 - INPUT, 0 - OUTPUT
        TRISA = 0x44;
        TRISB = 0xFF;
        TRISC = 0x7E;
     
     
    To put it to sleep (after idle time of 10 sec) I have this:
       if( idleTimer > 1000 ) {
          idleTimer = 0;
          
          RBIE = 1;
          CM1CON0bits.nC1WU = 1;
          CM2CON0bits.nC2WU = 1;
          INTCON1bits.WUR = 1;
          INTCON1bits.RBIE = 1;
          INTIE_REGbits.WUR = 1;
          
          
          // go to sleep
          SLEEP();
       }
     
     
    But it does'n wake up on the RB pins being pressed.
    #6
    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 14:38:14 (permalink)
    0
    CAUTION:
    Right before entering Sleep, read the input pins. When in Sleep, wake-up occurs when the values at the pins change from the state they were in at the last reading. If a wake-up on change occurs and the pins are not read before re-entering Sleep, a wake-up will occur immediately even if no pins change while in Sleep mode.


    So just before your sleep...
    INTCON0bits.RBIF = 0;
    unsigned char portb_before = PORTB;
    //go to sleep
    SLEEP();


    Edit: Otherwise post your FULL code, and also a circuit diagram so we can see how PORTB is connected.
    post edited by pcbbc - 2019/04/01 14:40:09
    #7
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 14:47:46 (permalink)
    0
    n.b. a nice trick is you can get the same effect as
    unsigned char portb_before = PORTB;

    with just
    PORTB;    // read of PORTB to clear mismatch

    because PORTB is defined as "volatile", the compiler must read it.
     

    Nearly there...
    #8
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 14:55:19 (permalink)
    0
    I tried:
    INTCON0bits.RBIF = 0;
          unsigned char portb_before = PORTB;
     
    and just:
    INTCON0bits.RBIF = 0;
          PORTA;PORTB;PORTC;
     
     
    still not waking up. I'll see what else i can post.
     
    #9
    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 14:55:22 (permalink)
    0
    qhb this is true. However then you don’t have the option of checking later which pins have changed...
    Of course the OP may not be interested, so this approach is valid.
    #10
    1and0
    Access is Denied
    • Total Posts : 9252
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 15:14:23 (permalink)
    0
    The port must be read to end the mismatch condition before the flag can be cleared.

    PORTB;
    INTCON0bits.RBIF = 0;

    #11
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 15:27:08 (permalink)
    0
    1and0
    The port must be read to end the mismatch condition before the flag can be cleared.

     
    PORTB;
     
    INTCON0bits.RBIF = 0;
     



    I tried:
          PORTA;PORTB;PORTC;
          INTCON0bits.RBIF = 0;
     
    and just
          PORTB;
          INTCON0bits.RBIF = 0;
     
     
     
    Init code is:
        OPTION = 0x15;
        
        // 1 - INPUT, 0 - OUTPUT
        TRISA = 0x44;
        TRISB = 0xFF;
        TRISC = 0x7E;
       
        CM1CON0bits.C1ON = 0; // disable comparator
        CM2CON0bits.C2ON = 0; // disable comparator
        

        ANSEL = 0x03;
        

        OPACONbits.OPA1ON = 1; // enable opamp
        OPACONbits.OPA2ON = 1; // enable opamp
        
        ////////// ADC_Initialize:
        ADCON0bits.ADON = 1; // Enable ADC
     
     
     
    Still no wake up.
    #12
    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 15:40:27 (permalink)
    0
    Still no wake up.
    Still no full listing...
    Still no circuit diagram...

    Do you have interrupts enabled (GIE = 1) and an ISR in your code, for example?
    If you do, immmediate after wake from sleep the device will enter the ISR and get stuck there, unless you are testing and resetting INTCON0bits.RBIF in the ISR.
    That may well be totally indistinguishable from remaining in sleep.
    #13
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 15:58:35 (permalink)
    0
    pcbbc
    Still no wake up.
    Still no full listing...
    Still no circuit diagram...

    Do you have interrupts enabled (GIE = 1) and an ISR in your code, for example?
    If you do, immmediate after wake from sleep the device will enter the ISR and get stuck there, unless you are testing and resetting INTCON0bits.RBIF in the ISR.
    That may well be totally indistinguishable from remaining in sleep.

    Isr is onabled calling INTERRUPT_GlobalInterruptEnable(); at init.

    Can't give full code listing sorry. It's client code.
     
    void __interrupt() INTERRUPT_InterruptManager (void)
    {

        if(INTCON0bits.T0IF == 1)
        {
            Timer0Overflow = 1;
            
            INTCON0bits.T0IF = 0;
        }

        
        INTCON0bits.RBIF = 0;
    }
     
    going to sleep:
     
    GIE = 1;
          ;PORTB;
          INTCON0bits.RBIF = 0;

          
          // go to sleep
          SLEEP();
     
    Not waking
     
    #14
    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 16:21:32 (permalink)
    0
    mplabuser10Can't give full code listing sorry. It's client code.
    Then write a minimal test program that just uses PORTB interrupt on change and show us that. No client code involved. Who knows, you may even find your own problem this way?
    #15
    mplabuser10
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2018/09/04 14:50:10
    • Location: 0
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 16:24:14 (permalink)
    0
    Ok pcbbc. I will try and report back.
    #16
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/01 17:12:42 (permalink)
    0
    mplabuser10
    ...
    void __interrupt() INTERRUPT_InterruptManager (void)
    {

       if(INTCON0bits.T0IF == 1)
       {
           Timer0Overflow = 1;
           
           INTCON0bits.T0IF = 0;
       }

       
       INTCON0bits.RBIF = 0;
    }

    As already discussed, you have to read PORTB before you can clear RBIF.
    Your ISR code is not doing that!
     

    Nearly there...
    #17
    Jams100001
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/04/12 13:37:33
    • Location: MCHP Chandler
    • Status: offline
    Re: How to wake up from sleep using Wake-up on pin Change? 2019/04/23 16:19:19 (permalink)
    0
    MPLabuser10,
     
    I know this post was almost a month ago but I noticed something in your code which might be giving you grief. The WUR bit in the INTCON1 register.  You are initializing it to 1 which as I understand it will Reset the part when it gets an interrupt.  So when you get the interrupt from PORTB the part starts at the reset vector and re-initializes the part and then puts it back to sleep. This will explain why the part is still asleep.
    Where if you load the WUR bit with 0 the interrupt will go to the ISR vector. Which function were you trying to accomplish?
    Jamus
    #18
    Jump to:
    © 2019 APG vNext Commercial Version 4.5