avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Using TWI for communication between two ATMega128


From: Martin Bammer
Subject: [avr-gcc-list] Using TWI for communication between two ATMega128
Date: Sun, 13 Feb 2005 18:39:54 +0100
User-agent: KMail/1.7.2

Hi!

I'm trying to get TWI working on ATMega128. I've connected two ATMega128 and 
pulled up the SCL and SDA signals with 4k7.
Master mode seems to work. Sending of data is no problem. But I can't get 
slave mode working.
In my project normally all two ATMega should be slave. Each ATMega can switch 
to master mode and send data to the other controller. They use different 
bitrates to indicate bus access errors. For reception in slave mode an 
interrupt routine is installed and enabled.
My problem is that reception doesn't work at all. The interrupt routine is 
only called when the controller is in master mode and sends data to the bus.
What could be the problem??
Code that I use is:

Init:
    TWAR = (unit_nr + 1) << 1;
    TWBR = unit_nr
    TWCR = BV(TWEN) | BV(TWIE) | BV(TWEA);

One controller hat unit_nr=1 the other has unit_nr=2.

Subroutines:

uint8_t i2c_start(uint8_t adr)
{
    uint8_t d = 0x10;

    /* Send START condition */
    TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
    while (!(TWCR & BV(TWINT)) && d) d--;
    if ((TWSR & TW_STATUS_MASK) != TW_START) return 1;
    /* Send ADDRESS */
    TWDR = adr << 1;    /* Bit 0=LOW: WRITE DATA */
    TWCR = BV(TWINT) | BV(TWEN);
    d = 0x20;
    while (!(TWCR & BV(TWINT)) && d) d--;
    if ((TWSR & TW_STATUS_MASK) != TW_MR_SLA_ACK) return 1;
    return 0;
}


uint8_t i2c_write(uint8_t data)
{
    uint8_t d = 0x20;

    TWDR = data;
    TWCR = BV(TWINT) | BV(TWEN) | BV(TWIE);
    while (!(TWCR & BV(TWINT)) && d) d--;
    if ((TWSR & TW_STATUS_MASK) != TW_MR_DATA_ACK) return 1;
    return 0;
}


void i2c_stop(void)
{
    uint8_t d = 0x10;

    /* Send STOP condition */
    TWCR = BV(TWINT) | BV(TWSTO) | BV(TWEN) | BV(TWIE);
    while ((TWCR & BV(TWSTO)) && d) d--;
}


Access example:

i2c_start(2);
i2c_write(0x12);
i2c_write(0x34);
i2c_stop();

The above code sends data but the slave doesn't receive anything.
Please help.

Cheers, Martin

reply via email to

[Prev in Thread] Current Thread [Next in Thread]