Shop OBEX P1 Docs P2 Docs Learn Events
changing individual bits in a byte? — Parallax Forums

changing individual bits in a byte?

Brian CarpenterBrian Carpenter Posts: 728
edited 2011-08-04 21:09 in Propeller 1
I have a challenge that i cant get past at the moment.

here is a snippet of code....
CON

'BIT MASKS
 ZoneMask_0 = $0000_0001
 ZoneMask_1 = $0000_0010
 ZoneMask_2 = $0000_0100
 ZoneMask_3 = $0000_1000
 ZoneMask_4 = $0001_0000
 ZoneMask_5 = $0010_0000
 ZoneMask_6 = $0100_0000
 ZoneMask_7 = $1000_0000 

  ADDRRLY = 56        'PCF8574A I2C Address (A0, A1, A2 all pulled low)
  ADDROPTO =63
  CLS  = 0
  PresetOnTime = 30

VAR
  long  longHIGH,longLOW,MM_DD_YYYY,DW_HH_MM_SS 'Expected 4-contigous variables
  byte  Buffer[BUFFER_SIZE]
  LONG  currentTime
  Long  ontime
  Long  offtime
  byte  Today
  byte  ZoneTimerFlag   ' if the bit is high, it means that the timezone states that it should be on
  byte  ZoneMotionFlag   'if the bit is high, it means that the motion was triggered in this zone.  requesting it to turn on.
  long  ZoneMotionTimer[6] ' this is to store the times for the motions
  long  MotionScan
  byte  RelayState

PUB MotionStatus | RunTil,  currentH, currentM

 ' Read inputs
  MotionScan:= IO.IN(ADDROPTO)
  currentH := (RTC.gethour) * 100
  currentM := (RTC.getminutes)
  PlxST.str(string(" OPTO READING = "))
  PlxST.bin(MotionScan,8)
  PlxST.char(13)
  
 
  IF (MotionScan & |< 0)
        'RunTil := currentMinute + 30
    IF currentm > PresetOnTime
      currentm -= PresetOnTime
      currenth += 100
      PlxST.dec(currentm)
    RunTil := currentm + PresetOnTime
    RunTil += currentH
    IF RunTil > 2400
      RunTil -= 2400
    ZoneMotionTimer[0] := RunTil
    ZoneMotionFlag |= ZoneMask_0
  

  currentH := (RTC.gethour) * 100
  currentM := (RTC.getminutes) 

  IF (MotionScan & |< 1)  
        'RunTil := currentMinute + 30
    IF currentm > PresetOnTime
      currentm := currentm - PresetOnTime
      currenth := currenth + 100
      PlxST.dec(currentm)
    RunTil := currentm + PresetOnTime
    RunTil := RunTil + currentH
    IF RunTil > 2400
      RunTil := RunTil - 2400
    ZoneMotionTimer[1] := RunTil
    ZoneMotionFlag |= ZoneMask_1

  currentH := (RTC.gethour) * 100
  currentM := (RTC.getminutes) 

  IF (MotionScan & |< 2)  
        'RunTil := currentMinute + 30
    IF currentm > PresetOnTime
      currentm := currentm - PresetOnTime
      currenth := currenth + 100
      PlxST.dec(currentm)
    RunTil := currentm + PresetOnTime
    RunTil := RunTil + currentH
    IF RunTil > 2400
      RunTil := RunTil - 2400
    ZoneMotionTimer[2] := RunTil
    ZoneMotionFlag |= ZoneMask_2


  currentH := (RTC.gethour) * 100
  currentM := (RTC.getminutes) 

  IF (MotionScan & |< 3)  
        'RunTil := currentMinute + 30
    IF currentm > PresetOnTime
      currentm := currentm - PresetOnTime
      currenth := currenth + 100
      PlxST.dec(currentm)
    RunTil := currentm + PresetOnTime
    RunTil := RunTil + currentH
    IF RunTil > 2400
      RunTil := RunTil - 2400
    ZoneMotionTimer[3] := RunTil
    ZoneMotionFlag |= ZoneMask_3        

  PlxST.str(string(" ZoneMotionFlag = "))
  PlxST.bin(ZoneMotionFlag,8)
  PlxST.char(13)



There is the setup...
What i am expecting to have happen in this code is the following-
    First the Input board is sampled and it populates variable MotionScan (this works correctly)
    Then it starts with the first IF statement where we do a test on the state of the LSB, Bit 0 of the byte MotionScan. If it is a 1, we capture the time, add a preset amount to it (30) and then set bit 0 high on the byte ZoneMotionFlag.
    Then the next IF where it is checking the next bit, bit_1 in the MotionScan and then proceeds like above. This is where we start to have problems. When This scenario is true and it goes to create bit_1 high on variable ZoneMotionFLag.
But this does not happen. Instead, bit_4 goes high.
For example if bit_0 and bit_1 on MotionScan were high, i get $0001_0001 as the output when i display variable ZoneMotionFlag.
I then went on to add the IF statements for bit_2 and bit_3 of the test against MotionScan. They dont write to ZoneMotionFlag correctly either.

ANY IDEAS as to why this is not working the way i had hoped?

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-08-04 21:04
    I think you want your zone masks defined as binary (%) instead of hex ($).
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2011-08-04 21:09
    WOW, it just take another set of eyes. You are exactly right. Let me give it a try
Sign In or Register to comment.