CONFIG TWI/de

From MCS Wiki AVR
Jump to: navigation, search

Contents

= (**COPIED FROM ENGLISH PAGE**) === Action

Configure the TWI (two wire serial interface).

 

 

Syntax

CONFIG TWI = clockspeed

CONFIG TWIC | TWID | TWIE | TWIF = clockspeed

 

(Config TWI is for ATMEGA and Config TWIX is for ATXMEGA chips)

 

Remarks

clockspeed

The desired clock frequency for SCL

 

 

CONFIG TWI will set TWSR pre scaler bits 0 and 1, and TWBR depending on the used $CRYSTAL frequency and the desired SCL clock speed.

Typical you need a speed of 400 KHz. Some devices will work on 100 KHz as well.

 

When TWI is used in SLAVE mode, you need to have a faster clock speed as the master.

 

XMEGA

The XMEGA can contain up to 4 TWI units. When not specifying TWIC, TWID, TWIE or TWIF, the TWIC will be used as  the default.

You MUST dimension a variable named TWI_START as a byte. It is used by the xmega TWI library code. Without it, you will get an error.

 

There are 2 manuals available from ATMEL for every ATXMEGA Chip

 

1. One Family Manual like for example for a ATXMEGA128A1 it is Atmel AVR XMEGA A Manual
2. Another Manual for the single chips like for example for an ATXMEGA128A1 it is the ATxmega64A1/128A1/192A1/256A1/384A1 Manual. In this Manual you find for example the Alternate Pin Functions. So you can find which Pin on Port C is the SDA and SCL Pin when you want to use the I2C/TWI Interface of this Port.

 

 

Notice.jpg
 It is important that you specify the proper crystal frequency. Otherwise it will result in a wrong TWI clock frequency.

 

 

See also

$CRYSTAL , OPEN

 

 

Example


'-----------------------------------------------------------------------
' (c) 2004 MCS Electronics
' This demo shows an example of the TWI
' Not all AVR chips have TWI (hardware I2C)
'------------------------------------------------------------------------
 
'The chip will work in TWI/I2C master mode
'Connected is a PCF8574A 8-bits port extender
 
 
$regfile="M8def.dat"' the used chip
$crystal= 4000000 ' frequency used
$baud= 19200 ' baud rate
 
 
$lib"i2c_twi.lbx"' we do not use software emulated I2C but the TWI
 
Config Scl =Portc.5 ' we need to provide the SCL pin name
Config Sda =Portc.4 ' we need to provide the SDA pin name
 
'On the Mega8, On the PCF8574A
'scl=PC5 , pin 28 pin 14
'sda=PC4 , pin 27 pin 15
 
 
I2cinit' we need to set the pins in the proper state
 
 
Config Twi = 100000 ' wanted clock frequency
'will set TWBR and TWSR
'Twbr = 12 'bit rate register
'Twsr = 0 'pre scaler bits
 
Dim B AsByte, X AsByte
Print"TWI master"
Do
Incr B ' increase value
I2csend&B01110000 , B ' send the value
Print"Error : ";Err' show error status
I2creceive&B01110000 , X ' get a byte
Print X ;" ";Err' show error
Waitms 500 'wait a bit
Loop
End

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language