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

 

© MCS Electronics, 1995-2007

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: GOTO, GOSUB and RETURN

'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

 

Goto Continue

 

Print "This code will not be executed"

 

Continue:

'end a label with

a colon

 

Print "We will start execution here"

 

Gosub Routine

 

Print "Back from Routine"

 

End

 

Routine:

'start a

subroutine

 

Print "This will be executed"

'return from

Return

subroutine

 

RIGHT

Action

Return a specified number of rightmost characters in a string.

Syntax

var = RIGHT(var1 ,n )

Remarks

var

The string that is assigned.

Var1

The source string.

st

The number of bytes to copy from the right of the string.

 

 

See also

LEFT , MID

page -617-

© MCS Electronics, 1995-2007

 

Example

 

Dim S As String * 15 , Z As String * 15

 

S ="ABCDEFG"

 

Z = Left(s , 5)

'ABCDE

Print Z

Z = Right(s , 3) : Print Z

 

Z = Mid(s , 2 , 3) : Print Z

 

End

 

RND

Action

Returns a random number.

Syntax

var = RND( limit )

Remarks

Limit

Word that limits the returned random number.

Var

The variable that is assigned with the random number.

 

 

The RND() function returns an Integer/Word and needs an internal storage of 2 bytes. (___RSEED). Each new call to Rnd() will give a new positive randomnumber.

Notice that it is a software based generated number. And each time you will restart your program the same sequence will be created.

You can use a different SEED value by dimensioning and assigning ___RSEED yourself: Dim ___rseed as word : ___rseed = 10234

Dim I as word : I = rnd(10)

When your application uses a timer you can assign ___RSEED with the timer value. This wil give a better random number.

See also

NONE

Example

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

 

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

: rnd.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo : RND() function

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

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

 

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

 

page -618-

 

 

© MCS Electronics, 1995-2007

 

$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

 

 

 

Dim I As Word

' dim variable

 

Do

'get random number

 

 

I = Rnd(40)

 

(0-39)

'print the value

 

 

Print I

 

 

Wait 1

'wait 1 second

 

Loop

'for ever

 

End

 

 

 

 

 

 

 

 

 

 

ROTATE

 

 

 

 

 

 

 

 

 

 

 

Action

 

 

 

Rotate all bits one place to the left or right.

Syntax

ROTATE var , LEFT/RIGHT[ , shifts]

Remarks

Var

Byte, Integer/Word or Long variable.

Shifts

The number of shifts to perform.

 

 

The ROTATE statement rotates all the bits in the variable to the left or right. All bits are preserved so no bits will be shifted out of the variable.

This means that after rotating a byte variable with a value of 1, eight times the variable will be unchanged.

When you want to shift out the MS bit or LS bit, use the SHIFT statement.

See also

SHIFT , SHIFTIN , SHIFTOUT

Example

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

 

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

: rotate.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: example for ROTATE and SHIFT statement

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

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

 

page -619-

 

© MCS Electronics, 1995-2007

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

 

$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

 

'dimension some variables

Dim B As Byte , I As Integer , L As Long

'the shift statement shift all the bits in a variable one 'place to the left or right

'An optional paramater can be provided for the number of shifts. 'When shifting out then number 128 in a byte, the result will be 0 'because the MS bit is shifted out

B = 1

Shift B , Left Print B

'B should be 2 now

B = 128

Shift B , Left Print B

'B should be 0 now

'The ROTATE statement preserves all the bits

'so for a byte when set to 128, after a ROTATE, LEFT , the value will 'be 1

'Now lets make a nice walking light 'First we use PORTB as an output

Config Portb = Output

'Assign value to portb Portb = 1

Do

For I = 1 To 8 Rotate Portb , Left

'wait for 1 second

Wait 1

Next

'and rotate the bit back to the right

For I = 1 To 8

Rotate Portb , Right Wait 1

Next Loop

End

ROUND

Action

Returns a value rounded to the nearest value.

page -620-