You are on page 1of 16

MENGENAL DAN BELAJAR PEMOGRAMAN DENGAN INSTRUKSI BASIC BASCOM-8051

PENGANTAR PROGRAM UNTUK MENGHITUNG FREKUENSI DETAK JANTUNG


$regfile = "8052.dat" $crystal = 12000000 Config Lcd = 40 * 2 Sw Alias 0.0 Dim A As Long , B_ As Byte , Jmld_jantung As Byte , D As Byte '-------------------------------------------------------Do Mulai_1: B_ = 0 Jmld_jantung = 0 A=0 If 0!1 = 1 "#en Lo$ate 1 , % L$d &ALA" D'"'()I JA*"+*, B'L+M A("I-& 'lseif 0!1 = 0 "#en .ls Lo$ate 1 , % L$d & '*,+(+/A* -/'(+'*)I D'"A( JA*"+*, A("I- & ,osu0 Mulai 'nd If Loo1 '-------------------------------------------------------Mulai: 'jumla# a no2mal 3000 = 40 se$ond -o2 A = 0 "o %00 Lo$ate 6 , 1 L$d A If )7 = 0 And B_ = 0 "#en In$2 B_ In$2 Jmld_jantung Lo$ate 1 , 1 L$d Jmld_jantung 'untu5 simulasi A = 300

'lseif )7 = 1 And B_ = 1 "#en B_ = 0 'lseif A = 300 "#en : ,osu0 .om1a2e 'nd If 8aitms 6 *e9t A /etu2n '-------------------------------------------------------.om1a2e: .ls If Jmld_jantung := ;< And Jmld_jantung == ;0 "#en Lo$ate 1 , 1 L$d & (>*DI)I JA*"+*, A*DA: & 'analisa 1 Lo$ate 1 , 1? L$d & DALAM ('ADAA* *>/MAL& '============================================= 'lseif Jmld_jantung = ?< "#en Lo$ate 1 , 1 L$d & (>*DI)I JA*"+*, A*DA & Lo$ate 1 , 1? L$d & DALAM ('ADAA* L'MA@& 'lseif Jml_jantung : 11 "#en '============================================= Lo$ate 1 , 1 L$d & (>*DI)I JA*"+*, :& Lo$ate 1 , 1? 'analisa 3 L$d & DALAM ('ADAA* + *>/MAL A "I*,,I & 'nd If 8aitms 1000 '============================================= .ls ,oto Mulai_1 '------------------------------------------------------

'analisa 6

MATERI
MENGENAL INSTRUKSI BASCOM PEMOGRAMAN OUTPUT LED

DATA INPUT / OUTPUT PORT OPERASI SHIFT T SHIFT LEF SHIFT RIGHT OPERASI COUNTER UP COUNTER DOWN COUNTER UP-DOWN COUNTER PEMOGRAMAN OUTPUT DIPSLAY OUTPUT 7 SEGMENT OUTPUT LCD

MENGENAL INSTRUKSI BASCOM


1WIRE 1Wire routines allow you to communicate with Dallas 1wire chips. 1WRESET, 1WREAD, 1WWRITE , 1WSEARCHFIRST, 1WSEARCHNEXT , 1WIRECOUNT Conditions Conditions execute a part of the program depending on the condition IF , ELSE , END IF , EXIT, DO , LOOP , SELECT , FOR , NEXT , TO , THEN , WHILE .. WEND Configuration Configuration command initialize the hardware to the desired state. CONFIG Conversion A conversion routine is a function that converts a number or string. ASC, BCD , CHR , FUSING , HEX , HEXVAL , HIGH , HIGHW ,MAKEDEC , MAKEBCD , MAKEINT , STR , VAL Delay Delay routines delay the program for the specified time. DELAY , WAIT , WAITMS

Directives Directives are special instructions for t e co!piler" T e# can overri$e a settin% fro! t e IDE" #IF , #ELSE , #ENDIF , $ASM - $END ASM ,$INCLUDE , $BAUD , $BGF , $CRYSTAL , $DEFAULT XRAM , $IRAMSTART , $LARGE , $LCD , $MAP , $NOBREAK , $NOINIT , $NONAN , $NONULL , $NORAMCLEAR , $NOSP , $OBJ , $RAMSI E , $RAMSTART , $REGFILE , $ROMSTART , $SERIALINPUT , $SERIALINPUT!LCD , $SERIALOUTPUT , $SIM Graphical LCD Graphical CD commands extend the normal text CD commands. !"#$ % "&'W!(C I2C ()C commands allow you to communicate with ()C chips with the $W( hardware or with emulated ()C hardware. ()C*#C#(+# % ()C"#,D % ()C"$A*$ % ()C"$'! % ()C*-.$# % ()CW-.$# Interrups (nterrupt related routines. ', (nterrupt % #,A- # % D("A- # % !*('*($. IO (/' commands are related to the (/' pins of the processor. A (A" % -($WA($ % D#-'0,C# % "#$ % *#"#$ Math 1ath functions A-" % A+G % 1A2 % 1(, % 1'D Micro 1icro statements are highly related to the micro processor. -*#A3 % !1%!4 % (D # % #,D % !'W#*D'W, % "$A*$ % "$'! % "$'! $(1#* Me ory Me!or# functions set or rea$ RAM & EE'ROM or flas !e!or#"

C'EEK & ERASE & IN' & OUT & 'EEK & 'OKE & DIM & READ & RESTORE & DATA & (AR'TR Re ote control *emote control statements send or receive (* commands for remote control. G#$*C5 R!"2#2 *"6)4) are serial routines that use the 0A*$ or emulate a 0A*$. WA($3#. % !*(,$ % !*(,$-(, % !*(,$&#2 % !0$ % '!#, %"!C % (,3#. % (,!0$ % (,!0$-(, % (,!0$&#2 % G#$ % C '"# !$I "!( routines communicate according to the "!( protocol with either hardware "!( or software emulated "!(. "!((, % "!('0$ % "!((,($ !tring "tring routines are used to manipulate strings. 1(D % CA"# % (,"$* % #7$ % #, % *(G&$ % "$*(,G % *#! AC# % "!AC# % 0CA"# %e&t LCD $ext CD routines wor8 with the normal text based CD displays. C " % C0*"'* % 7'0*$& (,# % &'1# % CD % CD(,($ % CD&#2 % 'CA$# % "&(7$ CD % "&(7$C0*"'* %D("! A. % 'W#* (,# % 0!!#* (,# % $&(*D (,#

'arious

$his section contains all statements that were hard to put into another group CA , C',"$ , C'0,$#* , D#C* , D#C A*# , D#7(,$ , D#7-($ ,D#7-.$# , D#7 CDC&A* , D#7W'*D , G#$AD , G#$AD)951 , G#$*C , , G'"0- , G'$' , (,C* , 'AD , ''30! , ''30!"$* , 'W , 'WW , ', +alue , *#AD1AGCA*D , *#1 , *#$0*, , *,D , *'$A$# , "&(7$ , "&(7$(, , "&(7$'0$ , "'0,D , "0- , "WA!

$EMOGR(M() D(!(R *(!COM +O*"1


1 DATA OUPUT PORT

PROGRAM-1 !"#$%&'( ) 1000000 D* P0 P1 P+ PP, P5 P. L**/

) ) ) ) ) ) )

