View previous topic :: View next topic |
Author |
Message |
linda p.r Guest
|
check a frequency in pic's mcu |
Posted: Sun May 17, 2009 1:02 am |
|
|
Hi
I am using pic's for many time and now we have to do a project in university, and I found myself in a problem with counting pulses.
What is the best way in ccs c to check if an input is 10 hertz ??
AND what is the best way to count pulses ?
If anyone has a code for the first one it will be good .
Thanks a lot. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun May 17, 2009 2:53 am |
|
|
For the frequency measurement, you get the fastest response by measuring the period , counting timer ticks between two pulses. There are different solutions depending on the PIC chip and your applications's overall structure.
Pulse counting can be done by a hardware or a software counter, the latter typically interrupt based (Ext or pin change interrupt).
I think, there are programming examples for both tasks shipped with the compiler. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
using internal timer1 with gate mode |
Posted: Sun May 17, 2009 9:29 am |
|
|
i've done counting of some pretty short pulses with fast polling and the following code on an F887 - 8mhz int clock and dealing with from 10hz to 1000hz frequencies using:
Code: |
// NOTE: timer one gate pin and C0 are tied together externally
// partial init code
unsigned int8 rotoken=0; // initial state
unsigned int16 rotcount;
set_tris_b( 0b00100000 ); // all output except #5 NOT T1G gate
// following must be done for TIMER 1 gate to work
setup_comparator(NC_NC|CP2_T1_GATE); // no comparators
// with an 8 mhz internal clock - each tick is 500ns duration
setup_timer_1( T1_INTERNAL|T1_GATE_INVERTED ); // set for rollovers
set_timer1(0); // so we get a TMR1IF rollover for later
// .........
// measure duration of a HI pulse on timer 1 gate
// pin C0 is used to alert hi level polling of zero between
// measured pulse width - duration of pulse positive
// is counted in "ticks" of
// TOKEN 0
void dunno ( void) { // no knowlege of where we are hi or low
if (!input(PIN_C0)) { // wait for LOW state of clock and advance
set_timer1(0); // reset timer
rotoken=1; // expecting to see a RISE next
}
}
// TOKEN 1
void hiyet ( void) { // wait for flag to go HI meaning we r counting
if (input(PIN_C0)) rotoken=2; // expecting to see a RISE next
}
// TOKEN 2
void harvest ( void) { // wait for flag to go HI meaning we are counting
if (0==input(PIN_C0)){
// detect overlong count first
if (TMR1IF) { rotcount =0; TMR1IF=0;} // overlong , low speed detected
else {
rotcount += get_timer1();
rotcount /= 2; // stack up the count, roll the average ahead
}
set_timer1(0); // reset timer for next
rotoken=1; // expecting to see a RISE next
// RPM 6 pole motor , counting TACH pulse [+ time] = (2635200/rotcount);
}
}
// rotmon is POLLED frequently enough from MAIN() to grab the counts
//
void rotmon(void){
switch (rotoken) {
// case 0 - dont know if we are low or high
case 0 : dunno(); break; // state unsure get in sync
case 1 : hiyet(); break; // did count start yet ?
case 2 : harvest(); break; // get reset value
default :
}
}
|
|
|
|
|