Problem in counting pulse input

Discussion in 'General Electronics Chat' started by scud123, Jun 23, 2016.

  1. scud123

    Thread Starter New Member

    Mar 20, 2014
    4
    0
    I'm trying to find RPM of a dc motor using PIC24F microcontroller. I've 200 RPM DC motor and IR sensor. I'm counting the pulse input from IR sensor when black material which attached with DC motor shaft contact with IR sensor. I'm printing pulse count for every 60 seconds using PIC timer interrupt.I don't want to use any delay inside. Each pulse count for each revolution respectively, but the problem is sometimes pulse count twice or thrice has happening for one revolution.

    CODE:
    When timer interrupt reaches 60 seconds i set count_clr=1

    while(1)
    {
    if(PULSE==0 && state==0)
    {
    pulse_cnt++;
    printf("%d\n",pulse_cnt);
    state=1;
    }
    else if(PULSE==1 && state==1)
    {
    state=0;
    }
    if(count_clr)
    {
    printf("Total Pulse:%d\n",pulse_cnt);
    pulse_cnt = 0;
    count_clr=0;
    }
    }
     
  2. Techno Tronix

    Member

    Jan 10, 2015
    140
    10
    Did you tried using Interrupt driven pulse counter and sleep mode?
    It can also use to reduce power consumption.
     
  3. scud123

    Thread Starter New Member

    Mar 20, 2014
    4
    0
    Thanks for the reply. No. Are you mentioning about I/O driven interrupt?
     
  4. Techno Tronix

    Member

    Jan 10, 2015
    140
    10
  5. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,548
    2,373
    I have used the timer capture mode of the Pic for this, the first pulse clears the timer the second pulse is captured and the timer interval read.
    There are all kind of examples on the pic site, including some in the Mechatronics board section.
    Max.
     
Loading...