Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BASCOM AVR, help reference (2007).PDF
Скачиваний:
281
Добавлен:
12.08.2013
Размер:
17.02 Mб
Скачать

© MCS Electronics, 1995-2007

Local X As Byte

X

=

5

'assign local

B

=

X

 

I

=

-1

 

W

=

40000

 

L

=

20000

 

S = "test"

 

End Sub

 

 

Sub Testarray(byval A As Byte , B1 As Byte)

'start sub

Print A ; " " ; B1

'print passed

variables

'change value of

B1 = 3

element with index 1

'specify the index

B1(1) = 3

which does the same as the line above

'modify other

B1(3) = 3

element of array

'You can change A, but since a copy is passed to the SUB, 'the change will not reflect to the calling variable

End Sub

'notice the empty() to indicate that a string array is passed

Sub Teststr(b As Byte , Dl() As String) Dl(b) = Dl(b) + "add"

End Sub

DEFxxx

Action

Declares all variables that are not dimensioned of the DefXXX type.

Syntax

DEFBIT b

Define BIT

DEFBYTE c

Define BYTE

DEFINT I

Define INTEGER

DEFWORD x

Define WORD

DEFLNG l

Define LONG

DEFSNG s

Define SINGLE

DEFDBL z

Define DOUBLE

 

 

Remarks

While you can DIM each individual variable you use, you can also let the compiler handle it for you.

All variables that start with a certain letter will then be dimmed as the specified type.

Example

Defbit b : DefInt c 'default type for bit and integers

Set b1

'set bit to 1

page -452-

© MCS Electronics, 1995-2007

c = 10

'let c = 10

DEFLCDCHAR

Action

Define a custom LCD character.

Syntax

DEFLCDCHAR char,r1,r2,r3,r4,r5,r6,r7,r8

Remarks

char

Constant representing the character (0-7).

r1-r8

The row values for the character.

 

 

You can use the LCD designer to build the characters.

It is important that a CLS follows the DEFLCDCHAR statement(s).

So make sure you use the DEFLCDCHAR before your CLS statement.

Special characters can be printed with the Chr() function.

LCD Text displays have a 64 byte memory that can be used to show your own custom characters. Each character uses 8 bytes as the character is an array from8x8 pixels. You can create a maximum of 8 characters this way. Or better said : you can show a maximum of 8 custom characters at the same time. You can redefine characters in your programbut with the previous mentioned restriction.

A custom character can be used to show characters that are not available in the LCDfont table. For example a Û.

You can also use custom characters to create a bar graph or a music note.

See also

Tools LCD designer

Partial Example

Deflcdchar 1 , 225 , 227 , 226 , 226 , 226 , 242 , 234 , 228

' replace ?

with number (0-7)

 

' replace ?

Deflcdchar 0 , 240 , 224 , 224 , 255 , 254 , 252 , 248 , 240

with number (0-7)

'select data RAM

Cls

Rem it is important that a CLS is following the deflcdchar statements because

it will set the controller back in datamode

'print the special

Lcd Chr(0) ; Chr(1)

character

 

DEG2RAD

Action

Converts an angle in to radians.

page -453-

© MCS Electronics, 1995-2007

Syntax

var = DEG2RAD( Source )

Remarks

Var

A numeric variable that is assigned with the degrees of variable

 

Source.

Source

The single or double variable to get the degrees of.

 

 

All trig functions work with radians. Use deg2rad and rad2deg to convert between radians and angles.

See Also

RAD2DEG

Example

'-----------------------------------------------------------------------------

 

--

: (c) 1995-2005, MCS Electronics

'copyright

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

'purpose

: demonstrates DEG2RAD function

'-----------------------------------------------------------------------------

 

--

 

Dim S As Single

 

S = 90

 

S = Deg2Rad(s)

 

Print S

 

S = Rad2deg(s)

 

Print S

 

End

 

DELAY

Action

Delay program execution for a short time.

Syntax

DELAY

Remarks

Use DELAY to wait for a short time.

The delay time is ca. 1000 microseconds.

Interrupts that occur frequently and/or take a long time to process, will let the delay

page -454-

© MCS Electronics, 1995-2007

last longer.

When you need a very accurate delay, you need to use a timer.

See also

WAIT , WAITMS

Example

'-----------------------------------------------------------------------------

 

------------

: delay.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: DELAY, WAIT, WAITMS

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "m48def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

 

Ddrb = &HFF

'port B as output

Portb = 255

 

Print "Starting"

'lets wait for a

Delay

very short time

 

Print "Now wait for 3 seconds"

 

Portb = 0

 

Wait 3

 

Print "Ready"

'wait 10

