Possible simulator bug

SD2100

New Member
Below is part of some code that I have been using without
any problems, now while making some changes and running it
in the simulator I have come across what looks to be a bug
in the simulator. With the "bit0=1" line removed pressing
pin1 (pins=2) then pin4 goes high now pressing pin2 (pins=6)
pressing pin1 again pins now equals 4 as expected.
With the "bit0=1 line inserted and pressing the input buttons
in the same sequence pin1 (pins=2) pin2 (pins=6) then pressing
pin1 again instead of pins=4 pins now equals 20.
Watching with the simulator slowed down pins equals 4 for a
moment then changes to 20, it appears pins is seeing pin4 as
an input. Pressing pin1 ON then OFF again fixes it and pins=4
as it should. I'm using PE 5.2.1

Code:
#Picaxe 08m
do
   if pins = 2 then
      high 4
      do
         if pins = 4 then
            bit0 = 1 'Any variables here cause a problem
            exit
         elseif pins = 0 then
            exit
         end if
      loop
   end if
loop
 

westaust55

Moderator
Picaxe 08m Io

Do not confuse the pins with respect to the input port and the output port. These are separate ports on all except the 8 pin PICAXE.

With the 8 physical pin PICAXE’s (08 and 08M), pins is a single register/port which holds the value of all IO. So pins equates to all 5 IO pins.
Thus when you set pin4 high pins has the 4th bit in the one and only register high. hence pins = %xxx1 xxxx.

If while you are stepping thru the program and you also watch the dirs register, you will also see that bit 4 of that register is set to a 1 when the high 4 command is executed.

By comparison, for the larger PICAXE there are separate registers for Inputs and outputs and b0 = pins and pins = b0 refere to input (pns) and output (pins) respectively.

Not sure I fully understand what you are trying to achieve, but is this piece of code something like what you are trying to do?
Code:
#Picaxe 08m
do
  do while pin1 = 0
  loop
  high 4
  do while pin2 =0 OR pin1 = 1
  loop
  bit0 = 1
loop
 
Last edited:

SD2100

New Member
westaust55
Thus when you set pin4 high pins has the 4th bit in the one and onlly register high. hence pins = %xxx1 xxxx.
Bit4 isn't set in the pins register when pin4 is high.

If while you are stepping thru the program you watch the dirs register, you will also see that bit 4 of that register is set to a 1 when the high 4 command is executed.
Bit 4 is set in the dirs register when pin4 goes high but not in the pins register

The code I have works, it's when a variable is added there is a problem,
if no variable is used pins=4. If bit0=1 or w5=50 or whatever is included
pins=20. Why should including a line like "b2=25" change the pins register.
In your code put a break point at the line "bit0=1", run the code until it hits
the break point, at this stage pin4 is high and bit4 of the pins register is low
now step through the break point and bit4 of the pins register will be set
after "bit0=1" is executed, why is this so...
 

westaust55

Moderator
You are right in that the Simulator is not showing bit 4 going high in the pins register immediately the high 4 command is actioned. Seems not to happen until the program is leaving the loop.

however, operating correctly, when you issue a high 4, the PICAXE does set the dirs regiter bit 4 high to make pin 4 an output and then sets dirs bit 4 high to turn on the output.

That is explained in Manual 2 bottom half of page 12

with respect to your code and adding a variable:
Code:
#Picaxe 08m
[B]b5 = 1[/B]
do
   if pins = 2 then
      high 4
      do
         if pins = 4 then
            bit0 = [B]b5[/B] 'Any variables here cause a problem
            exit
         elseif pins = 0 then
            exit
         end if
      loop
   end if
loop
works as before and if you watch variable b0 it will change to a 1

but change b5 to a 2 like this . . .

Code:
#Picaxe 08m
[B]b5 = 2[/B]
do
   if pins = 2 then
      high 4
      do
         if pins = 4 then
            bit0 = b5 'Any variables here cause a problem
            exit
         elseif pins = 0 then
            exit
         end if
      loop
   end if
loop
because a bit can only be a 0 or a 1 and therefore variable b0 stays as 0.

I do not have an 08M to test it with but from my understanding, since you set pin 4 high and never turn if off,
the pins register would (after a high 4 is given) in reality always have
a value of 16 if no input 1 or 2 was high,
a value of 18 when only input 1 is high, and
a value of 20 when only input 2 is high.
IMHO, I think your program may not work as you intend in the simulator if it worked correctly and will not work in an 08M.
 
Last edited:

SD2100

New Member
Here's the full program, it's works in the simulator and in the 08m, if the "bit0=1" line is allowed to execute it dosn't drop into the reverse loop because pins=20 instead of 4. It's rather annoying. :D

Code:
;Reader for quadrature encoder

#picaxe 08m
symbol Clock     = 0
symbol Direction = 4

low Direction

do
   ;Forward
   if pins = 2 then
      high Direction
      do
         if pins = 4 then
           'bit0=1
           pulsout clock,1
           exit
        elseif pins = 0 then
           exit
        end if
      loop	
   end if

   ;Reverse
   if pins = 4 then 
      low Direction
      do
         if pins = 2 then
            'bit0=0
            pulsout clock,1
            exit
         elseif pins = 0 then
            exit
         end if
      loop	
   end if	
loop
 

hippy

Ex-Staff (retired)
As westaust55 says, the operation is correct and for the reasons explained ( although the simulator may be a little slow in updating its status reports ).

There are two workrounds -

1) Masking the 'pins' variable. This cannot be done within an IF but it can be done prior to the IF using a separate variable ...

b0 = pins & %00000110
If b0 = 4 Then

2) Performing the test on combinations of single pins ...

If pin1 = 0 And pin2 = 1 Then
 

SD2100

New Member
Ok I can understand all that but it just seems strange in the simulator to turn an output ON and it dosn't set that bit in the pins register not straight away and not with a delay but not at all. The program can run happily until somewhere else something unrelated like "bit0=1" is executed then the pins register is updated. :rolleyes:


Thanks westaust55 & Hippy for your help.
 

westaust55

Moderator
pins register bug in PE simulator for 08M

Phil,

There are a few issues here:

1. Yes, there is a bug or delay in the PE simulator which does not show the state of outputs ( at least for output on pin 4) immediately when the output is made high.
Maybe Rev Ed can look into and correct that within the PE for the 08M.

2. To say that the 4th bit in the pins register/variable never goes high is not entirely the case. That is why you ultimately see the value of 20 instead of the 4 you are expecting. However the correct value for pins while output 4 is high and input 2 is high will be 20.

3. While you say that your program is working correctly in an actual 08M chip, it cannot be the case as it is written and posted previously because you are looking for pins = 4 but as bit 4 is also high you will never see the value 4. Your simulation is working in part due to the bug mentioned in 1 above.

You will definitely need to modify the program as both hippy and I have suggested. Masking bit 4 as hippy suggests is the simplest solution.

Apologies if this sounds abrupt, but it is how the program must be to work corectly.
 
Last edited:
Top