ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 1335
Скачиваний: 0
Basic for PIC Microcontrollers |
29 |
||
Operators of comparison |
|||
Operator |
Description |
||
= or == |
equal |
||
<> or !=| |
not equal |
||
< |
less then |
||
> |
bigger then |
||
<= |
less then or equal |
||
>= |
bigger then or equal |
||
These operators are most often used in examination of the conditions by the instructions such as IF...THEN.
Example:
If Seconds = 60 then minutes = minutes + 1
Seconds = Seconds + 1
If the variable " Seconds" equals 60 the condition of the comparison is true and the instruction "Minutes=Minutes+1" will be executed then. Unless the expression is not true the instruction "Seconds=Seconds+1" will be executed instead.
3.6 Logical operators
Logical operators serve for the operations over the variables, which take two possible values 0 or 1. These values may well be interpreted as "condition is fulfilled" what corresponds to state "1" and "condition is not fulfilled" which corresponds to the state "0". They are used in the very same way as the operators of comparison within the frame of the instruction IF...THEN. The list of the logical operators is shown in the table below.
Logical operators
Operator |
Description |
||
AND or && |
Logical A ND |
||
OR or || |
Logical OR |
||
XOR or ^^ |
Logical XOR |
||
NOT |
Logical NOT |
||
NOT AND |
Logical NAND |
||
NOT OR |
Logical NOR |
||
NOT XOR |
Logical NXOR |
||
Example1:
If A Or B THEN GOTO Lab
Basic for PIC Microcontrollers |
30 |
If the condition is fulfilled, i.e. if at least one of the operands A or B equal to one, then the program jumps to the label Lab.
Example2:
IF (Seconds>59) And (Minutes>59) THEN Hours=Hours+1
The conditions may be complex as well. Separating into the brackets is obligatory otherwise the result can be very unpredictable.
Basic for PIC Microcontrollers |
31 |
Chapter 4
INSTRUCTIONS (1/4)
Introduction |
|||
4.1 @ |
4.17 GOSUB |
4.33 LOOKUP2 |
4.49 RETURN |
4.2 ASM..ENDASM |
4.18 GOTO |
4.34 LOW |
4.50 REVERSE |
4.3 ADCIN |
4.19 HIGH |
4.35 NAP |
4.51 SELECT-CASE |
4.4 BRANCH |
4.20 HSERIN |
4.36 OUTPUT |
4.52 SERIN |
4.5 BRANCHL |
4.21 HPWM |
4.37 OWIN |
4.53 SERIN2 |
4.6 BUTTON |
4.22 HSEROUT |
4.38 OWOUT |
4.54 SEROUT |
4.7 CALL |
4.23 I2CREAD |
4.39 PAUSE |
4.55 SEROUT2 |
4.8 CLEAR |
4.24 I2CWRITE |
4.40 PAUSEUS |
4.56 SHIFTIN |
4.9 CLEARWDT |
4.25 INPUT |
4.41 POT |
4.57 SHIFTOUT |
4.10 COUNT |
4.26 IF-THEN-ELSE |
4.42 PULSIN |
4.58 SLEEP |
4.11 DATA |
4.27 LCDOUT |
4.43 PULSOUT |
4.59 SOUND |
4.12 DTMFOUT |
4.28 LCDIN |
4.44 PWM |
4.60 STOP |
4.13 EEPROM |
4.29 {LET} |
4.45 RANDOM |
4.61 SWAP |
4.14 END |
4.30 LOOKDOWN |
4.46 RCTIME |
4.62 TOGGLE |
4.15 FREQOUT |
4.31 LOOKDOWN2 |
4.47 READ |
4.63 WRITE |
4.16 FOR-NEXT |
4.32 LOOKUP |
4.48 READCODE |
4.64 WRITECODE |
4.65 WHILE-WEND |
Introduction
All the programs regardless of the fact how complicated or simple they may be are nothing else but a strict flow of the executions of instructions.
Instructions of branching are used in program for the decision-making (in which one of two or more program paths is being chosen). The basic instruction of branching in PIC BASIC language is instruction if. This instruction has several variations that furnish
necessary flexibility required for the realization of the logic of the decision-making (these variations comprise the use of term else and insertion of the instructions).
Instructions of repeating give the possibility of repeating one or more single instructions. The conducting expression determines how many times the repetition will be performed. The set of those instructions is composed of WHILE ... WEND and FOR ... NEXT.
Instructions of jump serve to change the flow of the program execution. The basic instruction of jump, GOTO, transfers the execution of the program to a signed instruction in a main program or inside subroutines. Other instructions of jump are BRANCH, BRANCHL, CALL, GOSUB, RETURN (these instructions are unavoidable in programs but their use is subject to certain restrictions).
Instructions of access to the peripheral devices facilitate the programmer's job. Now programmer can concentrate on the essence of the program he set out to solve, avoiding unnecessary waste of time in writing routine for LCD display or some other peripheral device he uses in his set. The set of instructions is such to satisfy the large part of needs in the design of even the most complicated microcontrollers systems.
Basic for PIC Microcontrollers |
32 |
4.1 @ Inserts one programming line of assembler code
Syntax: |
@ assembler's instruction |
||
Description |
If used at the beginning of the line @ enables free-style combining of the assemblers |
||
: |
code and PIC BASIC code. Instruction @ can be used for insertion of the libraries written |
||
in assembler as well. |
|||
It should be taken notice that the further access from assembler towards variables works |
|||
through the lower dash added to the variables name. In an example below, the variable |
|||
B0 is used as_B0 in assembler programming line. |
|||
Example: |
|||
@include "some_asm_program.asm" |
' inserts an assembler code library |
||
B0 |
var byte |
||
Main : |
|||
@ |
bsf _B0, 7 |
' sets the seventh bit of variable B0 |
|
Loop : goto Loop |
|||
end |
|||
4.2 ASM..ENDASM Inserts the block of assembler instructions
Syntax: ASM
/
assembler instructions
/
ENDASM
Description ASM and ENDASM instructions give the information that the code between ASM and
: ENDASM assembler type. Maximal size of the assembler code depends on the size of the programming memory of a microcontroller. In case of a PIC16F877 microcontroller the maximal value of an assembler code is 8K.
Example:
Basic for PIC Microcontrollers |
33 |
|||
Main : |
||||
asm |
' Beginning of asm part of the program |
|||
bsf |
PORTA, 0 |
' |
set RA0 to "1" |
|
bcf |
PORTB, 3 |
' |
set RB3 to "0" |
|
endasm |
' End of asm part of the program |
|||
Loop : goto Loop |
||||
end |
||||
4.3 ADCIN |
Write the values from the input of the internal AD converter |
||||
Syntax: |
ADCIN channel, variable |
||||
Description |
ADCIN performs A/D conversion of an input analogue signal in microcontrollers that have |
||||
: |
A/D converter built in chip (i.e. PIC16F877). The value read in is stored into a designated |
||||
variable. Before use of ADCIN instruction the appropriate TRIS register must be initiated |
|||||
so that the given is designated input one. Beside that in ADCON1 register one has to set |
|||||
the input pins for analogue working regime, format of the results and tact of A/D |
|||||
converter. |
|||||
Example: |
|||||
DEFINE ADC_BITS 8 |
' |
Converted result will have 8, 10 or 12 bits |
|||
DEFINE ADC_CLOCK 3 |
' |
Clock for A/D converter |
|||
DEFINE ADC_SAMPLEUS 10 |
' |
Sampling time expressed in us |
|||
B0 var byte |
|||||
Main : |
|||||
TRISA = $FF |
' |
All pins of port A are input |
|||
ADCON1 = 0 |
' |
PORTA is analog |
|||