Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem in counting pulse using IR sensor with PIC microcontroller

Status
Not open for further replies.

pimh123

New Member
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;
}
}
 
If I could see on an O-scope the wave form coming from the IR sensor, I would know what type of hardware/software de-bounce is needed.
Knowing how long the bounce is will help.
Also knowing how sharp the edges are will help. If the edge rises very slow, and it probably does, then that is different.

if(PULSE==0 && state1==0 && state2==0)
Some thing like this where you need three reads in a row might help.
Yes I know you don't want slower software.
 
Software debouncing is easy, but will take time. It's up to you to decide if the delay is acceptable or not. Alternatively, you can do debouncing from a timer interrupt.

The idea is very simple. You read the signal and if it changes, you read it again and accept the change only after it stays in the new state for certain amount of time.

C:
#define THRESHOLD 10 // how many times we want to see it changed until we believe it.

int debounce_counter; // used for debouncing
int pulse_counter; // counts the pulses between counter_clr signals
int motor_pulses; // the resulting variable
int state;

void InitState() {
  // this initializes the state
  state = PULSE; // assume steady state
  debounce_counter = 0;
  pulse_counter = 0;
  motor_pulses = 0;
}

void CheckState() {
   if (PULSE == state) {
     // forget what we were counting. No change took place
     debounce_counter = 0;
     return;
  }

  if (debounce_counter++ < THRESHOLD) {
    // There has been a change. But it didn't persist long enough
    return;
  }

  // The change persisted for a while. Record the change of state, reset the counter
  debounce_counter = 0;
  if (state = !state) {
     // We detected transition 0->1, do nothing
     return;
  }
 
  // transition 1->0. Record new pulse
  pulse_counter++;
   
  if (!pulse_clr) {
     // No pulse_clr signal yet
     return;
  }
   
  // set the resulting variable, reset the pulse counter
  motor_pulses = pulse_counter;
  pulse_counter = 0;
  pulse_clr = 0;
}

Then all you need to do is to call CheckState() periodically, either in a tight loop, or from fast timer interrupt.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top