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

PIC24 and 32-bit counter (timer2/timer3)

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



Joined: 01 Apr 2007
Posts: 195

View user's profile Send private message

PIC24 and 32-bit counter (timer2/timer3)
PostPosted: Tue Dec 08, 2015 10:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 08, 2015 2:34 pm     Reply with quote

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.

Code:

get_timer23();
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

View user's profile Send private message

PostPosted: Tue Dec 08, 2015 4:11 pm     Reply with quote

Excellent! That's exactly the kind of insight I was looking for.

Thanks very much!
starfire151



Joined: 01 Apr 2007
Posts: 195

View user's profile Send private message

PostPosted: Tue Dec 08, 2015 7:27 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 08, 2015 9:43 pm     Reply with quote

#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

View user's profile Send private message

PostPosted: Wed Dec 09, 2015 1:33 am     Reply with quote

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



Joined: 01 Apr 2007
Posts: 195

View user's profile Send private message

PostPosted: Wed Dec 09, 2015 12:47 pm     Reply with quote

Thanks, again. I should have seen that. My bad Embarassed
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