ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8345

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

© 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-