ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1342

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Basic for PIC Microcontrollers

49

string transfer, STR goes before the name of the string, and number of clocks after \ .

a var byte[8]

I2CREAD PORTC.4, PORTC.3, $a0, 0, [STR a\8]

If optional label is used, program will jump to the label if there is no response signal over

the I2C interface. Standard transfer rate (100kHz) is achieved with 8MHz oscillator. For

higher transfer rate (400kHz) 20MHz oscillator is used. If slower oscillator is used for the

transfer, following directive should be used :

DEFINE I2C_SLOW 1

In order to have bipolar I2C clock interface and not an open collector, following DEFINE

directive should be used:

DEFINE I2C_SCLOUT

Operating any peripheral units with I2C communication demands that you read supplier

manuals and specifications.

Example:

B0 var byte

addr var

byte

cont con

%10100000 ‘

Control address of EEPROM

addr = 17

Data address is 17

Main:

I2CREAD PORTA.0, PORTA.1, cont, addr, [B0] ‘ Get data to variable B0

Loop: goto Loop

end

4.24 I2CWRITE Writing data to I2C peripheral device

Syntax:

I2CWRITE Data, Frequency, Control_byte, {Address,} [Vari {, Vari...}]{,Label}

Description: I2WRITE sends control and address data via I2C interface. We define 8-bit or 16-bit address while defining variable put to address parameter (in order to correctly define address size, we must have accurate information on device we are communicating with).


Basic for PIC Microcontrollers

50

If peripheral device is serial EEPROM, it is necessary to wait for 10ms (depending on device) until writing has ended. New communication with device is possible after 10ms have elapsed. If new data write occurs before the last one has ended, request will be ignored. Address size is either 1 or 2 bytes, depending on device connected. A problem may occur when trying to write multiple bytes in one instruction, depending on specific EEPROM. Such instances can be avoided if, instead of EEPROM, we use devices without the need for pause between writing. If 2-byte data (WORD) is sent, higher byte goes first, then the lower. For string transfer, STR goes before the name of the string, and number of clocks after \ .

a var byte[8]

I2CWRITE PORTC.4, PORTC.3, $a0, 0, [STR a\8]

If optional label is used, program will jump onto the label if there is no response signal over the I2C interface. Standard transfer rate (100kHz) is achieved by 8MHz oscillator. For higher transfer rate (400kHz) 20MHz oscillator is used. If slower oscillator is used for the transfer program should contain the following directive:

DEFINE I2C_SLOW 1

In order to have bipolar I2C clock interface and not an open collector, following DEFINE directive should be used:

DEFINE I2C_SCLOUT

Operating any peripheral units with I2C communication requires that you study the supplier manual and specifications.

Example:

B0 var byte

addr var

byte

cont con

%10100000 ‘

Control address of EEPROM

Main:

addr = 17

EEPROM address where data will be written is 17

i2cwrite PORTA.0, PORTA.1, cont, addr, [6] ‘ Write number 6 to address 17

pause 10

Wait 10ms until writing is finished

addr = 1

Set address of writting to 1

B0 = 23


Basic for PIC Microcontrollers

51

i2cwrite PORTA.0, PORTA.1, cont, addr, [B0]

‘ Write value of variable B0 to

address 1

pause 10

‘ Wait 10ms until writing is finished

Loop: goto Loop

end

4.25 INPUT Designates I/O pin as input

Syntax:

INPUT Pin

Description:

INPUT designates the specific pin as input.

Example:

Main:

input PORTA.0

‘ Pin PORTA.0 is input. Instruction can be substituted with

TRISB.0=1

TRISB.0=1

Loop: goto Loop

end

4.26 IF-THEN-ELSE Conditional program branching

Syntax:

IF Expression1 { AND / OR Expression2} THEN Label

{instructions}

ELSE

{instructions}

ENDIF

Description: Instruction selects one of two possible program paths. Instruction IF is the fundamental instruction of program branching in PIC BASIC and it can be used in several ways to allow


Basic for PIC Microcontrollers

52

flexibility necessary for realization of decision making logic.

The simplest form of instruction is shown on the picture above. Sample program below tests the button connected to RB0 - when the button is pressed program jumps onto the label “Add” where value of variable “w” is increased. If the button is not pressed, program jumps back onto the label “Main”.

Example:

w var byte

Main :

IF PORTB.0=0 THEN Add goto Main

Add : W=W+1

End

More complex form of instruction is program branching with the ELSE part of instruction.

