Name:
let
Syntax:

{LET} variable = {-} value ?? value ...

Variable - will be operated on.

Value(s) - are variables/constants which operate on variable.

{LET} dirs = value {LET} dirsc = value

Value(s) - are variables/constants which operate on the data direction register.

{LET} dirsA = value {LET} dirsB = value {LET} dirsC = value {LET} dirsD = value

Value(s) - are variables/constants which operate on the data direction register for a specific port.

{LET} pins = value {LET} pinsc = value

Value(s) - are variables/constants which operate on the output port.

{LET} pinsA = value {LET} pinsB = value {LET} pinsC = value {LET} pinsD = value

Value(s) - are variables/constants which operate on a specific output port.

Description:

 

Perform variable manipulation (wordsize-to-wordsize).

Maths is performed strictly from left to right. The ‘let’ keyword is optional.

The microcontroller supports word (16 bit) mathematics. Valid integers are 0 to 65535. All mathematics can also be performed on byte (8 bit) variables (0-255). The microcontroller does not support fractions or negative numbers. However it is sometimes possible to rewrite equations to use integers instead of fractions, e.g. let w1 = w2 / 5.7 is not valid, but let w1 = w2 * 10 / 57 is mathematically equal and valid.

The mathematical functions supported by all parts

+ ; add
- ; subtract
* ; multiply (returns low word of result)
** ; multiply (returns high word of result)
/ ; divide (returns quotient)
// (or %) ; modulus divide (returns remainder)
MAX ; limit value to a maximum value
MIN ; limit value to a minimum value
AND & ; bitwise AND
OR ; bitwise OR (typed as SHIFT + \ on UK keyboard)
XOR ^ ; bitwise XOR (typed as SHIFT + 6 on UK keyboard)
NAND ; bitwise NAND
NOR ; bitwise NOR
ANDNOT &/ ; bitwise AND NOT (NB this is not the same as NAND)
ORNOT / ; bitwise OR NOT (NB this is not the same as NOR)
XNOR ^/ ; bitwise XOR NOT (same as XNOR)

The X1 and X2 parts also support

<< ; shift left
>> ; shift right
*/ ; multiply (returns middle word of result)

The X1 and X2 parts also support these unary commands

SIN ; sine of angle (0 to 65535) in degrees (value * 100 is returned)
COS ; cosine of angle in degrees (value * 100 is returned)
SQR ; square root
INV ; invert
NCD ; encoder (2n power encoder)
DCD ; decoder (2n power decoder)
BINTOBCD ; convert binary value to BCD
BCDTOBIN ; convert BCD value to binary
REV ; reverse a number of bits
DIG ; return a BCD digit

All mathematics is performed strictly from left to right. On X1 and X2 parts it is possible to enclose part equations in brackets e.g. let w1 = w2 / ( b5 + 2).  On all other chips it is not possible to enclose part equations in brackets e.g. let w1 = w2 / ( b5 + 2) is not valid. This would need to be entered as an equivalent e.g. let w1 = b5 + 2 let w1 = w2 / w1.


LET dirs / dirsA / dirsB / dirsC / dirsD = value

Some microcontrollers allow inputs to be configured as inputs or outputs. In these cases it is necessary to tell the microcontroller which pins to use as inputs and/or outputs (all are configured as inputs on first power up). There are a number of ways of doing this:

1) Use the input/output/reverse commands.

2) Use an output command (high, pulsout etc) that automatically configures the pin as an output.

3) Use the let dirs = statement. When working with this statement it is conventional to use binary notation. With binary notation pin 7 is on the left and pin 0 is on the right. If the bit is set to 0 the pin will be an input, if the bit is set to 1 the pin will be an output. Note that the 8 pin PICAXE have some pre-configured pins (e.g. pin 0 is always an output and pin 3 is always an input). Adjusting the bits for these pins will have no effect on the microcontroller.

For M2 and X2 parts which have more than one configurable port, you must use port specific commands by adding the letter of the port after dirs, eg LET dirsB = %00000010 to make pin1 of port B an output, and all other pins inputs.

Configure pins as inputs or outputs (let dirs =) (08/08M/08M2)
Configure pins as inputs or outputs on portc (let dirsc =) (14M)
Configure pins as inputs or outputs on portc (let dirsc =) (28X/40X)
Configure pins as inputs or outputs on portc (let dirsc =) (28X1/40X1)

 

LET pins / pinsA / pinsB / pinsC / pinsD = value   

 Set/clear all outputs on the main output port (let pins = ), or on a specific port (let pinsA/pinsB/pinsC/pinsD =).

High and low commands can be used to switch individual outputs high and low. However when working with multiple outputs it is often convenient to change all outputs simultaneously. When working with this statement it is conventional to use binary notation. With binary notation output7 is on the left and output0 is on the right. If the bit is set to 0 the output will be off (low), if the bit is set to 1 the output will be on (high).

Do not confuse the input port with the output port. These are separate ports on all except the 8 pin PICAXE. The command let pins = pins means 'make the output port the same as the input port'. Note that on devices that have input/output bi-directional pins (08 / 08M), this command will only function on pins configured as outputs. In this case it is necessary to configure the pins as outputs (using a let dirs/dirsX = command) before use of this command.

Applies To:
All
See Also:
Related Create:
    Share:
    Print:

    Play incrementing tone

    Play a sound with increasing pitch (the value of b0). When the pitch is > 50, reset b0 back to 10 and start again.

    Code Example:
    main:	inc b0			; increment b0
    	sound B.7,(b0,50)	; make a sound
    	if b0 > 50 then rest	; after 50 reset
    	goto main		; loop back to start
    
    rest:	let b0 = b0 max 10	; limit b0 back to 10 as 10 is the maximum value
    	goto main 		; loop back to start
    Copy Code Submit an Example

    Using dirs and pins

    This code uses let dirs to set two of the pins to outputs, then let pins to turn those two outputs high.

    Code Example:
    main:	let dirs = %00000011	; switch pins 0 and 1 to outputs
    	let pins = %00000011	; switch on outputs 0 and 1
    Copy Code Submit an Example

    Submit Your Own Code!

    You must be logged in to submit code examples. Login now.

    Comments

    • Login to leave a comment.