Solved: Transmitting using an ATtiny85 configured as a slave I2C

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The following code is running on an ATtiny85 configured to transmit a byte as as a slave I2C. The problem is that it is always transmitting 1 no matter what is put into USIBR (and I also tried USIDR).

 

It appears that all the stages are executed as expected. START, SLA+R, data, NACK, STOP. But somehow it is just 1 that is transmitted.

 

#include <stdint.h>
#include <avr/interrupt.h>

#define SLAVE_ADDRESS 0x20

volatile uint8_t i2cstage = 0;
void reset_i2c()
{
    USICR = (1 << USISIE) | (1 << USIWM1) | (1 << USICS1);
    DDRB &= ~(1 << DDB0);
    i2cstage = 0;
}

ISR(USI_START_vect)
{
    USISR |= (1 << USISIF);
    USICR |= (1 << USIOIE);
    i2cstage = 1;
}

ISR(USI_OVF_vect)
{
    USISR |= (1 << USIOIF);
    uint8_t data = USIBR;
    switch(i2cstage)
    {
        case 1:
            if (data == ((SLAVE_ADDRESS << 1) + 1))
            {
                i2cstage = 2;
                USIBR = 5;
                DDRB |= 1 << DDB0;
                PORTB ^= 1 << PORTB1;
            }
            else
            {
                reset_i2c();
            }
            break;
        case 2:
            USISR = 0x0E;
            DDRB &= ~(1 << DDB0);
            i2cstage = 3;
            break;
        default:
            reset_i2c();
            break;
    }
}

int main ()
{
    sei();
    DDRB |= 1 << DDB1;
    reset_i2c();

    while(1)
    {
        if((USISR & (1 << USIPF)) == (1 << USIPF))
        {
            USISR = (1 << USIPF);
            reset_i2c();
        }
    }

    return 0;
}

 

Last Edited: Thu. Oct 13, 2016 - 12:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First off. What do you want your Slave to send?
.
e.g. send "David Prentice" in 14 bytes.
e.g. send a 16-bit Temperature in 2 bytes.
e.g. send a single 8-bit byte.
e.g. send a NAK for the Slave address just because you are busy (or in a bad mood.)
.
If you can't think of anything useful, emulate a real hardware chip like a 24C02 or DS1307.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to send a single 8-bit byte.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take a look at the app note AVR312 and sample code. 

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I already did, but I missed the fact that Atmel START is a web-based tool. Hopefully, there is example code there that will point out my error. Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LogLog wrote:

I am trying to send a single 8-bit byte.

Well,  if you do not say what particular value of byte,   I do not see how anyone can help.

 

You will find life easier if you make up your mind what you want to send.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Take a look at the app note AVR312 and sample code. 

 

 

 

Thanks again. The problem was that I did not initialize the SDA and SCL pins and the USISR register correctly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the problem is now "Solved"?

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

So the problem is now "Solved"?

 

 

Yes, it is.