View previous topic :: View next topic |
Author |
Message |
jpts
Joined: 08 Mar 2017 Posts: 40
|
Timer4 setup |
Posted: Tue Jun 27, 2017 11:17 am |
|
|
Why TIMER4 is not running interrupt call....printout message "interrupt4"
Code: |
#INT_TIMER4
void TIMER4_isr(void)
{printf ("Interrupt 4!\r\n ");}
void Main(){
setup_timer_4(T4_CLK_INTERNAL | T4_DIV_BY_16,240,13); //over+- 10ms
enable_interrupts(INT_TIMER4);
enable_interrupts(GLOBAL);
While(true){}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jun 27, 2017 11:38 am |
|
|
You didn't provide a complete 'cut&paste' program so I can't test but
the usual culprits are:
A compiler bug, depends on version of compiler...which you don't say.
Possible PIC die problem (aka errata from uChip).. unknown PIC.
PIC not running, PCB layout? Does a 1Hz LED pgm run ??
Jay |
|
|
jpts
Joined: 08 Mar 2017 Posts: 40
|
|
Posted: Tue Jun 27, 2017 12:22 pm |
|
|
Complementing ...compiler 5.070
Code: |
#include <16F18857.h>
#device ADC=10
#use delay(crystal=20000000)
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES NOWDT
#FUSES NOPPS1WAY
#FUSES STVREN
#FUSES NOMCLR
#PIN_SELECT U1RX=PIN_C7
#PIN_SELECT U1TX=PIN_C6
#use rs232(baud=9600,parity=N,UART1,bits=8,stream=PORT1,errors) |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jun 27, 2017 2:22 pm |
|
|
I have to ask, does it run a 1Hz LED program? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Jun 28, 2017 1:09 am |
|
|
As far as I can see it is not writing to the T4CLK configuration register to select the clock source.
Tidied the code, and updated:
Code: |
#include <16F18857.h>
#device ADC=10
#use delay(crystal=20000000)
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES NOWDT
#FUSES NOPPS1WAY
#FUSES STVREN
#FUSES NOMCLR
#PIN_SELECT U1RX=PIN_C7
#PIN_SELECT U1TX=PIN_C6
#use rs232(baud=9600,parity=N,UART1,bits=8,stream=PORT1,errors)
#byte T4CLKCON=getenv("SFR:T4CLK")
int1 time_seen=FALSE;
#INT_TIMER4
void TIMER4_isr(void)
{
time_seen=TRUE;
}
void Main(void)
{
setup_timer_4(T4_CLK_INTERNAL | T4_START_IMMEDIATELY | T4_DIV_BY_16,239,13); //over+- 10ms
//Remember count is one greater than the PR2 value.
T4CLKCON=1;
enable_interrupts(INT_TIMER4);
enable_interrupts(GLOBAL);
While(TRUE)
{
if (time_seen)
{
time_seen=FALSE;
fprintf (PORT1, "Interrupt 4!\r\n ");
}
}
}
|
On 5.073, it does correctly set this register. |
|
|
jpts
Joined: 08 Mar 2017 Posts: 40
|
|
Posted: Wed Jun 28, 2017 7:58 am |
|
|
WORKED!
Adding in program:
#byte T4CLKCON=getenv("SFR:T4CLK")
T4CLKCON=1;
Note: Its worked without "T4_START_IMMEDIATELY" on setup timer4.
setup_timer_4(T4_CLK_INTERNAL | T4_DIV_BY_16,240,13);
Thanks, Jpts |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Jun 28, 2017 8:12 am |
|
|
I know it would. START_IMMEDIATELY is '0' on your chip, so will be what happens if you omit it. However this is not true on a couple of other chips, so it is worth having it there in case you ever change chips.... |
|
|
jpts
Joined: 08 Mar 2017 Posts: 40
|
|
Posted: Wed Jun 28, 2017 8:24 am |
|
|
Some examples in web use "clear_interrupt(INT_TIMER4);" every#INT_TIMER4 and in start main(). Its really necessary ....my program did not have any affect with or without "clear_interrupt(INT_TIMER4);"
Jpts |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Jun 28, 2017 8:47 am |
|
|
The CCS compiler _clears the interrupt for you in the handler_, unless you use the option 'NOCLEAR'. From the manual entry:
Quote: |
The MPU will jump to the function when the interrupt is detected. The compiler will generate code to save and restore the machine state, and will clear the interrupt flag. To prevent the flag from being cleared add NOCLEAR after the #INT_xxxx. To have the flag cleared at the beginning of the ISR, add CLEAR_FIRST after the #INT_xxx. The application program must call ENABLE_INTERRUPTS(INT_xxxx) to initially activate the interrupt.
|
Clearing it before enabling in the main ensures there is not a spurious call (which can happen if the timer has been running fast off another clock, before you configure it), but is unlikely if you are configuring at the start of the code. |
|
|
|