1 + , 8 1. -+ .,

('L+A/A* ADA >/"

Catatan Data yang akan dikeluarkan ke port langsung dapat diset

PEMOGRAMAN DATA 0HE1 2


!"#$%&'( ) 1000000 D* P0 P1 P+ PP, P5 P. L**/ KELUARAN PADA PORT

) ) ) ) ) ) )

3H01 3H0+ 3H0, 3H08 3H10 3H+0 3H,0

Catatan

Data yang dikeluarkan ke port dalam hexa

!"erasi S#$%& L'%&


(!)(A* s+ift left $crystal = 1000000 ,i- . As /yte 0 1 As /yte 1=0 2=0 2=0 ,o 0=3 %or 1 = 0 &o 8 S+ift 0 0 Left 0 1 4ait 1 1e5t Loo" @asil >ut1ut

rogra- !6 6& S#$%& 7 ($)#&


!"#$%&'( ) 1000000 D45 1 A% B$&6 7 N A% B$&6 P1 ) 0 P+ ) 0

P- ) 0 D* P0 ) 3H80 F*# N ) 0 T* 8 S849& P0 7 R4:8& 7 1 F*# 1 ) 1 T* 1000000 N6;& N6;& L**/

PROGRAM UP-COUNTER

!"#$%&'( ) 1000000 D45 1 A% B$&6 7 N A% B$&6 P1 ) 0 D* P1 ) P1 < 1 F*# N ) 0 T* 8 N6;& L**/

