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

© MCS Electronics, 1995-2007

ASM Libraries

I2C_TWI

By default BASCOM will use software routines when you use I2C statements. This because when the first AVR chips were introduced, there was no TWI yet. Atmel named it TWI because Philips is the inventor of I2C. But TWI is the same as I2C.

So BASCOM allows you to use I2C on every AVR chip. Most newer AVR chips have build in hardware support for I2C. With the I2C_TWI lib you can use the TWI which has advantages as it require less code.

Read more about I2C in the hardware section.

To force BASCOM to use the TWI, you need to insert the following statement into your code:

$LIB "I2C_TWI.LBX"

You also need to choose the correct SCL and SDA pins with the CONFIG SCL and CONFIG SDA statements.

The TWI will save code but the disadvantage is that you can only use the fixed SCL and SDA pins.

EXTENDED I2C

Action

Instruct the compiler to use parts of the extended i2c library

Syntax

$LIB = "i2c_extended.lib"

Remarks

The I2C library was written when the AVR architecture did not have extended registers. The designers of the AVR chips did not preserve enough space for registers. So when they made bigger chips with more ports they ran out of registers.

They solved it to use space from the RAM memory and move the RAM memory from &H60 to &H100.

In the free space from &60 to &H100 the new extended register were ocatedl.

While this is a practical solution, some ASM instructions could not be used anymore. This made it a problem to use the I2C statements on PORTF and PORTG of the Mega128.

The extended i2c library is inteded to use I2C on portF and portGon the M64 and M128. It uses a bit more space then the normal I2C lib.

Best would be that you use the TWI interface and the i2c_twi library as this uses less code. The disadvantage is that you need fixed pins as TWI used a fix pin for SCL and SDA.

page -727-

© MCS Electronics, 1995-2007

See also

I2C

ASM

NONE

Example

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

 

 

--

 

(c) 2005 MCS Electronics

'

 

'

This demo shows an example of I2C on the M128 portF

' PORTF is an extened port and requires a special I2C driver

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

 

 

--

 

 

$regfile = "m128def.dat"

' the used chip

$crystal = 8000000

 

' baud rate

$baud = 19200

 

$lib "i2c_extended.lib"

 

Config Scl = Portf.0

' we need to

provide the SCL pin name

' we need to

Config Sda = Portf.1

provide the SDA pin name

 

Dim B1 As Byte , B2 As Byte

 

Dim W As Word At B1 Overlay

 

I2cinit

 

' we need to set

the pins in the proper state

 

Dim B As Byte , X As Byte

 

Print "Mega128 master demo"

 

Print "Scan start"

 

 

For B = 1 To 254 Step 2

 

I2cstart

 

 

I2cwbyte B

 

 

If Err = 0 Then

 

 

Print "Slave at : " ; B

 

End If

 

 

I2cstop

 

 

Next

 

 

Print "End Scan"

 

 

Do

 

 

I2cstart

 

' slave address

I2cwbyte &H70

 

write

 

' write command

I2cwbyte &B10101010

I2cwbyte 2

 

 

I2cstop

Print Err

page -728-

© MCS Electronics, 1995-2007

I2cstart

 

I2cwbyte &H71

 

I2crbyte B1 , Ack

 

I2crbyte B2 , Nack

 

I2cstop

' show error

Print "Error : " ; Err

Waitms 500

'wait a bit

Loop

 

End

 

MCSBYTE

The numeric<>string conversion routines are optimized when used for byte, integer,word and longs.

When do you use a conversion routine ?

-When you use STR() , VAL() or HEX().

-When you print a numeric variable

-When you use INPUT on numeric variables.

To support all data types the built in routines are efficient in terms of code size. But when you use only conversion routines on bytes there is a overhead.

The mcsbyte.lib library is an optimized version that only support bytes. Use it by including : $LIB "mcsbyte.lbx" in your code.

Note that LBX is a compiled LIB file. In order to change the routines you need the commercial edition with the source code(lib files). After a change you should compile the library with the library manager.

See also

mcsbyteint.lib

MCSBYTEINT

The numeric<>string conversion routines are optimized when used for byte, integer,word and longs.

When do you use a conversion routine ?

-When you use STR() , VAL() or HEX(). -When you print a numeric variable

-When you use INPUT on numeric variables.

To support all data types the built in routines are efficient in terms of code size. But when you use only conversion routines on bytes there is a overhead.

The mcsbyteint.lib library is an optimized version that only support bytes, integers and words.

Use it by including : $LIB "mcsbyteint.lbx" in your code.

page -729-