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

© MCS Electronics, 1995-2007

No accurate timing is possible with this command.

When you use interrupts, the delay may be extended.

See also

DELAY , WAITMS

Example

WAIT 3 'wait for three seconds

Print "*"

WAITKEY

Action

Wait until a character is received.

Syntax

var = WAITKEY()

var = WAITKEY(#channel)

Remarks

var

Variable that receives the ASCII value of the serial buffer.

 

Can be a numeric variable or a string variable.

#channel

The channel used for the software UART.

 

 

While Inkey() returns a character from the serial buffer too, INKEY() continues when there is no character. Waitkey() waits until there is a character received. This blocks your program.

See also

INKEY , ISCHARWAITING

Example

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

 

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

: inkey.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: INKEY , WAITKEY

'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

page -713-

© MCS Electronics, 1995-2007

 

$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

 

Dim A As Byte , S As String * 2

 

Do

'get ascii value

A = Inkey()

from serial port

 

's = Inkey()

'we got something

If A > 0 Then

Print "ASCII code " ; A ; " from serial"

 

End If

'until ESC is

Loop Until A = 27

pressed

 

A = Waitkey()

'wait for a key

's = waitkey()

 

Print Chr(a)

 

'wait until ESC is pressed

 

Do

 

Loop Until Inkey() = 27

 

'When you need to receive binary data and the bibary value 0 , 'you can use the IScharwaiting() function.

'This will return 1 when there is a char waiting and 0 if there is no char waiting.

'You can get the char with inkey or waitkey then.

End

WAITMS

Action

Suspends program execution for a given time in mS.

Syntax

WAITMS mS

Remarks

Ms The number of milliseconds to wait. (1-65535)

No accurate timing is possible with this command.

In addition, the use of interrupts can slow this routine.

See also

DELAY , WAIT , WAITUS

ASM

WaitMS will call the routine _WAITMS. R24 and R25 are loaded with the number of milliseconds to wait.

page -714-

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

page -715-

© 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

 

page -716-