Hi,
Having trouble using an external timer1 connected
to the T13CKI pin to get the PIC to wake from sleep mode.
I am basically trying to build a pulse generator. I have a 32Khz
oscillator connected to the T13CKI pin. I then set up an interrupt in
flowcode using the built in TMR1 interrupt function. I then write to the
TMR1 registers to create an interrupt frequency of around 100Hz. The
interrupt service routine simply toggles portb.1 on and off and then
writes a value to the TMR1 counter registers. I have set the TMR1
interrupt on the T13CKI pin, not the int clk. At the bottom of the code
is an infinite while loop.
This works fine until i try and put the PIC into sleep mode by executing
the sleep() command. It never wakes up.
I am using flowcode 4 and a 18F2523 PIC. The PIC is driven by the same
external 32Khz clk, as on the TMR1.
I have attached my flowcode project.
Could anyone possible help me?
Timer1 and sleep
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Timer1 and sleep
Hello,
Hm this sounds bit strange. You could try adding a C code nop before and after the sleep instruction. I have seen several microchip examples that use a nop near the sleep.
I think the C code for nop is.
Nop();
Hm this sounds bit strange. You could try adding a C code nop before and after the sleep instruction. I have seen several microchip examples that use a nop near the sleep.
I think the C code for nop is.
Nop();
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Timer1 and sleep
Thanks for your reply. I tried using a nop(); before and after the sleep command, but it still doesn't wake from the sleep command. It does wake from an idle sleep, but not from a true sleep instruction.
Re: Timer1 and sleep
Here is the C code generated from the flowcode project. Can anyone see anything that im am doing wrong. The PIC never wakes from sleep mode, even though I am interupting the PIC using an external timer1 osc.
//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"
//Macro function declarations
void FCM_pulse();
//Variable declarations
char FCV_TMR1_H;
char FCV_TMR1_L;
short FCV_TMR1;
char FCV_COUNT;
//Macro implementations
void FCM_pulse()
{
//Delay
//Delay: 50 us
delay_us(50);
//C Code
//C Code:
tmr1l = 220;
tmr1h = 255;
//Output
//Output: 1 -> PORT B
trisb = 0x00;
portb = 1;
//Output
//Output: 0 -> PORT B
trisb = 0x00;
portb = 0;
}
void main()
{
//Initialisation
adcon1 = 0x0F;
//Interrupt initialisation code
//Delay
//Delay: 50 us
delay_us(50);
//Input
//Input: PORT C -> count
trisc = trisc | 0xff;
FCV_COUNT = portc;
//C Code
//C Code:
clear_bit(osccon,7);
//Input
//Input: C0 -> count
trisc = trisc | 0x01;
FCV_COUNT = ((portc & 0x01) == 0x01);
//C Code
//C Code:
tmr1l = 200;
tmr1h = 255;
//Interrupt
//Interrupt: Enable TMR1
st_bit(intcon, PEIE);
st_bit(t1con, T1OSCEN);
st_bit(t1con, TMR1ON);
st_bit(t1con, TMR1CS);
t1con = t1con & 0x0F;
st_bit(intcon, GIE);
st_bit(pie1, TMR1IE);
//Loop
//Loop: While 1
while (1)
{
//C Code
//C Code:
sleep();
nop();
nop();
}
mainendloop: goto mainendloop;
}
void MX_INTERRUPT_MACRO(void)
{
//Handler code for [TMR1]
#ifndef MX_INTHANDLER_pir1_TMR1IF
#define MX_INTHANDLER_pir1_TMR1IF
if (ts_bit(pir1, TMR1IF) && ts_bit(pie1, TMR1IE))
{
FCM_pulse();
cr_bit(pir1, TMR1IF);
}
#else
#warning "This interrupt has previously been enabled, so the macro <pulse> may never get called."
#endif
}
//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"
//Macro function declarations
void FCM_pulse();
//Variable declarations
char FCV_TMR1_H;
char FCV_TMR1_L;
short FCV_TMR1;
char FCV_COUNT;
//Macro implementations
void FCM_pulse()
{
//Delay
//Delay: 50 us
delay_us(50);
//C Code
//C Code:
tmr1l = 220;
tmr1h = 255;
//Output
//Output: 1 -> PORT B
trisb = 0x00;
portb = 1;
//Output
//Output: 0 -> PORT B
trisb = 0x00;
portb = 0;
}
void main()
{
//Initialisation
adcon1 = 0x0F;
//Interrupt initialisation code
//Delay
//Delay: 50 us
delay_us(50);
//Input
//Input: PORT C -> count
trisc = trisc | 0xff;
FCV_COUNT = portc;
//C Code
//C Code:
clear_bit(osccon,7);
//Input
//Input: C0 -> count
trisc = trisc | 0x01;
FCV_COUNT = ((portc & 0x01) == 0x01);
//C Code
//C Code:
tmr1l = 200;
tmr1h = 255;
//Interrupt
//Interrupt: Enable TMR1
st_bit(intcon, PEIE);
st_bit(t1con, T1OSCEN);
st_bit(t1con, TMR1ON);
st_bit(t1con, TMR1CS);
t1con = t1con & 0x0F;
st_bit(intcon, GIE);
st_bit(pie1, TMR1IE);
//Loop
//Loop: While 1
while (1)
{
//C Code
//C Code:
sleep();
nop();
nop();
}
mainendloop: goto mainendloop;
}
void MX_INTERRUPT_MACRO(void)
{
//Handler code for [TMR1]
#ifndef MX_INTHANDLER_pir1_TMR1IF
#define MX_INTHANDLER_pir1_TMR1IF
if (ts_bit(pir1, TMR1IF) && ts_bit(pie1, TMR1IE))
{
FCM_pulse();
cr_bit(pir1, TMR1IF);
}
#else
#warning "This interrupt has previously been enabled, so the macro <pulse> may never get called."
#endif
}
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Timer1 and sleep
Hello,
Section 12.6 of the device datasheet seems to have a bit of information regarding timer1 using sleep and an external clock source.
If your still having problems then let me know and I will have a dig for you.
Section 12.6 of the device datasheet seems to have a bit of information regarding timer1 using sleep and an external clock source.
If your still having problems then let me know and I will have a dig for you.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Timer1 and sleep
I've also just spotted this.
Section 16.4.10 In Sleep mode, all clock sources are disabled.
Maybe using the timer 1 in sleep mode is not possible on this device? The guys at the Microchip forums will be able to give you a definative answer about this.
Section 16.4.10 In Sleep mode, all clock sources are disabled.
Maybe using the timer 1 in sleep mode is not possible on this device? The guys at the Microchip forums will be able to give you a definative answer about this.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Timer1 and sleep
Thnaks for all your help. I still have not been able to get it working. According to the data sheet for the 18f2523 timer1 can be used in sleep mode.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Timer1 and sleep
Hello,
Yes your right it does look like timer 1 can be used in low power sleep mode. Seems theres a few versions of the datasheet kicking about. Im going of the 18F2420 product family datasheet from the microchip site at the mo.
Could be something to do with your oscillator settings or setup.
Maybe try changing the following config settings:
Oscillator setting to xt or lp.
Maybe also enable internal external switchover mode.
Also you will need to disable the low power timer 1 oscillator i think.
Let me know how you get on.
Yes your right it does look like timer 1 can be used in low power sleep mode. Seems theres a few versions of the datasheet kicking about. Im going of the 18F2420 product family datasheet from the microchip site at the mo.
Could be something to do with your oscillator settings or setup.
Maybe try changing the following config settings:
Oscillator setting to xt or lp.
Maybe also enable internal external switchover mode.
Also you will need to disable the low power timer 1 oscillator i think.
Let me know how you get on.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Timer1 and sleep
I am running the oscillator of a 32Khz external oscilator, but the lp and xt setting are for a external crystal!!
I currently have the internal and external switchover enabled and low power timer1 is disabled.
I currently have the internal and external switchover enabled and low power timer1 is disabled.