Waitms 10

milliseconds

 

Portb = 255

 

End

 

DIM

Action

Dimension a variable.

Syntax

DIM var AS [XRAM/SRAM/ERAM]type [AT location/variable] [OVERLAY]

Remarks

Var

Any valid variable name such as b1, i or longname. var can also

 

page -455-

 

© MCS Electronics, 1995-2007

 

 

 

 

be an array : ar(10) for example.

 

 

 

 

Type

Bit, Byte, Word, Integer, Long, Single, Double or String

 

XRAM

Specify XRAM to store variable into external memory

 

SRAM

Specify SRAM to store variable into internal memory (default)

 

ERAM

Specify ERAM to store the variable into EEPROM

 

OVERLAY

Specify that the variable is overlaid in memory.

 

location

The address of name of the variable when OVERLAY is used.

 

 

 

 

A string variable needs an additional length parameter:

Dim s As XRAM String * 10

In this case, the string can have a maximum length of 10 characters. Internally one additional byte is needed to store the end of string marker. Thus in the example above, 11 bytes will be used to store the string.

Note that BITS can only be stored in internal memory.

SCOPE

The scope for DIM is global. So no matter where you use the DIM statements, the variable will end up as a global visible variable that is visible in all modules, procedures and functions.

When you need a LOCAL variable that is local to the procedure or function, you can use LOCAL.

Since LOCAL variables are stored on the frame, it takes more code to dynamic generate and clean up these variables.

AT

The optional AT parameter lets you specify where in memory the variable must be stored. When the memory location already is occupied, the first free memory location will be used. You need to look in the report file to see where the variable is located in memory.

OVERLAY

The OVERLAY option will not use any variable space. It will create a sort of phantom variable:

Dim x as Long at $60 'long uses 60,61,62 and 63 hex of SRAM

Dim b1 as Byte at $60 OVERLAY

Dim b2 as Byte at $61 OVERLAY

B1 and B2 are no real variables! They refer to a place in memory. In this case to &H60 and &H61. By assigning the phantom variable B1, you will write to memory location &H60 that is used by variable X.

So to define it better, OVERLAY does create a normal usable variable, but it wil be stored at the sepcified memory location which could be already be occupied by another OVERLAY variable, or by a normal variable.

Take care with the OVERLAY option. Use it only when you understand it.

You can also read the content of B1: Print B1

This will print the content of memory location &H60.

By using a phantom variable you can manipulate the individual bytes of real variables.

page -456-

© MCS Electronics, 1995-2007

Another example

Dim L as Long at &H60

Dim W as Word at &H62 OVERLAY

W will now point to the upper two bytes of the long.

Using variable name instead of address

As variables can be moved though the program during development it is not always convenient to specify an address. You can also use the name of the variable :

DIM W as WORD

Dim B as BYTE AT W OVERLAY

Now B is located at the same address as variable W.

For XRAM variables, you need additional hardware : an external RAM and address decoder chip.

For ERAM variables, it is important to understand that these are not normal variables. ERAM variables serve as a way to simple read and write the EEPROM memory. You can use READEEPROM and WRITEEEPROM for that purpose too.

ERAM variables only can be assigned to SRAM variables, and ERAM variables can be assigned to SRAM variables. You can not use an ERAM variable as you would use a normal variable.

Dim b as byte, bx as ERAM byte

B= 1

Bx=b ' write to EEPROM

B=bx ' read from EEPROM

See Also

CONST , LOCAL

Example

'-----------------------------------------------------------------------------

 

------------

: dim.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: DIM

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "m48def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

 

 

page -457-

© MCS Electronics, 1995-2007

$framesize = 40

for the frame space

Dim B1 As Bit

Dim A As Byte

0-255

Dim C As Integer from -32767 - +32768

Dim L As Long

Dim W As Word

Dim S As String * 11 to 11 characters

' default use 40

'bit can be 0 or 1 'byte range from

'integer range

'length can be up

'new feature : you can specify the address of the variable

Dim K As Integer At &H120

'the next dimensioned variable will be placed after variable s

Dim Kk As Integer

'Assign bits

'or

B1 = 1

Set B1

'use set

'Assign bytes

 

A = 12

 

A = A + 1

 

'Assign integer

 

C = -12

 

C = C + 100

 

Print C

 

W = 50000

 

Print W

 

'Assign long

 

L = 12345678

 

Print L

 

'Assign string

 

S = "Hello world"

 

Print S

 

End

 

DIR

Action

Returns the filename that matches the specified filemask.

Syntax

sFile = DIR(mask)

sFile = DIR()

Remarks

SFile

A string variable that is assigned with the filename.

page -458-