You are on page 1of 24

AVR Simulator IDE BASIC Compiler Reference Manual

Table Of Contents: General info Sho !arnin"s# Do $ot Compile %nuse& Co&e# Initiali'e Variables On Declaration# Optimi'e Variables Declaration# About (ariables Dim# As# Bit# B)te# !or&# *on"# Sin"le# Reser(e# +,B# +*B# +,!# +*!# +-B# +.B# S)mbol# True# /alse# Const# 0ointer# ASM# Mathematical an& lo"ical operations Mo&# S1r# S1rt# *o"# E2p# Sin# Cos# $ot# An&# Or# 3or# $an&# $or# $2or# ,i"h# *o # To""le# Stan&ar& Basic lan"ua"e elements Goto# /or# To# Step# $e2t# E2it /or# !hile# !en&# If# Then# Else# En&if# Select Case# Case# En&Select# ,alt# Subroutines En&# Gosub# Return# Microcontroller relate& lan"ua"e elements Confi"0in# Input# Output# Define# C*OC45/RE6%E$C7# Special Basic lan"ua"e elements !aitMs# !ait%s# SIM%*ATIO$5!AITMS5VA*%E# Brea8# Shift*eft# ShiftRi"ht# *oo8%p# /re1Out# Structure& lan"ua"e support 9proce&ures an& functions: 0roc# En& 0roc# Call# E2it# /unction# En& /unction# Inclu&e# %sin" internal EE0ROM &ata memor) Rea&# !rite# %sin" internal A;D con(erter mo&ule A&cin# ADC5C*OC4# ADC5VRE/# ADC5SAM0*E%S# %sin" interrupts Enable# Disable# On Interrupt# Resume# Serial communication usin" internal har& are %SART ,seropen# ,serout# ,serin# ,ser"et# Cr*f# *f# A**O!5M%*TI0*E5,SERO0E$# SI$G*E5DECIMA*50*ACES# Soft are %ART implementation SERO%T5DE*A7%S# Serout# Serin# SeroutIn(# SerinIn(# Interfacin" character *CDs *CD5BITS# *CD5DREG# *CD5DBIT# *CD5RSREG# *CD5RSBIT# *CD5EREG# *CD5EBIT# *CD5R!REG# *CD5R!BIT# *CD5COMMA$D%S# *CD5DATA%S# *CD5I$ITMS# *CD5READ5B%S75/*AG# *c&init# *c&out# *c&cm&out# *c&Clear# *c&,ome 9*c&*ine<,ome:# *c&*ine=,ome# *c&Displa)On# *c&Displa)Off# *c&CurOff# *c&CurBlin8# *c&Cur%n&erline# *c&CurBlin8%n&erline# *c&*eft# *c&Ri"ht# *c&Shift*eft# *c&ShiftRi"ht# *c&*ine<Clear# *c&*ine=Clear# *c&*ine<0os# *c&*ine=0os# *c&&efchar# *c&*ine-,ome# *c&*ine.,ome# *c&*ine-Clear# *c&*ine.Clear# *c&*ine-0os# *c&*ine.0os# *CD5*I$ES# *CD5C,ARS#

I=C communication ith e2ternal I=C &e(ices I=C!rite# I=CRea&# I=C!rite<# I=CRea&<# I=CREAD5DE*A7%S# I=CC*OC45STRETC,# I=C0repare# I=CStart# I=CStop# I=CSen&# I=CRecA 9I=CRecei(eAc8:# I=CRec$ 9I=CRecei(e$Ac8:# Support for Serial 0eripheral Interface 9S0I: communication S0I5SC45REG# S0I5SC45BIT# S0I5SDI5REG# S0I5SDI5BIT# S0I5SDO5REG# S0I5SDO5BIT# S0I5CS5REG# S0I5CS5BIT# S0IC*OC45I$VERT# S0ICS5I$VERT# S0IC*OC45STRETC,# S0I0repare# S0ICSOn# S0ICSOff# S0ISen&# S0IRecei(e# S0ISen&Bits# Interfacin" "raphical *CDs ith <=>2?. &ot matri2 G*CD5DREG# G*CD5RSREG# G*CD5RSBIT# G*CD5EREG# G*CD5EBIT# G*CD5R!REG# G*CD5R!BIT# G*CD5CS<REG# G*CD5CS<BIT# G*CD5CS=REG# G*CD5CS=BIT# G*c&init# G*c&clear# G*c&pset# G*c&preset# G*c&clean# G*c&position# G*c& rite# G*c&out# G*c&in# G*c&cm&out# Interfacin" Ra&io Control 9R;C: ser(os Ser(oIn# Ser(oOut# Interfacin" Stepper Motors STE05A5REG# STE05A5BIT# STE05B5REG# STE05B5BIT# STE05C5REG# STE05C5BIT# STE05D5REG# STE05D5BIT# STE05MODE# Step,ol&# StepC!# StepCC!# Interfacin" <@!IRE &e(ices <!IRE5REG# <!IRE5BIT# < ireInit# < ireSen&Bit# < ireGetBit# < ireSen&B)te# < ireGetB)te# DS<>S=AStart# DS<>S=ARea&T# A&(ance& features Start/romBero#

C General info Basic compiler e&itor is compose& of e&itor panel 9for user pro"ram e&itin": an& source e2plorer 9for eas) na(i"ation throu"h all elements of user pro"ram @ (ariables# s)mbols# constants# subroutines# proce&ures an& functions:+ E&itor formats an& colori'es entere& lines of user pro"ram# that simplifies the &ebu""in" process+ The primar) output of the compiler is an assembler source file+ ,o e(er# ith an appropriate comman& from the menu it can be assemble& an& e(en loa&e& in the simulator ith a sin"le clic8+ Menu comman&s an& options are rich# as ell as the comman&s from the ri"ht@clic8 popup menus for the e&itor an& source e2plorer+ Basic compilerDs assembler output contains man) useful comment lines# that ma8es it (er) helpful for e&ucational purposes# also+ Sho !arnin"s If Sho !arnin"s option is enable&# in the !arnin"s in&o Basic compiler ill sho information about unuse& &eclarations# subroutines# proce&ures an& functions in the user basic pro"ram+ Do $ot Compile %nuse& Co&e If this option is enable&# Basic compiler ill not compile unuse& &eclarations# subroutines# proce&ures an& functions# in or&er to sa(e memor) resources+ Initiali'e Variables On Declaration If this option is enable&# Basic compiler ill reset to 'ero all memor) locations allocate& for (ariables# at the position of their &eclaration in the basic pro"ram+ This option is useful for be"inners# because SRAM memor) is fille& ith ran&om (alues at &e(ice po er@up# an& it is eas) to ma8e a mista8e to assume that all (ariables are reset to 'ero at po er@up+ E2perience& users can sa(e some pro"ram memor)# b) &isablin" this option an& ta8in" control of (ariable initial (alues b) user pro"ram here necessar)+ Optimi'e Variables Declaration This option ill turn on the compiler internal routine that ill optimi'e the (ariables &eclaration or&er

base& on the usa"e fre1uenc) of the (ariables+ In this a)# the most fre1uentl) use& (ariables ill be store& in lo er SRAM memor) locations# resultin" in possibl) smaller si'e of the "enerate& co&e+ C About (ariables /i(e &ata t)pes are supporte&: Bit @ <@bit# A or < B)te @ <@b)te inte"ers in the ran"e A to =EE !or& @ =@b)te inte"ers in the ran"e A to ?E#E-E *on" @ .@b)te inte"ers in the ran"e A to .#=F.#F?G#=FE Sin"le @ .@b)te sin"le precision floatin" point numbers# G &i"its precision# mo&ifie& IEEEGE. stan&ar& Variables can be "lobal 9&eclare& in the main pro"ram# before the En& statement: or local 9&eclare& in subroutines# proce&ures an& functions:+ Variable name use& for a (ariable ith "lobal scope can be use& a"ain for local (ariable names+ The compiler ill reser(e separate memor) locations for them+ The total number of (ariables is limite& b) the a(ailable microcontroller SRAM memor)+ Variables are &eclare& usin" DIM statement: Dim i As Bit Dim j As Byte Dim k As Word Dim x As Long Dim y As Single If necessar)# (ariable a&&ress can be specifie& &urin" &eclaration: Dim x As Byte @ 0x062 It is possible to use one@&imensional arra)s for B)te# !or&# *on" an& Sin"le (ariables+ /or e2ample: Dim x(10) As Byte &eclares an arra) of <A B)te (ariables ith arra) in&e2 in the ran"e HA@FI+ *on" &ata t)pe 9-=@bit inte"er math support: is an optional a&&@on mo&ule for the inte"rate& basic compiler+ Sin"le &ata t)pe 9floatin" point math support: is an optional a&&@on mo&ule for the inte"rate& basic compiler+ RESERVE statement allo s a&(ance& usa"e b) reser(in" some of the SRAM locations to be use& b) in@co&e assembler routines+ /or e2ample: Reserve 0x06 ,i"h an& lo b)te of a !or& (ariable can be a&&resse& b) +,B an& +*B e2tensions+ In&i(i&ual bits can be a&&resse& b) +A# +<# +++# +<. an& +<E e2tensions+ It is possible to ma8e con(ersions bet een B)te an& !or& &ata t)pes usin" +*B an& +,B e2tensions or &irectl): Dim x As Byte Dim y As Word x ! y"#B x ! y"LB $%&is st'tement is e()iv'lent to x ! y y"#B ! x y"LB ! x y ! x $%&is st'tement *ill 'lso +le'r t&e &ig& ,yte o- y v'ri',le ,i"h or& 9compose& b) b)tes - an& =: an& lo or& 9compose& b) b)tes < an& A: of a *on" 9Sin"le: (ariable can be a&&resse& b) +,! an& +*! e2tensions+ B)te A can be a&&resse& b) +*B an& b)te < b) +,B e2tensions+ The thir& an& the fourth b)te of *on" an& Sin"le (ariables can be a&&resse& b) +-B an& +.B e2tensions+ In&i(i&ual bits can be a&&resse& b) +A# +<# +++# +-< e2tensions+ /or e2ample: Dim i As Byte Dim j As Word Dim x As Long

