+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Newbie level 4
    Points: 755, Level: 6

    Join Date
    Feb 2015
    Posts
    7
    Helped
    0 / 0
    Points
    755
    Level
    6

    Pic16f88 sleep mode & wake from interrupt

    hello everyone!

    I need your help, im using mikro c and trying to use sleep mode on a pic16f88 mcu and wake from a interrupt on RB0 pin
    what I want to do is a simple project so I can understand how to use sleep mode for example:

    I have a sensor on RB0 pin, when this is '1' then a led will blink, when it goes '0' for more than 3secs then the mcu goes to sleep till RB0 goes '1' again.


    can anyone help me with the code? thank you!

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 68,799, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,040
    Helped
    3214 / 3214
    Points
    68,799
    Level
    64

    Re: Pic16f88 sleep mode & wake from interrupt

    Hi,

    If you want somemone to do jour job, then you should consider to pay for it.

    If you don't want others to do your job, you should show your own effort.
    Read the datasheets about sleep function, go through code examples. (Use / modify the code for your needs)
    Show your flow chart, show your setup, show your code.
    What did you understand so far, and where exactly do you need help?

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisment

        
       

  3. #3
    Super Moderator
    Points: 76,228, Level: 67
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,463
    Helped
    4152 / 4152
    Points
    76,228
    Level
    67

    Re: Pic16f88 sleep mode & wake from interrupt

    Klaus is right of course, we can guide your efforts but not do the work for you.

    The basics are though:
    1. write an ISR so it can recover when the RB0 interrupt triggers.
    2. enable RB0 as an interrupt source.
    3. read RB0 as a normal input pin and check for how long it has been low, if > 3 secs just use the 'sleep' instruction.

    There are many ways to check the pin timing but as you are flashing the LED anyway, I would suggest using a hardware timer and pre-load it to create a 3 second delay every time RB0 is high. Do not pre-load it if RB0 is low so it counts for 3 seconds then overflows. You can poll the timer or use a timer interrupt to tell when the 3 seconds has elapsed and maybe use one of the timer bits to flash the LED as well.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisment

        
       

  4. #4
    Newbie level 4
    Points: 755, Level: 6

    Join Date
    Feb 2015
    Posts
    7
    Helped
    0 / 0
    Points
    755
    Level
    6

    Re: Pic16f88 sleep mode & wake from interrupt

    my code is the one below but I think that mcu never goes to sleep the blinking led is just to undersand if its waken or still sleeping. im doing this project just to understand how to sleep and wake from interrupt

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    int k;
     
    void main() {
    osccon=0x70;                 // set osc
    ansel=0;                     //  set analog
    intcon.inte=1;               // enable RB0/INT
     
    trisb.f0=1;                 //set RB0 as input
    trisb.f3=0;                 //set RB3 as output
     
    for (;;) {
    start:
    while (portb.f0==1){         //  led blink till RB0=0
    portb.f3=0;
    delay_ms(400);
    portb.f3=1;
    delay_ms(400);}
    if (portb.f0==0){
    k=1;
    delay_ms (3000);
    if (portb.f0==0 && k==1){    //check RB0=0 for more than 3secs and then sleep
    intcon.gie=0;             //clear GIE before sleep
    portb.f3=0;
    asm sleep;}
    else{
    k=0;
    goto start;}
    }
    }
    }



  5. #5
    Super Moderator
    Points: 68,799, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,040
    Helped
    3214 / 3214
    Points
    68,799
    Level
    64

    Re: Pic16f88 sleep mode & wake from interrupt

    Hi,

    I assume "blinking" and check for "3 seconds" should be done in the same time.
    This best is done with interrupts, but also can be done in main loop.

    Let's assume blinking should be about: 400ms ON, 400ms OFF,
    And let's assume the shortest "key_down_time" you safely want to detect is about 100ms.

    Btw: it's your job to draw a flow chart...or any other plan for your software. Picture, chart, diagram, text...
    All the professionaks need to do this, too. Even if everyone finds it booring...


    pseudo code:
    Code:
    Init timeout variable 
    Init blink variable
    Init periferals 
    Init periferal that RB0=1 resumes from sleep
    
    Loop:
       Wait 100ms
       Increment blink
       If blink > 3 then (invert LED, blink =0) ; blink 0, 1, 2, 3, 0... every 4 turns (4 × 100ms) it inverts the LED state for blinking
       If RB0= 0 then (increment timeout) else (timeout = 0)
       If timeout > 29 then (sleep) ; 30 × 100ms = 3s
    End loop
    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisment

        
       

  6. #6
    Super Moderator
    Points: 250,892, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,678
    Helped
    13284 / 13284
    Points
    250,892
    Level
    100

    Re: Pic16f88 sleep mode & wake from interrupt

    I read the initial spec so that the input should be continuously monitored. delay_ms (3000) will ignore any input events during 3 sec, so that's probably not what you want.



--[[ ]]--