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

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

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

Добавлен: 12.06.2025

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

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

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

Basic for PIC Microcontrollers

19

2.15 ON INTERRUPT

On interrupt LABEL

With instruction "On interrupt" is indicated the label on which the program will "jump" when the interruption happened, i.e. from which label the interruption routine starts.

2.16 RES UME

RESUME

Return from the interruption routine to the main program.

Basic for PIC Microcontrollers

20

Chapter 3

OPERATORS

Introduction

3.1 Expressions

3.2 Instructions

3.3 Arithmetical operators

3.3.1 Multiplication

3.3.2 Division

3.3.3 Shift

3.3.4 ABS

3.3.5 COS

3.3.7 DIG

3.3.8 MAX and MIN

3.3.9 NCD

3.3.10 REV

3.3.11 SIN

3.3.12 SQR

3.4 Bit operators

3.5 The operators of comparison

3.6 Logical operators

Introduction

The PIC BASIC language possesses the operator set used to assign the values, compare objects and perform multitude of other operations. The objects manipulated for that purposes are called operands (which themselves can be variables or constants). The operators of PIC BASIC language must have at least two operands. They serve to create instructions and expressions that together with variables, constants and comments in effect compose the program.

3.1 Expressions

Combinations of operators and operands are called expressions. The expression does the computation and furnishes the result or starts some other activity.

A = B + C

' The expression that sums up the values of the variables B and C and

' stores the result into the variable A

In application of any expression the attention must be paid that the result of the computation must be within the range of variable A in

order to avoid the overflow and therefore the evident computational error. If the result of expression amounts to 428, and the variable A is of BYTE type having range between 0 and 255, the result accordingly obtained will be 172 - obviously the wrong one.


Basic for PIC Microcontrollers

21

3.2 Instructions

Each instruction determines an action to be performed. As a rule, the instructions are being executed in an exact order in which they are written in the program. However, the order of their execution can be changed as well emp loying the instructions for the change of the flow of a program to another segment of the program such as the instructions of the ramification, jump or interrupt.

IF Time = 60 THEN GOTO Minute

' if A = 23 jump to label Minute

Instruction IF...THEN contains the conducting expression Time=60 composed in its own rights of two operands, the variable Time, constant 60 and the operator of comparison (=). The instructions of PIC BASIC language can be distinguished as the instructions of

choice (decision making) repeating (loops), jump and specific instruction for an access to the peripheries of the microcontrollers. Each of these instructions is explained in detail in Chapter 4.

Operators are numerous, but for almost 90% of all the programs it is necessary to know only few of them. It suffices to look how many operators are used in the examples in Chapter 5, 6 and 7.

After the activities they perform, the operators can be classified into the following categories:

-Arithmetic operators

-Bit operators?

-The operators of comparison

-Logical operators

3.3 Arithmetic operators

All arithmetic operators work in 16-bit precision with the unsigned values what means that the range of the operand is from 0 to 65535. In order to group operations, one may use brackets.

A = (B + C) * (D - E)

In the following table all the supported arithmetic operators are listed.

Operator Description

Operator

Description

Operator

Description

+

summation

ABS

absolute value of a number

-

subtraction

COS

cosine of an angle

*

multiplication

DCD

bit decoding

**

the result is in higher 16 bits

DIG

value of the digit for a

decimal number

*/

the result is in middle 16 bits

MAX

maximum of a number


Basic for PIC Microcontrollers

22

/

division

MIN

minimum of a number

//

remainder

NCD

priority coding

<<

left shift

REV

bit reversing

>>

right shift

SIN

sine of an angle

=

assignment of value

SQR

square root of a number

3.3.1 Multiplication

Syntax: L0 = W1 * 100

L1 = W1 ** W2

L2 = W1 */ W2

Description: PIC BASIC pro does not support directly the work with the 32-bit numbers. It is usual to present a 32-bit variable as a two 16-bit variables. Operator '*' reverts lower 16 bits of a 32-bit result. Operator '**' reverts higher 16 bits of a 32-bit result. These two operators can be used in a combined way for computing 16x16 multiplications in order to produce 32-bit results.

Example:

3.3.2 Division

Syntax: W0 = W1 / 100

W2 = W1 // 100

Description: As it is the case with multiplication, the operation of division is done over the 16 bit operands. Operator '/' reverts 16-bit integer result while the operator '//' reverts the remainder.


Basic for PIC Microcontrollers

23

Example:

3.3.3 Shift

Syntax: W0 = W0 << 3

W0 = W0 >> 1

Description: Operators of the shift perform the shift towards left or right from 0 to 15 times. All the new bits that enter from the side have value 0. These two operators belong to the operators over the bits.

Example:

3.3.4 Absolute value of a number

Syntax:

B0 = ABS B1

