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

 

© MCS Electronics, 1995-2007

$framesize = 40

' default use 40

for the frame space

 

Dim A As Byte

 

A = 1

'assign var

While A < 10

'test expression

Print A

'print var

Incr A

'increase by one

Wend

'continue loop

End

 

WRITE

Action

Writes data to a sequential file

Syntax

WRITE #ch , data [,data1]

Remarks

Ch

A channel number, which

 

identifies an opened file. This can be a hard coded constant or a

 

variable.

Data , data1

A variable who’s content are written to the file.

 

 

When you write a variables value, you do not write the binary representation but the ASCII representation. When you look in a file it contains readable text.

When you use PUT, to write binary info, the files are not readable or contain unreadable characters.

Strings written are surrounded by string delimiters "". Multiple variables written are separated by a comma. Consider this example :

Dim S as String * 10 , W as Word

S="hello" : W = 100

OPEN "test.txt" For OUTPUT as #1

WRITE #1, S , W

CLOSE #1

The file content will look like this : "hello",100

Use INPUT to read the values from value.

See also

INITFILESYSTEM , OPEN , CLOSE, FLUSH , PRINT, LINE INPUT, LOC, LOF , EOF , FREEFILE , FILEATTR , SEEK , BSAVE , BLOAD , KILL , DISKFREE , GET , PUT , FILEDATE , FILETIME , FILEDATETIME , DIR , WRITE , INPUT

ASM

Calls

_FileWriteQuotationMark

_FileWriteDecInt

 

page -717-

 

© MCS Electronics, 1995-2007

 

_FileWriteDecByte

_FileWriteDecWord

 

_FileWriteDecLong

_FileWriteDecSingle

Input

Z points to variable

 

Output

 

 

 

 

 

Partial Example

Dim S As String * 10 , W As Word , L As Long

S = "write"

 

Open "write.dmo"for Output As #2

' write is also

Write #2 , S , W , L

supported

 

Close #2

 

Open "write.dmo"for Input As #2

' write is also

Input #2 , S , W , L

supported

 

Close #2

 

Print S ; " " ; W ; " " ; L

 

WRITEEEPROM

Action

Write a variables content to the DATA EEPROM.

Syntax

WRITEEEPROM var , address

Remarks

var

The name of the variable that must be stored

address

The address in the EEPROM where the variable must be stored.

 

A new option is that you can provide a label name for the address. See

 

example 2.

 

 

This statement is provided for compatibility with BASCOM-8051.

You can also use :

Dim V as Eram Byte 'store in EEPROM

Dim B As Byte 'normal variable

B = 10

V = B 'store variable in EEPROM

When you use the assignment version, the data types must be the same!

According to a data sheet from ATMEL, the first location in the EEPROM with address 0, can be overwritten during a reset. It is advised not to use this location.

For security, register R23 is set to a magic value before the data is written to the EEPROM. All interrupts are disabled while the EEPROM data is written. Interrupts are enabled automatic when the data is written.

page -718-

© MCS Electronics, 1995-2007

It is advised to use the Brownout circuit that is available on most AVR processors. This will prevent that data is written to the EEPROM when the voltage drops under the specified evel.

When data is written to the EEPROM, all interrupts are disabled, and after the EEPROM has been written, the interrupts are re-enabled.

See also

READEEPROM

ASM

NONE

Example

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

 

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

: eeprom2.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: shows how to use labels with READEEPROM

'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

 

'first dimension a variable

Dim B As Byte

Dim Yes As String * 1

'Usage for readeeprom and writeeprom : 'readeeprom var, address

'A new option is to use a label for the address of the data

'Since this data is in an external file and not in the code the eeprom data 'should be specified first. This in contrast with the normal DATA lines which must

'be placed at the end of your program!!

'first tell the compiler that we are using EEPROM to store the DATA $eeprom

'the generated EEP file is a binary file.

'Use $EEPROMHEX to create an Intel Hex file usable with AVR Studio. '$eepromhex

'specify a label Label1:

page -719-

© MCS Electronics, 1995-2007

Data 1 , 2 , 3 , 4 , 5

Label2:

Data 10 , 20 , 30 , 40 , 50

'Switch back to normal data lines in case they are used $data

'All the code above does not generate real object code 'It only creates a file with the EEP extension

'Use the new label option

 

Readeeprom B , Label1

'prints 1

Print B

'Succesive reads will read the next value

 

'But the first time the label must be specified so the start is known

Readeeprom B

Print B

'prints 2

Readeeprom B , Label2

'prints

10

Print B

Readeeprom B

'prints

20

Print B

'And it works for writing too :

'but since the programming can interfere we add a stop here Input "Ready?" , Yes

B = 100

Writeeeprom B , Label1 B = 101

Writeeeprom B

'read it back

 

Readeeprom B , Label1

'prints 1

Print B

'Succesive reads will read the next value

'But the first time the label must be specified so the start is known

Readeeprom B

'prints 2

Print B

End

 

X10DETECT

Action

Returns a byte that indicates if a X10 Power line interface is found.

Syntax

Result = X10DETECT( )

Remarks

Result

A variable that will be assigned with 0 if there is no Power Line Interface

 

found.

 

1 will be returned if the interface is found, and the detected mains frequency

 

 

 

 

page -720-

^--- transmission pin

© MCS Electronics, 1995-2007

is 50 Hz.

2 will be returned if the interface is found and tre detected mains frequency is 60 Hz.

When no TW-523 or other suitable interface is found, the other X10 routines will not work.

See also

CONFIG X10 , X10SEND

Example

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

 

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

: x10.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: example needs a TW-523 X10 interface

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

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

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

$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

 

'define the house code

' use code A-P

Const House = "M"

Waitms 500

' optional delay

not really needed

 

'dim the used variables

 

Dim X As Byte

 

'configure the zero cross pin and TX pin Config X10 = Pind.4 , Tx = Portb.0

'^--zero cross

'

'detect the TW-523 X = X10detect()

Print X ' 0 means error, 1 means 50 Hz, 2 means 60 Hz

Do

Input "Send (1-32) " , X 'enter a key code from 1-31 '1-16 to address a unit '17 all units off

'18 all lights on '19 ON

page -721-