Let's Make Robots!

picaxe 28x1 input/output direction

Hi,

I am using a picaxe 28x1 and I am developing new code like crazy. In one of my sub routines, I need to reconfigure one of the input pins (pin 7 in port C) to output mode. Then pull it low. Then wait a bit. Then make it high impedance again. That is to say, it must become an input again.

This code would do that:
down:  
  low portc 7   ' by bringing the pin low it automatically becomes an output pin
  pause 1000   ' wait a moment
  dirsc = %00000000   ' this blindly sets all pins to output mode
return

However, I do not want to set the input direction for ALL pins in portc, just the one I am using here. For all I know, other routines are making decisions about the other pins' directions. And I need my sub routine to respect that.

Now, if I would have all the direction bits for port C available in one byte (b13 for example), I could simply change the bit corresponding with my pin in that byte and use the byte instead of %00000000. Like this:
down:  
  low portc 7   ' by bringing the pin low it automatically becomes an output pin
  pause 1000   ' wait a moment
  b13 = dirsc  ' this function does not exist in picaxe 28x1 basic 8-(
  b13 = b13 AND %01111111  ' remember directions from all other pins, make bit 7 zero regardless
  dirsc = b13  ' set the new situation to port C
return

The simulator proves this second code to be valid in an X2, but not in an X1. Who knows a way to get me what I want, without upgrading the chip?

(I checked; the command "input C.7" does also not work on an X1.)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Perhaps I am missing something because I'm flat out and just skimming this but the dirc command does exist for the 28X1. I have used it before.

Rather than use a variable for storage why not use a memory location? Check the peek/poke commands.

dirsc on an X1 is used to set the direction, not to sense it.

With the disadvantages you mention. Must sacrifice one byte for housekeeping. And must make sure to consequently change that byte every time you are meddling with directions. Just the kind of thing you would want a special variable for.

Like the X2 has.

Kinda makes you wonder if they have that on X2s because it can be done with PICs they use in X2s or if they just left it out from X1 (among other features) to sell X2s. I'd bet on the latter :-)

 

Can you spare one byte for dir storage? If you can then do it like this (if I understood you correctly):

b13 = %01010101 ' Set portc dirs like this every time you change them
dirsc = b13     ' Just don't mess with b13 in any other place
                ' (Except when you want to change dirs)

main:
  ' ...
  gosub down
  ' ...
goto main

down: 
  low portc 7   ' by bringing the pin low it automatically becomes an output pin
  pause 1000   ' wait a moment
  b13 = b13 AND %01111111  ' remember directions from all other pins, make bit 7 zero regardless
  dirsc = b13  ' set the new situation to port C
return

That's the only way that comes to my mind at this time.

BTW, I noticed that my simulator doesn't show directions correctly on all pins (I'm using prog editor v5.2.7 / syntax dll 262144).