|
|
View previous topic :: View next topic |
Author |
Message |
burnsy66
Joined: 16 Feb 2011 Posts: 3
|
18F13K22 timer2 counts down?? |
Posted: Wed Feb 16, 2011 10:18 pm |
|
|
I've been using PICs since their conception and have only just drifted over to the 18F types. After the usual port address issues I seem to have become stuck on this one.
My timer2 appears to count down? I generate an interrupt and toggle a port pin in the interrupt service routine. Loading a smaller value into timer 2 makes the wavelength shorter. Can't figure it out?
I have used this line to setup timer two in about 400 designs. With an external 3.686MHz oscillator I get a nice 1khz square wave.
setup_timer_2(T2_DIV_BY_4,0x70,0x02);
Now with a 14.7456 crystal(4 times faster), I would have guessed that the interrupt output would be called 4 times faster but instead I have to use this line to get the same 1khz output.
setup_timer_2(T2_DIV_BY_4,0x3C,0x01);
does anyone have any ideas? I'd appreciate you input. I can upload some code if it helps.. I just wondered if anyone has seen this as known issue.?
thanks all! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 16, 2011 11:22 pm |
|
|
Post your two test programs. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Thu Feb 17, 2011 3:31 am |
|
|
And include the headers (PIC versions, fuses etc..).
However some comments. You talk about the first one giving a '1KHz' square wave. Yet calculation says this will give a 500Hz wave, not 1KHz, if you are toggling the pin in the ISR. The frequency would be two complete IRQ calls, and these are occurring at intervals of:
2.713E-7 *4 *4 *113 *2 = 9.81E-4
So a cycle of the waveform should be 1.96mSec. 509.67Hz.
A smaller value here _will_ make the wavelength shorter. You are not loading a value into timer2. You are loading the upper count _limit_. The counter counts up to the number given, then resets on the next cycle (hence the count is one more than the number you use - 113 above, not 112).
Now, calculating backwards, says that the CPU in your second case, is actually running at 1MHz (for 1HKz out), or 500KHz (if the actual frequency is 500Hz as in your PIC 16 example). As such I'd say your fuses are set wrong, and the chip is actually running off the internal oscillator, not off your crystal. Worth also remembering, that with this chip you could use the same crystal as the original, and the internal PLL, to give 14.7MHz operation, without having to source an extra part.
So as PCM programmer says, post some code, but make sure you post the headers you are using, and the compiler version. I'd say 'oscillator'.....
Best Wishes |
|
|
burnsy66
Joined: 16 Feb 2011 Posts: 3
|
|
Posted: Thu Feb 17, 2011 7:09 pm |
|
|
brilliant, thanks that solved my problem.
Firstly, I had the concept of timer 2 incorrect. I forgot it counted up to the value that I set. I also use two values 7C and 72 for 4mhz interl and 3.686 external crystal.
I use an ISR that fires once every millisecond(accuracy of about 5-10% was fine and this is what it does. Indeed the square wave produced is approx 500hz. oops. I set this isr about 7 years ago and have never changed it. The work is not complicated it is used to detect note and coin acceptor pulses, button presses and debounce them, plus drive matrix leds so accuracy not essential.
sometime we get stuck in our ways and can't see out of the square. thank very much for your help! guys |
|
|
burnsy66
Joined: 16 Feb 2011 Posts: 3
|
|
Posted: Thu Feb 17, 2011 7:36 pm |
|
|
The problem solved above was the issue of counting up and down. I'll get back to you on the other one soon. I did a work around and have replaced the 18F13K22 with a 16F690 as I seem to have all sort of issues. Everything is running sweet now but will want to know why the 18F series didn't work. I'll post some test code in the next little while if I can't fix it.
The reason I didn't use the PLL is because on this design I originally used a 16F chip with an internal Osc. I needed more accuracy as I needed to have a number of modules flashing IR light in a sequence after a command from a master. I found the internal oscillator would drift after a while so I changed to to a crystal and damn, I had loads of 3.686MHz crystals so could have used them with the PLL. will look at that in the future.
thanks again. I used this forum a few times a few years ago. Its good to know you guys are still around. |
|
|
|
|
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
|