PICAXE-18X Input / Output

copyright, Peter H Anderson, Baltimore, MD, May, '04


This discussion focuses on output and input associated with the PICAXE-18X.

With the PICAXE-18X, eight pins are dedicated as outputs and five pins are dedicated as inputs. The Outputs are associated with the PIC's PORTB and the inputs with PORTC. (I have attempted to poke the corresponding TRIS registers to see if I could change the direction of a pin, without success. The PICAXE monitor is continually making PORTB all outputs and PORTA, all inputs).

When the keyword PINS is used on the right hand side of the equation, it refers to all of the Inputs. When used on the left hand side of the equation, it refers to all of the outputs.

   x = PINS		' read all of the inputs

   PINS = %10101001	' output 1010 1001
Individual inputs may be read using Pin0, Pin1, Pin2, Pin6 or Pin7.

Thus;

   x = Pin2		' note that x is now either a 0 or a 1

   if Pin7 = 0 Then DoSomethingElse
There are times when one wishes to read multiple inputs;
   x = PINS & %00000111	' note that x is now the state of Pin2, Pin1, Pin0
Consider, reading Pin7 and Pin6;
   x = PINS & %11000000 / 64
The AND operation sets bits 5, 4, 3, 2, 1 and 0 to zero. The / 64 shifts the result, six places to the right. The result, x, is 0, 1, 2 or 3 depending on the state of the inputs on Pin7 and Pin6.

Individual outputs may be set to a logic one or zero;

   High 6
   Low 0
However, individual bits may also be changed by peeking location $06 which is the current state of PORTB.
   Peek $06, CurrentPortB
   Pins = CurrentPortB & %11110000 | x
The current state of PORTB is read into byte variable CurrentPortB. The lowest four bits are zeroed and variable x is ored into the low four bit locations.

Individual bits may be inverted using the exclusive or operator;

   Peek $06, CurrentPortB
   Pins = CurrentPortB ^ %00000100	' invert output 2