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

© MCS Electronics, 1995-2007

Example

Dim I As Integer , Z As Byte

I = &H1001

Z = High(i) ' is 10 hex or 16 dec

End

HIGHW

Action

Retrieves the most significant word of a long variable.

Syntax

var = HIGHW( s )

Remarks

Var

The variable that is assigned with the MS word of var S.

S

The source variable to get the MSB from.

 

 

There is no LowW() function. This because when you assign a Long to a word or nteger,i only the lower part is assigned. For this reason you do not need a Loww() function. W=L will do the same.

See also

LOW , HIGH

Example

Dim X As Word , L As Long

L = &H12345678

X = Highw(l)

Print Hex(x)

HOME

Action

Place the cursor at the specified line at location 1.

Syntax

HOME UPPER | LOWER | THIRD | FOURTH

Remarks

If only HOME is used than the cursor will be set to the upper line.

You may also specify the first letter of the line like: HOME U

page -522-

 

© MCS Electronics, 1995-2007

See also

 

CLS , LOCATE

 

For a complete example see LCD

 

Partial Example

'set cursor

Locate 2 , 1

position

'display this

Lcd "*"

Home Upper

'select line 1 and

return home

 

I2CINIT

Action

Initializes the SCL and SDA pins.

Syntax

I2CINIT

Remarks

By default the SCL and SDA pins are in the right state when you reset the chip. Both the PORT and the DDR bits are set to 0 in that case.

When you need to change the DDR and/or PORT bits you can use I2CINIT to bring the pins in the proper state again.

ASM

The I2C routines are located in i2c.lib. _i2c_init is called.

See also

I2CSEND , I2CSTART , I2CSTOP , I2CRBYTE , I2CWBYTE , I2C_TWI Library for using TWI

Example

Config Sda = Portb.5

Config Scl = Portb.7

I2cinit

Dim X As Byte , Slave As Byte

'reset variable

X = 0

Slave = &H40

'slave address of

a PCF 8574 I/O IC

'get the value

I2creceive Slave , X

Print X

'print it

page -523-

© MCS Electronics, 1995-2007

I2CRECEIVE

Action

Receives data from an I2C serial slave device.

Syntax

I2CRECEIVE slave, var

I2CRECEIVE slave, var , b2W, b2R

Remarks

Slave

A byte, Word/Integer variable or constant with the slave address

 

from the I2C-device.

Var

A byte or integer/word variable that will receive the information

 

from the I2C-device.

b2W

The number of bytes to write.

 

Be cautious not to specify too many bytes!

b2R

The number of bytes to receive.

 

Be cautious not to specify too many bytes!

 

 

You must specify the base address of the slave chip because the read/write bit is set/reset by the software.

When an error occurs, the internal ERR variable will return 1. Otherwise it wil be set to 0.

ASM

The I2C routines are located in the i2c.lib/i2c.lbx files.

See also

I2CSEND, I2CSTART , I2CSTOP, I2CRBYTE , I2CWBYTE

Example

Config Sda = Portb.5

 

Config Scl = Portb.7

 

Dim X As Byte , Slave As Byte

'reset variable

X = 0

Slave = &H40

'slave address of

a PCF 8574 I/O IC

'get the value

I2creceive Slave , X

Print X

'print it

Dim Buf(10)as Byte

 

Buf(1) = 1 : Buf(2) = 2

'send two bytes

I2creceive Slave , Buf(1) , 2 , 1

and receive one byte

'print the

Print Buf(1)

received byte

 

End

 

page -524-

© MCS Electronics, 1995-2007

I2CSEND

Action

Send data to an I2C-device.

Syntax

I2CSEND slave, var

I2CSEND slave, var , bytes

Remarks

Slave

The slave address off the I2C-device.

Var

A byte, integer/word or numbers that holds the value, which will be,

 

send to the I2C-device.

Bytes

The number of bytes to send.

 

 

When an error occurs, the internal ERR variable will return 1. Otherwise it wil be set to 0.