i ! x"LB j ! x"#W All "eneral purpose or8in" an& I;O re"isters are a(ailable as B)te (ariables in basic pro"rams+ In&i(i&ual bits of a B)te (ariable can be a&&resse& b) +A# +<# +=# +-# +.# +E# +? an& +G e2tensions or usin" official names of the bits+ The lists of these pre&efine& (ariables an& official bit names for the selecte& AVR &e(ice can be (ie e& b) selectin" Sho S)stem Variables an& Sho S)stem Bit $ames comman&s from the Options menu of the basic compiler in&o + Dim x As Bit Dim y As Byte x ! y". y"6 ! 1 R26 ! 10 /# ! R26 DDRD"1 ! 1 DDRB ! 2 01R%D"1 ! 1 01R%B ! 2 234S5"0637 ! 1 SR72"SR7283 ! 1 Stan&ar& short forms for accessin" port re"isters 90ORT2: an& in&i(i&ual chip pins 90ORT2+n: are also a(ailable 90A# 0B# 0C# +++ can be use& as B)te (ariablesJ 0AA# 0A<# 0A=# +++# 0BG# 0CA# +++ are a(ailable as Bit (ariables:+ It shoul& be note& that these short forms can not be use& to a&&ress port an& pin input states+ 0I$2 an& 0I$2+n shoul& be use& for that purpose 9D2D represents the port numberin" letterJ DnD represents the bit number:+ 0B ! 0x-0D ! 1 It is possible to use s)mbolic names 9s)mbols: in pro"rams# to easil) a&&ress s)stem (ariables+ S)mbols can be "lobal or local+ S7MBO* &irecti(e is use& to &eclare s)mbolic names: Sym,ol led1 ! 01R%B"0 led1 ! 1 Sym,ol 'd+8st'rt ! AD6SRA"ADS6 'd+8st'rt ! 1 Constants can be use& in &ecimal number s)stem ith no special mar8s# in he2a&ecimal number s)stem ith lea&in" A2 or lea&in" K notation 9or ith , at the en&: an& in binar) s)stem ith lea&in" L mar8 9or ith B at the en&:+ ASCII (alue of a character can be e2presse& in strin" format 9e+"+ MAM:+ 4e) or&s True an& /alse are also a(ailable for Bit t)pe constants+ /or e2ample: Dim x As Bit Dim y As Byte x ! %r)e y ! 0x y ! 901010101 y ! :;: Constants can be assi"ne& to s)mbolic names usin" CO$ST &irecti(e+ Constants can be "lobal or local+ One e2ample: Dim x As Single Dim y As Word 6onst <i ! ="1>1 ? 6onst &ig& ! 102= x ! <i y ! &ig& An) inte"er (ariable can be use& as a pointer to internal &ata SRAM memor) hen it is use& as an ar"ument of 0OI$TER function+ The (alue containe& in the (ariable that is use& as a pointer shoul& be in the appropriate ran"e+ ,ere is one e2ample: Dim x As Word Dim y As Byte

