CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Timer4 setup

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jpts



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

Timer4 setup
PostPosted: Tue Jun 27, 2017 11:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 27, 2017 11:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 27, 2017 12:22 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 27, 2017 2:22 pm     Reply with quote

I have to ask, does it run a 1Hz LED program?
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Jun 28, 2017 1:09 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 28, 2017 7:58 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 28, 2017 8:12 am     Reply with quote

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.... Smile
jpts



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

PostPosted: Wed Jun 28, 2017 8:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 28, 2017 8:47 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group