ASM

The I2C routines are located in the i2c.lib/i2c.lbx files.

See also

I2CRECEIVE , I2CSTART, I2CSTOP , I2CRBYTE , I2CWBYTE

Example

Config Sda = Portb.5

 

Config Scl = Portb.7

 

Dim X As Byte , A As Byte , Bytes As Byte

'assign variable

X = 5

to 5

 

Dim Ax(10)as Byte

'slave address of

Const Slave = &H40

a PCF 8574 I/O IC

'send the value or

I2csend Slave , X

For A = 1 To 10

'Fill dataspace

Ax(a) = A

Next

 

Bytes = 10

 

I2csend Slave , Ax(1) , Bytes

 

End

 

I2START,I2CSTOP, I2CRBYTE, I2CWBYTE

Action

page -525-

© MCS Electronics, 1995-2007

I2CSTART generates an I2C start condition.

I2CSTOP generates an I2C stop condition.

I2CRBYTE receives one byte from an I2C-device.

I2CWBYTE sends one byte to an I2C-device.

Syntax

I2CSTART

I2CSTOP

I2CRBYTE var, ack/nack

I2CWBYTE val

Remarks

Var

A variable that receives the value from the I2C-device.

ack/nack

Specify ACK if there are more bytes to read.

 

Specify NACK if it is the last byte to read.

Val

A variable or constant to write to the I2C-device.

 

 

These statements are provided as an addition to the I2CSEND and I2CRECEIVE statements. While I2CSEND and I2CRECEIVE are well suited for most tasks, a slave chip might need a special sequence that is not possible with the I2C routines.

When an error occurs, the internal ERR variable will return 1. Otherwise it wil be set to 0.

ASM

The I2C routines are located in the i2c.lib/i2c.lbx files.

See also

I2CSEND , I2CRECEIVE , I2CSTART , I2CSTOP, I2CRBYTE , I2CWBYTE

Example

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

 

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

: i2c.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: I2CSEND and I2CRECEIVE

'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

 

page -526-

© MCS Electronics, 1995-2007

Config Scl = Portb.4

Config Sda = Portb.5

Declare Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte) Declare Sub Read_eeprom(byval Adres As Byte , Value As Byte)

Const Addressw = 174

'slave write

address

'slave read

Const Addressr = 175

address

 

Dim B1 As Byte , Adres As Byte , Value As Byte

'dim byte

Call Write_eeprom(1 , 3)

'write value of

three to address 1 of EEPROM

 

Call Read_eeprom(1 , Value) : Print Value

'read it back

Call Read_eeprom(5 , Value) : Print Value

'again for address

5

 

'-------- now write to a PCF8474 I/O expander -------

'all outputs high

I2csend &H40 , 255

I2creceive &H40 , B1

'retrieve input

Print "Received data " ; B1

'print it

End

 

Rem Note That The Slaveaddress Is Adjusted Automaticly With I2csend & I2creceive

Rem This Means You Can Specify The Baseaddress Of The Chip.

'sample of writing a byte to EEPROM AT2404

 

Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)

'start condition

I2cstart

I2cwbyte Addressw

'slave address

I2cwbyte Adres

'asdress of EEPROM

I2cwbyte Value

'value to write

I2cstop

'stop condition

Waitms 10

'wait for 10

milliseconds

 

End Sub

 

'sample of reading a byte from EEPROM AT2404

 

Sub Read_eeprom(byval Adres As Byte , Value As Byte)

'generate start

I2cstart

I2cwbyte Addressw

'slave adsress

I2cwbyte Adres

'address of EEPROM

I2cstart

'repeated start

I2cwbyte Addressr

'slave address

(read)

'read byte

I2crbyte Value , Nack

I2cstop

'generate stop

End Sub

 

'when you want to control a chip with a larger memory like the 24c64 it requires an additional byte

'to be sent (consult the datasheet):

'Wires from the I2C address that are not connected will default to 0 in most cases!

page -527-