PORT P0..P4

 
Syntax

Pn ( expression )      '  where n is 0 through 4
Description


Px allows you to read or write individual pins using the NXP assigned port and pin number.  When Pn (expression) is read, the logic state of the pin corresponding to expression is returned.

When assigning a value to Pn(expression), then pin expression is set to that value if that pin has been assigned to be an output by writing to FIOxDIR. 

When read Pn(x) returns a 0 or -1. Why -1 and 0?  The main reason is that operations of operators like NOT are assumed to be bitwise until there is a Boolean operation in the expression, and NOT 0 is equal to -1.  When setting a pin state with Pn(x) = 0 then the pin becomes low, any other value and the pin becomes high, so Pn(x) =1 and Pn(x) = -1 both set the pin high.

These pin numbers correspond to the port pin assignments from NXP. 

This feature is part of the compiler and requires version 8.04c or later.  It has not been added to the on-chip compiler of the ARMweb.

Example

 

On the SuperPRO and PROplus:

#define FIO1DIR  *&H2009C020            ' or use #include <LPC17xx.bas>

' Set pin 9 as an output and drive it high
FIO1DIR = FIO1DIR or (1<<9)
P1(9) = 1

P1(9) = NOT (P1(9) and (1 <<9))   ' invert pin P1.9 -- works as you can always read the state of a pin

' read value of P1.8
x = P1(8)

' change bit 9 back to an input
FIO1DIR = FIO1DIR and NOT(1<<9)

On the ARMweb or DINkit:

#define FIO1DIR        *&H3FFFC020    ' or use #include <LPC21xx.bas>   
#define SCB_SCS     *&HE01FC1A0


SCB_SCS  = 3            ' required to enable port1 for firmware before 7.47


' Set pin 9 as an output and drive it high
FIO1DIR = FIO1DIR or (1<<9)
P1(9) = 1

P1(9) = NOT (P1(9) and (1 <<9))   ' invert pin P1.9 -- works as you can always read the state of a pin

' read value of P1.8
x = P1(8)

' change bit 9 back to an input
FIO1DIR = FIO1DIR and NOT(1<<9)

Or you can also define a macro, keeping with the IN/OUT BASIC style

#include <LPC17xx.bas>   

#define OUTPUT0(pin)   FIO0DIR = FIO0DIR or (1 << pin)
#define INPUT0 (pin)     FIO0DIR = FIO0DIR and not(1 << pin)
#define OUTPUT1(pin)   FIO1DIR = FIO1DIR or (1 << pin)
#define INPUT1 (pin)     FIO1DIR = FIO1DIR and not(1 << pin)
#define OUTPUT2(pin)   FIO2DIR = FIO2DIR or (1 << pin)
#define INPUT2 (pin)     FIO2DIR = FIO2DIR and not(1 << pin)
#define OUTPUT4(pin)   FIO4DIR = FIO4DIR or (1 << pin)
#define INPUT4 (pin)     FIO4DIR = FIO4DIR and not(1 << pin)

OUTPUT2(10)

while 1
  x=x+1
  P2(10) = x and 1           ' blinky for the SuperPRO and PROplus
  wait(500)
loop

Differences from other BASICs

See also