<<Previous

Next>>

 WHILE..WEND

WHILE Condition
	Statement...
WEND

Repeatedly execute Statements WHILE Condition is true. When the Condition is no longer true, execution continues at the statement following the WEND. Condition may be any comparison expression.

	i = 1
	WHILE i <= 10
		Serout 0,N2400,[”No:”,#i,13,10]
		i = i + 1
	WEND

WRITE

WRITE Address,Value

Write Value to the on-chip EEPROM at the specified Address. 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.

WRITE is used to set the values of the on-chip EEPROM at runtime. To set the values of the on-chip EEPROM at programming-time, use the DATA or EEPROM statement.

Each WRITE is self-timed and takes about 10 milliseconds to execute on a PICmicro.

If interrupts are used in a program, they must be turned off (masked, not DISABLEd) before executing a WRITE, and turned back on (if desired) after the write instruction is complete. An interrupt occurring during a WRITE may cause it to fail.

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

	WRITE 5,B0	' Send value in B0 to EEPROM location 5

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

w Var Word
WRITE 0,w.BYTE0
WRITE 1,w.BYTE1

 

  WRITECODE

WRITECODE Address,Value

Write Value to the code space at location Address.

The PIC16F87x devices allow code to be read and written at run-time. While writing self-modifying code can be a dangerous technique, it does allow non-volatile data storage above 64 - 256 bytes. One must be very careful not to write over active program memory. 

The listing file may be examined to determine program addresses.

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

For PIC18Fxxx devices, byte or word-sized data can be written to byte code space Addresses between 0 and 65535.

For block accessed devices, like the PIC16F877a and 18F452, a complete block must be written at once. This write block size is different for different PICmicro MCUs. See the Microchip data sheet for the particular device for information on the block size.

Additionally, some flash PICmicro MCUs, like the PIC18Fxxx series, require a portion of the code space to be erased before it can be rewritten with WRITECODE. See the section on ERASECODE for more information.

If interrupts are used in a program, they must be turned off (masked, not DISABLEd) before executing a WRITECODE, and turned back on (if desired) after the write instruction is complete. An interrupt occurring during a WRITECODE may cause it to fail.

	WRITECODE 100,W	' Send value in W to code space location 100

  XIN

XIN DataPin,ZeroPin,{Timeout,Label,}[Var{,...}]

Receive X-10 data and store the House Code and Key Code in Var.

XIN is used to receive information from X-10 devices that can send such information. X-10 modules are available from a wide variety of sources under several trade names. An interface is required to connect the microcontroller to the AC power line. The TW-523 for two-way X-10 communications is required by XIN. This device contains the power line interface and isolates the microcontroller from the AC line. Since the X-10 format is patented, this interface also covers the license fees.

DataPin is the automatically made an input to receive data from the X-10 interface. ZeroPin is automatically made an input to received the zero crossing timing from the X-10 interface. Both pins should be pulled up to 5 volts with 4.7K resistors. DataPin and ZeroPin 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).

An optional Timeout and Label may be included to allow the program to continue if X-10 data is not received within a certain amount of time. Timeout is specified in AC power line half-cycles (approximately 8.33 milliseconds).

XIN only processes data at each zero crossing of the AC power line as received on ZeroPin. If there are no transitions on this line, XIN will effectively wait forever.

If Var is word-sized, each House Code received is stored in the upper byte of the word. Each received Key Code is stored in the lower byte of the word. If Var is a byte, only the Key Code is stored.

The House Code is a number between 0 and 15 that corresponds to the House Code set on the X-10 module A through P.

The Key Code can be either the number of a specific X-10 module or the function that is to be performed by a module. In normal practice, first a command specifying the X-10 module number is sent, followed by a command specifying the function desired. Some functions operate on all modules at once so the module number is unnecessary. Hopefully, later examples will clarify things. Key Code numbers 0-15 correspond to module numbers 1-16.

These Key Code numbers are different from the actual numbers sent and received by the X10 modules. This difference is to match the Key Codes in the BS2. To remove this Stamp translation, the following DEFINE may be used:

DEFINE XINXLAT_ OFF 1

XIN is not supported on 12-bit core PICmicro MCUs due to RAM and stack constraints.

XOUT below lists the functions as well as the wiring information.

housekey	var	word

	' Get X-10 data
