Tuesday, November 8, 2011

HP 15C Programming Tutorial - Part 2: Basic Programming

Basic Programming

In Part 2, we will type our first two programs using the HP 15C calculator. Keystroke programming is fairly simple - all we are doing is recording a set of keystrokes to be used at a later time.

The HP15C allows for 25 "named" programs: A, B, C, D, E, the digits 0 through 9, and the decimal point/number names ".0" through ".9". Programs do not necessarily have to be named.

How to Get to Program Mode

The key sequence [g] [R/S] (P/R) switches the calculator between Run mode and Program mode.

Run mode is where programs are executed. In addition, the regular calculator operations take place in this mode.

Program mode is where programs are edited. When the 15C is in Program mode, an PRGM indicator is shown on the bottom right hand of the screen.

The Display in Program Mode

The display of the 15C in Program Mode looks something like this:

NNN - AA, BB, CC

where:
* NNN indicates the program steps.
* AA, BB, and CC are the key codes.

A key code is a two digit code consisting of a row number and a column number. Rows are labeled 1 through 4 from the top. Columns are labeled 1 through 9, then 0 (the rightmost column) from the left.

Examples:
[ √ ] has the key code 11.
[CHS] has the key code 16.
[SIN] has the key code 23.
[ × ] has the key code 20.
[R ↓ ] has the key code 33.
[ - ] has the key code 30.
[STO] has the key code 44.

Notable exceptions:
* The number keys [ 0 ] through [ 9 ] has their own digit key codes. The key code for the key [ 0 ] is 0, [ 1 ] is 1, [ 2 ] is 2, etc.
* The key code for the [ENTER] key is always 36.


[R/S] Key

The [R/S] key is the run/stop key, which stops program execution. Use this to display immediate or final results. The key also serves a prompt for additional input.

Clearing Programs

The key sequence [f] [R ↓ ] (CL PRGM) serves two different functions:

* In Run mode, it resets the program counter to step 000.

* In Program mode, it completely clears program memory.

Scrolling and Editing Program Contents

While in Program Mode:

[SST] views the next step in the program.

[g] [SST] (BST) view the previous step in the program.

[← ] deletes the program step in display.

Entering a new instruction in the middle of a program inserts the instruction at that step and pushes all proceeding instructions "below" it.

The First Program

Three students drove to a friend's house for a party. Each of the students live 30.5 miles from the party. Each of the students arrive at the party in 30 minutes, 40 minutes, and 50 minutes, respectfully. Calculate how fast each of the students were going.

From basic physics, we know that:

Distance = Speed × Time, or

Speed = Distance ÷ Time

In our case, we know that Distance is 30.5 miles so:

Speed = 30.5 ÷ Time

Keying in the program

In Run Mode:
[f] [R ↓ ] (CLEAR PRGM)
* Set the counter to 000
[g] [R/S] (P/R)
* Switch to Program mode

In Program Mode:
[f] [R ↓ ] (CLEAR PRGM) -- * Clears program mode
[ 3 ]
[ 0 ]
[ . ]
[ 5 ]
[x<>y] -- * Switches X and Y registers to set proper order for division
[ ÷ ]
[g] [GSB] (RTN) -- * End program
[g] [R/S] (P/R)
* Switch to Run mode

In Run Mode:
[f] [R ↓ ] (CLEAR PRGM)
* Set the counter to 000

Key Stroke Instructions
000-
001-			3		[ 3 ]
002-			0		[ 0 ]
003-			48		[ . ]
004-			5		[ 5 ]
005-			34		[x<>y]
006-			10		[ ÷ ]
007-		43	32		[ g ] [GSB] (RTN)


How to Use
In Run mode, enter the time and press [R/S].

So:
0.5 [R/S] yields 61.0000,
40 [ENTER] 60 [ ÷ ] [R/S] yields 45.75000, and
50 [ENTER] 60 [ ÷ ] [R/S] yields 36.60000.

The student who arrived in 30 minutes (1/2 an hour) drove 61 mph (miles per hour), the student who arrived in 40 minutes drove 45.75 mph, and the student who arrived in 50 minutes drove 36.6 mph.


Volume of a Cylinder

In second program of Part 2, we will learn how to label (or name) a program.

The [f] [SST] (LBL) key sequence followed by name (A-E, 0-9, .0-.9) gives the proceeding set of instructions a name, and is almost always ended with a RTN (return) command.

In Run mode, programs labeled A-E can be used by pressing [f] by the appropriate letter. All programs that are labeled can be ran by using the [GSB] (Gosub) key.

This program will calculate the volume of a cylinder given height and radius. The formula for the volume is:

V = π × h × r^2

The stack will be set up with h in the Y register and r in the X register.

The operations needed to calculate the volume are:
1. Square r
2. Multiply r^2 and h
3. Call up π
4. Multiply π and r^2 × h

Keying in the Program

In Run Mode:
[f] [R ↓ ] (CL PRGM)
[g] [R/S] (P/R)

In Program Mode:
[f] [SST] (LBL) [ √ ] (A) - * Label A
[g] [ √ ] (x^2)
[ × ]
[g] [EEX] ( π )
[ × ]
[g] [GSB] (RTN)
[g] [R/S] (P/R)

Key Stroke Instructions
000-
001-	42	21	11		[f] [SST] (LBL) [ √ ] (A)
002-		43	11		[g] [ √ ] (x^2)
003-			20		[ × ]
004-		43	26		[g] [EEX] ( π )
005-			20		[ × ]
006-		43	32		[ g ] [GSB] (RTN)


How to Use
1. Enter the height
2. Press [ENTER]
3. Enter the radius
4. Press [ f ] [ √ ] (A) or [GSB] [ √ ] (A)

Examples:
1. Find the volume of a cylinder with a height of 1.5 in and a radius of 1.2 in.

1.5 [ENTER] 1.2 [ f ] [ √ ] (A)

Result: (≈) 6.7858 in^2

2. Find the volume of a cylinder with a height of 2.08 cm and radius of 0.95 cm. Remember order is important!

2.08 [ENTER] 0.95 [ f ] [ √ ] (A)

Result: (≈) 5.8974 cm^2

Tip: If you accidentally enter radius first then height, just press the swap key, [x<>y] prior to pressing [ f ] [ √ ] (A).

That is all for Part 2. Next time, we will work storage and recall arithmetic, a very powerful feature of the HP 15C.

Happy programming,

Eddie





This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission of the author. © 2011