P#*:#'5 U/-C*=>&6# 0 FOR- NE1T 2


!"#$%&'( ) 1000000 D45 1 A% B$&6 7 N A% B$&6 P1 ) 0 F*# N ) 1 T* ,0 P1 ) N W'4& 1 N6;&

PROGRAM UP COUNTER 0 WHILE-WEND 2


!"#$%&'( ) 1000000 D45 1 A% B$&6 7 N A% B$&6 7 A A% B$&6 A ) 0 W84(6 A ?) 10

P1 ) A I>"# A W'4& 1 W6>@

P#*:#'5 U/-C*=>&6# 0 0 S/D ,0 2


!"#$%&'( ) 1000000 D45 1 A% B$&6 7 N A% B$&6 P1 ) 0 P+ ) 0 P- ) 0 D* F*# N ) 1 T* ,0 P0 ) N F*# 1 ) 1 T* 10 N6;& N6;& L**/

PROGRRAM UP-COUNTER DENGAN IF THEN


!"#$%&'( ) 1000000 D45 I A% B$&6 7 A A% B$&6 A)0 L'A6(B P0 ) A A)A<1 I9 A ?) 10 T86> W'4& 1 G*&* L'A6( E>@ I9 E>@

PROGRAM DOWN -COUNTER

!"#$%&'( ) 1000000 D45 1 A% B$&6 7 N A% B$&6 7 A A% I>&6:6#

P1 ) 0 P+ ) 0 P- ) 0 D* A ) .0 F*# N ) 1 T* ,0 P0 ) A F*# 1 ) 1 T* 10 N6;& A)A-1 N6;& L**/

PENJELASAN INSTRUKSI

DIM

Dimension a variable.

Syntax
DIM var AS [XRAM/IRAM] type

Remarks
Var !ype ()A* %)A* Any valid variable name such as b1, i or longname. var can also be an array : ar(10) for e ample. "it#"oolean, "yte, $ord, %nteger, &ong, 'ingle or 'tring 'pecify ()A* to store variable in e ternal memory 'pecify %)A* to store variable in internal memory (default)

A string variable needs an additional parameter that specifies the length of the string: Dim s As ()A* String * 10 %n this case, the string can have a length of 10 characters. +ote that "%!' can only be stored in internal memory.

Difference with Q
%n ," you don-t need to dimension each variable before you use it. %n "A'./* you must dimension each variable before you use it. Also the ()A*#%)A* options are not available in ,".

See A!s"

./+'! , 0)A'0