loop:	XIN PORTA.2,PORTA.0,[housekey]

	' Display X-10 data on LCD
	Lcdout $fe,1,“House=”,#housekey.byte1, “Key=”,#housekey.byte0

	Goto loop	' Do it forever


	' Check for X-10 data, go to nodata if none
	XIN PORTA.2,PORTA.0,1,nodata,[housekey]
 

  XOUT

XOUT DataPin,ZeroPin, [HouseCode\KeyCode{\Repeat}{,...}]

Send HouseCode followed by KeyCode, Repeat number of times in X-10 format. If the optional Repeat is left off, 2 times (the minimum) is assumed. Repeat is usually reserved for use with the Bright and Dim commands.

XOUT is used to send control information to X-10 modules. These modules are available from a wide variety of sources under several trade names. An interface is required to connect the microcontroller to the AC power line. Either the PL-513 for send only, or the TW-523 for two-way X-10 communications are required. These devices contain the power line interface and isolate the microcontroller from the AC line. Since the X-10 format is patented, these interfaces also cover the license fees.

DataPin is the automatically made an output to send data to the X-10 interface. ZeroPin is automatically made an input to received the zero crossing timing from the X-10 interface. It should be pulled up to 5 volts with a 4.7K resistor. DataPin and ZeroPin 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).

XOUT only processes data at each zero crossing of the AC power line as received on ZeroPin. If there are no transitions on this line, XOUT will effectively wait forever.

HouseCode is a number between 0 and 15 that corresponds to the House Code set on the X-10 module A through P. The proper HouseCode must be sent as part of each command.

The KeyCode can be either the number of a specific X-10 module or the function that is to be performed by a module. In normal practice, first a command specifying the X-10 module number is sent, followed by a command specifying the function desired. Some functions operate on all modules at once so the module number is unnecessary. Hopefully, later examples will clarify things. KeyCode numbers 0-15 correspond to module numbers 1-16.

The KeyCode (function) names (e.g. unitOn) are defined in the file MODEDEFS.BAS. To use them, add the line:

Include "modedefs.bas"

to the top of the PICBASIC PRO™ program. BS1DEFS.BAS and BS2DEFS.BAS already includes MODEDEFS.BAS. Do not include it again if one of these files is already included. The KeyCode numbers may be used without including this file.

KeyCode

KeyCode No.

Operation

unitOn

%10010

Turn module on

unitOff

%11010

Turn module off

unitsOff

%11100

Turn all modules off

lightsOn

%10100

Turn all light modules on

lightsOff

%10000

Turn all light modules off

bright

%10110

Brighten light module

dim

%11110

Dim light module

These Keycode numbers are different from the actual numbers sent and received by the X10 modules. This difference is to match the Keycodes in the BS2. To remove this Stamp translation, the following DEFINE may be used:

            DEFINE XOUTXLAT_ OFF 1

XOUT is not supported on 12-bit core PICmicro MCUs due to RAM and stack constraints.

Wiring to the X-10 interfaces requires 4 connections. Output from the X-10 interface (zero crossing and receive data) are open-collector and require a pull up resistor of around 4.7K to 5 volts. Wiring tables for each interface is shown below:

PL-513 Wiring

Wire No.

Wire Color

Connection

1

Black

Zero crossing output

2

Red

Zero crossing common

3

Green

X-10 transmit common

4

Yellow

X-10 transmit input

TW-523 Wiring

Wire No.

Wire Color

Connection

1

Black

Zero crossing output

2

Red

Common

3

Green

X-10 receive output

4

Yellow

X-10 transmit input

 

house	var	byte
unit	var	byte

	Include “modedefs.bas”

	house = 0			' Set house to 0 (A)
	unit = 8			' Set unit to 8 (9)
	' Turn on unit 8 in house 0
	XOUT PORTA.1,PORTA.0,[house\unit,house\unitOn]

	' Turn off all the lights in house 0
	XOUT PORTA.1,PORTA.0,[house\lightsOff]

	' Blink light 0 on and off every 10 seconds
	XOUT PORTA.1,PORTA.0,[house\0]

loop:	XOUT PORTA.1,PORTA.0,[house\unitOn]
	Pause 10000		' Wait 10 seconds

	XOUT PORTA.1,PORTA.0,[house\unitOff]
	Pause 10000		' Wait 10 seconds

	Goto loop
<<Previous

Next>>