Maker Pro
Maker Pro

TWI code example for two AVR

P

pidy

Jan 1, 1970
0
Hi,

I need code examples using TWI between two AVR units (in a Master->Slav
configuration)

I have seen examples either with only a Master and an EEPROM or somethin
else slave, but no examples of a full-blown communication channel betwee
two AVRs.

I m using AT90CAN128 but i think also code for generic AVR device its ok.

Your help is much appreciated,
Thanks,
Daniele
 
S

SioL

Jan 1, 1970
0
pidy said:
Hi,

I need code examples using TWI between two AVR units (in a Master->Slave
configuration)

I have seen examples either with only a Master and an EEPROM or something
else slave, but no examples of a full-blown communication channel between
two AVRs.

I m using AT90CAN128 but i think also code for generic AVR device its ok.

Your help is much appreciated,
Thanks,
Daniele

Does it really have to be I2C?

SioL
 
M

MK

Jan 1, 1970
0
pidy said:
yes I have to use TWI, if its what u mean

There is a good example on the Silabs website for their 8051 based
processors which seem to use exactly the same TWI/I2C interface hardware as
Atmel. Since the example is in C you can port it pretty much directly with
some name changes.

I've done it the other way round when moving a project from a Silabs part to
an Atmel (my own code I hasten to add NOT the Silabs code).

Michael Kellett

www.mkesc.co.uk
 
E

Eeyore

Jan 1, 1970
0
MK said:
There is a good example on the Silabs website for their 8051 based
processors which seem to use exactly the same TWI/I2C interface hardware as
Atmel. Since the example is in C you can port it pretty much directly with
some name changes.

I've done it the other way round when moving a project from a Silabs part to
an Atmel (my own code I hasten to add NOT the Silabs code).

Interesting. I must have missed out on this TWI thingy ! What is it ?

Graham
 
M

martin griffith

Jan 1, 1970
0
Interesting. I must have missed out on this TWI thingy ! What is it ?

Graham
its the dreaded firrips I2C protocol.

OT just been looking at the Grace design 802 remote mic amp, they use
I2C over a 1000 foot run, seems dumb to me


martin
 
S

Spehro Pefhany

Jan 1, 1970
0
Interesting. I must have missed out on this TWI thingy ! What is it ?

Graham

I'd guess "two wire interface" (prolly I2C by another name to get
around Philips licencing/licensing issues).


Best regards,
Spehro Pefhany
 
J

John Woodgate

Jan 1, 1970
0
dated Wed said:
its the dreaded firrips I2C protocol.

That dates you! That 'firrips' ad was run in 1978, I think.
 
M

martin griffith

Jan 1, 1970
0
That dates you! That 'firrips' ad was run in 1978, I think.


Advertising works, Havent bought anything with a philips label since


martin
 
P

pidy

Jan 1, 1970
0
There is a good example on the Silabs website for their 8051 based
processors which seem to use exactly the same TWI/I2C interface hardwar as
Atmel. Since the example is in C you can port it pretty much directl with
some name changes.

I've done it the other way round when moving a project from a Silabs par to
an Atmel (my own code I hasten to add NOT the Silabs code).

Michael Kellett

www.mkesc.co.uk

thank u Michael... but i couldnt find the example u was talking about..
couldnt find no example with TWI/I2C interface...can u give me link??

thanks a lot

Daniele
 
E

Eeyore

Jan 1, 1970
0
martin said:
its the dreaded firrips I2C protocol.

Oh ! No problem there then. I have my own I2C routines ( not for master slave
admittedly but I dare say that could be fixed ).

OT just been looking at the Grace design 802 remote mic amp, they use
I2C over a 1000 foot run, seems dumb to me

If it works ! One of the nice things about I2C is that you can run it at ony old
speed. Doesn't mind being interrupted either.

Graham
 
E

Eeyore

Jan 1, 1970
0
pidy said:
thank u Michael... but i couldnt find the example u was talking about...
couldnt find no example with TWI/I2C interface...can u give me link??

What *exactly* is it you want ?

Graham
 
P

pidy

Jan 1, 1970
0
What *exactly* is it you want ?

Graham


Hello Graham..

I should need the code for the master device and the code for the slav
device... but in my case the master and the slave are 2 AVR-AT90CAN128.

I have those 2 devices and want realize a comunications between them usin
TWI.

Thanks a lot

Daniele
 
E

Eeyore

Jan 1, 1970
0
pidy said:
Hello Graham..

I should need the code for the master device and the code for the slave
device... but in my case the master and the slave are 2 AVR-AT90CAN128.

I have those 2 devices and want realize a comunications between them using
TWI.

Why does it need to be I2C then ?

Don't they have serial ports ?

Graham
 
M

MK

Jan 1, 1970
0
pidy said:
thank u Michael... but i couldnt find the example u was talking about...
couldnt find no example with TWI/I2C interface...can u give me link??

thanks a lot

Daniele

Hello,

The Silabs website is www.silabs.com

You will need to register and then search the tech library for SMB (they
call I2C SMBus). You'll get a few hits but you need to look at the app note.

Michael Kellett
 
P

pidy

Jan 1, 1970
0
Why does it need to be I2C then ?