#xam$!e
,"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" , , file3 DIM4*(! , de o3 DIM ,"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" Di Di Di Di Di *1 (s *it ( (s *yte C (s Integer L (s Long ! (s !ingle ,or ,use set ,5it can 5e 1 or 1 ,5yte range fro 1"200 "#2626 " 7#2628 ,integer range fro -c. 1//0"2112 MC! Electronics ,""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

,(ssign 5its *1 9 1 !et *1 ,(ssign 5ytes ( 9 12 ( 9 ( 7 1 ,(ssign integer C 9 "12 C 9 C 7 111 $rint C ,(ssign long L 9 12#:0268 $rint L ,(ssign single ! 9 12#:4026 $rint ! End

DO C LOOP

Acti"n
)epeat a bloc1 of statements until condition is true.

Syntax
D% 'tatements &%%' [ ()*I& e pression ]

Remarks
2ou can e it a D/..&//3 4ith the 0(%! D/ statement.

See a!s"

0(%! , $5%&0 $0+D , 6/) , +0(!

#xam$!e
Dim A As Byte Do A = A + 1 Print A Loop Until A = 10 Print A 'start the loop 'increment A 'print it 'Repeat loop until A = 10 'A is still 10 here

FOR
Acti"n

NE1T

0 ecute a bloc1 of statements a number of times.

Syntax

+%R var 7 start *%/D%,)*% end 8S*#' value9

Remarks
Var 'tart 0nd Value -ar . start. en0 . ste$ . !he variable counter to use !he starting value of the variable var !he ending value of the variable var !he value var is increased#decreased 4ith each time +0(! is encountered.

yte/ Integer/ ,"r0/ &"ng/ Sing!e1 yte/ Integer/ ,"r0/ &"ng/ Sing!e/ 2"nstant1 yte/ Integer/ ,"r0/ &"ng/ Sing!e/ 2"nstant1 yte/ Integer/ ,"r0/ &"ng/ Sing!e/ 2"nstant1

6or incremental loops you must use !/. 6or decremental loops you must use D/$+!/. 2ou may use !/ for a decremental loop but in that case you must use a negative '!03 : 6or a 7 10 !o 1 '!03 31 2ou must end a 6/) structure 4ith the +0(! statement. !he use of '!03 is optional. "y default a value of 1 is used.

See a!s"
)#X* / #XI* +%R

#xam$!e D45 Y A% B$&6 7 A A% B$&67; '% A$&6 $ ) 10 D5'E6 $ 10 D@* &84% 10 &456% D&84% *>6 '(%* D/#4>& &86 G'(=6% D>6;& ; 0"*=>& @*H>2 D>6;& ' 0"*=>& =/2 F*# A ) 1 T* 10 F*# 1 ) Y T* 1 P#4>& 1 F A N6;& N6;& D45 S A% S4>:(6 F*# S ) 1 T* + S&6/ 0 1 P#4>& S N6;& E>@

WAITMS
Acti"n
'uspends program e ecution for a given time in m'.

Syntax
,AI*MS m'

Remarks
m' !he number of milliseconds to 4ait. (1:;<<)

!he delay time is based on the used (:tal (fre=uency). !he use of interrupts can slo4 do4n this routine. !his statement is provided for the %;. statements. $hen you 4rite to an 003)/* you must 4ait for 10 m' after the 4rite instruction.

See a!s"

D0&A2 , $A%!

#xam$!e

$A%!*' 10 3rint >?>

-4ait for 10 m'

SHIFT
Acti"n
'hifts all bits one place to the left or right.

Syntax

S4I+* var , &06!#)%@5!

8 , shifts9

Remarks
Var 'hifts "yte, %nteger#$ord or &ong variable. !he number of shifts to perform.

!he '5%6! statements shifts all bits to the left or right and so for a byte after A shifts, the byte 4ill be Bero.

See a!s"

'5%6!%+ , '5%6!/C!

)/!A!0

#xam$!e
Dim a as $ord a 7 1;A '5%6! a, &06! , 1 3rint a -;<D

WHILE WEND Acti"n

0 ecutes a series of statements in a loop, as long as a given condition is true.

Syntax
WHILE condition statements WEND

Remarks

%f the condition is true then any intervening statements are e ecuted until the $0+D statement is encountered. "A'./* then returns to the $5%&0 statement and chec1s condition. %f it is still true, the process is repeated. %f it is not true, e ecution resumes 4ith the statement follo4ing the $0+D statement.

See a!s"
D/ .. &//3 , 6/) .. +0(!

#xam$!e
Dim A As yte ,hi!e A <= 10 'rint A Incr A ,en0

You might also like