View previous topic :: View next topic |
Author |
Message |
starfire151
Joined: 01 Apr 2007 Posts: 195
|
PIC24 and 32-bit counter (timer2/timer3) |
Posted: Tue Dec 08, 2015 10:05 am |
|
|
PCWHD V5.044
PIC24FV32KA301 (20-pin DIP and SOIC)
I'm running the PIC with the internal 32MHz oscillator. I'm trying to run the PIC2 as a high-speed counter with an external pulse input. I can see from the datasheet that timer2 and timer3 can be joined to make a 32-bit counter.
In looking at the PCD reference manual #use timer pre-processor directive, I have a few questions:
1. Can the resolution of "tick" be the instruction cycle time (at 32MHz = 62.5ns)?
2. If I select "timer=2" and "bits=32" will that automatically use timer2 and timer3 or does something else need to be done?
3. Since I want to go as fast as possible, I would use a prescalar of 1 or "counter=1". How fast could I reasonably expect the counter to count without missing counts?
4. Would the "isr" or "noisr" be the faster approach?
Thanks for any help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Tue Dec 08, 2015 2:34 pm |
|
|
You seem to be talking about using the #USE TIMER ability, rather than directly programming the timer.
This will not cascade timers for you. 32bit is handled with this by maintaining a software 'high' word.
If you are reading the tick more often than the overflow interval, this will handle the overflow for you, without the ISR, but with it's own overhead.
Better (by far...), to setup the timer yourself, and set this to use 32bit mode.
Code: | setup_timer2(TMR_32_BIT | TIMER_EXTERNAL | TMR_DIV_BY_1); | Will set timer2/3 to be a 32bit counter.
Will read the 32bit value from the counter.
It'll count every pulse on the external signal. How often it changes will depend on this frequency. Data sheet will tell you the maximum frequency supported.
You will need to set the timer input up using #pin select.
The alternative is input capture mode. In this the system records the time using the internal clock, when an event occurs on the external pin.
Download data sections 39704a.pdf, and 70198d.pdf from Microchip, which describe the timers, and the input capture mode.
Forget #use timer, if you want to use the full capabilities of the chip use the actual timer functions. #use timer, is akin to the Wizard. Makes things apparently easier, but uses a subset of the real capabilities. |
|
|
starfire151
Joined: 01 Apr 2007 Posts: 195
|
|
Posted: Tue Dec 08, 2015 4:11 pm |
|
|
Excellent! That's exactly the kind of insight I was looking for.
Thanks very much! |
|
|
starfire151
Joined: 01 Apr 2007 Posts: 195
|
|
Posted: Tue Dec 08, 2015 7:27 pm |
|
|
I got the setup_timer2(TMR_32_BIT | TMR_EXTERNAL | TMR_DIV_BY_1); statement in the program. I can read the value with a get_timer23(); statement.
I'm having a problem getting the preprocessor to accept the #pin_select directive, though. I'm trying
#pin_select T2CK=PIN_B15 but I get:
***Error 7 Invalid Pre-Processor directive Invalid Pin.
According to the datasheet, the T2CK and/or T3CK inputs are on chip pin 18 or port pin B15.
What am I doing wrong? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1357
|
|
Posted: Tue Dec 08, 2015 9:43 pm |
|
|
#pin_select is for when pins can be moved around, like on a PIC24FJ64GA004. Your chip's pins are all set to specific pins already. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Wed Dec 09, 2015 1:33 am |
|
|
Apologies, I misled him there. I'm used to chips PIC24's which don't have anything 'fixed', but need you to setup everything. Hadn't checked the data sheet for his chip.
Basically if the chip data sheet shows a pin, then it doesn't need to be 'selected'. If the data sheet doesn't list a fixed pin, then it does... |
|
|
starfire151
Joined: 01 Apr 2007 Posts: 195
|
|
Posted: Wed Dec 09, 2015 12:47 pm |
|
|
Thanks, again. I should have seen that. My bad |
|
|
|