HP 48 tutorial - programming"

HP48 PROGRAMMING PRINCIPLES

An HP48 program, like any other program consists of a few basic components:

CHECK SETTINGS

INPUT DATA

COMPUTE RESULTS

OUTPUT AND DISPLAY RESULTS

The HP48 program is a series of commands within « » delimiters. A program

is itself an object as described above and is stored as a global variable.

Other programs (or sub-programs) can be called from the main program (as with the HP41/42) by typing the name of the program in the program code. If the program to be called is resident in another directory, the path to that program must also be typed in.

CHECKING SETTINGS

Settings such as angle format and vector type must be confirmed before the main body of the program is started. These have been already described above. Failure to confirm these settings will result in incorrect output (e.g. if the calculator is in RADIANS mode).

INPUTTING DATA

Data can be entered into a program in several ways. The simplest way to input data is to leave values on the stack that are required for the program. To make a program user friendly it is recommended to prompt for data. This can be done in several ways:

INPUT (Single prompt)

« ……..

‘Prompt name’ This is what is displayed when the program runs

’’ This is the string where the data is entered

INPUT OBJ\-> These are the commands to display the prompt

………»

INPUT (Multiple prompt)

«………

‘Input title’

{’:Prompt 1:

:Prompt 2:

:Prompt 3:’

{ 1 0 } }

INPUT OBJ\->

……………»

Other input forms such as PROMPT, INFORM & CHOOSE are described more fully in the User guide.

USING LOCAL VARIABLES IN A PROGRAM

Local variables differ from global variables in that they only exist for the duration of the running of the program and so must be declared before they are used. Local variables have the advantage of not using up permanent memory space and are accessed much faster than global variables, ensuring much quicker program operation.

To create a local variable in a program, values (or objects) must first be placed on the stack which are to be stored in the local variables. The process is best described with a simple example:

A slope distance is on level 2 of the stack

A zenith angle is on level 1 of the stack

« ……..

\-> sd za « program code »

……. »

The \-> starts the local variable declaration sequence

sd and za are the declared local variables where the values on level 2 and level 1 respectively are stored.

sd and za only exist in the part of the program « program code »

It is recommended to use lower-case variable names to distinguish local variables from global variables, though this not essential. The \-> removes values off the stack and stores them in the local variable list after the \->. « delimiters MUST come immediately after the sequence of local variable names.

If a local variable is to be used outside the « program code » it must be ‘compiled’. A compiled local variable must start with a¬ e.g.¬a .

DEBUGGING A PROGRAM

