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

© MCS Electronics, 1995-2007

= 8 , Clockpol = 0

Dim B As Byte

' assign value to B B = 45

Dim S As String * 10 'convert to string S = Bin(b)

'assign value to portb Portb = 33

Print Bin(portb)

'of course it also works for other numerics

End

BINVAL

Action

Converts a string representation of a binary number into a number.

Syntax

var = Binval( s)

Remarks

Var

A numeric variable that is assigned with the value of s.

S

Variable of the string type. Should contain only 0 and 1 digits.

 

 

See also

STR , HEXVAL , HEX , BIN , VAL

Example

$regfile = "m48def.dat"

' specify the used

micro

' used crystal

$crystal = 8000000

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

 

Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Dim S As String * 8

S = "11001100"

page -285-

© MCS Electronics, 1995-2007

Dim B As Byte

' assign value to B B = Binval(s)

Print B

End

BIN2GRAY

Action

Returns the Gray-code of a variable.

Syntax

var1 = Bin2gray(var2)

Remarks

var1

Variable that will be assigned with the Gray code.

var2

A variable that will be converted.

 

 

Gray code is used for rotary encoders. Bin2gray() works with byte , integer, word and long variables.

The data type of the variable that will be assigned determines if a byte, word or long conversion will be done.

See also

GRAY2BIN , ENCODER

ASM

Depending on the data type of the target variable the following routine will be called from mcs.lbx:

_grey2Bin for bytes , _grey2bin2 for integer/word and _grey2bin4 for longs.

Example

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

 

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

: graycode.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: show the Bin2Gray and Gray2Bin functions

'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

page -286-

© MCS Electronics, 1995-2007

 

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

 

'Bin2Gray() converts a byte,integer,word or long into grey code.

'Gray2Bin() converts a gray code into a binary value

 

Dim B As Byte

' could be

word,integer or long too

Print "BIN" ; Spc(8) ; "GREY"

For B = 0 To 15

Print B ; Spc(10) ; Bin2gray(b)

Next

Print "GREY" ; Spc(8) ; "BIN"

For B = 0 To 15

Print B ; Spc(10) ; Gray2bin(b)

Next

End

BITWAIT

Action

Wait until a bit is set or reset.

Syntax

BITWAIT x , SET/RESET

Remarks

X

Bit variable or internal register like PORTB.x , where x ranges from0-7.

 

 

When using bit variables make sure that they are set/reset by software otherwise your program will stay in a loop.

When you use internal registers that can be set/reset by hardware such as PINB.0 this doesn't apply since this state can change as a result from for example a key press.

See also

NONE

ASM

Calls: NONE

Input: NONE

Output: NONE

Code : shown for address 0-31

page -287-

© MCS Electronics, 1995-2007

label1:

Sbic PINB.0,label2 Rjmp label1 Label2:

Example

$regfile = "m48def.dat"

' specify the used

micro

' used crystal

$crystal = 8000000

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

 

Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Dim A As Bit

Bitwait A , Set 'wait until bit a is set

'the above will never contine because it is not set i software 'it could be set in an ISR routine

Bitwait Pinb.7 , Reset

'wait until bit 7

of Port B is 0.

 

End

 

BITS

Action

Set all specified bits to 1.

Syntax

Var = Bits( b1 [,bn])

Remarks

Var

The BYTE/PORT variable that is assigned with the constant.

 

B1 , bn A list of bit numbers that must be set to 1.

While it is simple to assign a value to a byte, and there is special boolean notation &B for assigning bits, the Bits() function makes it simple to assign a few bits.

B = &B1000001 : how many zero’s are there?

This would make it more readable : B = Bits(0, 6)

You can read from the code that bit 0 and bit 6 are set to 1.

It does not save code space as the effect is the same.

It can only be used on bytes and port registers.

page -288-