if bit set alternative method for use with 14M2

mortifyu

New Member
Hello ALL,

I have almost scratched a hole in my head thinking of how to get a similar result that comes from using IF BIT SET command referenced to a TIME variable, but with a 14M2.

I am hoping to make an output HIGH if a particular bit of a TIME variable value is set and LOW if it is clear. The purpose of trying to do this is to flash an LED as a timer is running. I anticipate depending on the bit tested within the programming, my LED should flash faster or slower.

Code:
main:
let w0 = time + 10

secondary:
if w0 bit 1 set then
     high b.5
else
     low b.5
endif

goto secondary
I do know the above code won't work because 'if bit' command is not supported by 14M2's.


So come on Guru's, come out of the woodwork and please show me the simple mathematical way to do this.


Regards,
Mort.
 

hippy

Technical Support
Staff member
As Aries says; just moving a value into w0, w1, b0 through b3, sets the bit variables to reflect the variable's binary value.

Another alternative for flashing a LED with respect to time, for example, on for 10 seconds and off for 10 -
Code:
Low B.0 ; Or "Output B.0" or "dirB = 1"
Do
  pinB.0 = time / 10
Loop
So for the 0.25Hz flashing you want, 2 seconds on, 2 seconds off -
Code:
secondary:
  dirB.5 = 1                     ; Not needed if B.5 already an output
  pinB.5 = time / 2
  goto secondary
You can play clever tricks as well with frequency and duty. This will flash the LED on for one second in every 4 -
Code:
pinB.5 = time // 4 Max 1 ^ 1
 

mortifyu

New Member
You can play clever tricks as well with frequency and duty. This will flash the LED on for one second in every 4 -
Code:
pinB.5 = time // 4 Max 1 ^ 1

Thanks for the TIP's Guru's.

Your suggestions have worked a treat for my application. Thank you very much.

Hippy, may I please ask you to explain the above CODE in detail. This is what I have used, but don't fully understand the equation.

Thanks in advance mate.


Regards,
Mort.
 

hippy

Technical Support
Staff member
Code:
pinB.5 = time // 4 Max 1 ^ 1
Hippy, may I please ask you to explain the above CODE in detail. This is what I have used, but don't fully understand the equation.
No problem. The 'time' increments, and the "// 4" gets the remainder after dividing by 4, so that is cyclic as per ...
Code:
time      time // 4
1808      0
1809      1
1810      2
1811      3
1812      0
1813      1
So that gives us our 'four second' grouping. By using "Max 1" we can limit the numbers to being 0 or 1 ...
Code:
time       time // 4 Max 1
1808   0   0
1809   1   1
1810   2   1
1811   3   1
We could output that using 'pinB.5 = time // 4 Max 1" but that would be on for three seconds out of four. What we want is the inverse of that and that can be achieved by using an exclusive-or, "^ 1", to swap 0 and 1 over ...
Code:
time           time // 4 Max 1 ^ 1
1808   0   0   1
1809   1   1   0
1810   2   1   0
1811   3   1   0
So now, we get the LED set only for a single second period during the four second period.
 

mortifyu

New Member
Code:
pinB.5 = time // 4 Max 1 ^ 1
No problem. The 'time' increments, and the "// 4" gets the remainder after dividing by 4, so that is cyclic as per ...
Code:
time      time // 4
1808      0
1809      1
1810      2
1811      3
1812      0
1813      1
So that gives us our 'four second' grouping. By using "Max 1" we can limit the numbers to being 0 or 1 ...
Code:
time       time // 4 Max 1
1808   0   0
1809   1   1
1810   2   1
1811   3   1
We could output that using 'pinB.5 = time // 4 Max 1" but that would be on for three seconds out of four. What we want is the inverse of that and that can be achieved by using an exclusive-or, "^ 1", to swap 0 and 1 over ...
Code:
time           time // 4 Max 1 ^ 1
1808   0   0   1
1809   1   1   0
1810   2   1   0
1811   3   1   0
So now, we get the LED set only for a single second period during the four second period.

Impressive!

Thank you for that clarification Hippy. You are a true PICAXE legend.



Regards,
Mort.
 
Top