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

18f45K40 Timer2 interrupt not firing [SOLVED]

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



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

18f45K40 Timer2 interrupt not firing [SOLVED]
PostPosted: Fri Jul 28, 2017 9:01 am     Reply with quote

Hi All,

PIC18F45K40
CCS 5.073

I have the following problem, TIMER 2 interrupt service routine not working, if I test the TIMER2 interrupt flag it is set and the program are working.

TIMER 0 works by polling and calling.

If I want to jump to the interrupt service routine it does not work.
Any idea why.

Code:
#include <18F45K40.h>

#FUSES NOEXTOSC                //External Oscillator not enabled
#FUSES NOCKS                   //Clock Switching Disabled
#FUSES NOFCMEN                 //Fail-safe clock monitor disabled
#FUSES NOMCLR                  //Master Clear pin used for I/O

#use delay(internal=16MHz)

int1 Tick;

#INT_TIMER2
void  TIMER2_isr(void) {
  Tick = TRUE;
}

void main() {
  unsigned int8 Counter=0;
  setup_timer_2(T2_DIV_BY_64|T2_CLK_INTERNAL,124,1);      //125 us overflow, 125 us interrupt, 16MHz

//  enable_interrupts(INT_TIMER2);
  enable_interrupts(GLOBAL);
 
  while(TRUE){
    if (Tick) {
//   if (interrupt_active(INT_TIMER2)) {
//     clear_interrupt(INT_TIMER2);
      if (Counter++ > 20) {
        output_toggle(PIN_A1);
        Counter = 0;
      }
    }
  }
}


Regards


Last edited by alan on Sat Jul 29, 2017 7:42 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9220
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 9:09 am     Reply with quote

this..

// enable_interrupts(INT_TIMER2);

would explain it.

You need to uncomment it !!

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 9:19 am     Reply with quote

I wish you would show us the actual program that shows the problem,
without the debug code that you have added. Get rid of the test code
and just post the first program you tried that doesn't work.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 10:03 am     Reply with quote

Sorry about the confusion.
This code works.
Code:
#include <18F45K40.h>

#FUSES NOEXTOSC                //External Oscillator not enabled
#FUSES NOCKS                   //Clock Switching Disabled
#FUSES NOFCMEN                 //Fail-safe clock monitor disabled
#FUSES NOMCLR                  //Master Clear pin used for I/O

#use delay(internal=16MHz)

void main() {
  setup_timer_2(T2_DIV_BY_64|T2_CLK_INTERNAL,124,1);      //125 us overflow, 125 us interrupt, 16MHz

  while(TRUE){
    if (interrupt_active(INT_TIMER2)) {
      clear_interrupt(INT_TIMER2);
      output_toggle(PIN_A1);
    }
  }
}


This does not work :
Code:
#include <18F45K40.h>

#FUSES NOEXTOSC                //External Oscillator not enabled
#FUSES NOCKS                   //Clock Switching Disabled
#FUSES NOFCMEN                 //Fail-safe clock monitor disabled
#FUSES NOMCLR                  //Master Clear pin used for I/O

#use delay(internal=16MHz)

int1 Tick;

#INT_TIMER2
void  TIMER2_isr(void) {
  Tick = TRUE;
}

void main() {
  setup_timer_2(T2_DIV_BY_64|T2_CLK_INTERNAL,124,1);      //125 us overflow, 125 us interrupt, 16MHz

  enable_interrupts(INT_TIMER2);
  enable_interrupts(GLOBAL);
 
  while(TRUE){
    if (Tick) {
        output_toggle(PIN_A1);
       Tick = FALSE;
    }
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19487

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 11:35 am     Reply with quote

Given your earlier problem on Timer1. What happens if you set it to ASYNC?.

It does look as if that chip has some significant undocumented oddities....
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 12:23 pm     Reply with quote

There are no ASYNC bit for this timer, already checked.

Also TIMER 0 didn't fire at all, not even polling the interrupt worked without setting ASYNC.

Closest I get to a similar bit for TIMER2 are:
bit 7 PSYNC: Timerx Prescaler Synchronization Enable bit(1, 2)
1 = TMRx Prescaler Output is synchronized to Fosc/4
0 = TMRx Prescaler Output is not synchronized to Fosc/4

and that is set to 0.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19487

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 1:01 pm     Reply with quote

That is the synchronisation bit. What happens if you try with it synchronised instead?.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 1:37 pm     Reply with quote

No change, still does not work when enabling the interrupt.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19487

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 1:40 pm     Reply with quote

OK.
Very odd, since if the interrupt flag is being set, the interrupt should be called.
If you have a debugger, I'd dump the interrupt enable registers. I'd be suspecting that the enable for the interrupt is not being correctly set by the compiler, or the wrong bit is being checked in the handler.
Will try to have a look at what the compiler generates later.
Ttelmah



Joined: 11 Mar 2010
Posts: 19487

View user's profile Send private message

PostPosted: Fri Jul 28, 2017 2:17 pm     Reply with quote

Even odder.

The compiler seems to be setting and accessing the correct bits.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Sat Jul 29, 2017 7:43 am     Reply with quote

Sorry for all who tried to help and it were me being an idiot, must have been late and I were using the wrong HEX file.

I confirm that INT_TIMER2 does indeed work. No wonder I couldn't find any mention of a problem there.
Ttelmah



Joined: 11 Mar 2010
Posts: 19487

View user's profile Send private message

PostPosted: Sat Jul 29, 2017 8:24 am     Reply with quote

Scream gently...

We have all been there sometime. I have just been having an oddity, where a particular machine insists on changing it's network mapping sometimes. Found I had saved loads of work to a different drive from the one I thought it was on. No wonder it was not giving the results I expected....
Whimper.
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