Best strategy for recording input pulses simultaneously?

Hi folks,

I’ve got some code for recording incoming pulses (and flashing on leds correspondingly):


costate {
  if (BitRdPortI(PBDR, 0)) {
    continue;  // if button not down skip out of costatement
  } else {
    pulsecount1++;
    printf("Timestamp of pulse is: %ld, pulsecount for INPUT 1 for this interval is %d
", SEC_TIMER, pulsecount1);

    while (1) {
      BitWrPortI(PADR, &PADRShadow, 1, 1);
      waitfor(BitRdPortI(PBDR, 0));  // wait for button to go up
      //waitfor(DelayMs(200));	// wait additional 200 milliseconds

      if (BitRdPortI(PBDR, 0)) {
        BitWrPortI(PADR, &PADRShadow, 0, 1);
        break;  // if button still up break out of while loop
      }
    }
  }
}

I’ve got multiple costate blocks for three inputs that I’m trying to record simultaneously. This works okay at slow pulse rates, but when the pulses are coming in faster, they block each other. I want to be able to record the pulses reliably independently of each other. Can you suggest a way to do this that would achieve this? I’m not necessarily looking for someone to give me a replacement block of code, even a pointer to “try using X” would be very helpful (not that I’d be unhappy with a block of code…heh).

Thanks!

Best,
Dave

Isn’t abort the appropriate command rather than continue (at line 3 in your example)?

How fast are the pulses when they block each other (wondering if the printf is the problem, but they’d probably have to be pretty fast for that to be the case)?

Isn’t abort the appropriate command rather than continue (at line 3 in your example)?

Hmm…I’ll review that and see! I think I lifted this code from somewhere, and while I’ve tweaked it a bit, that I’ve left in place. Goes to show that one should understand every bit of code one uses…

How fast are the pulses when they block each other (wondering if the printf is the problem, but they’d probably have to be pretty fast for that to be the case)?

I don’t think so, I’m pretty sure it is the while(1) loop that is causing this–the algorithm itself is bad I think because it essentially blocks until BitRdPortI(PBDR, 0) fails (er, succeeds). I’m saying this because I know I can get a ton of printfs from one costate, but if the next one is going slower, it will lag and block the first from printing at all until it’s done…do you see what I mean?

I’m just not sure how to do this without testing the return from BitRdPortI() over and over and breaking after it switches off…any thoughts?

The while(1) loop looks OK, as it uses waitfor(), which will allow the other costates to run.

I think the problem probably is with the continue. It will jump back to the top of the loop (not shown in your example) that contains the costates without letting the costates below this one to run. If you change it to abort, then control will transfer to the line following this costate, and thus will allow the following costate to run.

Hi sgt, sorry to second guess you. I tried that and it seems to be working great. Thank you very much for your help!!

Best,
Dave