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

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

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

Добавлен: 12.06.2025

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

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

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

© MCS Electronics, 1995-2007

Dim Ar(10) As Byte

Dim J As Byte

Ar(1) = 1

Ar(2) = 2

Ar(3) = 3

J = Crc8(ar(1) , 3)

'calculate value

which is 216

Print J

End

CRC16

Action

Returns the CRC16 value of a variable or array.

Syntax

Var = CRC16( source , L)

Remarks

Var

The variable that is assigned with the CRC16 of variable source. Should be a

word or integer variable.

Source

The source variable or first element of the array to get the CRC16 value from.

L

The number of bytes to check.

CRC16 is used in communication protocols to check if there are no transmission errors. The 1wire for example returns a crc byte as the last byte from it’s ID.

Use CRC8 for the 1wire routines.

There are a lot of different CRC16 routines. There is no real standard since the polynomial will vary from manufacture to manufacture.

The equivalent code in VB is shown below. There are multiple ways to implement it in VB. This is one of them.

VB CRC16 Sample

Private Sub Command1_Click()

Dim ar(10) As Byte

Dim b As Byte

Dim J As Integer

ar(1) = 1 ar(2) = 2 ar(3) = 3

b = Docrc8(ar(), 3) ' call funciton Print b

'calculate value which is 216

page -412-


© MCS Electronics, 1995-2007

J = CRC16(ar(), 3) ' call function

Print J

End Sub

Function Docrc8(ar() As Byte, bts As Byte) As Byte Dim J As Byte

Dim k As Byte Dim crc8 As Byte crc8 = 0

For m = 1 To bts

x = ar(m)

For k = 0 To 7

J = 1 And (x Xor crc8)

crc8 = Fix(crc8 / 2) And &HFF x = Fix(x / 2) And &HFF

If J <> 0 Then

crc8 = crc8 Xor &H8C End If

Next k Next

Docrc8 = crc8 End Function

'*****************************************************************

Public Function CRC16(buf() As Byte, lbuf As Integer) As Integer Dim CRC1 As Long

Dim b As Boolean CRC1 = 0 ' init CRC

For i = 1 To lbuf ' for each byte CRC_MSB = CRC1 \ 256 crc_LSB = CRC1 And 255 CRC_MSB = CRC_MSB Xor buf(i)

CRC1 = (CRC_MSB * 256) + crc_LSB

For J = 0 To 7 Step 1 ' for each bit

CRC1 = shl(CRC1, b)

If b Then CRC1 = CRC1 Xor &H1021

Next J

Next i

CRC16 = CRC1

End Function

'Shift Left function

Function shl(n As Long, ByRef b As Boolean) As Long Dim L As Long

L = n

L = L * 2

If (L > &HFFFF&) Then b = True

Else

b = False End If

shl = L And &HFFFF& End Function

page -413-

© MCS Electronics, 1995-2007

See also

CHECKSUM , CRC8, CRC32 , TCPCHECKSUM

ASM

The following routine is called from mcs.lib : _CRC16

The routine must be called with X pointing to the data. The soft stack–Y must contain the number of bytes to scan.

On return, R16 and R17 contain the CRC16 value. The used registers are : R16-R19, R25.

;##### X = Crc16(ar(1) , 7)

Ldi R24,$07 ; number of bytes

St –y, R24

Ldi R26,$64 ; address of ar(1)

Ldi R27,$00 ; load constant in register

Rcall _Crc16

; call routine

Ldi R26,$60

; address of X

St X+,R16 ; store crc16 LSB

St X , R17

; store CRC16 MSB

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 Ar(10) As Byte

Dim J As Byte

Dim W As Word

Dim L As Long

Ar(1) = 1

Ar(2) = 2

Ar(3) = 3

J = Crc8(ar(1) , 3)

'calculate value

which is 216

'24881

W = Crc16(ar(1) , 3)

L = Crc32(ar(1) , 3)

'494976085

End

page -414-


© MCS Electronics, 1995-2007

CRC32

Action

Returns the CRC32 value of a variable.

Syntax

Var = CRC32( source , L)

Remarks

Var

The

LONG variable that is assigned with the CRC32 of variable source.

Source

The source variable or first element of the array to get the CRC

32 value from.

L

The number of bytes to check.

CRC32 is used in communication protocols to check if there are no transmission errors.

See also

CHECKSUM , CRC8, CRC16 , TCPCHECKSUM

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 Ar(10) As Byte

Dim J As Byte

Dim W As Word

Dim L As Long

Ar(1) = 1

Ar(2) = 2

Ar(3) = 3

J = Crc8(ar(1) , 3)

'calculate value

which is 216

'24881

W = Crc16(ar(1) , 3)

L = Crc32(ar(1) , 3)

'494976085

End

page -415-


© MCS Electronics, 1995-2007

CRYSTAL

Action

Special byte variable that can be used with software UART routine to change the baudrate during runtime.

Syntax

CRYSTAL = var (old option do not use !!)

___CRYSTAL1 = var BAUD #1, 2400

Remarks

With the software UART you can generate good baud rates. But chips such as the ATtiny22 have an internal 1 MHz clock. The clock frequency can change during runtime by influence of temperature or voltage.

The crystal variable can be changed during runtime to change the baud rate.

The above has been changed in version 1.11

Now you still can change the baud rate with the crystal variable.

But you don't need to dimension it. And the name has been changed:

___CRYSTALx where x is the channel number.

When you opened the channel with #1, the variable will be named ___CRYSTAL1

But a better way is provided now to change the baud rate of the software uart at run time. You can use the BAUD option now:

Baud #1 , 2400 'change baud rate to 2400 for channel 1

When you use the baud # option, you must specify the baud rate before you print or use input on the channel. This will dimension the ___CRYSTALx variable and load it with the right value.

When you don't use the BAUD # option the value will be loaded from code and it will not use 2 bytes of your SRAM.

The ___CRYSTALx variable is hidden in the report file because it is a systemvariable. But you may assign a value to it after BAUD #x, zzzz has dimensioned it.

The old CRYSTAL variable does not exist anymore.

Some values for 1 MHz internal clock :

66 for 2400 baud

31 for 4800 baud

page -416-

© MCS Electronics, 1995-2007

14 for 9600 baud

See also

OPEN , CLOSE

Example

Dim B asbyte Open"comd.1:9600,8,n,1,inverted"ForOutputAs#1 Print#1 , B

Print#1 ,"serial output"

baud#1, 4800 'use 4800 baud now Print#1,"serial output"

___CRYSTAL1 = 255 Close#1

End

CURSOR

Action

Set the LCD Cursor State.

Syntax

CURSOR ON / OFF BLINK / NOBLINK

Remarks

You can use both the ON or OFF and BLINK or NOBLINK parameters.

At power up the cursor state is ON and NOBLINK.

See also

DISPLAY , LCD

Example

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

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

: lcd.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: LCD, CLS, LOWERLINE, SHIFTLCD, SHIFTCURSOR,

HOME

CURSOR, DISPLAY

'

'micro

: Mega8515

'suited for demo

: yes

'commercial addon needed

: no

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

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

$regfile = "m8515.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

page -417-