I2C
A biblioteca Wire usada para aplicações I2C não é compatível com o ATtiny85. Para a função de I2C podemos usar a biblioteca TinyWireM, disponibilizada pela Adafruit. Você pode baixá-la no github ou instalar através do gerenciador de bibliotecas no Arduino:
Instalação da Biblioteca
Os pinos de I2C na Franzininho são: P0(SDA) e P2(SCL), conforme pinout:

Exemplo

Vamos fazer a leitura do sensor de temperatura DS1620:
1
/* ATtiny85 as an I2C Master Ex1 BroHogan 1/21/11
2
* I2C master reading DS1621 temperature sensor. (display with leds)
3
* SETUP:
4
* ATtiny Pin 1 = (RESET) N/U ATtiny Pin 2 = (D3) LED3
5
* ATtiny Pin 3 = (D4) to LED1 ATtiny Pin 4 = GND
6
* ATtiny Pin 5 = SDA on DS1621 ATtiny Pin 6 = (D1) to LED2
7
* ATtiny Pin 7 = SCK on DS1621 ATtiny Pin 8 = VCC (2.7-5.5V)
8
* NOTE! - It's very important to use pullups on the SDA & SCL lines!
9
* DS1621 wired per data sheet. This ex assumes A0-A2 are set LOW for an addeess of 0x48
10
* TinyWireM USAGE & CREDITS: - see TinyWireM.h
11
* NOTES:
12
* The ATtiny85 + DS1621 draws 1.7mA @5V when leds are not on and not reading temp.
13
* Using sleep mode, they draw .2 @5V @ idle - see http://brownsofa.org/blog/archives/261
14
*/
15
16
#include <TinyWireM.h> // I2C Master lib for ATTinys which use USI
17
18
#define DS1621_ADDR 0x48 // 7 bit I2C address for DS1621 temperature sensor
19
#define LED1_PIN 4 // ATtiny Pin 3
20
#define LED2_PIN 1 // ATtiny Pin 6
21
#define LED3_PIN 3 // ATtiny Pin 2
22
23
int tempC = 0; // holds temp in C
24
int tempF = 0; // holds temp in F
25
26
27
void setup(){
28
pinMode(LED1_PIN,OUTPUT);
29
pinMode(LED2_PIN,OUTPUT);
30
pinMode(LED3_PIN,OUTPUT);
31
Blink(LED1_PIN,2); // show it's alive
32
TinyWireM.begin(); // initialize I2C lib
33
Init_Temp(); // Setup DS1621
34
delay (3000);
35
}
36
37
38
void loop(){
39
Get_Temp();
40
Blink(LED1_PIN,tempC/10); // blink 10's of temperature on LED 1
41
delay (1000);
42
Blink(LED2_PIN,tempC%10); // blink 1's of temperature on LED 2
43
delay (4000); // wait a few sec before next reading
44
}
45
46
47
void Init_Temp(){ // Setup the DS1621 for one-shot mode
48
TinyWireM.beginTransmission(DS1621_ADDR);
49
TinyWireM.send(0xAC); // Access Command Register
50
TinyWireM.send(B00000001); // Using one-shot mode for battery savings
51
//TinyWireM.send(B00000000); // if setting continious mode for fast reads
52
TinyWireM.endTransmission(); // Send to the slave
53
}
54
55
56
void Get_Temp(){ // Get the temperature from a DS1621
57
TinyWireM.beginTransmission(DS1621_ADDR);
58
TinyWireM.send(0xEE); // if one-shot, start conversions now
59
TinyWireM.endTransmission(); // Send 1 byte to the slave
60
delay(750); // if one-shot, must wait ~750 ms for conversion
61
TinyWireM.beginTransmission(DS1621_ADDR);
62
TinyWireM.send(0xAA); // read temperature (for either mode)
63
TinyWireM.endTransmission(); // Send 1 byte to the slave
64
TinyWireM.requestFrom(DS1621_ADDR,1); // Request 1 byte from slave
65
tempC = TinyWireM.receive(); // get the temperature
66
tempF = tempC * 9 / 5 + 32; // convert to Fahrenheit
67
}
68
69
70
void Blink(byte led, byte times){ // poor man's GUI
71
for (byte i=0; i< times; i++){
72
digitalWrite(led,HIGH);
73
delay (400);
74
digitalWrite(led,LOW);
75
delay (175);
76
}
77
}
Copied!
Fonte: TinyWireM
Last modified 2yr ago
Copy link
Contents
Exemplo