Don't they have serial ports ?

Graham


sure, they have serial ports but i need to use TWI
 
S

SioL

Jan 1, 1970
0
pidy said:
sure, they have serial ports but i need to use TWI

Will the master always be the master and the slave always the slave?

If so, code is available either from atmel website or a number of other
sites, just google. It may not be for exactly your model of AVR, but
adapting the code should be easy enough.

SioL
 
P

pidy

Jan 1, 1970
0
ok I'll post some code... look if it could be ok


Master.c

#include <avr/io.h>
#include <compat/twi.h>
#include <inttypes.h>


int TWI_Master_Transmitter(uint8_t data, uint8_t address);
void TWI_init(void);
void TWI_Master_Transmitter_Error(void);


int main(void)
{
DDRA=0xFF;
PORTA=0xFF;
TWI_init();
int a;

a=TWI_Master_Transmitter(0xAA,0xE0);
if(a!=0)
PORTA=a;

}


void TWI_init(void)
{
#define SCL_CLOCK 100000L
#define F_CPU 8000000UL

/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */

TWSR = 0; /* no prescaler */
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
TWCR=(0<<TWINT|1<<TWEA|1<<TWEN);
}



int TWI_Master_Transmitter(uint8_t data,uint8_t address)
{
uint8_t twst;
PORTA=0xFF;
TWCR=((1<<TWINT)|(1<<TWSTA)|(1<<TWEN)); //Start condition

while(!(TWCR&(1<<TWINT))); // TWINT 1

twst=TWSR;
twst&=0xF8;

//Check TWSR START=0x08 REP_START=0x10
(twst!=TW_START)&&(twst!=TW_REP_START))

{
TWI_Master_Transmitter_Error();
return 1;
}

PORTA=0xFE;

TWDR=address; //Load SLA + Write (Write=0)
TWCR=((1<<TWINT)|(1<<TWEN)); //Send address
PORTA=0xFD;
while(!(TWCR&(1<<TWINT))); //TWINT1


PORTA=0xFC;


twst=TWSR;
twst&=0xF8; // Prescaler

if (twst != TW_MT_SLA_ACK) // check TWSR SLA_ACK=0x18
{
TWI_Master_Transmitter_Error();
return 2;
}

TWDR=data; //data in TWDR
TWCR=((1<<TWINT)|(1<<TWEN)); //Send data

PORTA=0x8B;


while(!(TWCR&(1<<TWINT))); // TWINT 1

PORTA=0xFA;

twst=TWSR;
twst&=0xF8; //Prescaler

if(twst!=TW_MT_DATA_ACK) //check TWSR MT_DATA:ACK 0x28
{
TWI_Master_Transmitter_Error();
return 3;
}

PORTA=0xF9;

TWCR=((1<<TWINT)|(1<<TWEN)|(1<<TWSTO)); //Stop condition

PORTA=0xF8;

return 0;
}

void TWI_Master_Transmitter_Error(void)
{

TWCR=((1<<TWINT)|(1<<TWEN)|(1<<TWSTO)); //TWI - Stop



TWCR=(0<<TWEN); //TWI-Aus

PORTA=0xF7;
}







SLAVE.c


#include <stdlib.h>
#include <stdio.h>
#include <util/twi.h>
#include <inttypes.h>
#include <avr/io.h>


unsigned char s_addr, rdata;
unsigned char tdata;
const char CDATA[]={0x01,0x80,0x02,0x40,0x04,0x20,0x08,0x10};


void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
//PORTC = 0b1001111;
//DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0xFF;
}

//TWI initialize
// bit rate:1
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x01; //set bit rate
TWSR= 0x00; //set prescale
TWAR= 0xE0; //set slave address //0x01
TWCR= 0x04; //enable twi
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
//CLI(); //disable all interrupts
port_init();
twi_init();

}

void ERROR(unsigned char edata)
{
PORTD= ~edata;
while(1);
}

unsigned char SlaveReceive(void)
{
TWCR = (1 << TWINT) | (1 << TWEN);
// TWAMR = 0x00;
TWAR = ((s_addr <<1) | 0);
TWCR = (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));

if ((TWSR & 0xF8) != 0x60)
ERROR(3);
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));

if ((TWSR & 0xF8) != 0x080)
ERROR(4);
rdata = TWDR;
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));

if ((TWSR & 0xF8) != 0x0A0)
ERROR(5);
TWCR = (1 << TWINT) | (1 << TWEN);
return rdata;
}

void SlaveTransmit(void)
{
TWCR = (1 << TWINT) | (1 << TWEN);
// TWAMR = 0xFE;
TWAR = ((s_addr <<1) | 0);
TWCR = (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));

if ((TWSR & 0xF8) != 0xA8)
ERROR(0x30);
TWDR = tdata;
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));

if ((TWSR & 0xF8) != 0x0C0)
ERROR(0x40);
}


void main(void)
{
init_devices();
s_addr = 0xE0; //0x50
PORTD =0xAA;

while(1)
{
rdata = SlaveReceive();
tdata = CDATA[rdata];
SlaveTransmit();
PORTD = ~rdata;
}
}
 
Top