Programming ATtiny412

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

Hi everyone,

 

I want to program ATtiny 412 with i2c communication protocol. Can anybody please help me how to start with for i2c programming?

 

Thank you

Kishore.G

Last Edited: Tue. Jun 11, 2019 - 06:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hi gkishor,

 

Welcome to AVRfreaks,

 

have you tried to look for examples in ATMEL start ?. The attiny412 MUX table in the datasheet is somehow wrong and contains wrong information..this is the right table:

 

 

for a simple application you can start looking in this forum for some codes, you can find it easily by a simple search.

 

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

Hi sir,

 

Thanks for your response. I am trying to understand some example i2c codes. After some study, I have following doubts

 

1) Which register & bits [SCTRLA, SCTRLB, SDATA], I have to configure to Enable TWI, Enable ack generation

2) Which register & bits are to be checked for,  whether the master is requesting data or sending data to slave

3) How to use SDATA register to transmit multiple bytes of data to i2c master

4) How to configure PA3/PA6/PA7 to use them as GPIO pins

 

figure: TWI registers

 

I have read the datasheet, regarding configuration of the above registers for i2c slave operation. But I'm not getting proper information from datasheet.

Please help me.

 

Thank you sir,

Kishore.G

Last Edited: Tue. Jun 11, 2019 - 11:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

have you checked the forum for examples ?

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

First Wecome to AVRFreaks!

 

Can you start by telling us what your experience writing micro controller code is? 

If this is your first project, you are starting with a fairly complex project.   Tell us what your I2C slave will do other then communicating with an I2C master (and tell us what your I2C master is as well).

 

Jim

 

 

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

share.robinhood.com/jamesc3274

 

 

 

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

Try to start from here:

 

https://www.avrfreaks.net/forum/...

 

these application notes descibes most of the functions to get through in ATtiny 1 & 0 series (compatible with your attiny412)

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

gkishore wrote:
Can anybody please help me how to start with for i2c programming?
Couldn't find any app notes on unified memory AVR TWI; hopefully AVR libc's TWI will work.

avr-libc: Example using the two-wire interface (TWI)

 

"Dare to be naïve." - Buckminster Fuller

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

gchapman wrote:

hopefully AVR libc's TWI will work.

That would be QUITE a stretch. The tiny412 IO header has:

/* Two-Wire Interface */
typedef struct TWI_struct
{
    register8_t CTRLA;  /* Control A */
    register8_t reserved_0x01;
    register8_t DBGCTRL;  /* Debug Control Register */
    register8_t MCTRLA;  /* Master Control A */
    register8_t MCTRLB;  /* Master Control B */
    register8_t MSTATUS;  /* Master Status */
    register8_t MBAUD;  /* Master Baurd Rate Control */
    register8_t MADDR;  /* Master Address */
    register8_t MDATA;  /* Master Data */
    register8_t SCTRLA;  /* Slave Control A */
    register8_t SCTRLB;  /* Slave Control B */
    register8_t SSTATUS;  /* Slave Status */
    register8_t SADDR;  /* Slave Address */
    register8_t SDATA;  /* Slave Data */
    register8_t SADDRMASK;  /* Slave Address Mask */
    register8_t reserved_0x0F;
} TWI_t;

The avr-libc example code has:

C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\doc\avr-libc\examples\twitest>grep "TW.. " twitest.c
 * register to have valid contents while the TWINT bit in TWCR is set.
  /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
  TWSR = 0;
  TWBR = 10;                    /* smallest TWBR value, see note [5] */
  TWBR = (F_CPU / 100000UL - 16) / 2;
  TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = sla | TW_WRITE;
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = (eeaddr >> 8);         /* 16-bit word address device, send high 8 bits of addr */
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = eeaddr;                /* low 8 bits of addr */
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send (rep.) start condition */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = sla | TW_READ;
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
      TWCR = twcr;              /* clear int to start transmission */
      while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
  TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = sla | TW_WRITE;
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = (eeaddr>>8);           /* 16 bit word address device, send high 8 bits of addr */
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWDR = eeaddr;                /* low 8 bits of addr */
  TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
  while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
      TWDR = *buf++;
      TWCR = _BV(TWINT) | _BV(TWEN); /* start transmission */
      while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */

To quote Sesame Street "one of these things is not like the other one" ;-)

 

EDIT working on the assumption that in an AVR-0/AVR-1 there's got to be a strong chance that TWIn.MCTRLA is going to be accessed at some stage then a google for:

 

avr twi "MCTRLA"

 

seems quite fruitful (mainly back here at Freaks in fact)

Last Edited: Tue. Jun 11, 2019 - 03:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have checked some threads in the forum. Still I didn't get specific information

Kishore.G

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

Hi Sir,

 

I am new to microcontroller programming.

Functions of ATtiny I2C slave

  • To transmit some bytes of information to I2C master and processing the information sent by I2C master and acting according to it
  • Driving some data to some other I2C slave through GPIOs available with it

 

 

Kishore.G

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

This thread has useful information regarding bit checks for slave read/write

https://www.avrfreaks.net/forum/attiny416-acting-twii2c-slave-device-failure-second-read

 

But how to configure registers to use ATtiny pins as GPIOs?

 

Kishore.G

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

gkishore wrote:

This thread has useful information regarding bit checks for slave read/write

https://www.avrfreaks.net/forum/attiny416-acting-twii2c-slave-device-failure-second-read

 

But how to configure registers to use ATtiny pins as GPIOs?

 

 

What is your experience in programming ? maybe you should start with arduino better

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

ASF4?

ASF4 API Reference Manual - I2C Drivers

via START (Help tab)

 

No TWI in ASF3 for tinyAVR.

 

"Dare to be naïve." - Buckminster Fuller