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

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

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


From: gouy yann
Subject: Re: [avr-gcc-list] Using TWI for communication between two ATMega128
Date: Mon, 14 Feb 2005 08:26:14 +0100 (CET)

Hi Martin,

Have you tried to poll the TWSR on the slave side and/or the TWIINT in TWCR to
check whether it recognizes its own address?

You call also enable the TWGCE in TWAR to react on every address as a first
debugging step.

I assume you've double check the electrical connection.

courage!

    Yann

 --- Martin Bammer <address@hidden> a écrit : 
> 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
> 
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list
>  


        

        
                
Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos mails ! 
Créez votre Yahoo! Mail sur http://fr.mail.yahoo.com/

reply via email to

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