Programming ATtiny412

Go To Last Post
20 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
stack gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 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

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

I am trying to program ATtiny416 which is embedded in ATtiny416 xplained nano board for I2C communication . One ATtiny416 is used as master and another ATtiny416 as slave.

In atmel start I have configured the following to use ATtiny416 as I2C master

1) I2C master

2) PB0 & PB1 as SCL & SDA

3) Clock frequency: 416kHz (20Mhz/48 prescaler division)

4) PB5: digital output which is connected to a test LED on Xnano board

5) Fast mode plus is enabled

6) Pull up resistor is connected between Vcc & PB0

 

The reference code with the above configuration is exported to atmel studio IDE. And the code is dumped to ATtiny416. 

When I checked the clock signal frequency using oscilloscope, it is highly fluctuating. I tried blinking LED, which is working fine.

 

Here I am attaching solution explorer also. Kindly requesting you to help.

Attachment(s): 

Kishore.G

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

gkishore wrote:
Kindly requesting you to help.
But you haven't said what's wrong?

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

I am not able to observe clock frequency 416kHz.

And, where can I observe printf statements in atmel studio?

Kishore.G

Last Edited: Tue. Aug 13, 2019 - 09:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gkishore wrote:
And, where can I observe printf statements in atmel studio?
You need to provide an output channel such as UART or LCD then you use FDEV_SETUP_STREAM to associate this with a filing stream with stdout and then printf()/puts()/etc will redirect to the output device. See the user manual:

 

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#details

 

BTW your .rar file above does not contain all your project files. It has only the single .atsln file so is of no use to explore your code.

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

clawson wrote:

gkishore wrote:
And, where can I observe printf statements in atmel studio?
You need to provide an output channel such as UART or LCD then you use FDEV_SETUP_STREAM to associate this with a filing stream with stdout and then printf()/puts()/etc will redirect to the output device. See the user manual:

 

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#details

 

BTW your .rar file above does not contain all your project files. It has only the single .atsln file so is of no use to explore your code.

 

Whole project is attached now

Attachment(s): 

Kishore.G

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

Clock issue is resolved. Now I am able to use i2c for data transfer.

New issue araised regarding serial communication.

 

I am using ATtiny416 Xnano board to program on board ATtiny416.

I have programmed i2c & USART in ATtiny416. I am using puTTy to 

observe the log of serial communication.

Configuration:

1) Tx & Rx pins of ATtiny416 are connected to Tx & Rx of USB to serial converter. 

And I tried vice versa connections also.

2) I have configured to puTTy to COM1 port and baud rate to 9600.

 

But I am not able to watch log on puTTy terminal window.

 

 

Kishore.G

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

gkishore wrote:
of USB to serial converter. 
USB-RS232 or USB-TTL ?