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

© MCS Electronics, 1995-2007

COUNTER0 and COUNTER1

Action

Set or retrieve the internal 16 bit hardware register.

Syntax

COUNTER0 = var

TIMER0 can also be used

var = COUNTER0

 

COUNTER1 = var

TIMER1 can also be used

var = COUNTER1

 

CAPTURE1 = var

TIMER1 capture register

var = CAPTURE1

 

COMPARE1A = var

TIMER1 COMPARE A register

var = COMPARE1A

 

COMARE1B = var

TIMER1 COMPARE B register

var = COMPARE1B

 

PWM1A = var

TIMER1 COMPAREA register. (Is used for PWM)

var = PWM1A

 

PWM1B = var

TIMER1 COMPARE B register. (Is used for PWM)

var = PRM1B

 

 

 

Remarks

Var

A byte, Integer/Word variable or constant that is assigned to the register

 

or is read from the register.

 

 

Because the above 16 bit register pairs must be accessed somewhat differently than you may expect, they are implemented as variables.

The exception is TIMER0/COUNTER0, this is a normal 8 bit register and is supplied for compatibility with the syntax.

When the CPU reads the low byte of the register, the data of the low byte is sent to the CPU and the data of the high byte is placed in a temp register. When the CPU reads the data in the high byte, the CPU receives the data in the temp register.

When the CPU writes to the high byte of the register pair, the written data is placed in a temp register. Next when the CPU writes the low byte, this byte of data is combined with the byte data in the temp register and all 16 bits are written to the register pairs. So the MSB must be accessed first.

All of the above is handled automatically by BASCOM when accessing the above registers.

Note that the available registers may vary from chip to chip.

The BASCOM documentation used the 90S8515 to describe the different hardware registers.

CPEEK

page -407-

© MCS Electronics, 1995-2007

Action

Returns a byte stored in code memory.

Syntax

var = CPEEK( address )

Remarks

Var

Numeric variable that is assigned with the content of the program memory

 

at

 

address

Address

Numeric variable or constant with the address location

 

 

There is no CPOKE statement because you can not write into programmemory. Cpeek(0) will return the first byte of the file. Cpeek(1) will return the second byte of the binary file.

See also

PEEK , POKE , INP , OUT

Example

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

 

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

: peek.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstrates PEEk, POKE, CPEEK, INP and OUT

'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

 

Dim I As Integer , B1 As Byte

 

'dump internal memory

'only 32 registers

For I = 0 To 31

in AVR

'get byte from

B1 = Peek(i)

internal memory

 

Print Hex(b1) ; " ";

'write a value into memory

'Poke I , 1

Next

'new line

Print

'be careful when writing into internal memory !!

page -408-

 

© MCS Electronics, 1995-2007

'now dump a part ofthe code-memory(program)

For I = 0 To 255

'get byte from

B1 = Cpeek(i)

internal memory

";

Print Hex(b1) ; "

Next

 

'note that you can not write into codememory!!

Out &H8000 , 1

'write 1 into XRAM

at address 8000

'return value from

B1 = Inp(&H8000)

XRAM

 

Print B1

 

End

 

CPEEKH

Action

Returns a byte stored in upper page of code memory of micro with more then 64KB such as M103, M128.

Syntax

var = CPEEKH( address )

Remarks

Var

Numeric variable that is assigned with the content of the program memory

 

at

 

address

Address

Numeric variable or constant with the address location

 

 

CpeekH(0) will return the first byte of the upper 64KB.

Since the M103 has 64K words of code space the LPM nstruction can not access the 64 upper Kbytes.

The CpeekH() function peeks in the upper 64 KB.

This function should be used with the M103 or M128 only.

See also

PEEK , POKE , INP , OUT

Example

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

 

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

: peek.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstrates PEEk, POKE, CPEEK, INP and OUT

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

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

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

$regfile = "m48def.dat"

' specify the used

 

page -409-

 

© MCS Electronics, 1995-2007

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 Integer , B1 As Byte

'dump internal memory

For I = 0 To 31

'only 32 registers

in AVR

'get byte from

B1 = Peek(i)

internal memory

";

Print Hex(b1) ; "

'Poke I , 1

'write a value into memory

Next

'new line

Print

'be careful when writing into internal memory !!

'now dump a part ofthe code-memory(program)

For I = 0 To 255

'get byte from

B1 = Cpeek(i)

internal memory

";

Print Hex(b1) ; "

Next

'note that you can not write into codememory!!

Out &H8000 , 1

'write 1 into XRAM

at address 8000

'return value from

B1 = Inp(&H8000)

XRAM

 

Print B1

 

End

 

CRC8

Action

Returns the CRC8 value of a variable or array.

Syntax

Var = CRC8( source , L)

Remarks

Var

The variable that is assigned with the CRC8 of variable source.

Source

The source variable or first element of the array to get the CRC8 of.

L

The number of bytes to check.

 

 

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

The code below shows a VB function of crc8

page -410-

© MCS Electronics, 1995-2007

Function Docrc8(s As String) As Byte Dim j As Byte

Dim k As Byte Dim crc8 As Byte crc8 = 0

For m = 1 To Len(s)

x = Asc(Mid(s, m, 1)) 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

See also

CHECKSUM , CRC16, CRC32 , TCPCHECKSUM

ASM

The following routine is called from mcs.lib : _CRC8

The routine must be called with Z pointing to the data and R24 must contain the number of bytes to check.

On return, R16 contains the CRC8 value. The used registers are : R16-R19, R25.

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

Ldi R24,$07 ; number of bytes

Ldi R30,$64

; address of ar(1)

Ldi R31,$00 ; load constant in register

Rcall _Crc8

; call routine

Ldi R26,$60

; address of X

St X,R16

; store crc8

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

page -411-

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