View previous topic :: View next topic |
Author |
Message |
aria
Joined: 08 Aug 2016 Posts: 5
|
Pic18f87k22 wakeup |
Posted: Mon Aug 08, 2016 8:04 pm |
|
|
Hi,
How do I start the timer4 interrupt to fire after wakeup via serial input?
This is what I have and timer4 does not fire:
Code: |
disable_interrupts(INT_TIMER4);
clear_interrupt(INT_TIMER4);
setup_timer_4(T4_DIV_BY_4, xxxx, 16);
enable_interrupts(INT_TIMER4);
|
The same lines of code work on startup (before initiating sleep()).
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 08, 2016 11:48 pm |
|
|
Are global interrupts enabled ? The program won't jump to the Timer4
interrupt routine if global interrupts are disabled.
Also, post your sleep() setup code. I want to see if you are putting the
PIC into full sleep or sleep idle mode . |
|
|
aria
Joined: 08 Aug 2016 Posts: 5
|
|
Posted: Tue Aug 09, 2016 4:54 am |
|
|
enabling global interrupts did not help; I used this:
Code: | enable_interrupts(GLOBAL); |
after enabling the timer4.
Here is my sleep steps:
Code: |
shutdown();
clear_interrupt(INT_EXT);
set_usart_int();
kill_wd();
sleep();
delay_cycles(1); // pre-fetched NOP |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 09, 2016 10:16 am |
|
|
Quote: | shutdown();
clear_interrupt(INT_EXT);
set_usart_int();
kill_wd();
sleep();
delay_cycles(1); // pre-fetched NOP
|
You're putting the PIC into full sleep mode. You need to use SLEEP_IDLE
mode. See this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=46111
Also read the thread for an alternate method. |
|
|
aria
Joined: 08 Aug 2016 Posts: 5
|
|
Posted: Tue Aug 09, 2016 1:09 pm |
|
|
Thanks for your response,
I do wake-up from sleep via serial data.
How do I get the timer 4 running again after wake-up from deep sleep? |
|
|
aria
Joined: 08 Aug 2016 Posts: 5
|
|
Posted: Wed Aug 10, 2016 9:25 am |
|
|
Does anyone have an idea of how to start timer 4 after wake-up from a deep sleep?
Thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Aug 10, 2016 9:48 am |
|
|
You really need to post a small, compilable program that shows what you're doing AND your compiler version. If possible, post the listing file as well.
You may have a compiler 'bug' that only showing the code and listing will tell.
Jay |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Wed Aug 10, 2016 11:53 am |
|
|
Just as a point of interest in why this is important:
We have found that for some PIC24 parts, that version 5.049 of the compiler has a bug when generating sleep commands. Later versions fix this bug. It is very important to supply the data requested in the forum posting guidelines. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 10, 2016 2:38 pm |
|
|
Quote: | Does anyone have an idea of how to start timer 4 after wake-up from a deep sleep? |
The following test program will wake-up from sleep and run Timer4
interrupts. Start the program, press a push-button on Pin B0 (see
schematic below) and the LED on pin D0 will start blinking at a 1 Hz rate.
This program was compiled with CCS compiler version 5.061.
I didn't have an 18F87K22, so I used a 46K22 instead.
The INT0 pin defaults to interrupting on the rising edge. I left it at the
default, so the interrupt will actually occur when the pushbutton is pushed
and then released.
Code: | #include <18F46K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS, stream=HW_UART)
#define LED_PIN PIN_D0
//------------------------
// Blink an LED at a 1 Hz rate
#int_timer4
void timer4_isr(void)
{
static int i = 0;
if(i == 30)
{
output_toggle(LED_PIN);
i = 0;
}
i++;
}
//======================================
void main(void)
{
output_low(LED_PIN);
clear_interrupt(INT_EXT);
enable_interrupts(INT_EXT);
sleep(); // Wake-up when pushbutton on Pin B0 is pressed
delay_cycles(1);
disable_interrupts(INT_TIMER4);
clear_interrupt(INT_TIMER4);
setup_timer_4(T4_DIV_BY_4, 255, 16);
enable_interrupts(INT_TIMER4);
enable_interrupts(GLOBAL);
while(TRUE);
} |
Circuit on Pin B0:
Code: | +5v
|
<
> 4.7K
< ___ Switch
To | _|_|_
PIC -----------------o o------
pin |
B0 --- GND
- |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Aug 11, 2016 12:39 am |
|
|
Only 'caveat' to PCM_programmer's code, is that you should disable INT_EXT, after the sleep, before enabling 'INT_GLOBAL'. Otherwise, the code will try to call a non-existent INT_EXT handler.
Key point to understand, is that at the start, 'GLOBAL' is disabled. INT_EXT, can therefore trigger and wake the chip, without a handler ever being called/needed. However the flag for this interrupt is then set, so if you enable the 'GLOBAL' interrupt bit, INT_EXT will attempt to be called. So this interrupt needs to be disabled before this is done....
You could clear the INT_EXT bit, but there would still be the potential to call an INT_EXT handler, if the button triggered again. So better to disable this interrupt after it is used.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 11, 2016 12:48 am |
|
|
Thank you for pointing that out. I missed it but I should have seen it. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Aug 11, 2016 1:30 am |
|
|
You spot my mistakes often enough.
Be interesting to hear whether it works. |
|
|
aria
Joined: 08 Aug 2016 Posts: 5
|
|
Posted: Thu Aug 11, 2016 4:22 pm |
|
|
Many thanks guys; I'll give it a try. |
|
|
|