<<Previous

Next>>

 IF..THEN

IF Comp {AND/OR Comp...} THEN Label

IF Comp {AND/OR Comp...} THEN

Statement...

ELSE

Statement...

ENDIF

Performs one or more comparisons. Each Comp term can relate a variable to a constant or other variable and includes one of the comparison operators listed previously.

If..Then evaluates the comparison terms for true or false. If it evaluates to true, the operation after the Then is executed. If it evaluates to false, the operation after the Then is not executed. Comparisons that evaluate to 0 are considered false. Any other value is considered true. 

All comparisons are unsigned since PBP only supports unsigned types.  IF..THEN cannot be used to check if a number is less than 0.

Be sure to use parenthesis to specify the order the operations should be tested in. Otherwise, operator precedence will determine it for you and the result may not be as expected.

IF..THEN can operate in 2 manners. In one form, the THEN in an IF..THEN is essentially a GOTO. If the condition is true, the program will GOTO the label after the THEN. If the condition evaluates to false, the program will continue at the next line after the IF..THEN. Another statement may not be placed after the THEN, it must be a label.

	If Pin0 = 0 Then pushd	' If button connected to Pin0 is pushed (0), jump to label pushd
	If B0 >= 40 Then old	' If the value in variable B0 is greater than or equal to 40, jump to old
	If PORTB.0 Then itson	' If PORTB, pin 0 is high (1), jump to itson
	If (B0 = 10) AND (B1 = 20) Then loop

In the second form, IF..THEN can conditionally execute a group of Statements following the THEN. The Statements must be followed by an ELSE or ENDIF to complete the structure.

	If B0 <> 10 Then
		B0 = B0 + 1
		B1 = B1 - 1
	Endif

	If B0 = 20 Then
		led = 1
	Else
		led = 0
	Endif

 INPUT

INPUT Pin

Makes the specified Pin 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).

	INPUT 0		' Make Pin0 an input
	INPUT PORTA.0	' Make PORTA, pin 0 an input

Alternatively, the pin may be set to an input in a much quicker and shorter way (from a generated code standpoint):

	TRISB.0 = 1		' Set PORTB, pin 0 to an input

All of the pins on a port may be set to inputs by setting the whole TRIS register at once:

	TRISB = %11111111	' Set all of PORTB to inputs

 LCDIN

LCDIN {Address,}[Var{,Var...}]

Read LCD RAM at Address and store data to Var.

LCDs have RAM onboard that is used for character memory. Most LCDs have more RAM available that is necessary for the displayable area. This RAM can be written using the LCDOUT instruction. The LCDIN instruction allows this RAM to be read.

CG (character generator) RAM runs from address $40 to $7f. Display data RAM starts at address $80. See the data sheet for the specific LCD for these addresses and functions.

It is necessary to connect the LCD read/write line to a PICmicro pin so that it may be controlled to select either a read (LCDIN) or write (LCDOUT) operation. Two DEFINEs control the pin address:

	DEFINE LCD_RWREG PORTE	' LCD read/write pin port
	DEFINE LCD_RWBIT 2	' LCD read/write pin bit

See LCDOUT for information on connecting an LCD to a PICmicro MCU.

    LCDIN [B0]

 

 LCDOUT

LCDOUT Item{,Item...}

Display Items on an intelligent Liquid Crystal Display. PBP supports LCD modules with a Hitachi 44780 controller or equivalent. These LCDs usually have a 14- or 16-pin single- or dual-row header at one edge.

