Adding values one at a time and sending them serially
Hi, I have 5 sensors attached to a PIC. I need to get one variable, word size (I suppose) and load the number(s) of the triggered sensor in this variable.
I have 5 bit size variables which are set when the corresponding numbered sensor gets triggered.
I want something like this:
Sens1 Var bit
Sens2 Var bit
Sens3 Var bit
Sens4 Var bit
Sens5 Var bit
If lets say sensor 1 and 5 gets activated, Sens1 and Sens5 will be set to 1. So I want to load the value 15 in my variable. Similarly if sensor 2,3,5 gets activated, I want to load 235 in my variable. The max value will be 12345 if all sensors get triggered.
Re: Adding values one at a time and sending them serially
Hi,
This is untested but I think it'll do what you want
Code:
Sens VAR BIT[5] ' 5bit array for sensor bits
Sens1 VAR Sens[0] ' Aliases to the array bits.
Sens2 VAR Sens[1]
Sens3 VAR Sens[2]
Sens4 VAR Sens[3]
Sens5 VAR Sens[4]
Multiplicator VAR WORD
Value VAR WORD
i VAR BYTE
CalculateValue:
Multiplicator = 1
Value = 0
For i = 0 to 4 ' Index the 5 sensor bits of the array
IF Sens[i] THEN
Value = Value + ((i+1) * Multiplicator)
Multiplicator = Multiplicator * 10
ENDIF
NEXT
/Henrik.
Re: Adding values one at a time and sending them serially
Hi Megahertz, the code below is tested for a PIC12F675, it's the Henrik's code but with 3 minor modifications (Sens var type, Aliases index, and inverted counter)
All the credit is to Mr. Olsson
Code:
#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF
#ENDCONFIG
DEFINE OSCCAL_1K 1 'Internal Clock Auto-cal (for reliable RS232)
ansel=0 'Don't use ADC's
cmcon=7 'Don't use analog comparators
DEFINE DEBUG_REG GPIO 'Set Debug pin port
DEFINE DEBUG_BIT 0 'Set Debug pin bit
DEFINE DEBUG_BAUD 2400 'Set Debug baud rate
DEFINE DEBUG_MODE 0 'Set Debug mode: 0 = true, 1 = inverted
Sens VAR Byte ' 5bit array for sensor bits
Sens1 VAR Sens[1] ' Aliases to the array bits.
Sens2 VAR Sens[2]
Sens3 VAR Sens[3]
Sens4 VAR Sens[4]
Sens5 VAR Sens[5]
Multiplicator VAR WORD
Value VAR WORD
i VAR BYTE
Begin:
sens1 = 1 'Sensor 1 is high
sens2 = 0 'Sensor 2 is low
sens3 = 1 'Sensor 3 is high
sens4 = 0 'Sensor 4 is low
sens5 = 1 'Sensor 5 is high
CalculateValue:
Multiplicator = 1
Value = 0
For i = 5 to 1 step -1 ' Index the 5 sensor bits of the array
IF Sens[i] THEN
Value = Value + (i * Multiplicator)
Multiplicator = Multiplicator * 10
ENDIF
NEXT
debug "Sensors : ",#value,13,10
pause 1000
goto begin
Ivan.
Re: Adding values one at a time and sending them serially
Here's another way to do it. Fully tested.
Code:
Sens1 Var bit
Sens2 Var bit
Sens3 Var bit
Sens4 Var bit
Sens5 Var bit
Value VAR WORD
Idx VAR BYTE
Abit VAR BIT
Value = 0
FOR Idx = 1 TO 5
LOOKUP2 Idx-1,[Sens1,Sens2,Sens3,Sens4,Sens5],Abit
IF Abit THEN Value = Value * 10 + Idx
NEXT Idx
Or, if you change the bits to an array similar to what henrik did, you can do this ...
Code:
Senser VAR BYTE
Sens1 Var Senser.0
Sens2 Var Senser.1
Sens3 Var Senser.2
Sens4 Var Senser.3
Sens5 Var Senser.4
Value VAR WORD
Idx VAR BYTE
Value = 0
FOR Idx = 1 TO 5
IF Senser.0(Idx-1) THEN Value = Value * 10 + Idx
NEXT Idx
Re: Adding values one at a time and sending them serially
Thanks. Another problem resolved :)