counting pulses

Thread Starter

quantumlab

Joined Dec 4, 2008
19
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);
T1_count++;

PORTC = T1_count;}
} while(1);
}
 

Thread Starter

quantumlab

Joined Dec 4, 2008
19
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?
 

AlexR

Joined Jan 16, 2008
732
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.
 

Thread Starter

quantumlab

Joined Dec 4, 2008
19
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?

Thanks
 
Top