Identify 01 in a set of 001001001

Discussion in 'Embedded Systems and Microcontrollers' started by Neyolight, Oct 9, 2012.

  1. Neyolight

    Thread Starter Member

    Dec 13, 2011
    Hi All

    I have a very simple question that I "kind of" know the answer to. But I would like to see what you guys think.

    I have a series of digital data for example 001110001100000111100. I want to count how many 0 to 1 transitions are present in the data. For the data set in the example, I should get 3.

    The simples way to do this is to store current data if its 0 and check if the next data coming in is 1. This is the theory behind it but what is the fastest and most efficient way to implement it ?

    Although I havent started implementing this, but when I do I would use a PIC micro.

    Thanks :)
    Last edited: Oct 9, 2012
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    count = 0;
    lastx = 1;
    if(newx == 1 && lastx == 0) count++;
    lastx = newx;
    goto loop;

    The loop part needs to get the newx from whatever data source you are using (you did not say). Also the loop should probably check for the process being finished, then end the loop.
    Neyolight likes this.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Here Total and Last are global variables, so they need to be reset for every string tested. It is assumed Bit will be either 1 or 0. If not, an AND mask would be required in the routine.

    Code ( (Unknown Language)):
    2. // globals
    3. int Total = 0;
    4. char Last = 0;
    6. void Check(char Bit);
    9. void Check(char Bit)
    10. {
    11.    Total += Bit & Last;
    12.    Last = !Bit;
    13. }
  4. Markd77

    Senior Member

    Sep 7, 2009
    If you wanted a really fast way, you could store a table of 256 values, for all the possible combinations in a byte, returning the number of transitions. You would have to do a little checking to see if there was a transition between bytes.
    If the data was coming from a pin, you could just use one of the timer pins as a counter.