> I have a sensor that gives output simular to this: > empty: 000000100000010 > midfull: 011100011011101 > full: 11101111001111101 > As you can see I mostly get 1:s when the level is high. > When empty, I get mostly 0:s when the sensor is polled. I guess what you meant to say is that the sensor produces a bit stream, with the fraction of 1s out of the whole providing the level value? This would be as if it had a comparator input dithered with full scale white noise. If this is incorrect then ignore the rest of this message. One way to look at the signal is that it contains the low frequency level value superimposed with high frequency noise. > My basic idea for this, is to poll the sensor and: > if Sensor=1 then X=X+1 > if Sensor=0 then X=X-1 > Timeconstant: (exact timing, nor speed, is an issue) > If Sensor=0, X will count down to min (LOW) in 3 secs > If Sensor=1, X will count up to max (HIGH) in 3 secs In other words, you want to integrate the bit stream over a fixed interval (3 sec in this case). I'm assuming you initialize X to 0 at the start of the interval although you didn't say this. Yes, that will work, although there are probably better signal reconstruction techniques. This will result in one reading every interval that is the box filtered value of the interval. You could just: if Sensor = 1, then X <-- X + 1 if Sensor = 0, then leave X alone This will yeild a value from 0 to . You can then compare to whatever threshold you want. > A pic running at 4MHz polls the sensor at approx 80kHz, if the > loop takes 50 cycles. 80000* 3sec gives X will be in the > 0-240 000 range. 18 bits math. First, you're only incrementing (or not) a number, so 24 bit "math" is trivial: incf cnt0 ;increment the counter low byte skip_ncarr ;no carry into next byte ? incf cnt1 ;propagate the carry skip_ncarr ;no carry from middle to high byte ? incf cnt2 ;propagate the carry Second, just because you CAN sample at 80KHz doesn't mean you have to. > It would be more convenient to have a 0-255 range instead. You can arrange for the integrator output to easily be from 0 to whatever maximum value you want by taking exactly that many samples. However, I would worry about a lot of noise left after only 255 samples. If you want to stick to your simple box filter approach, then I would use a 16 bit counter and take exactly 65535 samples per iteration. The high byte of the 16 bit counter yields the 0 to 255 level value directly. Arrange the sample frequency so that this happens in your desired response time. 65535 samples in 3 sec is about 22KHz, or 46uS/sample. That would be no big deal even for a 12C508A. Note that the exact clock rate isn't that important since you don't care about 3 sec exactly. The important part is to integrate over exactly 65535 samples, which is easy to guarantee. This all being said, I don't like the frequency response and therefore noise rejection of a box filter. My first reaction would be to do continuous low pass filtering on the signal and use the output of the filter as the "live" level value whenever it was needed. Two cascaded single pole filters each with a filter fraction of 1/256 looks like would work well here. In other words, the operation of each filter would look like this: new filt val <-- - /256 + /256 A divide by 256 is a shift right of 8 bits, which is just moving bytes around. Two such filter stages gives you enough noise rejection so that the top 8 bits should be pretty good, and the overall step response time is 90% after 1000 samples. If you take one sample every millisecond, this takes only a small fraction of the CPU and provides a 90% step response in one second. Adjust to vary your mileage as desired. Three 1/256 filter stages provides more noise rejection and a 90% step response in under 1400 samples. ******************************************************************** Olin Lathrop, embedded systems consultant in Littleton Massachusetts (978) 742-9014, olin@embedinc.com, http://www.embedinc.com -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.