Description: ABS gives the absolute value of a number. If ABS gets applied to the variable of the BYTE type greater then 127 (set MSB) the result is 256. If the ABS gets applied to the variable of WORD type greater then 32767 (the bit set is of the biggest weight - MSB) result is 65536.

Basic for PIC Microcontrollers

24

Example:

3.3.5 Cosine of an angle

Syntax:

B0 = COS B1

Description: COS reverts the 8-bit value of the cosine. The result is in the second complement (i.e. within the range -127 to 127). For that reason it is necessary to use the lookup table in order to determine the result (cosine of an angle goes in the binary range between 0 and 255 in contrast with usual 0 to 359 degrees).

Example:

3.3.6 The decoded bit value

Syntax:

B0 = DCD N

Description: DCD gives the decoded bit value of the operand whose value is in the range within 0-15. If the operand is 0 then the zeroth bit of the result 1, and if the operand reads as 7, the seventh bit of the result is 1.

Basic for PIC Microcontrollers

25

Example:

3.3.7 DIG The value of the digit for a decimal number

Syntax: W = W1 DIG N

Description: DIG furnishes the value of the digit of a decimal number. The number whose digits are looked for is 0-3 where 0 is a last right digit i.e. digit of the smallest weight (it is most often used for the work with seven-segment digits for extraction of the digits to be displayed).

Example:

3.3.8 MAX and MIN Maximum and Minimum of a number

Syntax:

B0 = B1

MAX 100

B0 = B1

MIN 100

Description: The operator's maximum and minimum are used whenever it is necessary to revert one out of two values that are being compared. If those numbers are for example 100 and 200 operator Max will revert the value 200 and operator Min, value 100. To the difference from the operators "bigger then" and "less then" they revert the entire value and not only the quantification whether some value is smaller or bigger then the other.


Basic for PIC Microcontrollers

26

Example:

3.3.9 NCD Priority coding

Syntax:

B0 = NCD %01001000

B0 = NCD %00001111

Description: NCD furnishes the value that is coded with the priority code. That gives the position of the first unit, which it encounters from the left side. If the operand is 0 the result is 0 as well.

Example:

3.3.10 REV Reverting of the lowest bits of the operand

Syntax:

B0 = %10101100 REV 4

Description: REV reverts the order of the lowest bits of the operand. The number of the bits that can be reverted goes from 1 to 16.

Example:

Basic for PIC Microcontrollers

27

3.3.11 SIN Sine of an angle

Syntax:

B0 = SIN B1

Description: SIN reverts the 8-bit value of the sine. The result is in the second Complement (i.e. within the range -127 to 127). For that reason it is necessary to use the lookup table in order to determine the result (sine of an angle goes in the binary range between 0 and 255 in contrast with usual 0 to 359 degrees).

Example:

3.3.12 SQR Square root

Syntax:

B0 = SQR W1

Description: SQR reverts a value of a square root. Result is stored into the variable of BYTE type.

Example:

3.4 Bit operators

Basic for PIC Microcontrollers

28

One of the more important properties of higher programming languages is their capacity to go down to the lower level i.e. the level of the assembler. Bit operators furnish the access to the registers and memory of a microcontrollers at the level of a single bit. Operators supported by the language PIC BASIC are given in the table below:

Bit operators

Operator

Description

&

Logical AND over the bits

|

Logical OR over the bits

^

Logical XOR over the bits

~

Logical NOT over the bits

&/

Logical NAND over the bits

|/

Logical NOR over the bits

^/

Logical NXOR over the bits

The value result of the expression depends on the fact which of the listed logical operations is executed over the bits of the operand. In that way, it is possible to extract, delete, set or invert the certain bit of the operand.

Example1:

B0 = B0 & %00000001

The upper instruction extracts the value of the lowest bit of the variable B0. When the logical "AND" is performed with the zero, there

will be 0 at the position of a corresponding bit (so that all the bits 1-7 will be zeroes). The value will depend on bit 0 in the variable B0 and if it is "0", the value of variable B0 will be "0" and if it is "1" the value of B0 will accordingly be "1".

Example2:

B0 = B0 & %00000100

The upper instruction sets bit2 in the variable B0. When the logical "or" is performed with the unity the result is always equal to "1" regardless of the state of the corresponding bit from B0.

Example 3:

B0 = B0 & %00000010

The upper instruction inverts the bit 1 in variable B0. If the bit was "1" then it turns into "0" and vice versa. The other logical operators are used only rarely so there's no need for their detailed explanation.

3.5 The operators of comparison

The expressions that contain the operators of comparison give after having compared the two operands the result true or false. If the expression of comparison is true then the instruction to be executed is the one on the left side, otherwise the execution of the program continues with the next instruction. The operators of comparison are shown in the table below: