View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
Problem with timers of 18F67K22 |
Posted: Sat Aug 29, 2015 7:33 am |
|
|
Greetings! I want to use a few timers of 18F67K22, but I can activate the interrupt only of timer 0. Here is my code for timer3:
Code: |
void SetUpTimer3()
{
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
enable_interrupts(INT_TIMER3);
enable_interrupts(GLOBAL);
set_timer3(100);
}
#INT_TIMER3
void Timer3()
{
clear_interrupt(INT_TIMER3);
set_timer3(5);
}
|
I can't trigger the interrupt! I tried with other timers -> same result!
Can you tell me what I'm doing wrong?!
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Aug 29, 2015 4:24 pm |
|
|
Quote: | I can activate the interrupt only of timer 0. |
It could be something like this thread, where the lack of an #int_tbe
interrupt handler causes the program to interrupt continuously, and
does very little except for that:
http://www.ccsinfo.com/forum/viewtopic.php?t=54209&start=5
We can't know for sure until you post a full test program, with #fuses,
main(), etc.
Also post your compiler version. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Sun Aug 30, 2015 12:21 am |
|
|
Full test program:
Code: |
#include <18F67K22.h>
#fuses HSH,NOWDT
#use delay(clock=40M)
#INT_TIMER3
void Timer3()
{
clear_interrupt(INT_TIMER3);
set_timer3(5);
}
void SetUpTimer3()
{
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
enable_interrupts(INT_TIMER3);
enable_interrupts(GLOBAL);
set_timer3(100);
}
void main()
{
delay_ms(100);
SetUpTimer3();
while(1)
{
}
}
|
I`m using MPLab v8.91 and CCS v5.025.
Best regards! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Aug 30, 2015 2:00 am |
|
|
First thing is your clock is not legal.....
HSH, supports crystals _up to 25MHz_. Not 40Mhz. The only way to get 40Mhz, is to use an external oscillator module, or the PLL. The chip won't actually run (or run at the wrong rate), with the settings shown...
Get rid of the clear_interrupt call. The compiler automatically clears interrupts for you, unless you specify the interrupt handler with 'NOCLEAR'. Clearing it as well is just a waste.
Then, the question is 'how are you testing this'?. From the illegal clock, suggests, it suggests in the simulator?. If so, there were problems with this and timers on these chips. If you switch the settings to use an external clock, and then toggle RB5, you will merrily find the timer counting. With the internal clock, you will find that the timer is not actually counting at all....
The compiler correctly configures the internal instruction clock/8 selection (0x37 into T3CON, and 0 into T3GCON), but the simulator does not handle it. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Sun Aug 30, 2015 11:17 am |
|
|
Sorry for the mistake in the fuses. Yes, I`m using simulator because I want to adjust the timer to count 10us. Does that mean I have to use only calculation without verifying it with the simulator?!
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Aug 30, 2015 11:20 am |
|
|
Test on a real chip. Only takes a few seconds to modify the interrupt code to toggle a pin. If it works you know where the problem is, and also can verify the speed you are getting. |
|
|
|