<<Previous

Next>>

 RANDOM

RANDOM Var

Perform one iteration of pseudo-randomization on Var. Var should be a 16-bit variable. Array variables with a variable index may not be used in RANDOM although array variables with a constant index are allowed. Var is used both as the seed and to store the result. The pseudo-random algorithm used has a walking length of 65535 (only zero is not produced).

	RANDOM W4	' Get a random number to W4

 RCTIME

RCTIME Pin,State,Var

RCTIME measures the time a Pin stays in a particular State. It is basically half a PULSIN. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

RCTIME may be used to read a potentiometer (or some other resistive device). Resistance can be measured by discharging and timing the charge (or vice versa) of a capacitor through the resistor (typically 5K to 50K).

The resolution of RCTIME is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the time in state is returned in 10us increments. If a 20MHz oscillator is used, the time in state will have a 2us resolution. Defining an OSC value has no effect on RCTIME. The resolution always changes with the actual oscillator speed.

If the pin never changes state, 0 is returned. RCTIME normally waits a maximum of 65535 counts before it determines there is no pulse change of state. If it is desired to wait fewer counts before it stops looking for this change, a DEFINE can be added:

DEFINE PULSIN_ MAX 1000 

This DEFINE also affects PULSIN in the same manner. 

 

	Low PORTB.3		' Discharge cap to start
	Pause 10		' Discharge for 10ms
	RCTIME PORTB.3,0,W0	' Read potentiometer on Pin3

 READ

READ Address,Var

Read the on-chip EEPROM at the specified Address and stores the result in Var. This instruction may only be used with a PICmicro that has an on-chip EEPROM data area such as the PIC16F84, PIC16C84 and the PIC16F87x series.

READ will not work on devices with on-chip I2C interfaced serial EEPROM like the 12CE67x and 16CE62x parts. Use the I2CREAD instruction instead.

	READ 5,B2	' Put the value at EEPROM location 5 into B2

To read a word, each of the 2 bytes that make up the word must be read separately:

w     Var     Word

READ 0, w. BYTE0 
READ 1, w. BYTE1

 READCODE

READCODE Address,Var

Read the code at location Address into Var.

Some PIC16Fxxx and PIC18Xxxx devices allow program code to be read at run-time. This may be useful for additional data storage or to verify the validity of the program code.

For PIC16Fxxx devices, 14-bit-sized data can be read from word code space Addresses.

For PIC18Xxxx devices, byte or word-sized data can be read from byte code space Addresses between 0 and 65535.

The listing file may be examined to determine program addresses.

	READCODE 100,W	' Put the code word at location 100 into W

 RESUME

RESUME {Label}

Pick up where program left off after handling an interrupt. RESUME is similar to RETURN but is used at the end of a PICBASIC PRO™ interrupt handler.

If the optional Label is used, program execution will continue at the Label instead of where it was when it was interrupted. In this case, any other return addresses on the stack will no longer be accessible.

See ON INTERRUPT for more information.

clockint:	seconds = seconds + 1	' Count time
		RESUME			' Return to program after interrupt

error:		High errorled		' Turn on error LED
		RESUME restart		' Resume somewhere else

 RETURN

RETURN

Return from subroutine. RETURN resumes execution at the statement following the GOSUB which called the subroutine.

	Gosub sub1	' Go to subroutine labeled sub1
		...
sub1:	Serout 0,N2400,[“Lunch”]	' Send “Lunch” out Pin0 serially
	RETURN		' Return to main program after Gosub

 REVERSE

REVERSE Pin

If Pin is an input, it is made an output. If Pin is an output, it is made an input. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

	Output 4	' Make Pin4 an output
	REVERSE 4	' Change Pin4 to an input
 

SELECT CASE 

	SELECT CASE Var 
		CASE Expr1 {, Expr...} 
	    		Statement... 
		CASE Expr2 {, Expr...} 
	    		Statement...
		{CASE ELSE 
	    		Statement...}
	END SELECT

CASE statements, in some cases, are easier to use than multiple IF.. THENs. These statements are used to compare a variable with different values or ranges of values, and take action based on the value.

The Variable used in all of the comparisons is specified in the SELECT CASE statement. Each CASE is followed by the Statements to be executed if the CASE is true. IS may be used to specify a comparison other than equal to. If none of the CASEs are true, the Statements under the optional CASE ELSE statement are executed. An END SELECT closes the SELECT CASE.

SELECT CASE

CASE
    y = 10 
CASE 2, 3 
    y = 20
CASE IS > 5 
    y = 100 
CASE ELSE 
    y = 0 

END SELECT

 
<<Previous

Next>>