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 support@ccsinfo.com

check a frequency in pic's mcu

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
linda p.r
Guest







check a frequency in pic's mcu
PostPosted: Sun May 17, 2009 1:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 17, 2009 2:53 am     Reply with quote

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

View user's profile Send private message AIM Address

using internal timer1 with gate mode
PostPosted: Sun May 17, 2009 9:29 am     Reply with quote

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 :
            }
}
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