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

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

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

Добавлен: 12.06.2025

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

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

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

© MCS Electronics, 1995-2007

Calls: _BcdStr

Input: X hold address of variable

Output: R0 with number of bytes, frame with data.

Example

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

---

: bcd.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstration of split and combine BCD Bytes

'suited for demo

: yes

'commercial addon needed

: no

'use in simulator

: possible

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

---

' specify the used

$regfile = "m48def.dat"

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 19200

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

= 8 , Clockpol = 0

$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

'=============================================================================

==

' Set up Variables '=============================================================================

==

'Setup A Variable

Dim A As Byte

Dim B As Byte

'Setup B Variable

Dim C As Byte

'Setup C Variable

A = &H89 '=============================================================================

==

' Main '=============================================================================

==

Main:

" ; Hex(a)

'Print A

Print "Combined :

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

--

'Mask To Get Only High

B = A And &B1111_0000

Nibble Of Byte

'Shift High Nibble To

Shift B , Right , 4

Low Nibble Position , Store As B

C = A And &B0000_1111

'Mask To Get Only Low

Nibble Of Byte , Store As C

Print "Split :

" ; B ; " " ; C

'Print B (High Nibble)

, C(low Nibble)

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

page -283-


© MCS Electronics, 1995-2007

--

'Shift Data From Low

Shift B , Left , 4

Nibble Into High Nibble Position

A = B + C

'Add B (High Nibble)

And C(low Nibble) Together

Print "Re-Combined: " ; Hex(a)

'Print A (re -combined

Byte)

'End Program

End

BIN

Action

Convert a numeric variable into the binary string representation.

Syntax

Var = Bin(source)

Remarks

Var

The target string that will be assigned with the binary

representation of the variable source.

Source

The numeric variable that will be converted.

The BIN() function can be used to display the state of a port.

When the variable source has the value &B10100011 the string named var will be assigned with "10100011".

It can be easily printed to the serial port.

See also

HEX , STR , VAL , HEXVAL , BINVAL

ASM

NONE

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

page -284-


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