Giáo trình Kỹ thuật vi xử lý - Chương 4: Viết chương trình cho MCU 8051

Häc tr×nh IV. ViÕt ch-¬ng tr×nh cho MCU 8051  
IV-1. Giíi thiÖu chung vÒ viÕt ch-¬ng tr×nh cho MCU 8051.  
1/ CPU vµ ng«n ng÷ m¸y.  
- Qua c¸c phÇn ®· nghiªn cøu ë trªn ta thÊy mét Kit VSL hay mét PC bao giê  
còng bao gåm hai phÇn ®ã lµ:  
- PhÇn cøng (HARD WARE).  
- PhÇn mÒm (SOFT WARE).  
- Ta l¹i biÕt phÇn cøng (hard ware) lµ mét tËp hîp c¸c m¹ch ®iÖn sè, tËp hîp c¸c  
m¹ch ®iÖn nµy sÏ kh«ng thÓ ho¹t ®éng ®-îc nÕu kh«ng cã c¸c phÇn mÒm ®iÒu  
khiÓn nã. Ta cã thÓ gäi phÇn mÒm cña mét hÖ thèng VSL lµ linh hån cña nã bëi vú  
nÕu kh«ng cã nã th× m¸y tÝnh kh«ng thÓ ho¹t ®éng ®-îc.  
- PhÇn mÒm cña mét hÖ thèng VSL l¹i bao gåm 2 phÇn ®ã lµ phÇn mÒm hÖ thèng vµ  
phÇn mÒm øng dông.  
- PhÇn mÒm hÖ thèng dïng ®Ó ®iÒu khiÓn ch-¬ng tr×nh nh- lµ phÇn mÒm  
WINDOW,  
MSDOS..  
- PhÇn mÒm øng dông lµ c¸c phÇn mÒm dïng ®Ó ch¹y ch-¬ng tr×nh nh- lµ phÇn  
mÒm BASCOM, $S51, C, C++......  
- Nh-ng mét ®iÒu ®¸ng chó ý lµ mét hÖ thèng VSL chØ cã kh¶ n¨ng hiÓu ®-îc c¸c  
ký tù lµ “0” vµ “1” bëi v× mét hÖ thèng VSL chÝnh lµ mét tæ hîp tËp hîp c¸c m¹ch  
®iÖn nªn nã chØ cã kh¶ n¨ng hiÓu ®-îc mét tËp lÖnh bao gåm c¸c ký tù “0” vµ  
“1”®-îc viÕt d-íi d¹ng nh÷ng tõ m· hay cßn gäi lµ nh÷ng chØ lÖnh.  
- Nh-ng ta l¹i biÕt mét ch-¬ng tr×nh øng dông bao giê còng rÊt dµi cã khi tíi hµng  
ngh×n trang bëi vËy viÖc viÕt mét ch-¬ng tr×nh b»ng ng«n ng÷ m¸y sÏ rÊt phøc t¹p  
cho ng-êi viÕt v× nã khã nhí vµ dÔ g©y nhÇm lÉn bëi mét chuçi nh÷ng ký tù chØ lµ  
nh÷ng sè “0” vµ “1”, bëi vËy viÖc cho ra ®êi nh÷ng phÇn mÒm võa gÇn gòi víi con  
ng-êi, võa lµm cho m¸y tÝnh hiÓu ®-îc lµ ®iÒu cÇn thiÕt.  
- XuÊt ph¸t tõ c¸ch ®Æt vÊn ®Ò trªn cho nªn song song víi viÖc cho ra ®êi c¸c phÇn  
cøng c¸c h·ng lín trªn thÕ giíi ®· cho ra ®êi rÊt nhiÒu phÇn mÒm kh¸c nhau cßn gäi  
lµ ng«n ng÷ . Nh×n chung chóng thuéc 3 ng«n ng÷ lo¹i c¬ b¶n sau:  
- Ng«n ng÷ m¸y (MACHINE LANGUAGE)  
- Ng«n ng÷ bËc thÊp (LOW DEGREE). Nh- lµ ASSEMBLY.  
- Ng«n ng÷ bËc cao (HIGH DEGREE), nh- lµ PASCAL, C, C++, BASIC.....  
* Vµ ngµy nay ng-êi ta chØ viÕt ch-¬ng tr×nh b»ng ng«n ng÷ bËc cao (rÊt Ýt khi viÕt  
b»ng ng«n ng÷ bËc thÊp), vµ ng-êi ta sö dông mét tr×nh liªn kÕt (COMPILE) ®Ó  
chuyÓn ®æi tõ ng«n ng÷ bËc cao hay ng«n ng÷ bËc thÊp ra ng«n ng÷ m¸y  
- ViÖc viÕt ch-¬ng tr×nh b»ng ng«n ng÷ bËc thÊp tuy dÔ hiÓu vµ ®é chÝnh x¸c cao  
nh-ng c©u lÖnh rÊt dµi v× vËy viÖc viÕt ch-¬ng tr×nh rÊt mÊt thêi gian vµ hÇu hÕt c¸c  
nhµ s¶n xuÊt phÇn mÒm kh«ng sö dông lo¹i ng«n ng÷ nµy.  
- Ng«n ng÷ bËc cao rÊt ®a d¹ng nªn tuú theo tõng lÜnh vùc mµ ng-êi ta lùa chän  
phÇn mÒm cho phï hîp.  
- §èi víi c¸c chuyªn ngµnh §iÖn, §iÖn tö ng-êi ta hay dïng c¸c phÇn mÒm C,  
C++, BASIC vµ ®«i khi ng-êi ta dïng ASSEMBLY.  
2/ Ng«n ng÷ ASSEMBLY.  
- ASSEMBLY lµ ng«n ng÷ gÇn gòi nhÊt víi ng«n ng÷ m¸y vµ nã t¹o ra nh÷ng gîi  
nhí trong c©u lÖnh lµm cho ng-êi lËp tr×nh dÔ hiÓu ®Ó viÕt ch-¬ng tr×nh.  
- Do dÔ hiÓu nªn dÔ söa lçi ch-¬ng tr×nh.  
- Do nã gÇn víi ng«n ng÷ m¸y nªn ®é chÝnh s¸c cña ch-¬ng tr×nh cao.  
- Nã cã nh-îc ®iÓm lµ c©u lÖnh rÊt dµi nªn viÕt l©u vµ ch-¬ng tr×nh ch¹y chËm.  
EG.  
- Mét c©u lÖnh cña ASSEMBLYnh- sau:  
- N¹p néi dung chøa trong thanh ghi B  
LD A,B  
vµo thanh ghi A (FOR Z80)  
-
- LÖnh nµy ®-îc ph©n tÝch nh- sau:  
LD = OPCODE - gäi lµ m· lÖnh  
A,B gäi lµ c¸c to¸n h¹ng (OPERAND)  
A lµ to¸n h¹ng ®Ých  
B lµ to¸n h¹ng nguån.  
- LÖnh nµy ®-îc m· ho¸ nh- sau:  
0111.100(B) = 78(H).  
EG.  
LD A,C. N¹p néi dung chøa trong thanh ghi C vµo thanh ghi A (cho Z80).  
- LÖnh nµy ®-îc m· ho¸ nh- sau:  
0111.1001(B) = 79(H).  
EG.  
LÖnh ADD. Hai sè nhÞ ph©n cña 8051 ®-îc m· ho¸ nh- sau:  
1011.0011(B) = B3(H).  
IV- 2. CÊu tróc cña ch-¬ng tr×nh. (STRUCTURED PPROGRAMMING)  
- CÊu tróc cña ch-¬ng tr×nh hay lËp tr×nh cã cÊu tróc lµ mét kü thuËt dïng ®Ó tæ  
chøc vµ viÕt ch-¬ng tr×nh sao cho gi¶m ®-îc ®é phøc t¹p, dÔ hiÓu, dÔ gì rèi , dÔ  
thay ®æi. ý t-ëng cña ch-¬ng tr×nh ®-îc cÊu tróc mét c¸ch râ rµng m¹ch l¹c. §Ó  
ph¶n ¸nh ý t-ëng ®ã mét ch-¬ng tr×nh hay mét ng-êi viÕt ch-¬ng tr×nh ®Òu cã thÓ  
sö dông ba lo¹i cÊu tróc c¬ b¶n sau ®©y:  
- CÊu tróc d¹ng ph¸t biÓu (STATMEN).  
- CÊu tróc d¹ng vßng lÆp (LOOP).  
- CÊu tróc d¹ng lùa chän (CHOICE)  
1/ C¸c ph¸t biÓu  
- §ã lÇ viÖc g¸n mét gi¸ trÞ cho mét biÕn nµo ®ã nh-:  
- [ COUNT = 0].  
- HoÆc gäi mét ch-¬ng tr×nh con nh-:  
- PRINT-STRING.  
2/ CÊu tróc vßng lÆp.  
- §©y lµ mét cÊu tróc c¬ b¶n ®-îc øng dông rÊt nhiÒu, cã 2 d¹ng vßng lÆp nh- sau:  
* Ph¸t biÓu WHILE- DO.  
EG. Trong khi cê nhí cña 8051 cßn b»ng 1, mét ph¸t biÓu ®-îc thùc thi nh- sau:  
- Gi¶i m· (PSEUDO):  
WHILE [C = = 1] DO.  
ENTER  
C = 1  
?
EXIT  
STATMENT  
- Ch-¬ng tr×nh ®-îc viÕt cho 8051 nh- sau:  
ENTER  
STTATEMENT........  
JMP ENTER.  
EXIT ...... (CONTINUE).  
* Ph¸t biÓu REPEAT- UNTIL.  
JNC EXIT  
- CÊu tróc d¹ng nµy ®-îc dïng cho c¸c bµi to¸n mµ c«ng viÖc ®-îc lÆp l¹i Ýt nhÊt 1  
lÇn.  
- Gi¶ m·:  
REPEAT  
[ACC = @POINTER].  
[INCREAMENT POINTER].  
UNTIL  
[ACC = = “Z” OR ACC = = 0].  
- L-u ®å:  
ENTER  
GET A CHRACTER  
INCREAMENT  
POINT  
NO  
NO  
CHARACTER = Z ?  
CHARACTER = 0 ?  
YES  
YES  
EXIT  
- Ch-¬ng tr×nh cho 8051 ®-îc viÕt nh- sau:  
STATEMENT:  
MOV A,@R0.  
INC R0.  
JZ EXIT  
CJNE A,#”Z”,STATEMENT  
RET  
EXIT:  
3/ CÊu tróc lùa chän.  
- Lµ cÊu tróc t¹o nªn sù rÏ nh¸nh trªn ®-êng.  
* Ph¸t biÓu IF- THEN- ELSE.  
EG.  
- H·y viÕt mét chuçi lÖnh ®Ó nhËp vµ kiÓm tra mét ký tù cña PORT nèi tiÕp. NÕu ký  
tù lµ m· ASCII hiÓn thÞ ®-îc cho hiÓn thÞ ký tù., ng-îc l¹i cho hiÓn thÞ ký tù (. ).  
- Gi¶ m·:  
[INPUT CHARACTER]  
IF [CHARACTER = = GRAPHIC]  
THEN [ECHO CHARACTER]  
ELSE [ECHO “.” ].  
- L-u ®å:  
ENTER  
INPUT CHRACTER  
GRAPHIC  
CHARACTER  
NO  
YES  
ECHO  
“.”  
ECHO  
CHARD  
ACTER  
EXIT  
- Ch-¬ng tr×nh ®-îc viÕt cho 8051.  
ENTER:  
ACALL INCH  
ACALL ISGPH  
JNC STMENT2  
STMENT1:  
STMENT2:  
ACALL OUTCHR  
JMP EXIT  
MOV A,#’.’  
ACALL OUTCHR  
EXIT: (CONTINUE)  
ACALL INCH  
ACALL ISGRPH  
JC SKIP  
MOV A,#’.’  
ACALL OUTCHR  
SKIP  
(CONTINUE)  
DO BEGIN  
WHILE[1]  
[INPUT CHARACTER]  
IF [CHARACTER = = GRAPHIC]  
THEN [ECHO CHARACTER]  
ELSE [ECHO’.’ ]  
END  
* Ph¸t biÓu CASE:  
- Ph¸t biÓu CASE lµ biÕn thÓ cña ph¸t biÓu IF THEN, ®-îc øng dông khi cã mét  
trong nhiÒu ph¸t biÓu ®-îc lùa chän.  
- Gi¶ m·:  
CASE [EXPRESSION] OF  
0: [STATEMENT 0]  
1: [ STATEMENT1]  
2: [STATEMENT 2]  
N: [STATEMENT N]  
[DEFAULT STATEMENT]  
END - CASE  
- L-u ®å.  
ENTER  
EXPRES.N  
EXPRESS.3  
EXPRESS.2  
EXPRESS.1  
FALSE  
FALSE  
FALSE  
FALSE  
TRUE  
TRUE  
TRUE  
TRUE  
STMEN. N  
STMENT 3  
DEFALT  
ST.MEN  
STMEN. 1  
STMENT.2  
EXIT  
IV-3. C¸c vÝ dô vÒ lËp ch-¬ng tr×nh cho 8051.  
§Ó viÕt ch-¬ng tr×nh cho 8051 ta cã thÓ thùc hiÖn b»ng c¸ch viÕt ch-¬ng tr×nh  
b»ng ng«n ng«n ng÷ ASSEBLY hoÆc b»ng bÊt kú mét lo¹i ng«n ng÷ kh¸c. §Ó vÊn  
®Ò tr×nh bÇy ®-îc ®¬n gi¶n vµ ng-êi häc dÔ thùc hiÖn, trong tµi liÖu nµy xin tr×nh  
bÇy hai c¸ch viÕt b»ng ng«n ng÷ ASSEMBLY vµ b»ng ng«n ng÷ BASCOM, chñ  
yÕu lµ b»ng ng«n ng÷ BASCOM, chØ cã nh÷ng vÝ dô ®¬n gi¶n míi tr×nh bµy b»ng  
ng«n ng÷ ASSEMBLY.  
1/ ViÕt ch-¬ng tr×nh b»ng ng«n ng÷ sEMBLY.  
§Ó viÕt ch-¬ng trinh b»ng ng«n ng÷ ASSEMBLY ta cÇn n¾m v÷ng tËp lÖnh cña  
8051 ®· ®-îc tr×nh bÇy á môc III-5.  
VÝ dô 1:  
H·y viÕt ch-¬ng tr×nh ®Ó t¹o xung vu«ng t¹i ch©n P1.1, víi F= 10HZ.  
$MOD51  
ORG 0000H  
LOOP:  
SETB P1.1  
LCALL DELAY  
CLR P1.1  
;P1.1=1  
;P1.1= 0  
LCALL DELAY  
LJMP LOOP  
DELAY:  
MOV R2,#200  
X: DJNZ R1,X  
MOV r1,#250  
MOV R1,#250  
DJNZ R2,X  
RET  
END.  
VÝ dô 2:  
H·y viÕt ch-¬ng tr×nh ®Ó ®iÒu khiÓn 3 LED g¾n vµo PORT1 sao cho c¸c LED s¸ng  
dÇn tõng c¸i mét.  
( Anode cña c¸c LED nèi lªn d-¬ng nguån, kh«ng dïng vßng lÆp).  
; B¾t ®Çu ch-¬ng tr×nh  
$MOD51 ORG 0000H  
LOOP  
MOV A,#1111.1110B.  
MOV P1,A  
; P1.0 = 0.  
LCALL  
RL  
MOV P1,A  
LCALL  
RL  
DELAY  
A
; A = 1111.1101B  
; P1.1 = 0  
DELAY  
A
P1,A  
; A = 1111.1011B  
; P1.2 =0  
MOV  
LCALL  
RL  
LCALL  
SJMP  
DELAY  
A
DELAY  
LOOP  
R1,#250  
R2,#200  
R1,X  
; A = 1111.0111B  
DELAY:MOV  
MOV  
X: DJNZ  
MOV  
R1,#250  
R2,X  
DJNZ  
RET  
END  
VÝ dô 3:  
; ViÕt ch-¬ng tr×nh ®Ó ®iÒu khiÓn 8 LED g¾n vµo PORT1, sao cho c¸c lED s¸ng dÇn  
lªn.  
; Ch-¬ng tr×nh b¾t ®Çu.  
$MOD51ORG 0000H  
LOOP: MOV A,#1111.1110B ; A = 01H  
NEXT: MOV P1,A  
; P1.0 = 1.  
LCALL  
CLR  
RLC  
DELAY  
C
A
; C =0  
; A = 1111.1100B  
JC  
SJMP  
DELAY: MOV  
MOV  
NEXT  
LOOP  
R1,#250  
R2,#200  
R1,X  
R1,#250  
R2,X  
; C = 1 th× tiÕp tôc.  
X:  
DJNZ  
MOV  
DJNZ  
RET  
END.  
2/ ViÕt ch-¬ng tr×nh b»ng ng«n ng÷ BASCOM  
A/ Gi¬Ý thiÖu tËp lÖnh cña BASCOM.  
(BASCOM Language Fundamenttals).  
Language Fundamentals  
(Ng«n ng÷ c¬ b¶n cña BASCOM)  
Characters from the BASCOM character set are put together to form labels,  
keywords, variables and operators.  
These in turn combine to form statements that make up a program.  
This chapter describes the character set and the format of BASCOM program lines.  
In particular, it discusses:  
* The specific characters in the character set and the special meanings of some  
characters.  
* The format of a line in a BASCOM program.  
*Line labels.  
* Program line length.  
Character Set  
(Bé ký tù cña BASCOM)  
The BASCOM BASIC character set consist of alphabetic characters,  
numeric characters, and special characters.  
The alphabetic characters in BASCOM are the uppercase letters (A-Z) and  
lowercase letters (az) of the alphabet.  
The BASCOM numeric characters are the digits 0-9.  
The letters can be used as parts of hexadecimal numbers.  
The following characters have special meanings in BASCOM statements and  
expressions:  
Character  
name  
ENTER  
Terminates input of a line (KÕt thóc viÖc nhËp d÷ liÖu cho 1 dßng)  
Blank ( or space) (Kho¶ng trèng)  
'
Single quotation mark (apostrophe)  
(DÊu ngoÆc ®¬n)  
*
Asterisks (multiplication symbol)  
(Dêu sao, biÓu t-îng cña phÐp nh©n)  
Plus sign  
(DÊu céng)  
Comma  
+
,
(Dêu phÈy)  
Minus sign  
-
(DÊu trõ)  
.
Period (decimal point)  
(DÊu chÊm thËp ph©n)  
Slash (division symbol) will be handled as \  
(DÊu g¹ch chÐo- biÓu t-îng cña phÐp chia)  
Colon  
/
:
(DÊu hai chÊm)  
Double quotation mark  
(DÊu ngoÆc kÐp)  
"
;
Semicolon  
(DÊu chÊm phÈy)  
Less than  
(DÊu nhá h¬n)  
Equal sign (assignment symbol or relational operator)  
(DÊu b»ng)  
Greater than  
(DÊu lín h¬n)  
Backslash (integer/word division symbol)  
(DÊu g¹ch chÐo ng-îc)  
<
=
>
\
The BASCOM program line  
(Dßng lÖnh cña BASCOM)  
BASCOM program lines have the following syntax:  
(Dßng lÖnh cã d¹ng nh- sau)  
[[line-identifier]] [[statement]] [[:statement]] ... [[comment]]  
Using Line Identifiers  
(NhËn d¹ng c¸c dßng lÖnh)  
BASCOM support one type of line-identifier; alphanumeric line labels:  
An alphabetic line label may be any combination of from 1 to 32 letters and digits,  
starting with a letter and ending with a colon.  
BASCOM keywords are not permitted. The following are valid alphanumeric line  
labels:  
Alpha:  
ScreenSUB:  
Test3A:  
Case is not significant. The following line labels are equivalent:  
alpha:  
Alpha:  
ALPHA:  
Line labels may begin in any column, as long as they are the first characters other  
than blanks on the line.  
Blanks are not allowed between an alphabetic label and the colon following it.  
A line can have only one label.  
BASCOM Statements  
(Ph¸t biÓu cña BASCOM)  
A BASCOM statement is either " executable" or " nonexecutable" .  
An executable statement advances the flow of a programs logic by telling the  
program what tot do next.  
Non executable statement perform tasks such as allocating storage for variables,  
declaring and defining variable types.  
The following BASCOM statements are examples of nonexecutable statements:  
*
*
REM or (starts a comment)  
DIM  
A " comment" is a nonexecutable statement used to clarify a programs operation  
and purpose.  
A comment is introduced by the REM statement or a single quote character(').  
The following lines are equivalent:  
PRINT " Quantity remaining" : REM Print report label.  
PRINT " Quantity remaining" ' Print report label.  
More than one BASCOM statement can be placed on a line, but colons(:) must  
separate statements, as illustrated below.  
FOR I = 1 TO 5 : PRINT " Gday, mate." : NEXT I  
BASCOM LineLength  
If you enter your programs using the built-in editor, you are not limited to any line  
length, although it is advised to shorten your lines to 80 characters for clarity.  
Data Types  
(KiÓu d÷ liÖu)  
Every variable in BASCOM has a data type that determines what can be stored in  
the variable. The next section summarizes the elementary data types.  
Elementary Data Types  
(C¸c kiÓu d÷ liÖu c¬ b¶n)  
*
*
Bit (1/8 byte)  
Byte (1 byte).  
Bytes are stores as unsigned 8-bit binary numbers ranging in value from 0 to 255.  
Integer (two bytes).  
*
Integers are stored as signed sixteen-bit binary numbers ranging in value from -  
32,768 to +32,767.  
*
Word (two bytes).  
Words are stored as unsigned sixteen-bit binary numbers ranging in value from 0  
to 65535.  
*
Long (four bytes).  
Longs are stored as signed 32-bit binary numbers ranging in value from -  
2147483648 to 2147483647.  
*
Single  
Singles are stored as signed 32 bit binary numbers.  
String (up to 254 bytes).  
*
Strings are stored as bytes and are terminated with a 0-byte.  
A string dimensioned with a length of 10 bytes will occupy 11 bytes.  
Variables can be stored internal (default) or external.  
(C¸c biÕn sè cã thÓ ®· ®-îc l-u tr÷ trong bé nhí hoÆc l;-u tr÷ ë bªn ngoµi )  
Variables  
(C¸c biÕn sè)  
A variable is a name that refers to an object--a particular number.  
(Tªn cña biÕn sè ®-îc tham kh¶o chi tiÕt ë môc nãi vÒ sè)  
A numeric variable, can be assigned only a numeric value (either integer,byte long,  
single or bit).  
The following list shows some examples of variable assignments:  
(B¶ng d-íi ®©y chØ ra c¸c vÝ dô vÒ viÖc g¸n biÕn sè nh- thÕ nµo)  
*
*
*
A constant value:  
A = 5  
C = 1.1  
The value of another numeric variable:  
abc = def  
k = g  
The value obtained by combining other variables, constants, and  
operators:  
(C¸c to¸n tö):  
Temp = a + 5  
Temp = C + 5  
Variable Names  
(C¸c tªn cña biÕn sè)  
A BASCOM variable name may contain up to 32 characters.  
The characters allowed in a variable name are letters and numbers.  
The first character in a variable name must be a letter.  
A variable name cannot be a reserved word, but embedded reserved words are  
allowed.  
For example, the following statement is illegal because AND is a reserved word.  
AND = 8  
However, the following statement is legal:  
ToAND = 8  
Reserved words include all BASCOM commands, statements, function names,  
internal registers and operator names.  
(see BASCOM Reserved Words , for a complete list of reserved words).  
You can specify a hexadecimal or binary number with the prefix &H or &B.  
a = &HA , a = &B1010 and a = 10 are all the same.  
Before assigning a variable you must tell the compiler about it with the DIM  
statement.  
Dim b1 As Bit, I as Integer, k as Byte , s As String * 10  
You can also use DEFINT, DEFBIT, DEFBYTE and/or DEFWORD.  
For example DEFINT c tells the compiler that all variables that are not dimensioned  
and that are beginning with the character c are of the Integer type.  
Expressions and Operators  
(C¸c to¸n tö vµ c¸c c¸ch tr×nh bÇy)  
This chapter discusses how to combine, modify, compare, or get information about  
expressions by using the operators available in BASCOM.  
Anytime you do a calculation you are using expressions and operators.  
This chapter describes how expressions are formed and concludes by describing the  
following kind of operators:  
*
*
*
Arithmetic operators, used to perform calculations.  
Relational operators, used to compare numeric values.  
Logical operators, used to test conditions or manipulate individual  
bits.  
*
Functional operators, used to supplement simple operators.  
Expressions and Operators  
An expression can be a numeric constant, a variable, or a single value  
obtained by combining constants, variables, and other expressions with operators.  
Operators perform mathematical or logical operations on values.  
The operators provides by BASCOM can be divided into four categories, as follows:  
1.  
2.  
3.  
4.  
Arithmetic (Sè häc)  
Relational (Quan hÖ)  
Logical (Logic)  
Functional (Hµm sè)  
Arithmetic (Sè häc)  
Arithmetic operators are +, - , * and \.  
*
Integer  
Integer division is denoted by the backslash (\).  
Example:  
Z = X \ Y  
*
Modulo Arithmetic  
Modulo arithmetic is denoted by the modulus operator MOD.  
Modulo arithmetic provides the remainder, rather than the quotient,  
of an  
integer division.  
Example: X = 10 \ 4 : remainder = 10 MOD 4  
*
Overflow and division by zero  
Division by zero, produces an error.  
At this moment there is no message, so you have to insure yourself that such wont  
happen.  
Relational Operators  
(C¸c to¸n tö quan hÖ)  
Relational operators are used to compare two values as shown in the table below.  
The result can be used to make a decision regarding program flow.  
Operator  
=
<>  
<
Relation Tested Expression  
Equality X = Y (B»ng)  
Inequality X <> Y (Kh«ng b»ng)  
Less than X < Y (Nhá h¬n)  
>
<=  
>=  
Greater than X > Y (Lín h¬n)  
Less than or equal toX <= Y (Nhá h¬n hoÆc b»ng)  
Greater than or equal to X >= Y (Lín h¬n hoÆc b»ng)  
Logical Operators  
(C¸c to¸n tö Logic)  
Logical operators perform tests on relations, bit manipulations, or Boolean  
operators.  
There are four operators in BASCOM, they are :  
(Cã 4 to¸n tö Logic lµ):  
Operator  
NOT  
Meaning  
Logical complement  
(Bï logic)  
AND  
OR  
Conjunction  
(KÕt hîp)  
Disjunction  
(Ph©n t¸ch)  
XOR  
Exclusive or  
(HoÆc lo¹i trõ)  
It is possible to use logical operators to test bytes for a particular bit pattern.  
For example the AND operator can be used to mask all but one of the bits  
of a status byte, while OR can be used to merge two bytes to create a particular  
binary value.  
Example  
A = 63 And 19  
PRINT A  
A = 10 Or 9  
PRINT A  
Output  
16  
11  
Floating point  
(DÊu chÊm ®éng)  
A new datatype is added to BASCOM : the single.  
(Mét kiÓu d÷ liÖu míi ®-îc thªm vµo BASCOM ®ols lµ: SING (DUY NHAT)  
Single numbers conform to the IEEE binary floating point standard.  
An eight-bit exponent and 24 bit mantissa are supported.  
Using four bytes, the format is shown below:  
31 30________23 22______________________________0  
S exponent mantiss  
(sè mò)  
(phÇn ®Þnh trÞ)  
The exponent is biased by 128. Above 128 are positive exponents and  
below are negative. The sign bit is 0 for positive numbers and 1 for  
negative. The mantissa is stored in hidden bit normalized format so  
that 24 bits of precision can be obtained.  
All mathematical operations are supported by the single.  
(TÊt c¶ c¸c to¸n tö to¸n cña phÐp to¸n th× ®-îc cung cÊp t¹i pphÇn d÷ liÖu SINH)  
You can also convert a single to an integer or word or vise versa:  
Dim I as Integer, S as Single  
S = 100.1  
I = S  
'assign the single  
'will convert the single to an integer  
Take a look at the single.bas example for more information.  
Arrays  
(M¶ng)  
Arrays are also a new extension to BASCOM.  
An array is a set of sequentially indexed elements having the same type. Each  
element of an array has a unique index number that identifies it. Changes made to  
an element of an array do not affect the other elements.  
The index must be a numeric constant, a byte, an integer or a word. This means that  
an array can hold 65535 elements as a maximum. The minimum value is 1 and not  
zero as in QB.  
Arrays can be used on each place where a 'normal' variable is expected but there are  
a few exceptions.  
These exceptions are shown in the help topics.  
Note that there are no BIT arrays in BASCOM-8051.  
Example:  
Dim a(10) as byte 'make an array named a,with 10 elements (1 to 10)  
Dim c as Integer  
For C = 1 To 10  
a(c) = c  
Print a(c)  
'assign array element  
'print it  
Next  
Strings  
(Chuçi)  
Strings can be up to 254 characters long in BASCOM.  
To save memory you must sepcify how long each string must be with the DIM  
statement.  
Dim S As String * 10  
This will reserve space for the string S with a length of 10 bytes. The actually length  
is 11 bytes because a nul(0) is used to terminate the string.  
You can concatenate string with the + sign.  
Dim S As String * 10 , Z As String * 10  
S = "test"  
Z = S + "abc"  
In QB you can assign a string with a value and add the original string (or a part of  
it) too :.  
S = "test"  
S = "a" + s  
This will result in the string "atest"  
In BASCOM this is NOT possible because this would requiere a copy of the string.  
In BASCOM the string S is assigned with "a" and on that moment the original  
string S is destroyed. So you must make a copy of the string yourself in the event  
you need this functionality.  
B.BASCOM STATEMENTS  
-1-  
1WRESET, 1WREAD, 1WWRITE  
-COMPILER DIRECTIVES-  
#IF  
#ELSE  
#ENDIF  
$ASM - $END ASM  
$INCLUDE  
$BAUD  
$BGF  
$CRYSTAL  
$DEFAULT XRAM  
$IRAMSTART  
$LARGE  
$LCD  
$MAP  
$NOBREAK  
$NOINIT  
$NONAN  
$NONULL  
$NOSP  
$OBJ  
$RAMSIZE  
$RAMSTART  
$REGFILE  
$ROMSTART  
$SERIALINPUT  
$SERIALINPUT2LCD  
$SERIALOUTPUT  
$SIM  
-A-  
ABS  
ALIAS  
ASC  
AVG  
-B-  
BITWAIT  
BCD  
BREAK  
-C-  
CALL  
CLOSE  
CLS  
CHR  
CONFIG  
CONST  
Tải về để xem bản đầy đủ
doc 45 trang Thùy Anh 14600 Free
Bạn đang xem 20 trang mẫu của tài liệu "Giáo trình Kỹ thuật vi xử lý - Chương 4: Viết chương trình cho MCU 8051", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • docgiao_trinh_ky_thuat_vi_xu_ly_chuong_4_viet_chuong_trinh_cho.doc