Adding values one at a time and sending them serially


+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Location
    London
    Posts
    251

    Question 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.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,415


    Did you find this post helpful? Yes | No

    Default 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.

  3. #3
    Join Date
    Aug 2011
    Location
    Guadalajara, México
    Posts
    28


    Did you find this post helpful? Yes | No

    Default 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.
    My English doesn't sucks, it's just fugly...

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default 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
    Last edited by Darrel Taylor; - 20th August 2013 at 19:59.
    DT

  5. #5
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default Re: Adding values one at a time and sending them serially

    Thanks. Another problem resolved

Similar Threads

  1. Why does this PBP statement post the wrong time values?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 22nd November 2011, 03:10
  2. Question about sending data serially
    By FromTheCockpit in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 1st August 2011, 01:21
  3. Replies: 1
    Last Post: - 4th June 2010, 03:34
  4. Replies: 9
    Last Post: - 8th October 2008, 11:15
  5. it's possible to drive four max7221 serially connected?
    By lutherblissett in forum General
    Replies: 0
    Last Post: - 23rd November 2005, 18:38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts