I2C library for attiny84

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

Hello everyone.
Where can I find a library (.h and/or .c) to make work the attiny84 as a master i2c comunication?? I found one in atmel (AVR155) but there are many registers on the header file that are not defined and I can´t compile.

THANK YOU VERY MUCH FOR EVERYTHING.

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

Google Peter Fleury.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

The T84 doesn't have hardware TWI (I2C) so you have a choice of either a bit banged version or something which uses the USI.
ie AVR155 is written for a harware TWI which you don't have.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Just use the Fleury i2cmaster.S
It will work with any type of AVR

When you migrate your code to a more suitable AVR with TWI, you simply link with twimaster.c

David.

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

Thanks. I´ll look for the Fleury´s i2cmaster library....
JS--> How is that the T84 doesn´t have hardware, if it can use the pin PA4 as SCL, and PA6 as SDA??

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

The Tiny84 has USI which can implement I2C on the specific USI hardware pins.

To be honest, as a I2C Master, the bit-banged i2cmaster.S works just as well as the USI. And you can use any pins that you like. even the USI pins!

If you wanted an I2C Slave, the USI is your only option. Bit-banging is not practical for a Slave.

David.

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

There's also a TWI library on Atmel Spaces, but I don't know if it works for that device:
http://spaces.atmel.com/gf/proje...

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

Quote:
How is that the T84 doesn´t have hardware
That's the reason why
Quote:
there are many registers on the header file that are not defined and I can´t compile.
it doesn't have TWI hardware.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Here's the code I use on the ATtiny84. I started from Atmel's AVR310 Application Note (Using the USI module as a I2C master). All changes are noted in the code.

I had posted this code several years ago but here's the latest update.

EDIT: correction, I don't believe I've ever posted this code before. I was thinking of the code for AVR312 (Using the USI module as a I2C slave).

EDIT: Added usiTwiMasterConfig.h to the .zip file.

Don

Attachment(s): 

Last Edited: Thu. Aug 29, 2013 - 10:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Don, another one going into my bottomless pit of AVR things on my computer. :-)

I have used your hardware TWI C code a couple of times (the one for the DS1307, 8 years old now). This looks pretty much the same code but adapted for the USI.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Don, thanks for that driver; I found it a few weeks ago and it's been most helpful with my Tiny44A-based analog to digital converter board.

It should work - bot-thoughts.com

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

I had tried the Peter Fleury library and I get many mistakes when compile. At the error list there are only two fails... but repeated.

The fails are (on the i2cmaster.S file):
-Error 1 constant value required line 94
-Error 2 number must be positive and less than 32 line 94

And the lines afected are always the same commands:
94 cbi SDA_DDR,SDA ;release SDA
113 sbi SDA_DDR,SDA ;force SDA low
132 sbi SCL_DDR,SCL ;force SCL low

I am going to try the Don´s library (I hope it works)... but anybody knows why this mistakes?

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

I have a problem with Don´s file.... the #include line cannot be resolved because the Atmel Studio 6 doesn´t have this file. Does it have a solution??

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

It is always wise to download a fresh copy of the ZIP file and start from scratch.

There are HTML files that show the documentation.
There are also brief comments directly in the "i2cmaster.S" file :

; USAGE
;	These routines can be called from C, refere to file i2cmaster.h.
;       See example test_i2cmaster.c 
; 	Adapt the SCL and SDA port and pin definitions and eventually 
;	the delay routine to your target !
; 	Use 4.7k pull-up resistor on the SDA and SCL pin.

and you have to set your specific pins. e.g. to use PORTA.6 for SDA and PORTA.4 for SCL:

;***** Adapt these SCA and SCL port and pin definition to your target !!
;
#define SDA     	6		// SDA Port A, Pin 6   
#define SCL		4		// SCL Port A, Pin 4
#define SDA_PORT        PORTA           // SDA Port A
#define SCL_PORT        PORTA           // SCL Port A

If your Tiny84 is running at 4MHz, you don't need to do anything else.
For 8MHz, you would do:

	.func i2c_delay_T2	; delay 5.0 microsec with 8 Mhz crystal	
i2c_delay_T2:        
        rcall i2c_delay_20cycles
        rjmp  i2c_delay_20cycles
	.endfunc     ; total 40 cyles = 5.0 microsec with 8 Mhz crystal 

i2c_delay_20cycles:        ; 4 cycles
	rjmp 1f      ; 2   "
1:	rjmp 2f      ; 2   "
2:	rjmp 3f      ; 2   "
3:	rjmp 4f      ; 2   "
4:	rjmp 5f      ; 2   "
5: 	rjmp 6f      ; 2   "
6:	nop          ; 1   "
	ret          ; 3   "
	.endfunc     ; total 20 cyles 

There are many ways to 'adjust' the 5 microsec delay. I have just shown one method. In my humble opinion, it would be much simpler to use a scratch register.

e.g. 20MHz needs 100 cycles for 5us
e.g. 8MHz needs 40 cycles for 5us
e.g. 1MHz needs 5 cycles for 5us

David.

Edit. If you are a beginner, I think you will find the Fleury code easier to use.
Obviously Don's USI code will work equally well. However you will need a bit more experience to use it. e.g. copying the files to your project directory.

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

Ok!! David Thank you for the corrections.
I could erease the problems that I commented before, but I find with the same troubles that I had been finding me with all the libraries I used (or many of them).

The registers that the twimaster.c uses are not defined. When I try to compile the error list fills of errors like:
'TWSR' undeclared (first use in this function) twimaster.c 31
'TWBR' undeclared (first use in this function) twimaster.c 32
'TWCR' undeclared (first use in this function) twimaster.c 46
'TWINT' undeclared (first use in this function) twimaster.c 46

And many more on the same way. Wath can I do??? I am desesperate.

THANK YOU FOR EVERYTHING. (and I hope you understand my english... it isn´t
the best...)

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

As David said above, twimaster.c implements a hardware master -- the AtTiny84 doesn't have hardware TWI. You need to use i2cMaster.S which implements a software master. Remove twimaster.c from your compilation and try again.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

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

Quote:
When I try to compile the error list fills of errors like:
I really don't know know to explain this better, I have said several times that the T84 doesn't have a hardware TWI and that's the reason for the errors!

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

eloyucu wrote:
I have a problem with Don´s file.... the #include line cannot be resolved because the Atmel Studio 6 doesn´t have this file. Does it have a solution??
Oh, sorry, I forgot to include that file.

Don

Attachment(s): 

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

JS... I don´t want to listen you!! You say bad things, and makes me sad... (I´m joking). But eventhought the AT84 doesn´t have the specific hardware... a solution exists.
Thanks at every one. Peter Fleury library works, I remove twimaster.c from my compilation (eventhought I didn´t have a include) as mckeemj says... and work.
Don... thank you for the fast answer. I´ll download the usiTwiMasterCode for foreshight... or for other projects.
THANK YOU VERY MUCH.

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

I have put together a small project to test Don's USI driver with a NXP SAA1064 7 seg led driver chip and a T2313. I have an old board with 4 displays and it is usually my test board for TWI tests.

The SAA1064 driver code needed a small twig to accomodate the address as the 1st byte in the buffer and I fell off the chair when it just worked first go! :-) The program displays a couple of "text" lines and then counts from 1 to 9999 and back again.

Of course of not much use unless you have a SAA1064 chip wired up to displays but it may show how to use the driver code, I have only used the write bit of the function.

Attachment(s): 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Why should there be a mystery?

You have an I2C hardware chip.
You give it correct instructions.
It does exactly what you ask!

Of course, you knew the hardware was correct.
But you still ignore using the return value from usiTwiStartTransceiverWithData().

So if you had forgotten to insert the chip, omitted pull-up resistors, used the wrong Slave address, ...
You would have no idea what was wrong.

I can understand the adrenalin rush that you might get from night-driving without headlights (and a blindfold).
OTOH, it is not very impressive for road safety or even getting to your destination.

David.

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

But I never make mistakes.....

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I'm trying to use the Peter Fleury library with the Arduino IDE and an AT84. But once I include i2master.h, my code doesn't seem to run at all. It does compile though, also the ASM file. Has anybody ever attempted on getting this to work?

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

If you are using a real Arduino, it has a ATmega328 or ATmega2560. The class and methods will work fine.

I don't know what an AT84 is.

David.

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

Quote:

what an AT84 is

Well the thread title says "attiny84" so I presume it means that.

As the tiny48 has USI not TWI I assume the idea would be to use Fleury's "soft" implementation of TWI?

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

You can add CPP files to an Arduino project very easily.
I suspect that ASM or C functions will need a "C" prototype for them to link properly.

I find it difficult to understand "Arduino IDE" and ATtiny84.

Whereas it is possible to build the Arduino core for wierd MCUs like the ATtiny84, it is unlikely that all the third party libraries will be compatible.

What is wrong with simply using ATmega168/328 ?
A TQFP-32 is not much different in size to a SOIC-20.
The QFN-20 / MLF-28 are equally tiny. (to my eyesight)

David.

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

I'm using the arduino-tiny core to run Arduino code on a Tiny. The libary compiles fine but simply linking it in (not even calling i2c_init()) seems to halt any execution.

I've now switched over to avr studio, but that got me even further away from solving it. Cannot even get a LED switched on..but I'll open another topic for that if need be.

As for the reason for using attiny..it's a cost thing. At 100,000 units I could save over $100,000

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

With some of your $100000 you could invest in a book on C / C++.

Seriously, Arduino is "C++" and as such will expect all the other files to be C++.

Oh, if you intend to buy 100000 units of any chip, I doubt that you would be paying $100000.

And if you want to pay me $1000 I would happily port the library to run on the USI.

David.

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

I can't use USI because I need those pins for PWM. With Atmel Studio I could eventually get the library compiled in and the MCU still responding. Didn't get actual I2C to work, but that's another issue to solve.

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

Did you understand the "C" declaration?

Look at your Linker error. I suspect that you just need to add extern "C" declaration to the "i2cmaster.h"

David.

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

Sorry I haven't been fully clear on the problem. I never had any linker error, the program would compile, link and upload, but the the device would just grind to a halt with library included. It's an ASM lib by the way with just a header file that exports the calls..

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

So why not explain your problem? Please post a minimal program / sketch that exhibits the symptoms.

I might try linking "i2cmaster.S" with a regular UNO sketch. Then I could see for myself.

David.

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

Arduino:

#include 

#define LED_PIN 2

void setup() {
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, HIGH);
}

void loop() {
}

The led on pin 2 (PB2) will not come on. So basically just including the library will freeze the tiny84

Atmel Studio:

#include 
#include 

#define LED_PIN PB2

int main(void)
{
    PORTB |= (1 << LED_PIN);
    i2c_init();
    PORTB &= ~(1 << LED_PIN);
}

The led on pin 2 (PB2) will stay on. So i2c_init() will freeze the tiny84.

Event though it has hardware TWI, I'll test on a regular Arduino board aswell.

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

Quote:
So i2c_init() will freeze the tiny84.
That's pretty much correct if the code doesn't find a working I2C device on the bus and it doesn't have any timeout.

Of course you would want to put a

for (;;); 

at the end of the main, maybe the compiler does it for you but no point taking a chance.

Edit just checked and the init code MUST return as it is only

i2c_init:
	cbi SDA_DDR,SDA		;release SDA
	cbi SCL_DDR,SCL		;release SCL
	cbi SDA_OUT,SDA
	cbi SCL_OUT,SCL
	ret

The code is just a GCC adaptation of AVR300 ASM I2C which I have used in the past.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I'm not entirely sure what I changed but I get more logical behavior now. This problem coexisted with another issue I had with uploading code to the device. That has now disappeared and I'm just stuck with a non-responsive I2C device after calling i2c_start_wait. That needs some more investigation before crying for help again ;-). Thanks!

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

Solved. I had a number of things wrong:

- got the lib to work in Arduino by wrapping all declarations in extern "C". thanks david.prentice

- sent the wrong device address to my I2C device. figured that out by looking at the implementation of the Wire lib and comparing that to i2cmaster. Wire does the left shift + 0 or 1 for you, in i2cmaster you have to do that yourself..

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

Hej,

I found this thread because I'm trying to Run a I2C OLED Display from a Attiny84. I did a prototype on a Attiny85 works great. Now I need some extra Pins so i switched to the Attiny84. Everything compiles like before but the screen won't start, But the program runs.

Then i read here that the Attiny doesn't has I2C but on the Atmel page it is written that it has one... http://www.atmel.com/devices/ATT...

Now I'm confused...

The other question is: does anyone has successfully ran a I2C OLED Display from an ATtiny84?

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

flobo wrote:
 i read here that the Attiny doesn't has I2C 

Where, exactly, did you read that?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

in the third post in this thread

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

So did you read #6 ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes but that was to confusing for me... so then my question is why does the code doesn't work on a 84 but on a 85 what is the big difference between those besides the Number of Pins?

I already change the SDA/SCL in my library from PB0 and PB2 to PA6 and PA4 but that didn't helped.

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

flobo wrote:
 what is the big difference between those 

Have you studied the datasheets of the two parts?

 

Those are the definitive documents ... 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Post #9 seems to have code for your chip.  In post #18 he includes a file he left out in post #9.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

flobo wrote:

I already change the SDA/SCL in my library from PB0 and PB2 to PA6 and PA4 but that didn't helped.

 

So does this library use the USI or does it bit-bang the interface?

 

Show your code...

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Hey! I've been playing with an ATtiny and one of those SAA1064. The thing is that I managed to do what you did, using my Raspberry Pi. But now I am trying with the ATtiny84 and I can't make it work. I'm using some of your code. The code runs fine, but it seems like the communication between the ATtiny84 and the SAA1064 is not smooth... any suggestions?

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

zKai wrote:
seems like the communication between the ATtiny84 and the SAA1064 is not smooth

Not smooth in what way?  Please explain...

 

You do know this thread is 3 yrs old!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"