© MCS Electronics, 1995-2007
Uses and saves R30 and R31.
Depending on the used XTAL the asm code can look like :
_WaitMS: _WaitMS1F: Push R30 ; save Z Push R31 _WaitMS_1:
Ldi R30,$E8 ;delay for 1 mS Ldi R31,$03
_WaitMS_2: Sbiw R30,1 ; -1
Brne _WaitMS_2 ; until 1 mS is ticked away Sbiw R24,1
Brne _WaitMS_1 ; for number of mS Pop R31
Pop R30 Ret
Example
WAITMS 10 'wait for 10 mS
Print "*"
WAITUS
Action
Suspends program execution for a given time in uS.
Syntax
WAITUS uS
Remarks
US |
The number of microseconds to wait. (1-65535) |
|
This must be a constant. Not a variable! |
|
|
No accurate timing is possible with this command.
In addition, the use of interrupts can slow this routine.
The minimum delay possible is determined by the used frequency. The number of cycles that are needed to set and save registers is 17.
When the loop is set to 1, the minimum delay is 21 uS. In this case you can better use a NOP that generates 1 clock cycle delay.
At 4 MHz the minimum delay is 5 uS. So a waitus 3 will also generate 5 uS delay. Above these values the delay will become accurate.
When you really need an accurate delay you can use a timer for this purpose.
Set the timer to a value and poll until the overflow flag is set. The disadvantage is that you can not use the timer for other tasks during this hardware delay.
The philosophy behind BASCOM is that it should not use hardware resources unless there is no other way to accomplish a task.
© MCS Electronics, 1995-2007
See also
DELAY , WAIT , WAITMS
Example
WAITUS 10 'wait for 10 uS
Print "*"
WHILE-WEND
Action
Executes a series of statements in a loop, as long as a given condition is true.
Syntax
WHILE condition statements
WEND
Remarks
If the condition is true then any intervening statements are executed until the WEND statement is encountered.
BASCOM then returns to the WHILE statement and checks the condition. If it is still true, the process is repeated.
If it is not true, execution resumes with the statement following the WEND statement.
So in contrast with the DO-LOOP structure, a WHILE-WEND condition is tested first so that if the condition fails, the statements in the WHILE-WEND structure are never executed.
See also
DO-LOOP
Example
'----------------------------------------------------------------------------- |
|
------------ |
: while_w.bas |
'name |
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: demo: WHILE, WEND |
'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 |
|
|
© MCS Electronics, 1995-2007 |
$framesize = 40 |
' default use 40 |
for the frame space |
|
Dim A As Byte |
|
A = 1 |
'assign var |
While A < 10 |
'test expression |
Print A |
'print var |
Incr A |
'increase by one |
Wend |
'continue loop |
End |
|
WRITE
Action
Writes data to a sequential file
Syntax
WRITE #ch , data [,data1]
Remarks
Ch |
A channel number, which |
|
identifies an opened file. This can be a hard coded constant or a |
|
variable. |
Data , data1 |
A variable who’s content are written to the file. |
|
|
When you write a variables value, you do not write the binary representation but the ASCII representation. When you look in a file it contains readable text.
When you use PUT, to write binary info, the files are not readable or contain unreadable characters.
Strings written are surrounded by string delimiters "". Multiple variables written are separated by a comma. Consider this example :
Dim S as String * 10 , W as Word
S="hello" : W = 100
OPEN "test.txt" For OUTPUT as #1
WRITE #1, S , W
CLOSE #1
The file content will look like this : "hello",100
Use INPUT to read the values from value.
See also
INITFILESYSTEM , OPEN , CLOSE, FLUSH , PRINT, LINE INPUT, LOC, LOF , EOF , FREEFILE , FILEATTR , SEEK , BSAVE , BLOAD , KILL , DISKFREE , GET , PUT , FILEDATE , FILETIME , FILEDATETIME , DIR , WRITE , INPUT
ASM
Calls |
_FileWriteQuotationMark |
_FileWriteDecInt |
|
page -717- |
|
© MCS Electronics, 1995-2007
|
_FileWriteDecByte |
_FileWriteDecWord |
|
_FileWriteDecLong |
_FileWriteDecSingle |
Input |
Z points to variable |
|
Output |
|
|
|
|
|
Partial Example
Dim S As String * 10 , W As Word , L As Long
S = "write" |
|
Open "write.dmo"for Output As #2 |
' write is also |
Write #2 , S , W , L |
supported |
|
Close #2 |
|
Open "write.dmo"for Input As #2 |
' write is also |
Input #2 , S , W , L |
supported |
|
Close #2 |
|
Print S ; " " ; W ; " " ; L |
|
WRITEEEPROM
Action
Write a variables content to the DATA EEPROM.
Syntax
WRITEEEPROM var , address
Remarks
var |
The name of the variable that must be stored |
address |
The address in the EEPROM where the variable must be stored. |
|
A new option is that you can provide a label name for the address. See |
|
example 2. |
|
|
This statement is provided for compatibility with BASCOM-8051.
You can also use :
Dim V as Eram Byte 'store in EEPROM
Dim B As Byte 'normal variable
B = 10
V = B 'store variable in EEPROM
When you use the assignment version, the data types must be the same!
According to a data sheet from ATMEL, the first location in the EEPROM with address 0, can be overwritten during a reset. It is advised not to use this location.
For security, register R23 is set to a magic value before the data is written to the EEPROM. All interrupts are disabled while the EEPROM data is written. Interrupts are enabled automatic when the data is written.
page -718-
© MCS Electronics, 1995-2007
It is advised to use the Brownout circuit that is available on most AVR processors. This will prevent that data is written to the EEPROM when the voltage drops under the specified evel.
When data is written to the EEPROM, all interrupts are disabled, and after the EEPROM has been written, the interrupts are re-enabled.
See also
READEEPROM
ASM
NONE
Example
'----------------------------------------------------------------------------- |
|
------------ |
: eeprom2.bas |
'name |
'copyright |
: (c) 1995-2005, MCS Electronics |
'purpose |
: shows how to use labels with READEEPROM |
'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 |
|
'first dimension a variable
Dim B As Byte
Dim Yes As String * 1
'Usage for readeeprom and writeeprom : 'readeeprom var, address
'A new option is to use a label for the address of the data
'Since this data is in an external file and not in the code the eeprom data 'should be specified first. This in contrast with the normal DATA lines which must
'be placed at the end of your program!!
'first tell the compiler that we are using EEPROM to store the DATA $eeprom
'the generated EEP file is a binary file.
'Use $EEPROMHEX to create an Intel Hex file usable with AVR Studio. '$eepromhex
'specify a label Label1: