Katsumi_S:
Vermutlich hängt Deine LED an VCC und am Ausgang des Attiny. Deshalb ist die Logik invertiert. Oder nicht?
Gruß, Jürgen
Ja, stimmt, das ist der Grund. Bin nur durcheinander gekommen, weil es in der Interrupt-Funktion scheinbar anders ist, aber da kommt es mit der Invertierung auch hin.
Also das Impulse zählen funktioniert schon sehr gut. Jetzt bin ich gerade dran, die i2c Kommunikation zu programmieren und da komme ich gerade nicht so richtig weiter. Vorerst programmiere ich mal nur die i2c Kommunikation, um es übersichtlicher zu halten. Später kombiniere ich dann beide Programme.
Ich wollte die Sache erstmal grundsätzlich verstehen und um zu sehen, ob der Wert, der gesendet wird auch gespeichert wird, gebe ich den empfangenen Wert bei einem Request wieder zurück. Das hat sporadisch mal funkioniert, aber jetzt geht irgendwie garnichts mehr.
Der Master ist übrigens ein Raspberry Pi 3b+
Wenn ich beim RPi den Befehl "i2cdetect -y 1" in der Konsole eingebe, wird der ATtiny85 auch unter der Adresse 0x20 erkannt. 0x60 ist ein Digital-Analog-Wandler.
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Wenn ich dann den Befehl "i2cget -y 1 0x20" eingebe, kommt als Wert immer nur 0xff zurück.
Wenn ich z.B. "i2cset -y 1 0x20 0x64" eingebe und danach wieder "i2cget -y 1 0x20", kommt nach wie vor 0xff zurück.
Hier mal mein Code bis jetzt:
// ATMEL ATtiny45/85
//
// +-\/-+
// Reset 1| |8 Vcc
// Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1 SCL
// Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1
// GND 4| |5 PB0 (D 0) pwm0 SDA
// +----+
#include <TinyWireS.h> // Bezieht i2c Bibliothek ein
#define I2C_SLAVE_ADDRESS 0x20 // i2c Slave Adresse
#define LED PB1
volatile uint8_t counter = 100; // Encoder Zähler
void setup() {
TinyWireS.onRequest(requestEvent); // Deklaration der Funktion für eine i2c Anfrage
TinyWireS.onReceive(receiveEvent); // Deklaration der Funktion für einen i2c Empfang
TinyWireS.begin(I2C_SLAVE_ADDRESS); // Verbindet mit dem i2c Bus
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
}
void loop() {
TinyWireS_stop_check();
}
void requestEvent() {
TinyWireS.send(counter); // Sendet i2c Nachricht
}
void receiveEvent(uint8_t value) {
while (TinyWireS.available())
counter = TinyWireS.receive(); // Empfängt i2c Nachricht
}
Was mache ich hier falsch?
Übrigens habe ich auch noch einen sehr guten Code im Internet gefunden, der perfekt für meine Anforderung passen würde, aber auch hier kommt nur der Wert 0xff zurück...
/*
I2C library used:
https://github.com/rambo/TinyWire
For ATtiny44a/84a running at 20 MHz.
Available EEPROM: 256/512 bytes.
Flash: 4/8 kB.
RAM: 256/512 bytes.
Prepare for ATTINY816: 128 bytes EEPROM, 512 bytes RAM, 8kB flash.
ATTINY416: 128 bytes EEPROM, 256 bytes RAM, 4kB flash.
All data is expected to be transmitted as highByte first.
All data will be stored in EEPROM as highByte first.
*/
#include <TinyWireS.h>
#define I2C_DEFAULT 0x20 // The default used if no address found in EEPROM.
// The available I2C commands.
#define READ_EC 0x11 // Read the EC probe - produces 2 bytes of data (the cycle count).
#define CALIBRATE_EC 0x12 // Set calibration of EC probe - comes with 2 bytes of data (value, pos).
#define GET_CALIBRATION 0x13 // Get calibration of EC probe - comes with 1 byte of data (pos).
#define CLEAR_CALIBRATION 0x14 // Remove a calibration point - comes with 1 byte of data (pos).
#define SET_EC_OVERSAMPLING 0x15 // Set the oversampling factor (ranging from 0 = 1x to 10 = 1024x).
#define READ_NTC 0x20 // Read the NTC probe - produces 2 bytes of data (the ADC reading).
#define SET_NTC_OVERSAMPLING 0x21 // Set the oversampling factor (ranging from 0 = 1x to 10 = 1024x).
#define SET_ADDRESS 0x30 // Set the I2C address of this device - comes with 1 byte of data (the address).
// Other I2C paramenters.
#define MAX_TRANSMISSION 16 // No more than this many bytes in a single I2C transmission for this application, ever.
volatile uint8_t request[MAX_TRANSMISSION]; // Stores the incoming I2C data.
volatile uint8_t response[MAX_TRANSMISSION]; // Stores the outgoing I2C data.
volatile uint8_t availableBytes; // The number of valid data bytes available.
// Advance declaration of callback functions.
void wireRequest(void);
void wireReceive(void);
uint8_t address;
void setup() {
// Set up I2C interface and callback functions.
address = I2C_DEFAULT;
TinyWireS.begin(address);
TinyWireS.onRequest(wireRequest);
TinyWireS.onReceive(wireReceive);
}
bool handleCommand = false;
void loop() {
if (handleCommand) {
handleCommand = false;
availableBytes = 1; // Dealing with a new command: reset the response data.
response[0] = 6; // Set response status to "in progress".
switch (request[0]) {
case READ_EC:
response[0] = 6;
{
uint32_t startMillis = millis();
while (millis() - startMillis < 1000) {
TinyWireS_stop_check();
}
}
response[0] = 1;
response[1] = 10;
response[2] = 100;
response[3] = 10;
response[4] = 1;
availableBytes = 5;
break;
case CALIBRATE_EC:
response[0] = 12;
break;
case GET_CALIBRATION:
for (uint8_t i = 0; i < 10; i++) {
response[i] = i * 11 + 1;
}
availableBytes = 10;
break;
case CLEAR_CALIBRATION:
response[0] = 14;
availableBytes = 1;
break;
case SET_EC_OVERSAMPLING:
response[0] = 15;
availableBytes = 1;
break;
case READ_NTC:
response[0] = 2;
response[1] = 20;
response[2] = 200;
availableBytes = 3;
break;
case SET_NTC_OVERSAMPLING:
response[0] = 5;
if (request[1] > 0 && request[1] <= 10) {
response[0] = 0;
}
availableBytes = 1;
break;
case SET_ADDRESS:
response[0] = 99;
availableBytes = 1;
}
}
// This needs to be here for the TinyWireS lib
TinyWireS_stop_check();
}
void wireReceive(uint8_t n) {
// Make sure we don't try to read more bytes than that fit in our data structure.
if (n > MAX_TRANSMISSION) {
n = MAX_TRANSMISSION;
}
for (uint8_t i = 0; i < n; i++) {
if (TinyWireS.available()) {
request[i] = TinyWireS.receive();
}
else {
break;
}
}
// Clear the buffer.
// There should not be more data in the buffer than the already received bytes, unless the master
// is misconfigured and sends more data than it should.
while (TinyWireS.available())
TinyWireS.receive();
handleCommand = true; // Tell loop() that there's a command to be taken care of.
}
void wireRequest() {
for (uint8_t i = 0; i < availableBytes; i++) {
TinyWireS.send(response[i]);
}
}