Menu

how to get 8 PIC inputs merged in to a single unique value eg BCD

Help
Steve33
2015-11-08
2015-11-09
  • Steve33

    Steve33 - 2015-11-08

    Hi
    First I must thank the GC team for the good work they have been doing on improving both GC itself and the Help section.

    I have working PIC (16LF1828) hardware that was created with GC, It takes Alarm inputs in and sends them to the serial port where they are sent via RF to a master unit which updates a excel picture.
    Currently the inputs are read on wake up and a series of If-then statements used to arrive at the correct number to be sent. While this fine for a few inputs I would like to come up with a better approach for say 8 Inputs.

    I expect this type of issue has been resolved before by more experienced people but nothing has come up in my forum searches.

    My first thoughts were to take the 8 inputs add them in binary and then send them, but was unable to add in binary. Not all inputs are on a single port.

    I have also tried to create a string from the various inputs and then convert them to binary using VAL. but this did not work either.

    There is something very basic missing from my understanding so any help/guidance would be appreciated.

    Thanks

     
  • Anobium

    Anobium - 2015-11-08

    Thank you.

    If you are simply sending the status of 8 ports I would set the bits of a special byte that you can transfer. Byte.0, byte.1 etc. You can set any bit by set byte.0=1

    But, a little more information about your data stream may helpif I have this wrong.
    Anobium

     
    • Steve33

      Steve33 - 2015-11-08

      Thanks for the reply.
      I need to think about your reply a bit this is an area in GC I have not used before, Have you any examples ?

      My Data stream is 12 Bytes Long, It has a Start & Stop byte, identification, bytes, two A To D Bytes and 3 Alarm Bytes the other two bytes are reserved for re-routing connection paths.
      The Alarm bytes are the ones I am trying to fill; I have one for setting the two outputs on the slave device, and two bytes for Alarms from the slave. One byte is for the 8 Input Alarms, the other for internal Alarms such as Battery status and unit temperature.
      It works fine with two Alarms in each of the two Alarm Bytes, what I am looking to achieve is a byte that is a unique number representing the 8 Alarms, I can then decode this in VB net at the master end into the individual Alarms, BCD would be easy but anything else will do.
      Best regards

       
    • Steve33

      Steve33 - 2015-11-09

      Hi Anobium
      Thank you very much for the for the help it works a treat and is easy to understand.
      I just did not know you could set bits within a byte this easily.
      I have just tried a 4 Alarm version code and tested result below.

      SUB Inputs1T04
      'Alarm1In to Alarm4 mapped to ports earlier at programme start
      'AlmsByteA Dim as byte at programme start
      'SendArray(6) is the byte number for one of the Alarm banks transmitted serially

      AlmsByteA = 0 'reset
      If Alarm1In = 1 then AlmsByteA.0 = 1 'Sets AlmsByteA byte Bit 0 to 1
      If Alarm2In = 1 then AlmsByteA.1 = 1 'Sets AlmsByteA byte Bit 1 to 1
      If Alarm3In = 1 then AlmsByteA.2 = 1 'Sets AlmsByteA byte Bit 2 to 1
      If Alarm4In = 1 then AlmsByteA.3 = 1 'Sets AlmsByteA byte Bit 3 to 1

      SendArray(6) = AlmsByteA + 48 'Added 48 For backwards Compatibility reasons

      End sub

      'No BCD Conversation required

      Test Results as expected

      Received RX Codes My packes are sent and Received in bytes not Text
      RX Code Count Alarm-4 Alarm-3 Alarm-2 Alarm-1
      48 0 0 0 0 0
      49 1 0 0 0 1
      50 2 0 0 1 0
      51 3 0 0 1 1
      52 4 0 1 0 0
      53 5 0 1 0 1
      54 6 0 1 1 0
      55 7 0 1 1 1
      56 8 1 0 0 0
      57 9 1 0 0 1
      58 10 1 0 1 0
      59 11 1 0 1 1
      60 12 1 1 0 0
      61 13 1 1 0 1
      62 14 1 1 1 0
      63 15 1 1 1 1

      So now we have a unique code for every Alarm State with very little code. In this example 16. With 8 bits it give a max of 8 Alarms 256 unique combinations.
      Easy to process at the far end with a PC.
      I expect it could be simplified further but it works this way for me.
      Thanks again.

      Steve

       
  • kent_twt4

    kent_twt4 - 2015-11-09

    Alternate solution (Not Tested): " If " you could somehow arrange the 8 alarm bits into two nibbles (RC0-RC3, RB4-RB7, etc.), on two ports? Then you could bit mask the read portX value and add them together. If you ended up with two low nibbles, then you could use the swap4 command as an intermediate step.

    Alarm = PortC & b'00001111'
    Alarm = Alarm + (PortB & b'11110000')
    
     
  • Anobium

    Anobium - 2015-11-09

    I have just thought we have two functions that support BCD.

    DecToBcd_GCB
    BcdToDec_GCB

    These functions are in the help file.

     
  • Chris Roper

    Chris Roper - 2015-11-09

    I am a little confused about the relavance of BCD in this situation. Surely Binary would be better if you need to report 8 Allarm states that would be 8 bits or one Byte with each bit being the on/off state of the coresponding Allarm.

    I am not familiar with VB.NET but if it has BitWise functions then that is definately the way to go.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.