Getting at a byte's bits - reading a byte from porta, but using at it as 8 bits
My project reads a sensor with 8 pins (binary data) . I am doing 3 functions over the data, initially I read the pins for each function, but am concerned about values changing between reads of INA so started looking at how to read it once to a VAR, then do same process with a VAR rather then INA.
Working example using INA:
With the working example, the algorithm an ina[0..7] = 255 (1111111) will yield an average of 9.
My attempt was confused by looking at BYTE [offset] as the bits, but it is really a BYTE array:
Thanks for the suggestions...
Working example using INA:
bit_count = 0
sensor0_pin = 0
SensorValue = ina[(sensor0_pin..sensor0_pin +7)]
repeat bit_count from 0 to 7 'repeat for each pin's bit read off of sensor
average += ina[(sensor0_pin + bit_count)] * ((bit_count * 2) + 2) 'check each bit, add weighted value
average_denom += ina[(sensor0_pin + bit_count)] 'determine how many bits were high to determine total number calculate in average
return average /= average_denom 'calculate average of weighted bits from sensor
With the working example, the algorithm an ina[0..7] = 255 (1111111) will yield an average of 9.
My attempt was confused by looking at BYTE [offset] as the bits, but it is really a BYTE array:
bit_count = 0
sensor0_pin = 0
SensorValue = ina[(sensor0_pin..sensor0_pin +7)]
repeat bit_count from 0 to 7 'repeat for each bit read off of sensor
average += BYTE[sensorValue][bit_count] * ((bit_count * 2) + 2) 'check each bit, add weighted value
average_denom += BYTE[sensorValue][bit_count] 'determine how many bits were high to determine total number calculate in average
return average /= average_denom 'calculate average of weighted bits from sensor
Thanks for the suggestions...

Comments
bit_count = 0 sensor0_pin = 0 [COLOR="orange"]outb[0..7][/COLOR] = ina[(sensor0_pin..sensor0_pin +7)] repeat bit_count from 0 to 7 'repeat for each pin's bit read off of sensor average += [COLOR="orange"]outb[/COLOR][bit_count] * ((bit_count * 2) + 2) 'check each bit, add weighted value average_denom += [COLOR="orange"]outb[/COLOR][bit_count] 'determine how many bits were high to determine total number calculate in average return average /= average_denom 'calculate average of weighted bits from sensorAs an alternative you'd have to extract the bits from the byte, e.g. (value >> bit_number) & 1.WOW. I am gonna have to squire that idea away for stuff later... Seems perhaps you have bumped into this case before?
Tested a couple different ways, and worked for what I was thinking. I had to change the methods around. Going to keep thinking about it.
Thanks. Solved.