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

 

© MCS Electronics, 1995-2007

with number (0-7)

'select data RAM

Cls

Rem it is important that a CLS is following the deflcdchar statements because it will set the controller back in datamode

Lcd Chr(0) ; Chr(1)

'print the special

character

 

'----------------- Now use an internal routine ------------

'value into ACC

_temp1 = 1

!rCall _write_lcd

'put it on LCD

End

 

$LCDVFO

Action

Instruct the compiler to generate very short Enable pulse for VFO displays.

Syntax

$LCDVFO

Remarks

VFO based displays need a very short Enable pulse. Normal LCD displays need a longer pulse. To support VFO displays this compiler directive has been added.

The display need to be instruction compatible with normal HD44780 based text displays. Noritake is the biggest manufactor of VFO displays.

The $LCDVFO directive is intended to be used in combination with the LCDroutines.

ASM

NONE

See also

NONE

Example

NONE

$LIB

Action

Informs the compiler about the used libraries.

Syntax

$LIB "libname1" [, "libname2"]

page -227-

© MCS Electronics, 1995-2007

Remarks

Libname1 is the name of the library that holds ASM routines that are used by your program. More filenames can be specified by separating the names by a comma.

The specified libraries will be searched when you specify the routines to use with the $EXTERNAL directive.

The search order is the same as the order you specify the library names.

The MCS.LBX will be searched last and is always included so you don't need to specify ti with the $LIB directive.

Because the MCS.LBX is searched last you can include duplicate routines in your own library. These routines will be used instead of the ones from the default MCS.LBX library. This is a good way when you want to enhance the MCS.LBX routines. Just copy the MCS.LIB to a new file and make the changes in this new file. When we make changes to the library your changes will be preserved.

Creating your own LIB file

A library file is a simple ASCII file. It can be created with the BASCOM editor, notepad or any other ASCII editor.

When you use BASCOM, make sure that the LIB extension is added to the Options, Environment, Editor, "No reformat extension".

This will prevent the editor to reformat the LIB file when you open it.

The file must include the following header information. It is not used yet but will be later. copyright = Your name

www = optional location where people can find the latest source email = your email address

comment = AVR compiler library

libversion = the version of the library in the format : 1.00 date = date of last modification

statement = A statement with copyright and usage information

The routine must start with the name in brackets and must end with the [END].

The following ASM routine example is from the MYLIB.LIB library.

[test]

Test:

ldd r26,y+2 ; load address of X

ldd r27,y+3

ld r24,x ; get value into r24

Inc r24 ; value + 1

St x,r24 ; put back

ldd r26,y+0 ; address pf Y

page -228-

© MCS Electronics, 1995-2007

ldd r27,y+1

st x,r24 ; store

ret ; ready

[END]

After you have saved your library in the LIB subdirectory you must compile it with the LIB Manager. Or you can include it with the LIB extension in which case you don’t have to compile it.

About the assembler.

When you reference constants that are declared in your basic programyou need to put a star(*) before the line.

'basic program

CONST myconst = 7

'asm lib

* sbi portb, myconst

By adding the *, the line will be compiled when the basic program is compiled. It will not be changed into object code in the LBX file.

When you use constants you need to use valid BASIC constants:

Ldi r24,12

Ldi r24, 1+1

Ldi r24, &B001

Ldi r24,0b001

Ldi r24,&HFF

Ldi r24,$FF

Ldi r24,0xFF

Other syntax is NOT supported.

See also

$EXTERNAL

Example

$regfile = "m48def.dat" $crystal = 4000000 $baud = 19200

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

'In order to let this work you must put the mylib.lib file in the LIB dir 'And compile it to a LBX '-------------------------------------------------------------------------

page -229-

© MCS Electronics, 1995-2007

'define the used library $lib"mylib.lbx"

'you can also use the original ASM : '$LIB "mylib.LIB"

'also define the used routines $external Test

'this is needed so the parameters will be placed correct on the stack Declare Sub Test(byval X Asbyte , Y Asbyte)

'reserve some space

Dim Z As Byte

'call our own sub routine Call Test(1 , Z)

'z will be 2 in the used example

End

$LOADER

Action