Dim k As Long x ! 0x0.0 y ! 0ointer(x) $%&e +ontent o- SRA4 lo+'tion @0.0 *ill ,e lo'ded to v'ri',le y y ! y A 0x x ! x B 1 0ointer(x) ! y $4odi-ied y v'l)e *ill ,e stored to SRA4 lo+'tion @06C x ! 0x0.. k ! 0x12=> 6.D 0ointer(x) ! k $Co)r SRA4 lo+'tions @0..B@0.A *ill ,e lo'ded *it& t&e >B,yte v'l)e +ont'ined in v'ri',le k It is possible to use comments in basic source pro"rams+ The comments must be"in ith sin"le 1uote s)mbol 9D: an& ma) be place& an) here in the pro"ram+ *ines of assembler source co&e ma) be place& an) here in basic source pro"ram an& must be"in ith ASM: prefi2+ /or e2ample: AS4E F10 AS4ELAB7L1E LD3 R16G0xCC S)mbolic names of "lobal &eclare& (ariables can be use& in assembler routines because proper (ariable a&&ress ill be assi"ne& to those names b) E6% &irecti(e: Dim v'rn'me As Byte v'rn'me ! 0 AS4E LD3 R16G0xCC AS4E S%S HARFA47GR16 C Mathematical an& lo"ical operations /i(e arithmetic operations 9N# @# O# ;# MOD: are a(ailable for inte"er &ata t)pes+ MOD operation is not applicable for Sin"le &ata t)pe (ariables+ The compiler is able to compile all possible comple2 arithmetic e2pressions# inclu&in" those containin" math functions an& user &efine& functions+ /or e2ample: Dim i As Word Dim j As Word Dim x As Word i ! 12= j ! i I 2=> x ! 2 x ! (j I x B 12=> ) J (i A x) S1uare root of a inte"er number 9A@?EE-E ran"e: can be calculate& usin" S6R function: Dim x As Word x ! =600 x ! S(r(x) There are fi(e sin"le precision mathematical functions 9S6RT# *OG# E30# SI$# COS: that can be use& ith Sin"le &ata t)pe (ariables+ *OG ill compute the natural lo"arithm of a real number+ All math functions can also be use& in comple2 math e2pressions+ /or e2ample: Dim x As Single x ! 2 x ! S(rt(x) /or Bit &ata t)pe (ariables se(en lo"ical operations are a(ailable+ It is possible to ma8e onl) one lo"ical operation in one sin"le statement+ *o"ical operations are also a(ailable for other (ariable t)pes+ /or e2ample: E2ample <: Dim i As Bit Dim j As Bit Dim x As Bit x ! Fot i

x x x x x x

! ! ! ! ! !

i i i i i i

And j 1r j /or j F'nd j For j Fxor j

E2ample =: Dim x As Word Dim y As Word x ! x 1r y 01R%B ! 01R%D And 911110000 There are three statements that are use& for bit manipulation @ ,IG,# *O! an& TOGG*E+ If the ar"ument of these statements is a bit in one of the 0ORT re"isters# then the same bit in the correspon&in" DDR re"ister is automaticall) set# confi"urin" the affecte& pin as an output pin+ Some e2amples: #ig& 01R%B"0 Lo* AD6SRA"AD7F %oggle 0B1 C Stan&ar& Basic lan"ua"e elements %ncon&itional Pumps are performe& b) GOTO statement+ It uses line label name as ar"ument+ *ine labels can be "lobal or local+ *ine labels must be follo e& b) colon mar8 D:D+ ,ere is one e2ample: Dim x As Word x ! 0 loo<E x ! x A 1 2oto loo< /our stan&ar& BASIC structures are supporte&: /OR@TO@STE0@$E3T# !,I*E@!E$D# I/@T,E$@ E*SE@E$DI/ an& SE*ECT CASE@CASE@E$DSE*ECT+ ,ere are se(eral e2amples: E2ample <: Dim x As Byte DDRB ! 2 x ! 2 W&ile x K 0 01R%B ! x x ! x B 1 W'it4s 100 Wend 01R%B ! x E2ample =: DDRB"0 ! 1 loo<E 3- 03FA"0 %&en 01R%B"0 ! 1 7lse 01R%B"0 ! 0 7ndi2oto loo< E2ample -: Dim x As Word DDRB ! 2 Cor x ! 0 %o 10000 Ste< 10 01R%B ! x"LB Fext x E2ample .:

Dim i As Byte Dim j As Byte Dim x As Byte j ! 2 x ! 2 DDRB ! 2 Cor i ! j %o 0 Ste< Bx 01R%B ! i Fext i E2ample E: Dim x As Byte loo<E Sele+t 6'se x 6'se 2 x ! 1 6'se L! 12. x ! x A 1 6'se 7lse x ! 2 7ndSele+t 2oto loo< /or statement ill accept all a(ailable (ariable t)pes for the runnin" (ariable+ E2it /or statement pro(i&es a a) to e2it a /or@$e2t loop+ It transfers control to the statement follo in" the $e2t statement+ After I/@T,E$ statement in the same line can be place& almost e(er) other possible statement an& then E$DI/ is not use&+ There are no limits for the number of neste& statements of an) 8in&+ In the test e2pressions of I/@T,E$ an& !,I*E statements it is possible to use multiple ORe& an& multiple A$De& con&itions+ Si2 stan&ar& comparison operators are a(ailable: Q# RS# S# SQ# R# RQ+ Multiple comma separate& con&itions can be use& ith CASE statements# also+ If there is a nee& to insert an infinite loop in basic pro"ram# that can be &one ith ,A*T statement+ C Subroutines Structure& pro"rams can be ritten usin" subroutine calls ith GOS%B statement that uses line label name as ar"ument+ Return from a subroutine is performe& b) RET%R$ statement+ %ser nee& to ta8e care that the pro"ram structure is consistent+ !hen usin" subroutines# main routine nee& to be en&e& ith E$D statement+ E$D statement is compile& as an infinite loop+ ,ere is an e2ample: Sym,ol 'd+8st'rt ! AD6SRA"ADS6 Sym,ol dis<l'y ! 01R%B DDRB ! 911111111 AD4M/ ! 0x01 $Sele+t AD61 in<)t +&'nnel AD6SRB ! 0x10 $Le-t 'dj)st AD6 res)lt AD6SRA"AD7F ! 1 m'inE 2os), get'd+res)lt dis<l'y ! AD6# 2oto m'in 7nd get'd+res)ltE #ig& 'd+8st'rt W&ile 'd+8st'rt Wend Ret)rn C Microcontroller relate& lan"ua"e elements

Microcontroller ports an& pins can be confi"ure& as inputs or outputs b) assi"nin" proper (alues to DDR2 re"isters or their bits+ That tas8 can also be accomplishe& b) a CO$/IG0I$ statement+ Its s)nta2 is apparent from the follo in" e2amples: 6on-ig0in 01R%B ! 1)t<)t 6on-ig0in 0A0 ! 1)t<)t 6on-ig0in 01R%6"= ! 3n<)t 6on-ig0in 0D ! 3n<)t The cloc8 fre1uenc) of the tar"et &e(ice can be specifie& b) settin" the C*OC45/RE6%E$C7 parameter ith the DE/I$E &irecti(e 9the (alue is e2presse& in M,':+ This parameter shoul& be setup at the be"innin" of the basic pro"ram+ /or e2ample: De-ine 6L1658CR7NM7F6; ! 20 C Special Basic lan"ua"e elements !AITMS an& !AIT%S statements can be use& to force pro"ram to ait for the specifie& number of millisecon&s or microsecon&s+ It is also possible to use (ariable ar"ument of B)te or !or& &ata t)pe+ These routines use Cloc8 /re1uenc) parameter that can be chan"e& from the Options menu+ !AIT%S routine has minimal &ela) an& step that also &epen& on the Cloc8 /re1uenc) parameter+ Dim x As Word x ! 100 W'it4s x W'itMs 0 Important $ote: !hen ritin" pro"rams for real AVR &e(ices )ou ill most li8el) use &ela) inter(als that are comparable to < secon& or <AAA millisecon&s+ Man) e2amples in this help file also use such Dreal@timeD inter(als+ But# if )ou ant to simulate those pro"rams )ou ha(e to be (er) patient to see somethin" to happen# e(en on (er) po erful 0Cs a(ailable to&a)+ /or simulation of D!aitMs <AAAD statement on .M,' )ou ha(e to ait the simulator to simulate .AAAAAA instructions an& it ill ta8e consi&erable amount of time e(en if De2tremel) fastD simulation rate is selecte&+ So# Pust for the purpose of simulation )ou shoul& recompile )our pro"rams ith a&Puste& &ela) inter(als# that shoul& not e2cee& <@<Ams+ But# be sure to recompile )our pro"ram ith ori"inal &ela)s before )ou &o nloa& it to a real &e(ice+ There is an eas) a) to chan"e ar"uments of all !AITMS statements in a lar"e basic pro"ram ith a (alue in the ran"e <@<A for simulation purposes+ !ith one line of co&e settin" parameter SIM%*ATIO$5!AITMS5VA*%E ith DE/I$E &irecti(e# the ar"uments of all !AITMS statements in the pro"ram ill be i"nore& an& the specifie& (alue ill be use& instea& &urin" compilin"+ Settin" the (alue A 9&efault: for this parameter 9or omittin" the hole line: ill cancel its effect an& the compile& co&e ill be rea&) a"ain for the real har& are+ It is possible to insert brea8points for the simulator &irectl) in basic pro"rams usin" BREA4 statement+ It is compile& as BREA4 assembler instruction an& the simulator ill interpret this opco&e as a brea8point an& s itch the simulation rate to Step B) Step+ S,I/T*E/T an& S,I/TRIG,T functions can be use& to shift bit@le(el representation of a (ariable left an& ri"ht+ The first ar"ument is input (ariable an& the secon& ar"ument is number of shifts to be performe&+ ,ere are t o e2amples: E2ample <: DDRB ! 0x-01R%B ! 900000011 gole-tE W'it4s 2 0 01R%B ! S&i-tLe-t(01R%BG 1) 3- 01R%B ! 911000000 %&en 2oto gorig&t 2oto gole-t gorig&tE W'it4s 2 0 01R%B ! S&i-tRig&t(01R%BG 1) 3- 01R%B ! 900000011 %&en 2oto gole-t 2oto gorig&t

E2ample =: DDRB ! 0x-01R%B ! 900000001 gole-tE W'it4s 2 0 01R%B ! S&i-tLe-t(01R%BG 1) 3- 01R%B". %&en 2oto gorig&t 2oto gole-t gorig&tE W'it4s 2 0 01R%B ! S&i-tRig&t(01R%BG 1) 3- 01R%B"0 %&en 2oto gole-t 2oto gorig&t *OO4%0 function can be use& to select one from the list of B)te constants# base& on the (alue in the in&e2 (ariable# that is supplie& as the last separate& ar"ument of the function+ The first constant in the list has in&e2 (alue A+ The selecte& B)te constant ill be loa&e& into the result (ariable of the function+ If the (alue in the in&e2 (ariable "oes be)on& the number of constants in the list# the result (ariable ill not be affecte& b) the function+ ,ere is one small e2ample for a G@se"ment *ED &ispla): Dim digit As Byte Dim m'sk As Byte DDRB ! 911111111 loo<E Cor digit ! 0 %o ? m'sk ! LookM<(0x=-G 0x06G 0x ,G 0x>-G 0x66G 0x6dG 0x.dG 0x0.G 0x.-G 0x6-)G digit 01R%B ! m'sk W'it4s 1000 Fext digit 2oto loo< If all constants in the list 9or part of them: are ASCII (alues# then shorter form of the list can be create& b) usin" strin" ar"uments+ /or e2ample: m'sk ! LookM<(:AB6D7C2#3O5:)G index /RE6O%T statement can be use& to "enerate a train of pulses 9soun& tone: on the specifie& pin ith constant fre1uenc) an& specifie& &uration+ It has three ar"uments+ The first ar"ument is the pin that the tone ill be "enerate& on+ The statement ill setup the pin as an output pin+ The secon& ar"ument specif) the tone fre1uenc) an& it must be a constant in the ran"e <@<AAAA,'+ The thir& ar"ument &efines the tone &uration an& it also must be a numeric constant in the ran"e <@<AAAAms+ Choosin" hi"her tone fre1uencies ith lo microcontroller cloc8 fre1uenc) use& ma) result in some hat inaccurate fre1uenc) of the "enerate& tones+ /RE6O%T statement can be alternati(el) use& in D(ariable mo&eD ith !or& &ata t)pe (ariables instea& of constants for the last t o ar"uments+ In this mo&e of usa"e the secon& ar"ument is suppose& to hol& the half@perio& of the tone 9in microsecon&s: an& the thir& ar"ument must hol& the total number of pulses that ill be "enerate&+ The follo in" co&e ill "enerate one secon& lon" tone on 0BA pin ith ?AA,' fre1uenc): Cre(1)t 01R%B"0G 600G 1000 C Structure& lan"ua"e support 9proce&ures an& functions: 0roce&ures can be &eclare& ith 0ROC statement+ The) can contain up to E ar"uments 9comma separate& list: an& all a(ailable &ata t)pes can be use& for ar"ument (ariables+ Ar"ument (ariables are &eclare& locall)# so the) &o not nee& to ha(e uni1ue names in relation to the rest of user basic pro"ram# that ma8es (er) eas) to re@use once ritten proce&ures in other basic pro"rams+ The proce&ures can be e2ite& ith E3IT statement+ The) must be en&e& ith E$D 0ROC statement an& must be place& after the E$D statement in pro"ram+ Calls to proce&ures are implemente& ith

CA** statement+ The list of passe& ar"uments can contain both (ariables an& numeric constants+ /or e2ample: Dim x As Byte DDRB ! 2 Cor x ! 0 %o 2 6'll <ort,8dis<l'y(x) W'it4s 100 Fext x 7nd 0ro+ <ort,8dis<l'y('rg1 As Byte) 01R%B ! 'rg1 7nd 0ro+ All facts state& for proce&ures are (ali& for functions# also+ /unctions can be &eclare& ith /%$CTIO$ statement+ The) can contain up to E ar"uments an& ar"ument (ariables are &eclare& locall)+ /unctions can be e2ite& ith E3IT statement an& must be en&e& ith E$D /%$CTIO$+ The name of the function is &eclare& as a "lobal (ariable# so if the function is calle& ith CA** statement# after its e2ecution the function (ariable ill contain the result+ Stan&ar& a) of function calls in assi"nment statements can be use&# also+ One simple e2ample: Dim x As Byte Dim y As Word Cor x ! 0 %o 2 y ! s()'re(x) Fext x 7nd C)n+tion s()'re('rg1 As Word) As Word s()'re ! 'rg1 I 'rg1 7nd C)n+tion Basic source co&e from an e2ternal file can be inclu&e& to the current pro"ram b) usin" I$C*%DE &irecti(e+ Its onl) ar"ument is a strin" containin" the path to the e2ternal +BAS file+ This can be the full path or onl) the file name# if the e2ternal file is locate& in the same fol&er as the current basic pro"ram file+ Durin" the compilation process the e2ternal basic source ill be appen&e& to the current pro"ram+ Multiple files can be inclu&e& ith separate I$C*%DE &irecti(es+ To maintain the o(erall basic co&e structure# it is stron"l) su""este& that the e2ternal file contains "lobal &eclarations# subroutines# proce&ures an& functions# onl)+ ,ere is one (er) simple e2ample for the &emonstration: main+bas: Dim i As Word Dim j As Word 3n+l)de :in+1",'s: 3n+l)de :in+2",'s: Cor i ! 1 %o 10 j ! -)n+1(iG 100) 6'll <ro+1(j) Fext i 7nd inc<+bas: Dim tot'l As Word 0ro+ <ro+1(i As Word) tot'l ! tot'l A i 7nd 0ro+ inc=+bas: C)n+tion -)n+1(i As WordG j As Word) As Word

10

-)n+1 ! i A j 7nd C)n+tion Structure& lan"ua"e support is an optional a&&@on mo&ule for the inte"rate& basic compiler+ C %sin" internal EE0ROM &ata memor) Access to internal EE0ROM &ata memor) can be implemente& usin" READ an& !RITE statements+ The first ar"ument is the a&&ress of a b)te in EE0ROM memor) an& can be a constant or a (ariable+ The secon& ar"ument is &ata that is rea& or ritten 9for READ statement it must be a (ariable ar"umentJ for !RITE statement it can also be a constant:+ Dim x As Byte Dim y As Byte x ! 10 Re'd xG y Write 11G y C %sin" internal A;D con(erter mo&ule ADCI$ statement is a(ailable as a support for internal A;D con(erter+ It has t o ar"uments+ The first ar"ument is ADC channel number an& the secon& ar"ument is a (ariable that ill be use& to store the result of A;D con(ersion+ It is not onl) possible to con(ert sin"le@en&e& inputs ith ADCI$ statement# but also &ifferential inputs ith (arious "ain selections+ /or that purpose the appropriate (alue for M%3 bits 9ADM%3 re"ister: shoul& be use& as ACD channel number ar"ument of ADCI$ statement+ If a B)te (ariable is use& for the con(ersion result# >@bit con(ersion ill be performe& 9left a&Puste& result:+ ADCI$ statement uses (alues of three parameters ADC5C*OC4# ADC5VRE/ an& ADC5SAM0*E%S+ Their &efault (alues can be chan"e& usin" DE/I$E &irecti(e+ ADC5C*OC4 parameter &etermines the choice for ADC input cloc8 prescaler selection+ Allo e& ran"e is A@G an& the &efault (alue is - for <:> prescaler selection+ ADC5VRE/ parameter &efines the ADC (olta"e reference selection+ Its (alue is use& to setup RE/S bits in ADM%3 re"ister+ The &efault (alue is A+ ADC5SAM0*E%S parameter 9&efault (alue =A: sets the &esire& ADC ac1uisition time in microsecon&s 9A@=EE:+ ,ere is one e2ample: Dim v( ) As Byte Dim vm As Word Dim i As Byte De-ine De-ine De-ine DDRA ! DDRB ! AD686L165 ! = AD68HR7C ! 0 AD68SA40L7MS ! 0x00 0x--

Cor i ! 0 %o > Ad+in 0G v(i) Fext i vm ! 0 Cor i ! 0 %o > vm ! vm A v(i) Fext i vm ! vm J 01R%B ! vm"LB C %sin" interrupts Interrupt routines shoul& be place& as all other subroutines after the E$D statement+ A separate routine shoul& be implemente& for e(er) interrupt source use&+ The list of a(ailable interrupt sources for the selecte& AVR &e(ice ith their official names can be (ie e& b) selectin" Sho S)stem Interrupt $ames comman& from the Options menu of the basic compiler in&o + Interrupt routine shoul& be"in ith O$ I$TERR%0T 8e) or& follo e& b) the official name of its interrupt source an&

11

it shoul& en& ith RES%ME statement+ E$AB*E an& DISAB*E statements follo e& b) an interrupt source name can be use& in the pro"ram to control the status of its enable fla"+ If use& ith no ar"uments the) ill control the status of the "lobal interrupt enable fla" 9I@fla": in the SREG re"ister+ RES%ME statement ill automaticall) set the I@fla" an& enable ne interrupts+ One e2ample: Dim x As Byte x ! 2 DDRB ! 0x-01R%B ! x DDRD"2 ! 0 $+on-ig)re 3F%0 <in 's 'n in<)t <in 46M6R"3S600 ! 1 46M6R"3S601 ! 1 $3F%0 rising edge gener'tes 'n interr)<t 7n',le 3F%0 7n',le 7nd 1n 3nterr)<t 3F%0 x ! x B 1 01R%B ! x Res)me C Serial communication usin" internal har& are %SART The support for both har& are an& soft are serial communication is also a(ailable+ ,SERO0E$# ,SERO%T# ,SERI$ an& ,SERGET statements can be use& ith AVR &e(ices that feature internal har& are %SART+ ,SERO0E$ statement sets up the har& are %SART+ Its onl) ar"ument is bau& rate+ All bau& rates in the ran"e <AA@=AAAAA ill be accepte&# but it is su""este& to use the stan&ar& bau& rates: -AA# ?AA# <=AA# =.AA# .>AA# F?AA# <..AA# <F=AA# =>>AA# -<=EA# ->.AA# E?AAA or EG?AA+ If the ar"ument is omitte& %SART ill be set up for F?AA bau& rate+ If parameter A**O!5M%*TI0*E5,SERO0E$ is set to < usin" DE/I$E &irecti(e# it ill be possible to use ,SERO0E$ statement more than once in the pro"ram# for e2ample to chan"e the selecte& bau& rate+ ,SERO%T statement is use& for serial transmission+ ,SERO%T statement ma) ha(e multiple ar"uments separate& b) D#D+ 7ou can use strin"s# */ 8e) or& for *ine /ee& character or CR*/ 8e) or& for Carria"e Return @ *ine /ee& se1uence# constants an& (ariables+ If DTD si"n is use& before the name of a (ariable then its &ecimal representation is sent to the serial port+ ,SERI$ statement can be use& to loa& a list of B)te# !or& an& *on" (ariables ith the (alues recei(e& on serial port+ This statement ill ait until the re1uire& number of b)tes is recei(e& on serial port+ ,SERGET statement ha(e one ar"ument that must be a B)te (ariable+ If there is a character aitin" in the recei(e buffer it ill be loa&e& in the (ariable# other ise A (alue ill be loa&e&+ ,ere are some e2amples: E2ample <: Dim i As Byte #sero<en =D>00 W'it4s 1000 Cor i ! 20 %o 0 Ste< B1 #sero)t :F)m,erE :G PiG 6rLW'it4s 00 Fext i E2ample =: Dim i As Byte #sero<en 1?200 loo<E #serin i #sero)t :F)m,erE :G PiG 6rL2oto loo< E2ample -: Dim i As Byte #sero<en 1?200 loo<E #serget i

12

3- i K 0 %&en #sero)t :F)m,erE :G PiG 6rLW'it4s 0 7ndi2oto loo< If DTD si"n is use& before the name of a (ariable 9(ali& for B)te# !or&# *on" an& Sin"le &ata t)pe (ariables: then its &ecimal representation is sent to the &e(ice 9ASCII characters:+ This is applicable to *c&out# G*c& rite# ,serout# Serout an& SeroutIn( statements+ SI$G*E5DECIMA*50*ACES parameter is use& to &efine the number of &ecimal places printe& after the &ecimal point in &ecimal representation of Sin"le &ata t)pe (ariables+ Vali& parameter (alues are in the ran"e <@?+ The &efault (alue is -+ De-ine S3F2L78D7634AL80LA67S ! > C Soft are %ART implementation On all supporte& AVR &e(ices soft are serial communication can be implemente& ith SERO%T an& SERI$ statements+ The first ar"ument of both statements must be one of the microcontrollerDs pins# an& the secon& ar"ument is bau& rate+ All bau& rates in the ran"e <AA@?AAAA ill be accepte&# but it is su""este& to use the follo in" stan&ar& bau& rates: -AA# ?AA# <=AA# =.AA# .>AA or F?AA+ %sin" hi"her bau& rates ith lo cloc8 fre1uenc) coul& cause inaccurate timin" an& framin" errors+ Small a&Pustments 9<@EL: coul& possibl) fi2 the problem+ /or SERO%T statement then follo s the list of ar"uments to be sent to serial port+ 7ou can use strin"s# */ 8e) or& for *ine /ee& character or CR*/ 8e) or& for Carria"e Return @ *ine /ee& se1uence# constants an& (ariables+ If DTD si"n is use& before the name of a (ariable then its &ecimal representation is sent to the serial port+ SERO%T statement uses SERO%T5DE*A7%S parameter that can be set b) DE/I$E &irecti(e an& has &efault (alue of <AAA microsecon&s+ This &efines the &ela) inter(al before a character is actuall) sent to the port an& it is use& to increase the reliabilit) of soft are SERO%T routine+ /or SERI$ statement then follo s the list of B)te# !or& an& *on" (ariables to be loa&e& ith the (alues recei(e& on serial port+ This statement ill ait until the re1uire& number of b)tes is recei(e& on serial port+ /or serial interface ith in(erte& lo"ic le(els there are SERI$I$V an& SERO%TI$V statements a(ailable+ Some e2amples: E2ample <: De-ine S7R1M%8D7LA;MS ! 000 Sero)t 01R%D"1G 1200G :#ello *orldQ:G 6rLE2ample =: Dim i As Byte loo<E Serin 01R%D"0G ?600G i Sero)t 01R%D"1G ?600G :F)m,erE :G PiG 6rL2oto loo< C Interfacin" character *CDs Basic compiler also features the support for *CD mo&ules base& on ,D..G>A or compatible controller chip+ 0rior to usin" *CD relate& statements# user shoul& set up *CD interface usin" DE/I$E &irecti(es+ ,ere is the list of a(ailable parameters: *CD5BITS @ &efines the number of &ata interface lines 9allo e& (alues are . an& >J &efault is .: *CD5DREG @ &efines the port here &ata lines are connecte& to 9&efault is 0ORTB: *CD5DBIT @ &efines the position of &ata lines for .@bit interface 9A or .J &efault is .:# i"nore& for >@bit interface *CD5RSREG @ &efines the port here RS line is connecte& to 9&efault is 0ORTB: *CD5RSBIT @ &efines the pin here RS line is connecte& to 9&efault is -: *CD5EREG @ &efines the port here E line is connecte& to 9&efault is 0ORTB: *CD5EBIT @ &efines the pin here E line is connecte& to 9&efault is =: *CD5R!REG @ &efines the port here R;! line is connecte& to 9set to A if not use&J A is &efault: *CD5R!BIT @ &efines the pin here R;! line is connecte& to 9set to A if not use&J A is &efault: *CD5COMMA$D%S @ &efines the &ela) after *CDCMDO%T statement 9&efault (alue is EAAA: *CD5DATA%S @ &efines the &ela) after *CDO%T statement 9&efault (alue is <AA: *CD5I$ITMS @ &efines the &ela) for *CDI$IT statement 9&efault (alue is <AA:

13

The last three parameters shoul& be set to lo (alues hen usin" inte"rate& *CD mo&ule simulator+ If R;! line is connecte& to microcontroller an& parameter *CD5READ5B%S75/*AG is set to < usin" DE/I$E &irecti(e# then these &ela) parameters ill be i"nore& b) compiler an& correct timin" ill be implemente& b) rea&in" the status of the bus) fla" in the *CD+ *CDI$IT statement shoul& be place& in the pro"ram before an) of *CDO%T 9use& for sen&in" &ata: an& *CDCMDO%T 9use& for sen&in" comman&s: statements+ $umeric constant ar"ument of *CDI$IT is use& to &efine the cursor t)pe: A Q no cursor 9&efault:# < Q blin8# = Q un&erline# - Q blin8 N un&erline+ *CDO%T an& *CDCMDO%T statements ma) ha(e multiple ar"uments separate& b) D#D+ Strin"s# constants an& (ariables can be use& as ar"uments of *CDO%T statement+ If DTD si"n is use& before the name of a (ariable then its &ecimal representation is sent to the *CD mo&ule+ Constants an& (ariables can be use& as ar"uments of *CDCMDO%T statement an& the follo in" 8e) or&s are also a(ailable: *c&Clear# *c&,ome# *c&*ine=,ome# *c&Displa)On# *c&Displa)Off# *c&CurOff# *c&CurBlin8# *c&Cur%n&erline# *c&CurBlin8%n&erline# *c&*eft# *c&Ri"ht# *c&Shift*eft# *c&ShiftRi"ht# *c&*ine<Clear# *c&*ine=Clear# *c&*ine<0os9: an& *c&*ine=0os9:+ Ar"ument of *c&*ine<0os9: an& *c&*ine=0os9: can be a number in the ran"e 9<@.A: or a (ariable+ The (alue containe& in that (ariable shoul& be in the same ran"e+ *c&Displa)On an& *c&Displa)Off ill turn the cursor off+ Cursor relate& s)mbolic comman&s can be use& as ar"uments of *CDI$IT+ ,ere are some e2amples: E2ample <: De-ine L6D8B3%S ! D De-ine L6D8DR72 ! 01R%B De-ine L6D8DB3% ! 0 De-ine L6D8RSR72 ! 01R%D De-ine L6D8RSB3% ! 1 De-ine L6D87R72 ! 01R%D De-ine L6D87B3% ! = De-ine L6D8RWR72 ! 01R%D De-ine L6D8RWB3% ! 2 L+dinit L+d6)rBlink loo<E L+do)t :#ello *orldQ: W'it4s 1000 L+d+mdo)t L+d6le'r W'it4s 1000 2oto loo< E2ample =: De-ine L6D8B3%S ! D De-ine L6D8DR72 ! 01R%B De-ine L6D8DB3% ! 0 De-ine L6D8RSR72 ! 01R%D De-ine L6D8RSB3% ! 1 De-ine L6D87R72 ! 01R%D De-ine L6D87B3% ! = De-ine L6D8RWR72 ! 01R%D De-ine L6D8RWB3% ! 2 Dim x As Word x ! 6 = L+dinit = W'it4s 1000 loo<E L+do)t :3 'm +o)ntingQ: L+d+mdo)t L+dLine2#ome L+do)t Px x ! x B 1 W'it4s 2 0 L+d+mdo)t L+d6le'r

14

2oto loo< *CD relate& statements ill ta8e control o(er DDR re"isters connecte& ith pins use& for *CD interface+ 7ou can setup up to ei"ht user &efine& characters to be use& on *CD+ This can easil) be &one ith *CDDE/C,AR statement+ The first ar"ument of this statement is char number an& must be in the ran"e A@G+ $e2t > ar"uments form >@line char pattern 9from the top to the bottom: an& must be in the ran"e A@-< 9E@bits i&e:+ These > user characters are assi"ne& to char co&es A@G an& >@<E an& can be &ispla)e& usin" *CDO%T statement+ After *CDDE/C,AR statement the cursor ill be in ,OME position+ /or e2ample: L+dde-+&'r 0G 10G 10G 10G 10G 10G 10G 10G 10 L+dde-+&'r 1G 911111G 910101G 910101G 910101G 910101G 910101G 910101G 911111 L+do)t 0G 1G :#elloQ:G 1G 0 /or *CDs ith four lines of characters a&&itional s)mbolic ar"uments of *CDCMDO%T statement can be use&: *c&*ine-,ome# *c&*ine.,ome# *c&*ine-Clear# *c&*ine.Clear# *c&*ine-0os9: an& *c&*ine.0os9:+ Ar"ument of *c&*ine-0os9: an& *c&*ine.0os9: can be a number in the ran"e 9<@.A: or a (ariable+ The (alue containe& in that (ariable shoul& be in the same ran"e+ 0rior to usin" these lan"ua"e elements# correct (alues &eterminin" *CD t)pe shoul& be assi"ne& to *CD5*I$ES an& *CD5C,ARS parameters usin" DE/I$E &irecti(es+ De-ine L6D8L3F7S ! > De-ine L6D86#ARS ! 16 De-ine L6D8B3%S ! D De-ine L6D8DR72 ! 01R%B De-ine L6D8DB3% ! 0 De-ine L6D8RSR72 ! 01R%D De-ine L6D8RSB3% ! 1 De-ine L6D87R72 ! 01R%D De-ine L6D87B3% ! = De-ine L6D8RWR72 ! 01R%D De-ine L6D8RWB3% ! 2 L+dinit = loo<E L+d+mdo)t L+d6le'r L+d+mdo)t L+dLine1#ome L+do)t :%&is is line 1: L+d+mdo)t L+dLine2#ome L+do)t :%&is is line 2: L+d+mdo)t L+dLine=#ome L+do)t :%&is is line =: L+d+mdo)t L+dLine>#ome L+do)t :%&is is line >: W'it4s 1000 L+d+mdo)t L+dLine16le'r L+d+mdo)t L+dLine26le'r L+d+mdo)t L+dLine=6le'r L+d+mdo)t L+dLine>6le'r L+d+mdo)t L+dLine10os(1) L+do)t :Line 1: L+d+mdo)t L+dLine20os(2) L+do)t :Line 2: L+d+mdo)t L+dLine=0os(=) L+do)t :Line =: L+d+mdo)t L+dLine>0os(>) L+do)t :Line >: W'it4s 1000 2oto loo<

15

C I=C communication ith e2ternal I=C &e(ices I=C communication can be implemente& in basic pro"rams usin" I=C!RITE an& I=CREAD statements+ The first ar"ument of both statements must be one of the microcontrollerDs pins that is connecte& to the SDA line of the e2ternal I=C &e(ice+ The secon& ar"ument of both statements must be one of the microcontrollerDs pins that is connecte& to the SC* line+ The thir& ar"ument of both statements must be a constant (alue or B)te (ariable calle& Dsla(e a&&ressD+ Its format is &escribe& in the &atasheet of the use& &e(ice+ /or e2ample# for EE0ROMs from =.C famil) 9 ith &e(ice a&&ress inputs connecte& to "roun&: the (alue A2AA shoul& be use& for sla(e a&&ress parameter+ Both statements ill ta8e control o(er bit A of sla(e a&&ress &urin" communication+ The forth ar"ument of both statements must be a B)te or !or& (ariable 9this &epen&s on the &e(ice use&: that contains the a&&ress of the location that ill be accesse&+ If a constant (alue is use& for a&&ress parameter it must be in B)te (alue ran"e+ The last 9fifth: ar"ument of I=C!RITE statement is a B)te constant or (ariable that ill be ritten to the specifie& a&&ress# an& for I=CREAD statement it must be a B)te (ariable to store the (alue that ill be rea& from the specifie& a&&ress+ It is allo e& to use more than one D&ataD ar"ument+ /or I=C &e(ices that &o not support &ata a&&ress ar"ument there is short form of I=C statements 9I=C!RITE< an& I=CREAD<: a(ailable here sla(e a&&ress ar"ument is follo e& ith one or more &ata ar"uments &irectl)+ /or some I=C sla(e &e(ices it is necessar) to ma8e a &ela) to ma8e sure &e(ice is rea&) to respon& to I=CREAD statement+ /or that purpose there is I=CREAD5DE*A7%S parameter that can be set b) DE/I$E &irecti(e an& has &efault (alue of A microsecon&s+ Also# for slo er I=C &e(ices# it mi"ht be necessar) to use lon"er cloc8 pulses+ That can be &one b) settin" I=CC*OC45STRETC, parameter usin" DE/I$E &irecti(e+ This parameter ill set cloc8 stretch factor+ Its &efault (alue is <+ ,ere is one combine& e2ample ith *CD mo&ule an& =.C?. EE0ROM 9SDA connecte& to RC<J SC* connecte& to RCA:: E2ample <: De-ine L6D8B3%S ! D De-ine L6D8DR72 ! 01R%B De-ine L6D8DB3% ! 0 De-ine L6D8RSR72 ! 01R%D De-ine L6D8RSB3% ! 1 De-ine L6D87R72 ! 01R%D De-ine L6D87B3% ! = De-ine L6D8RWR72 ! 01R%D De-ine L6D8RWB3% ! 2 Dim 'ddr As Word Dim d't' As Byte Sym,ol sd' ! 01R%6"1 Sym,ol s+l ! 01R%6"0 L+dinit = W'it4s 1000 Cor 'ddr ! 0 %o =1 L+d+mdo)t L+d6le'r d't' ! 2 B 'ddr 326Write sd'G s+lG 0x'0G 'ddrG d't' L+do)t :Write %o 770R14: L+d+mdo)t L+dLine2#ome L+do)t :(:G P'ddrG :) ! :G Pd't' W'it4s 1000 Fext 'ddr Cor 'ddr ! 0 %o =1 L+d+mdo)t L+d6le'r 326Re'd sd'G s+lG 0x'0G 'ddrG d't' L+do)t :Re'd Crom 770R14: L+d+mdo)t L+dLine2#ome L+do)t :(:G P'ddrG :) ! :G Pd't' W'it4s 1000 Fext 'ddr

16

There is a set of lo @le(el I=C communication statements a(ailable# if it is nee&e& to ha(e more control o(er I=C communication process+ I=C0RE0ARE statement has t o ar"uments that must be one of the microcontrollerDs pins+ The first ar"ument &efines SDA line an& secon& ar"ument &efines SC* line+ This statement ill prepare these lines for I=C communication+ I=CSTART statement ill "enerate start con&ition# an& I=CSTO0 statement ill "enerate stop con&ition+ One b)te can be sent to the I=C sla(e usin" I=CSE$D statement+ After the statement is e2ecute& T bit in SREG re"ister ill hol& the cop) of the state on the SDA line &urin" the ac8no le&"e c)cle+ There are t o statements that can be use& to recei(e one b)te from I=C sla(e+ I=CRECA or I=CRECEIVEAC4 ill "enerate ac8no le&"e si"nal &urin" ac8no le"&e c)cle after the b)te is recei(e&+ I=CREC$ or I=CRECEIVE$AC4 ill "enerate not ac8no le&"e si"nal &urin" ac8no le"&e c)cle after the b)te is recei(e&+ One e2ample: E2ample =: Dim 'ddr As Word Dim d't'(=1) As Byte Sym,ol sd' ! 01R%6"1 Sym,ol s+l ! 01R%6"0 'ddr ! 0 3260re<'re sd'G s+l 326St'rt 326Send 0x'0 326Send 'ddr"#B 326Send 'ddr"LB 326Sto< 326St'rt 326Send 0x'1 Cor 'ddr ! 0 %o =0 326Re+eiveA+k d't'('ddr) Fext 'ddr 326Re+F d't'(=1) 326Sto< C Support for Serial 0eripheral Interface 9S0I: communication 0rior to usin" S0I relate& statements# S0I interface shoul& be set up usin" DE/I$E &irecti(es+ There are ei"ht a(ailable parameters to &efine the connection of SC4# SDI# SDO an& 9optionall): CS lines: S0I5SC45REG @ &efines the port here SC4 line is connecte& to S0I5SC45BIT @ &efines the pin here SC4 line is connecte& to S0I5SDI5REG @ &efines the port here SDI line is connecte& to S0I5SDI5BIT @ &efines the pin here SDI line is connecte& to S0I5SDO5REG @ &efines the port here SDO line is connecte& to S0I5SDO5BIT @ &efines the pin here SDO line is connecte& to S0I5CS5REG @ &efines the port here CS line is connecte& to S0I5CS5BIT @ &efines the pin here CS line is connecte& to The assume& settin"s are acti(e@hi"h for Cloc8 line an& acti(e@lo for ChipSelect line+ That can be chan"e& b) assi"nin" the (alue < to S0IC*OC45I$VERT an&;or S0ICS5I$VERT parameters b) DE/I$E &irecti(e+ /or slo er S0I &e(ices# it mi"ht be necessar) to use lon"er cloc8 pulses+ The &efault cloc8 stretch factor 9<: can be chan"e& b) settin" S0IC*OC45STRETC, parameter+ S0I0RE0ARE statement 9no ar"uments: ill prepare interface lines for S0I communication+ S0ICSO$ an& S0ICSO// statements ill enable; &isable the ChipSelect line of the interface+ One b)te can be sent to the S0I peripheral usin" S0ISE$D statement+ To recei(e a b)te from the peripheral S0IRECEIVE statement shoul& be use&+ To sen& the specifie& number of bits there is S0ISE$DBITS statement a(ailable+ Its first ar"ument shoul& be the number of bits to be sent H<@>I an& the secon& ar"ument is a b)te (ariable or constant+ ,ere is one e2ample for usin" =ECA.A S0I eeprom: E2ample <: De-ine S0386S8R72 ! 01R%6

17

De-ine S0386S8B3% ! = De-ine S038S658R72 ! 01R%6 De-ine S038S658B3% ! 2 De-ine S038SD38R72 ! 01R%6 De-ine S038SD38B3% ! De-ine S038SD18R72 ! 01R%6 De-ine S038SD18B3% ! > S030re<'re De-ine L6D8B3%S ! D De-ine L6D8DR72 ! 01R%B De-ine L6D8DB3% ! 0 De-ine L6D8RSR72 ! 01R%D De-ine L6D8RSB3% ! 1 De-ine L6D87R72 ! 01R%D De-ine L6D87B3% ! = De-ine L6D8RWR72 ! 01R%D De-ine L6D8RWB3% ! 2 De-ine L6D8R7AD8BMS;8CLA2 ! 1 L+dinit Dim 'ddr As Byte Dim d't' As Byte Cor 'ddr ! 0 %o 10 d't' ! 200 B 'ddr S036S1n S03Send 0x06 S036S1-S036S1n S03Send 0x02 S03Send 'ddr S03Send d't' S036S1-L+d+mdo)t L+d6le'r L+do)t :Write %o 770R14: L+d+mdo)t L+dLine2#ome L+do)t :(:G P'ddrG :) ! :G Pd't' W'it4s 00 Fext 'ddr Cor 'ddr ! 0 %o 10 S036S1n S03Send 0x0= S03Send 'ddr S03Re+eive d't' S036S1-L+d+mdo)t L+d6le'r L+do)t :Re'd Crom 770R14: L+d+mdo)t L+dLine2#ome L+do)t :(:G P'ddrG :) ! :G Pd't' W'it4s 00 Fext 'ddr ,ere is the same e2ample ritten for F-C>? Micro ire EE0ROM: E2ample =: De-ine S0386S8R72 ! 01R%6 De-ine S0386S8B3% ! = De-ine S036S83FH7R% ! 1 De-ine S038S658R72 ! 01R%6 De-ine S038S658B3% ! 2

18

De-ine S038SD38R72 De-ine S038SD38B3% De-ine S038SD18R72 De-ine S038SD18B3% S030re<'re

! 01R%6 ! ! 01R%6 ! >

De-ine L6D8B3%S ! D De-ine L6D8DR72 ! 01R%B De-ine L6D8DB3% ! 0 De-ine L6D8RSR72 ! 01R%D De-ine L6D8RSB3% ! 1 De-ine L6D87R72 ! 01R%D De-ine L6D87B3% ! = De-ine L6D8RWR72 ! 01R%D De-ine L6D8RWB3% ! 2 De-ine L6D8R7AD8BMS;8CLA2 ! 1 L+dinit Dim 'ddr As Byte Dim d't' As Byte S036S1n S03SendBits 6G 9100110 S03SendBits DG 900000000 S036S1-Cor 'ddr ! 0 %o 10 d't' ! 200 B 'ddr S036S1n S03SendBits 6G 9101000 S03SendBits DG 'ddr S03Send d't' S036S1-S036S1n S03Send 0x00 S036S1-L+d+mdo)t L+d6le'r L+do)t :Write %o 770R14: L+d+mdo)t L+dLine2#ome L+do)t :(:G P'ddrG :) ! :G Pd't' W'it4s 00 Fext 'ddr Cor 'ddr ! 0 %o 10 S036S1n S03SendBits 6G 9110000 S03SendBits DG 'ddr S03Re+eive d't' S036S1-L+d+mdo)t L+d6le'r L+do)t :Re'd Crom 770R14: L+d+mdo)t L+dLine2#ome L+do)t :(:G P'ddrG :) ! :G Pd't' W'it4s 00 Fext 'ddr C Interfacin" "raphical *CDs ith <=>2?. &ot matri2 Interfacin" "raphical *CDs ith &ot matri2 resolution <=>2?. controlle& b) 4SA<A> or compatible chip is supporte& ith the follo in" list of Basic lan"ua"e elements: G*CDI$IT# G*CDC*EAR# G*CD0SET# G*CD0RESET# G*CD0OSITIO$# G*CD!RITE# G*CDC*EA$# G*CDO%T# G*CDI$#

19

G*CDCMDO%T+ 0rior to usin" Graphical *CDs relate& statements# user shoul& set up the interface ith the "raphical *CD mo&ule usin" DE/I$E &irecti(es+ ,ere is the list of a(ailable parameters: G*CD5DREG @ &efines the port here &ata lines are connecte& to 9it has to be a full >@pins port: G*CD5RSREG @ &efines the port here RS line is connecte& to G*CD5RSBIT @ &efines the pin here RS line is connecte& to G*CD5EREG @ &efines the port here E line is connecte& to G*CD5EBIT @ &efines the pin here E line is connecte& to G*CD5R!REG @ &efines the port here R;! line is connecte& to G*CD5R!BIT @ &efines the pin here R;! line is connecte& to G*CD5CS<REG @ &efines the port here CS< line is connecte& to G*CD5CS<BIT @ &efines the pin here CS< line is connecte& to G*CD5CS=REG @ &efines the port here CS= line is connecte& to G*CD5CS=BIT @ &efines the pin here CS= line is connecte& to G*CDI$IT statement shoul& be place& some here at the be"innin" of the basic pro"ram before an) other "raphical *CD relate& stetements are use&+ Graphical *CD relate& statements ill ta8e control o(er DDR re"isters connecte& ith pins use& for *CD interface+ G*CDC*EAR statement ill clear the hole &ispla)+ It can be use& ith one optional constant ar"ument in the ran"e A@=EE that ill be place& on e(er) b)te position on the &ispla) 9<=>2?. "raphical &ispla)s are internal) &i(i&e& in t o ?.2?. hal(esJ both hal(es are &i(i&e& in ei"ht ?.2> hori'ontal pa"esJ e(er) pa"e has its a&&ressin" number in the ran"e A@<EJ pa"e in upper@left corner has number AJ pa"e in lo er@left corner has number GJ pa"e in upper@ri"ht corner has number >J pa"e in lo er@ri"ht corner has number <EJ e(er) pa"e has ?. b)te positions a&&resse& ith numbers in the ran"e A@?-J e(er) b)te position has > bitsJ the uppermost bit is *SB an& the lo ermost bit is MSB:+ /or e2ample: 2L+dinit loo<E 2L+d+le'r 0x'' W'it4s 1000 2L+d+le'r 0x W'it4s 1000 2oto loo< G*CD0SET an& G*CD0RESET statements are use& to turn on an& turn off one of the &ots on the "raphical &ispla)+ The first ar"ument is the hori'ontal coor&inate an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@<=G+ The secon& ar"ument is the (ertical coor&inate an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@?-+ The &ot in the upper@left corner of the &ispla) is the ori"in ith coor&inates A#A+ /or e2ample: Dim i As Byte Dim j As Byte 2L+dinit Cor i ! 0 %o 12. Cor j ! 0 %o 6= 2L+d<set iG j Fext j Fext i G*CDC*EA$ statement is use& to clear a section of the pa"e on the &ispla)+ It has three ar"uments+ The first ar"ument is pa"e a&&ress an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@<E+ The secon& ar"ument is the first b)te position on the pa"e that ill be cleane& an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@?-+ The thir& ar"ument is the last b)te position on the pa"e that ill be cleane& an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@?-+ If the last t o ar"uments are omitte& the hole pa"e ill be cleare&+ /or e2ample: Dim i As Byte 2L+dinit 2L+d+le'r 0x-Cor i ! 0 %o 1 2L+d+le'n i W'it4s 00 Fext i

20

G*CD0OSITIO$ statement is use& to a&&ress a b)te position on the &ispla)+ It must be use& before an) of the G*CD!RITE# G*CDI$# G*CDO%T an& G*CDCMDO%T statements+ The first ar"ument is pa"e a&&ress an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@<E+ The secon& ar"ument is the tar"et b)te position on the pa"e an& it must be a b)te &ata t)pe (ariable or constant in the ran"e A@?-+ If the secon& ar"ument is omitte&# 'ero b)te position is use&+ G*CD!RITE statement is use& to rite te2t on the &ispla)+ It ill start ritin" from the current b)te position on the &ispla)+ It must be use& carefull)# because hen the b)te position 9?-: of the pa"e is reache&# the ritin" ill continue from the b)te position A sta)in" on the same pa"e+ The i&th of e(er) character ritten is E b)te positions plus one clear b)te position+ After the statement is e2ecute& the current b)te position ill be at the en& of the te2t ritten+ G*CD!RITE statement ma) ha(e multiple ar"uments separate& b) D#D+ Strin"s# constants an& b)te (ariables can be use& as its ar"uments+ Constants an& (ariable (alues are interprete& as ASCII co&es+ If DTD si"n is use& before the name of a (ariable then its &ecimal representation is ritten+ /or e2ample: Dim i As Byte 2L+dinit Cor i ! 0 %o 1 2L+d<osition iG 0 2L+d*rite :0'geE :G Pi W'it4s 2 0 Fext i G*CDO%T statement is use& to rite the (alue of the b)te (ariable or constant at the current b)te position on the &ispla)+ The current b)te position ill be incremente& b) one+ G*CDI$ statement ill rea& the (alue from the current b)te position on the &ispla) an& put it in the b)te (ariable specifie& as its ar"ument+ G*CDCMDO%T statement is use& to sen& lo @le(el comman&s to the "raphical *CD+ Its ar"ument can be a constant or b)te &ata t)pe (ariable+ All these three statements can be use& ith multiple ar"uments separate& b) D#D+ C Interfacin" Ra&io Control 9R;C: ser(os /or ritin" applications to interface R;C ser(os there are t o statements a(ailable: SERVOI$ an& SERVOO%T+ R;C ser(o is controlle& b) a train of pulses 9<E@=A pulses per secon&: hose len"th &efine the position of the ser(o arm+ The (ali& len"th of pulses is in the ran"e <@=ms+ These t o statements ha(e t o ar"uments+ The first ar"ument of both statements is the microcontroller pin here the ser(o si"nal is recei(e& or transmitte&+ /or SERVOI$ statement that pin shoul& be pre(iousl) setup as an input pin an& for SERVOO%T statement the pin shoul& be setup for output+ The secon& ar"ument of SERVOI$ statement must be a B)te (ariable here the len"th of the pulse ill be sa(e&+ The pulses are measure& in <Aus units# so it is possible to measure pulses in the ran"e A+A<@=+EEms+ The (alue store& in the (ariable for normal ser(os shoul& be in the ran"e <AA@ =AA+ The secon& ar"ument of the SERVOO%T statement shoul& be a B)te (ariable or constant that &etermines the len"th of the "enerate& pulse+ /or proper operation of the tar"et ser(o SERVOO%T statement shoul& be e2ecute& <E@=A times &urin" one secon&+ ,ere is an e2ample of the ser(o re(erse operation: Dim lengt& As Byte DDRB"0 ! 0 DDRB"1 ! 1 loo<E Servo3n 01R%B"0G lengt& 3- lengt& L 100 %&en lengt& ! 100 3- lengt& K 200 %&en lengt& ! 200 lengt& ! lengt& B 100 lengt& ! 100 B lengt& lengt& ! lengt& A 100 Servo1)t 01R%B"1G lengt& 2oto loo< C Interfacin" Stepper Motors 0rior to usin" stepper motor relate& statements# its connection an& &esire& &ri(e mo&e shoul& be

21

set up usin" DE/I$E &irecti(es+ There are ei"ht a(ailable parameters to &efine the connection of A# B# C an& D coils: STE05A5REG @ &efines the port here A coil is connecte& to STE05A5BIT @ &efines the pin here A coil is connecte& to STE05B5REG @ &efines the port here B coil is connecte& to STE05B5BIT @ &efines the pin here B coil is connecte& to STE05C5REG @ &efines the port here C coil is connecte& to STE05C5BIT @ &efines the pin here C coil is connecte& to STE05D5REG @ &efines the port here D coil is connecte& to STE05D5BIT @ &efines the pin here A coil is connecte& to Coils A an& C are actuall) parts of one sin"le coil ith common connection+ The same is (ali& for B an& D coil connections+ There is also STE05MODE parameter use& to &efine the &ri(e mo&e+ If it is set to < 9&efault: the motor ill be &ri(en in full@step mo&e+ The (alue = shoul& be use& for half@step mo&e+ The first basic statement that shoul& be use& is STE0,O*D+ It ill confi"ure use& pins as outputs an& also ener"i'e A an& B coils to fi2 the rotor in its initial position+ /or mo(in" rotor in cloc8 ise an& countercloc8 ise &irections there are STE0C! an& STE0CC! statements a(ailable+ Their first ar"ument is the number of rotor steps that ill be performe& an& it can be B)te &ata t)pe constant or (ariable+ The secon& ar"ument &efines the &ela) bet een consecuti(e steps e2presse& in microsecon&s b) a B)te or !or& &ata t)pe (ariable or constant+ If usin" STE0C! statement results in rotor mo(ement in countercloc8 ise &irection then connection settin"s for B an& D coils shoul& be e2chan"e&+ ,ere are t o e2amples 9the secon& e2ample uses &ela)s suitable for simulation in the simulator:: E2ample <: De-ine S%708A8R72 ! 01R%B De-ine S%708A8B3% ! . De-ine S%708B8R72 ! 01R%B De-ine S%708B8B3% ! 6 De-ine S%70868R72 ! 01R%B De-ine S%70868B3% ! De-ine S%708D8R72 ! 01R%B De-ine S%708D8B3% ! > De-ine S%70841D7 ! 2 W'it4s 1000 Ste<#old W'it4s 1000 Dim 'n0 As Word loo<E Ad+in 0G 'n0 'n0 ! 'n0 I 60 'n0 ! 'n0 A 2000 Ste<6W 1G 'n0 2oto loo< E2ample =: De-ine S%708A8R72 ! 01R%B De-ine S%708A8B3% ! . De-ine S%708B8R72 ! 01R%B De-ine S%708B8B3% ! 6 De-ine S%70868R72 ! 01R%B De-ine S%70868B3% ! De-ine S%708D8R72 ! 01R%B De-ine S%708D8B3% ! > De-ine S%70841D7 ! 2 W'itMs =00 Ste<#old W'itMs 1000

22

loo<E Ste<66W 16G =00 W'itMs 1000 Ste<6W 2>G =00 W'itMs 1000 2oto loo< C Interfacin" <@!IRE &e(ices 0rior to usin" <@!IRE relate& statements# user shoul& &efine the pin here the &e(ice is connecte& to usin" DE/I$E &irecti(es+ A(ailable parameters are <!IRE5REG an& <!IRE5BIT+ /or e2ample: De-ine 1W3R78R72 ! 01R%B De-ine 1W3R78B3% ! 0 Initiali'ation se1uence can be performe& b) <!IREI$IT statement+ It can ha(e an optional ar"ument 9Bit &ata t)pe (ariable: that ill be set to A if the presence of the &e(ice has been &etecte& an& set to < if there is no &e(ice on the line+ In&i(i&ual bits 9time slots: can be sent to an& recei(e& from the &e(ice usin" <!IRESE$DBIT an& <!IREGETBIT statements+ Both statements can ha(e multiple ar"uments @ comma separate& list of Bit &ata t)pe (ariables 9or Bit constants for <!IRESE$DBIT statement:+ <!IRESE$DB7TE an& <!IREGETB7TE statements can be use& to sen& to an& recei(e b)tes from the &e(ice+ Both statements can ha(e multiple ar"uments @ comma separate& list of B)te &ata t)pe (ariables 9or B)te constants for <!IRESE$DB7TE statement:+ ,ere is one e2ample for measurin" temperature usin" DS<>S=A &e(ice: Dim -inis& As Bit Dim tem< As Byte Dim sign As Byte 1*ire3nit 1*ireSendByte 0x++G 0x>> W'it4s 1 loo<E 1*ire2etBit -inis& 3- -inis& ! 0 %&en 2oto loo< 1*ire3nit 1*ireSendByte 0x++G 0x,e 1*ire2etByte tem<G sign This e2ample can be (er) short b) usin" t o DS<>S=A specific hi"h le(el basic statements+ DS<>S=ASTART statement ill initiate a sin"le temperature con(ersion+ Accor&in" to the &e(ice &atasheet the con(ersion ill be complete& in at most GEAms+ After that perio& the measure& (alue can be rea& b) DS<>S=AREADT statement that re1uires t o B)te &ata t)pe (ariables as ar"uments+ The first ar"ument ill contain the temperature (alue in A+E &e"rees centi"ra&e units 9for e2ample# the (alue <AA represents the temperature of EA &e"rees:+ The secon& ar"ument ill contain the (alue A2AA if the temperature is positi(e an& A2// (alue if it is ne"ati(e+ /or e2ample: Dim tem< As Byte Dim sign As Byte DS1DS20St'rt W'it4s 1000 DS1DS20Re'd% tem<G sign C A&(ance& features If START/ROMBERO &irecti(e is use& the compiler ill start the pro"ram from 'ero flash pro"ram memor) location 9reset (ector: an& use the a(ailable pro"ram memor) continuousl)+ Interrupt routines if use& shoul& be implemente& b) usin" inline assembler co&e+ This a&(ance& feature can

23

be use& hen &e(elopin" bootloa&er applications# for e2ample+

24

You might also like