Identify 01 in a set of 001001001

Thread Starter

Neyolight

Joined Dec 13, 2011
54
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:

THE_RB

Joined Feb 11, 2008
5,438
count = 0;
lastx = 1;
loop;
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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.

Rich (BB code):
// globals
int Total = 0;
char Last = 0;

void Check(char Bit);


void Check(char Bit)
{
   Total += Bit & Last;
   Last = !Bit;
}
 

Markd77

Joined Sep 7, 2009
2,806
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.
 
Top