Instruct the compiler to create a bootloader at the specified address.

Syntax

$LOADER = address

Remarks

address

The address where the bootloader is located. You can find this address in

 

the datasheet.

 

 

Most AVR chips have a so called boot section. Normally a chip will start at address 0 when it resets. This is also called the reset vector.

Chips that have a bootsection, split the flash memory in two parts. The bootsection is a small part of the normal flash and by setting a fusebit you select that the chip runs code at the bootsector when it resets instead of the normal reset vector.

Some chips also have fusebits to select the size of the bootloader.

The MCS bootloader sample is a serial bootloader that uses the serial port. It uses the X-modem checksum protocol to receive the data. Most terminal emulators can send X-modem checksum.

The sample is written so it supports all chips with a bootsection. You need to do the following :

indentify the $regfile directive for your chip

un-remark the line and the line with the CONST that is used for conditional compilation

remark all other $regfile lines and CONST lines.

compile the file

program the chip

set the fusebit so reset is pointed to the bootloader

set the fusebit so the bootsize is 1024 words

select the MCS Bootloader programmer.

page -230-

© MCS Electronics, 1995-2007

The bootloader is written to work at a baudrate of 57600. This works for most chips that use the internal oscillator. But it is best to check it first with a simple program. When you use a crystal you might even use a higher speed.

Do not forget that the MCS bootloader must be set to the same baud rate as the bootloader program.

Now make a new test program and compile it. Press F4 to start the MCS bootloader. You now need to reset the chip so that it will start the bootloader section. The bootloader will send a byte with value of 123 and the bascom bootloader receives this and thus starts the loader process.

There will be a stand alone bootloader available too. And the sample will be extended to support other AVR chips with bootsection too.

There is a $BOOT directive too. It is advised to use $LOADER as it allows you to write the bootloader in BASIC.

You can not use interrupts in your bootloader program as the interrupts will point to the reset vector which is located in the lower section of the flash. When you start to writing pages, you overwrite this part.

See also

$BOOT , $LOADERSIZE

Example

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

(c) 1995-2005, MCS

'

'

Bootloader.bas

'This sample demonstrates how you can write your own bootloader

'in BASCOM BASIC

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

'This sample will be extended to support other chips with bootloader 'The loader is supported from the IDE

'$regfile = "m88def.dat" 'Const Loader = 88

'$regfile = "m32def.dat" 'Const Loaderchip = 32

'$regfile = "m88def.dat" 'Const Loaderchip = 88

$regfile = "m162def.dat" Const Loaderchip = 162

#if Loaderchip = 88

'Mega88

$loader = $c00

'this address you

can find in the datasheet

'the loader address is the same as the boot vector address Const Maxwordbit = 5

page -231-

© MCS Electronics, 1995-2007

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

Databits = 8 , Clockpol = 0

 

 

#endif

' Mega32

#if Loaderchip = 32

$loader = $3c00

' 1024

words

Const Maxwordbit = 6

'Z6 is

maximum bit

 

'

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

Databits = 8 , Clockpol = 0

 

#endif

' Mega8

#if Loaderchip = 8

$loader = $c00

' 1024 words

Const Maxwordbit = 5

'Z5 is maximum bit

 

'

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

Databits = 8 , Clockpol = 0

 

#endif

' Mega161

#if Loaderchip = 161

$loader = $1e00

' 1024 words

Const Maxwordbit = 6

'Z5 is maximum bit

#endif

'

' Mega162

#if Loaderchip = 162

$loader = $1c00

' 1024 words

Const Maxwordbit = 6

'Z5 is maximum bit

'

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

#endif

Const

Maxword =(2 ^ Maxwordbit) *

2

'128

Const

Maxwordshift = Maxwordbit +

1

 

$crystal = 8000000 '$crystal = 14745600

$baud = 57600 'this loader uses serial com

'It is VERY IMPORTANT that the baud rate matches the one of the boot loader

'do not try to use buffered com as we can not use interrupts

'Dim the used variables

Dim Bstatus As Byte , Bretries As Byte , Bblock As Byte , Bblocklocal As Byte Dim Bcsum1 As Byte , Bcsum2 As Byte , Buf(128) As Byte , Csum As Byte

