View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
Can't wake up from sleep on Timer 1 interrupt |
Posted: Thu May 11, 2017 8:32 am |
|
|
I make a silly program to wake up the PIC16f627 from sleep, the timer OSC is working, I can see the 32768KHz waveform in the oscilloscope, but the PIC refuses to wake up.
Can somebody tell me what I'm doing wrong?
Code: | #include <16F627.h>
#FUSES PROTECT,NOWDT,INTRC_IO,NOBROWNOUT,NOLVP
#use fast_io (ALL)
#define mosfet PIN_A4
void main(void)
{
set_tris_a(0b00100000);
set_tris_b(0b11011111);
SETUP_TIMER_0(T0_INTERNAL|T0_DIV_256|T0_8_BIT);
output_high(mosfet);
clear_interrupt(INT_TIMER1);
set_timer1(0);
setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_1);//Two seconds.
enable_interrupts(INT_TIMER1);
while(1)
{
output_toggle(mosfet);
output_toggle(PIN_A1);
sleep();
clear_interrupt(INT_TIMER1);
}
}
|
Another thing is that RA4 doesn't toggle even with the proper pull up resistor but work ok if I use output_high and output low instruction. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19595
|
|
Posted: Thu May 11, 2017 8:55 am |
|
|
Your chip does not support this....
Devices where he timer can wake from sleep have to keep it's oscillator running. Yours doesn't.
Quote: |
"The device can wake-up from SLEEP through one of"
"the following events:"
"1. External RESET input on MCLR pin"
"2. Watchdog Timer Wake-up (if WDT was enabled)"
"3. Interrupt from RB0/INT pin, RB Port change, or the Peripheral Interrupt (Comparator)."
|
Is timer1 there?....
When a pin is 'toggled', it is the level on the pin that is read, and inverted. If (for instance) the load is such that the pin doesn't reach the voltage seen as Vih, then it won't toggle.
Whatever you have as a pullup on A4, is not getting it high enough, or the circuit attached it preventing it getting high enough. The input on that pin is a Schmitt type, so has to get to 4v (with a 5v supply) to be seen as 'high'.... |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu May 11, 2017 9:50 am |
|
|
I'm looking the figure 14-16 from page 107 and TMR1IF is in the schematic, also the timer 1 is not synchronized and the oscillator keeps running even in sleep mode.
The text that you remark is opposed with that graphic, as far I understand.
Also in the same page that you remark says
If the global interrupts are disabled (GIE is
cleared), but any interrupt source has both
its interrupt enable bit and the corresponding
interrupt flag bits set, the device will
immediately wakeup from sleep _________________ Electric Blue |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9273 Location: Greensville,Ontario
|
|
Posted: Thu May 11, 2017 10:26 am |
|
|
I don't see an 'enable-global command......
but then again I'm hungry... |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu May 11, 2017 11:11 am |
|
|
temtronic wrote: | I don't see an 'enable-global command......
but then again I'm hungry... |
I enabled PEIE but get the same result.
GIE is not needed, as in the datasheet says.
Quote: | If the global interrupts are disabled (GIE is cleared), but any interrupt source has both bits interrupt enable bit and the corresponding interrupt flag bits set, the device will immediately wakeup from sleep |
_________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19595
|
|
Posted: Thu May 11, 2017 11:28 am |
|
|
temtronic wrote: | I don't see an 'enable-global command......
but then again I'm hungry... |
You never need to enable the global interrupt to wake from sleep.
If you do, then you need to have a handler.
The instruction after the sleep should always be a 'nop' (delay_cycles(1)). Otherwise this may not execute correctly (this is 'prefetched' when you go to sleep). However that isn't the problem here.
I agree the data sheet disagrees with itself on this....
I'd say it ought to work provided the timer is running in async mode.
If you look, the logic is reversed. The SYNC bit has to be set to run in ASYNC mode. I wonder if your compiler has it wrong (you don't tell us the version). The current compilers do default to setting this bit.
So:
Code: |
#bit T1SYNC=getenv("BIT:T1SYNC")
SETUP_TIMER_0(T0_INTERNAL|T0_DIV_256|T0_8_BIT);
T1SYNC=TRUE; //set the bit to run asynchronous
output_high(mosfet);
clear_interrupt(INT_TIMER1);
set_timer1(0);
setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_1);//Two seconds.
enable_interrupts(INT_TIMER1);
while(1)
{
output_toggle(mosfet);
output_toggle(PIN_A1);
sleep();
delay_cycles(1);
clear_interrupt(INT_TIMER1);
|
|
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
Re: Can't wake up from sleep on Timer 1 interrupt |
Posted: Thu May 11, 2017 2:04 pm |
|
|
On the wakeup issue. I noticed there was an errata on the timer 1 control register. it isn't related to your sleep issue, but you might want to be safe and dump out the contents of the register to a serial terminal (or out the ICD-U64 if you have one of those) and verify.
E_Blue wrote: |
Another thing is that RA4 doesn't toggle even with the proper pull up resistor but work ok if I use output_high and output low instruction. |
How are you toggling the output for the pull up? it'll need to be something similar to
Code: |
static BOOLEAN laststate = 0;
...
if(lastState){
output_float(PIN_A4);
}else{
output_drive(PIN_A4);
}
lastState = !lastState;
|
Though I never use fast_io, so I don't know if that will interfere at all. You also need to make sure it doesn't toggle too fast relative to the pullup value you are using. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Fri May 12, 2017 7:20 am |
|
|
@Ttelmah Yes, I check T1SYNC, I have 0x0F on T1CON.
I get bored of trying with sleep command so I change to a main external oscillator of 32.768KHz and now I'm using TMR0 interrupt and waiting in a loop.
@jeremiah
I was using output_toggle CCS instruction.
I had 4.22V and the supply was 5V, so seems to be a voltage level problem as Ttelmah said. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19595
|
|
Posted: Fri May 12, 2017 7:34 am |
|
|
It is interesting that the main part about interrupts doesn't mention waking from Timer1. Almost looks as if the connection is in some way disabled when asleep. Unusual to find an oddity on such an old chip. Except if perhaps it is an old version (they don't actually have the errata for chips pre RevC any more on the site). |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Fri May 12, 2017 8:09 am |
|
|
Yes, is an old chip, I know, but we have several of this chips and now we need a simple timer and a PIC16F627 is more than enough for that job.
To bad that the errata is no longer on Microchip web site. _________________ Electric Blue |
|
|
|