If a pound sign (#) precedes an Item, the ASCII representation for each digit is sent to the LCD. LCDOUT (on all devices except 12-bit core) can also use any of the modifiers used with SEROUT2. See the section on SEROUT2 for this information.

Modifier

Operation

{I}{S}BIN{1..16}

Send binary digits

{I}{S}DEC{1..5}

Send decimal digits

{I}{S}HEX{1..4}

Send hexadecimal digits

REP c\n

Send character c repeated n times

STR ArrayVar{\n}

Send string of n characters

A program should wait for at least half a second before sending the first command to an LCD. It can take quite a while for an LCD to start up.

The LCD is initialized the first time any character or command is sent to it using LCDOUT. If it is powered down and then powered back up for some reason during operation, an internal flag can be reset to tell the program to reinitialize it the next time it uses LCDOUT:

FLAGS = 0

Commands are sent to the LCD by sending a $FE followed by the command. Some useful commands are listed in the following table:

Command

Operation

$FE, 1

Clear display

$FE, 2

Return home (beginning of first line)

$FE, $0C

Cursor off

$FE, $0E

Underline cursor on

$FE, $0F

Blinking cursor on

$FE, $10

Move cursor left one position

$FE, $14

Move cursor right one position

$FE, $C0

Move cursor to beginning of second line

$FE, $94

Move cursor to beginning of third line

$FE, $D4

Move cursor to beginning of fourth line

Note that there is a command to move the cursor to the beginning of the second line of a 2-line display. For most LCDs, the displayed characters and lines are not consecutive in display memory - there can be a break in between locations. For most 16x2 displays, the first line starts at $0 and the second line starts at $40. The command:

LCDOUT $FE, $80 + 4

sets the display to start writing characters at the forth position of the first line. 16x1 displays are usually formatted as 8x2 displays with a break between the memory locations for the first and second 8 characters. 4- line displays also have a mixed up memory map, as shown in the table above.

See the data sheet for the particular LCD device for the character memory locations and additional commands..

	LCDOUT $FE, 1, “Hello”	' Clear display and show “Hello”
	LCDOUT $FE,$C0,“World” 	' Jump to second line and show “World”
	LCDOUT B0, #B1		' Display B0 and decimal ASCII value of B1The LCD may be connected to the PICmicro using either a 4-bit bus or an 8-bit bus. If an 8-bit bus is used, all 8 bits must be on one port. If a 4-bit bus is used, it must be connected to either the bottom 4 or top 4 bits of one port. Enable and Register Select may be connected to any port pin. R/W should be tied to ground as the LCDOUT command is write only.

The LCD may be connected to the PICmicro MCU using either a 4-bit bus or an 8-bit bus. If an 8-bit bus is used, all 8 bits must be on one port. If a 4-bit bus is used, the top 4 LCD data bits must be connected to either the bottom 4 or top 4 bits of one port. Enable and Register Select may be connected to any port pin. R/W may be tied to ground if the LCDIN command is not used.

PBP assumes the LCD is connected to specific pins unless told otherwise. It assumes the LCD will be used with a 4-bit bus with data lines DB4 - DB7 connected to PICmicro PORTA.0 - PORTA.3, Register Select to PORTA.4 and Enable to PORTB.3.

It is also preset to initialize the LCD to a 2 line display.

To change this setup, place one or more of the following DEFINEs, all in upper-case, at the top of your PICBASIC PRO™ program:

	' Set LCD Data port
	DEFINE LCD_DREG	PORTB
	' Set starting Data bit (0 or 4) if 4-bit bus
	DEFINE LCD_DBIT	4
	' Set LCD Register Select port
	DEFINE LCD_RSREG	PORTB
	' Set LCD Register Select bit
	DEFINE LCD_RSBIT	1
	' Set LCD Enable port
	DEFINE LCD_EREG	PORTB
	' Set LCD Enable bit
	DEFINE LCD_EBIT	0
	' Set LCD bus size (4 or 8 bits)
	DEFINE LCD_BITS	4
	' Set number of lines on LCD
	DEFINE LCD_LINES	2
	' Set command delay time in us
	DEFINE LCD_COMMANDUS	2000
	' Set data delay time in us
	DEFINE LCD_DATAUS	50

This setup will tell PBP a 2-line LCD is connected in 4-bit mode with the data bus on the top 4 bits of PORTB, Register Select on PORTB.1, and Enable on PORTB.0.

The following schematic shows one way to connect an LCD to a PICmicro, using the defaults:

<<Previous

Next>>