Forum: Mikrocontroller und Digitale Elektronik I2C zwischen Attiny85s Probleme


von CptnRtrd (Gast)


Lesenswert?

Hallo Leute,

ich hoffe ihr könnt mir helfen. Ich habe noch nicht viel Erfahrung mit 
Microcontrollern und versuche mich gerade testweise am Aufbau einer I2C 
Kommunikation.

Setup:

-Arduino UNO as ISP
-2x Attiny85 (1x Master + 1x Slave)
-Arduino IDE
-TinyWireS und TinyWireM Bibliothek
-Verwendete Pull-Ups: 10k oder 2x10k Parallel(5k)

Problem:
Der Bus macht was er will und meine LEDs ändern ihre Zustände wie sie 
wollen alle paar Sekunden/Minuten. Wenn ich im Code vom Master keine 
Delays verwende funktioniert es, 1 LED blinken zu lassen. Aber alle 3 
LEDs mit gewissen Abständen abwewechselnd leuchten zu lassen geht nicht.

Master:
1
#include <TinyWireM.h>
2
//#include <USI_TWI_Master.h>
3
4
int num=3;
5
6
void setup() {
7
  // put your setup code here, to run once:
8
TinyWireM.begin();
9
//delay(3000);
10
}
11
12
void loop() {
13
  // put your main code here, to run repeatedly:
14
15
//TinyWireM.beginTransmission(0x1);
16
//TinyWireM.send(B00000001);
17
//TinyWireM.endTransmission();
18
LED3();
19
delay(500);
20
LEDoff();
21
delay(500);
22
LED4();
23
delay(500);
24
LEDoff();
25
delay(500);
26
LED5();
27
delay(500);
28
LEDoff();
29
30
31
}
32
33
34
void LED3(){
35
  TinyWireM.beginTransmission(0x1);
36
TinyWireM.send(B011);
37
TinyWireM.endTransmission();
38
}
39
void LED4(){
40
  TinyWireM.beginTransmission(0x1);
41
TinyWireM.send(B100);
42
TinyWireM.endTransmission();
43
}
44
void LED5(){
45
  TinyWireM.beginTransmission(0x1);
46
TinyWireM.send(B101);
47
TinyWireM.endTransmission();
48
}
49
void LEDoff(){
50
  TinyWireM.beginTransmission(0x1);
51
TinyWireM.send(B110);
52
TinyWireM.endTransmission();
53
}


Slave:
1
#include <TinyWireS.h>
2
3
4
5
#define I2C_SLAVE_ADDR 0x1
6
7
uint8_t master_data[16];
8
9
uint8_t master_bytes;
10
11
void receiveEvent(uint8_t num_bytes)
12
{
13
  uint8_t i;
14
  uint8_t master_data[16];
15
16
  master_bytes = num_bytes;
17
18
  for (i = 0; i < master_bytes; i++)
19
    master_data[i] = TinyWireS.receive();
20
}
21
22
23
int DREI = 1;
24
int VIER = 3;
25
int FUENF = 4;
26
byte msg = 0;
27
28
void setup() {
29
  // put your setup code here, to run once:
30
  pinMode(DREI, OUTPUT);
31
  pinMode(VIER, OUTPUT);
32
  pinMode(FUENF, OUTPUT);
33
  TinyWireS.begin(I2C_SLAVE_ADDR);
34
//  TinyWireS.onReceive(receiveEvent);
35
Blink(DREI,5);
36
}
37
38
void loop() {
39
   if (TinyWireS.available()) {
40
    msg = TinyWireS.receive();
41
    if (msg == 1){Blink(DREI,2);}
42
    if (msg == 3) {
43
      digitalWrite(DREI, HIGH);
44
    }
45
    if (msg == 4) {
46
      digitalWrite(VIER, HIGH);
47
    }
48
    if (msg == 5) {
49
      digitalWrite(FUENF, HIGH);
50
    }
51
    if (msg == 6) {
52
      digitalWrite(DREI, LOW);
53
      digitalWrite(VIER, LOW);
54
      digitalWrite(FUENF, LOW);
55
      }
56
  }
57
58
 // TinyWireS_stop_check();
59
60
}
61
void Blink(byte led, byte times){ // poor man's display
62
  for (byte i=0; i< times; i++){
63
    digitalWrite(led,HIGH);
64
    delay (250);
65
    digitalWrite(led,LOW);
66
    delay (175);
67
  }
68
}

Ich hoffe ihr könnt mir helfen...ich suche seit gestern im Internet, 
habe aber noch nichts passendes gefunden.

von Winfried M. (win)


Lesenswert?

CptnRtrd schrieb:
> TinyWireM.send(B011);

Muss das nciht 0b011 heißen?

https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html

von CptnRtrd (Gast)


Lesenswert?

Nein, das funktioniert auch so. Die richtigen LEDs werden ja 
angesteuert. Nur nicht mit den zeitlichen Abständen. Die LEDs schalten 
sich aber nicht nach meiner zeitl. Vorgabe ein, sondern unterschiedlich 
durcheinander mit Abständen von einigen Sekunden bis wenige Minuten.

von CptnRtrd (Gast)


Lesenswert?

Aber ich probiere es heute Nachmittag trotzdem nochmal aus.

von CptnRtrd (Gast)


Lesenswert?

funktioniert leider nicht :/

von CptnRtrd (Gast)


Lesenswert?

Habe das Problem gefunden...die TinyWireS Bibliothek läuft nur auf 8 Mhz 
und nicht auf 1 Mhz.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.