Basic for PIC Microcontrollers

53

w var byte Main :

IF PORTB.0=0 THEN Add

ELSE Subtract

ENDIF

goto Main Add : W=W+1 Subtract : W=W-1

End

Same effect can be achieved directly :

w var byte

Basic for PIC Microcontrollers

54

Main :

IF PORTB.0=0 THEN W=W+1

ELSE W=W-1

ENDIF

goto Main

End

4.27 LCDOUT Prints data on LCD display

Syntax:

LCDOUT Data {, Data...}

Description: LCDOUT sends the data to the LCD (Liquid Crystal Display). PIC BASIC supports various LCD models which have Hitachi 44780 controller or compatible one. LCD usually has either 14 or 16 pins for connection to a microcontroller. If there is character # before data, ASCII value of every data is sent to LCD. LCDOUT has the same modifiers as the instruction SEROUT2.

Modifier

Sends

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

binary number

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

decimal number

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

hexadecimal number

REP c/n

character c repeated n times

STR ArrayVar {\n}

n character string

Before the first instruction is sent to LCD, program should wait for at least half a second for LCD to initialize.

LCD display can be connected to PIC microcontrollers by either 4-bit or 8-bit bus. If 8-bit bus is used, all of 8 bits mus t be connected to the same port, while in the case of 4-bit bus all 4 bits must be either in the upper or the lower part of byte. R/W line should be connected to ground if LCD is used only for data display. PIC BASIC assumes that LCD is connected to specific pins if DEFINE directives do not say otherwise. Default is 4-bit bus with lines DB4-DB7 connected to RA0-RA3, RS pin connected to RA4 and E pin connected to RB 3. Also, it is assumed that LCD is 2x16. For changing any of the default settings, appropriate DEFINE directives can be used.


Basic for PIC Microcontrollers

55

If LCD is connected to some other microcontroller lines it has to be defined with DEFINE directives, as shown in the following example.

DEFINE LCD_DREG

PORTB

‘ port selection

DEFINE LCD_DBIT

4

‘ initial bit (0 or 4) selection in case of 4-bit bus

DEFINE LCD_RSREG

PORTB

‘ port Register select

DEFINE LCD_RSBIT

1

‘ Register Select bit

DEFINE LCD_EREG

PORTB

‘ Enable port

DEFINE LCD_EBIT

0

‘ Enable bit

DEFINE LCD_BITS

4

‘ bus size – 4 or 8 bits

DEFINE LCD_LINES

2

‘ number of LCD lines

DEFINE LCD_COMMANDS 2000 ‘ command delay in microseconds

DEFINE LCD_DATAUS 50

‘ data delay in microseconds

Definitions above define 2-line LCD on 4 -bit bus on the upper 4 bits of microcont roller port D. Register Select (RS pin) is on PORTD.2 and Enable is on PORTD.3.

Every LCD controller is in charge of certain commands. Commands are sent by instruction: LCDOUT $FE, $Kod. List of commands is shown in table below.

Command

Operation

$FE, 1

clear display

$FE, 2

Return home (beginning of the first line)

$FE, $0C

Turn off cursor

$FE, $0E

Underline cursor on

$FE, $0F

Blinking cursor on

$FE, $10

Shifting cursor left

$FE, $14

Shifting cursor right

Basic for PIC Microcontrollers

56

$FE, $C0

set cursor to the beginning of the second line

$FE, $94

set cursor to the beginning of the third line

$FE, $D4

set cursor to the beginning of the fourth line

Example:

B0 var byte

B1 var byte

Main:

lcdout

$FE, 1, “Hello”

‘ Clear display and print “Hello”

lcdout

$FE, $C0

switch to second line

lcdout

B0

Display the value of B0

lcdout

#B1

Display the value of B1 in ASCII code

Loop: goto Loop

end

4.28 LCDIN Reads data from LCD RAM

Syntax:

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

Description:

LCDIN reads the given address of LCD RAM and stores data into a variable. When using

this instruction, LCD Read/Write line must be connected to microcontroller. In case when

LCD is used for data printing exclusively, this line can be connected to a logical zero.

DEFINE directives inform the program about port and pin which Read/Write line is

connected to:

DEFINE LCD_RWREG PORTE

‘ LCD read/write port

DEFINE LCD_RWBIT 2

‘ LCD read/write bit on port

Example:

B0 var byte

Main:

Lcdin $40, B0 ‘ Read data from LCD location $40 and store it into B0