counting pulses

Discussion in 'Embedded Systems and Microcontrollers' started by quantumlab, Feb 23, 2009.

  1. quantumlab

    Thread Starter Member

    Dec 4, 2008
    Hi the code below counts heart rate pulses indefinitely. We would like to count how many heart rate pulses there are every ten seconds. Im not sure how to set up a timer to count the amount of pulses every ten seconds. We are using MikroC. Can you please help?

    unsigned short T1_count = 0;
    unsigned int temp_res;
    int i;
    void main() {

    ANSEL = 0x04; // Configure AN2 pin as analog
    TRISA = 0xFF; // PORTA is input
    ANSELH = 0; // Configure other AN pins as digital I/O
    TRISB = 0x3F; // Pins RB7, RB6 are outputs
    TRISD = 0; // PORTD is output
    TRISC = 0;

    T1_count = 0;
    do {
    temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
    PORTD = temp_res; // Send lower 8 bits to PORTD
    PORTB = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
    if (temp_res > 100){
    delay_ms (100);

    PORTC = T1_count;}
    } while(1);
  2. quantumlab

    Thread Starter Member

    Dec 4, 2008
    Even help how to count the time between two pulses would be appreiciated. Can i start a timer and just check the time on it wen the pulse goes high?
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    You don't say what model of PIC you are using but if it has a CCP (capture and compare) module then you can use this to count your pulses. See the data sheet or download PIC Mid-Range MCU Family Reference Manual from the Microchip site and look up CCP.

    If your PIC does not have a CCP module you can still do it manually. Set up your 16 bit timer (timer1 I think) to interrupt every 10 seconds and set aside a variable to hold your count.
    Start the timer and start counting heartbeats.
    When an interrupt occurs read the heartbeat count, rest the count to 0 and restart the timer for the next reading.
  4. LesterPhil


    Feb 25, 2009
    It so simple just tell me the specific PIC that you use.... in order me to know what registers are involve.
  5. quantumlab

    Thread Starter Member

    Dec 4, 2008
    The pic were using is the 16f887,

    if (cnt == 76) { // if cnt is 76
    PORTB = ~PORTB; // then toggle PORTB leds and
    cnt = 0;

    this the section of timer 1 we think we should change,say 76 to 10 for 10 seconds or this completly incorrect?