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

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

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

Добавлен: 12.06.2025

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

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

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

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