A program can be single stepped through, displaying the stack at the completion of each command in the program. To SST through a program put the name of the program on level one i.e. `PROGRAM’

Press [PRG] [NXT] |RUN| |DBUG|

Press |SST| to single step through the program code

Press |SST¯| to single step through a sub-program

Press |NEXT| to preview the next two program commands

Press [ON] to alter the stack if necessary.

HALTING PROGRAMS

A program can be suspended at a certain point with the insertion of a HALT command. To continue program operation press [L-S] [ON] (CONT) or type CONT on the command line.

Any halted programs can be cancelled by typing KILL.

USEFUL PROGRAMMING COMMANDS

MATHEMATICAL

Trigonometry functions: SIN, ASIN, COS, ACOS, TAN, ATAN

Time and Angle functions:

DATE Puts Date in DD.MMYYYY format on level 1

TIME Puts the Time in HH.MMSSS format on level 1

\->DATE Takes level one in DD.MMYYYY format and sets date.

\->TIME Takes level one in HH.MMSSS format and sets the time.

DATE+ Adds two dates

DDAYS Gives the number of days between two dates

HMS+ Adds two times/angles in HH.MMSS format

HMS- Difference between two times/angles in HH.MMSS format

\->HMS Converts a time/angle in decimal to HH.MMSSSS format

HMS\-> Converts a time/angle in HH.MMSSSS format to decimal

D\->R Converts an angle in decimal degrees to radians

R\->D Converts an angle in radians to decimal degrees

Number manipulation commands:

IP Returns the Integer part of a number

FP Returns the Fractional part of a number

RND Takes a number on level 2 and rounds it off to level 1 decimal places

ABS Returns the absolute value of a number (always positive)

NEG Changes the sign of level 1

INV returns the inverse ( 1/x) of level one

^ Takes a number on level 2 and raises it to the power of level 1

360 MOD Takes an angle on level 1 and returns in the range (0-360)

Program control commands

OFF Turns the HP48 off !

CHR Returns the character from the code number on level 1

LASTARG Displays the last command argument

->STR Converts level 1 to a text string ’ '

STR-> The opposite

DISPLAYING OUTPUT

Results can be left on the stack (and tagged with a prefix title) (\->TAG)

or built up into a text string to be displayed in a presentable format

If values are tagged they can be untagged by the command TAG\->

DISPLAYING RESULTS AT END OF PROGRAM

« ……..

‘Output title’ Starts the display string

Value + adds the value to the string

CLLCD 1 DISP 3 FREEZE Displays the string

………>> (CLLCD clears the LCD display)

(1 DISP displays from the top of the screen)

DISPLAYING INTERMEDIATE RESULTS

If results are to be displayed in the middle of the program (i.e. suspended)

the following commands have to be added after the FREEZE command:

0 WAIT DROP to freeze the display until another key is pressed

n WAIT to freeze the display for n seconds before continuing

OTHER INPUT AND OUTPUT FORMS

INFORM, CHOOSE , MSGBOX are displayed in more depth in the USER Guide

AN EXAMPLE OF A SIMPLE PROGRAM

Program comments are after the @ character

«

@ Program to reduce a slope distance to horizontal

@ Set HP 48 modes

DEG @ Ensures degrees mode is set

@ prompt for input

‘SLOPE REDUCTION’ @ Title of input / program

{’:Zenith <) :

:Slope Dist:’

{ 1 0 } }

INPUT OBJ\->

@ Store input data as local variables

-> za sd

«

@ Compute horizontal distance

za HMS\-> SIN sd *

@ Display result

3 FIX @ set decimal places to 3

‘HD = ’ SWAP + ’ m’ +

CLLCD 1 DISP 3 FREEZE

>

VECTORS

The HP48 handles both 2D and 3D vectors to define lines or points in space. As the principles for manipulating 3D vectors are similar to 2D, only 2D will be described here in detail.

A Vector in 2D plane surveying terms can be described in two ways:

In RECT mode as [DNDE ]

In CYLIN (POLAR) mode as [ Distance Bearing ]

TO BUILD A RECTANGULAR VECTOR FROM THE STACK:

Enter the difference in Northings onto the stack

Enter the difference in Eastings onto the stack

Press [MTH] |VECTR| [NXT] |RECT| to ensure the HP48 is in RECT mode

Press [NXT] |->V2| To convert the 2 levels of the stack into a RECT vector

TO BUILD A CYLIN (POLAR) VECTOR FROM THE STACK:

Enter the distance onto the stack

Enter the bearing (in decimal degrees) onto the stack, ensuring the HP48 is in degrees mode.

Press [MTH] |VECTR| [NXT] |CYLIN| to ensure the HP48 is in CYLIN mode

Press [NXT] |->V2| to convert the 2 levels of the stack into a CYLIN vector

PROGRAM COMMANDS TO CONVERT FROM RECTANGULAR TO POLAR (DEGREES)

Difference in Northings is on level 2 of the stack

Difference in Eastings is on level 1 of the stack

« DEG RECT \->V2 CYLIN V\-> RECT »

Distance is returned on level 2 of the stack

Bearing is returned on level 1 of the stack

PROGRAM COMMANDS TO CONVERT FROM POLAR TO RECTANGULAR (DEGREES)

Distance is on level 2 of the stack

Bearing is on level 1 of the stack

« DEG CYLIN \->V2 RECT V\-> »

Difference in Northings is returned on level 2 of the stack

Difference in Eastings is returned on level 1 of the stack

ADDING AND SUBTRACTING VECTORS

Vectors can be added and subtracted in the same manner as real numbers, but the HP48 should be in RECT mode.

MATRIX OPERATIONS

A Matrix is delimited as follows:

[ [ row 1 ]

[ row 2 ]

.

.

[ row n ] ]

TO ASSEMBLE A MATRIX BY ROWS FROM A SERIES OF VECTORS

Enter each vector (representing rows of the matrix) onto the stack in the order you want them to

appear in the matrix. Enter the number of rows of the matrix onto level one. Press [MTH] |MATR| |ROW| |ROW->|

TO ASSEMBLE A MATRIX BY COLUMNS FROM A SERIES OF VECTORS

Enter each vector (representing columns of the matrix) onto the stack in the order you want them to

appear in the matrix. Enter the number of columns of the matrix onto level one. Press [MTH] |MATR| |COL| |COL->|

TO ASSEMBLE A DIAGONAL MATRIX (ZEROS ELSEWHERE)

Enter the vector containing the diagonal elements. Enter the dimension of the matrix (No. of rows/columns).

Press [MTH] |MATR| [NXT] |DIAG->|

TO ASSEMBLE AN MATRIX FROM ELEMENTS

Enter all the elements of the matrix onto the stack in row order (like a book). Enter a list of { rows columns } onto level one of the stack to define the matrix. Press [PRG] |TYPE| |->ARR|

USEFUL MATRIX AND VECTOR COMMANDS

DET returns the determinant of a square matrix on level one

INV returns the inverse of a square matrix on level one

TRN transposes a matrix on level one

\* + - all perform arithmetric operations on matrices (if compatible)