Dim J As Byte , Spmcrval As Byte

' self program

command byte value

 

Dim Z As Word

'this is the Z

pointer word

' these bytes are

Dim Vl As Byte , Vh As Byte

used for the data values

'these vars

Dim Wrd As Byte , Page As Byte

contain the page and word address

 

'Mega 88 : 32 words, 128 pages

 

Disable Interrupts

'we do not use

ints

 

Waitms 1000

'wait 1 sec

page -232-

© MCS Electronics, 1995-2007

'We start with receiving a file. The PC must send this binary file

'some constants used in serial com Const Nak = &H15

Const Ack = &H06

Const Can = &H18

'we use some leds as indication in this sample , you might want to remove it

Config Portb = Output

'the stk200 has

Portb = 255

inverted logic for the leds

 

'$timeout = 1000000

'we use a timeout

$timeout = 1000000

'we use a timeout

'Do

'wait for the

Bstatus = Waitkey()

loader to send a byte

 

Print Chr(bstatus);

'did we received

If Bstatus = 123 Then

value 123 ?

 

Goto Loader

 

End If

 

'Loop

 

For J = 1 To 10

'this is a simple

indication that we start the normal reset vector

 

Toggle Portb : Waitms 100

 

Next

 

Goto _reset

'goto the normal

reset vector at address 0

 

'this is the loader routine. It is a Xmodem-checksum reception routine

Loader:

'this is a simple

For J = 1 To 3

indication that we start the normal reset vector

 

Toggle Portb : Waitms 500

 

Next

 

Spmcrval = 3 : Gosub Do_spm

' erase the first

page

' re-enable page

Spmcrval = 17 : Gosub Do_spm

Bretries = 10

'number of retries

Do

'checksum is 0

Csum = 0

when we start

' firt time send a

Print Chr(nak);

nack

 

Do

'wait for statuse

Bstatus = Waitkey()

byte

 

Select Case Bstatus

' start of

Case 1:

heading, PC is ready to send

'increase local

Incr Bblocklocal

block count

'checksum is 1

Csum = 1

Bblock = Waitkey() : Csum = Csum + Bblock

'get block

Bcsum1 = Waitkey() : Csum = Csum + Bcsum1

'get checksum

first byte

'get 128 bytes

For J = 1 To 128

page -233-

© MCS Electronics, 1995-2007

Buf(j) = Waitkey() : Csum = Csum + Buf(j)

 

Next

'get second

Bcsum2 = Waitkey()

checksum byte

'are the blocks

If Bblocklocal = Bblock Then

the same?

'is the checksum

If Bcsum2 = Csum Then

the same?

'yes go write the

Gosub Writepage

page

'acknowledge

Print Chr(ack);

Else

'no match so send

nak

 

Print Chr(nak);

 

End If

 

Else

'blocks do not

Print Chr(nak);

match

 

End If

' end of

Case 4:

transmission , file is transmitted

' send ack and

Print Chr(ack);

ready

 

Portb.3 = 0

' simple

indication that we are finished and ok

' start new

Goto _reset

program

' PC aborts

Case &H18:

transmission

' ready

Goto _reset

Case Else

' no valid data

Exit Do

End Select

 

Loop

'attempte left?

If Bretries > 0 Then

Waitms 1000

'decrease attempts

Decr Bretries

Else

'reset chip

Goto _reset

End If

 

Loop

 

'write one or more pages

 

Writepage:

'we write 2 bytes

For J = 1 To 128 Step 2

into a page

'get Low and High

Vl = Buf(j) : Vh = Buf(j + 1)

bytes

'store them into

lds r0, {vl}

r0 and r1 registers

 

lds r1, {vh}

'write value into

Spmcrval = 1 : Gosub Do_spm

page at word address

' word address

Wrd = Wrd + 2

increases with 2 because LS bit of Z is not used

' page is full

If Wrd = Maxword Then

Wrd = 0

'Z pointer needs

wrd to be 0

'write page

Spmcrval = 5 : Gosub Do_spm

Page = Page + 1

'next page

Spmcrval = 3 : Gosub Do_spm

' erase next page

page -234-