-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slave onRequest
send more than one byte
#12
Comments
I can't believe I know this one. The You would do the following, adding as many void onRequestHandler()
{
int8_t responseByte1 = 123;
int8_t responseByte2 = 112;
TinyWire.send(responseByte1);
TinyWire.send(responseByte2);
}
void setup() {
TinyWire.begin(I2C_SLAVE_ADDRESS);
TinyWire.onRequest(onRequestHandler);
}
void loop() {
} |
@tatobari I tried the exact same thing a while back, and unfortunately it is unreliable. In my experience it worked on the first 2 bytes, but not the third, and then on the fourth, but not the fifth or sixth. If somebody really wants I can link to the code that I was using to get this to happen, but it is hard to test as it was communicating with a flow sensor and sending the information it got over i2c to another device. |
@RyanLoringCooper I think sharing the code might be a good idea. The In other words, the callback must be as simple and small as possible. |
Hopefully it helps. I am willing to answer questions. // SCL on physical pin 9 (PA4)
// SDA on physical pin 7 (PA6)
// sensor on physical pin 3 (PB1)
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "TinyWire.h"
// this is for physical pin 3, PB1, pin 8 if using pinMode(2) or other convience functions
#define SENSOR_PIN PB1
#define mAddr 0x0A
#define STRING_LENGTH 5
volatile int blipsSeen = 0;
long lastTime = 0;
float flow = 0;
volatile char flowString[STRING_LENGTH];
ISR(INT0_vect) {
blipsSeen++;
}
void i2cISR() {
TinyWire.send(flowString[0]);
TinyWire.send(flowString[1]);
TinyWire.send(flowString[2]);
TinyWire.send(flowString[3]);
TinyWire.send(flowString[4]);
TinyWire.send(flowString[5]);
}
void setupFlowSensor() {
blipsSeen = 0;
DDRB &= ~(1<<SENSOR_PIN); // enables SENSOR_PIN to be an input
// PUEB |= 1<<SENSOR_PIN; // enables a 10kOhm pullup resistor on SENSOR_PIN
pinMode(8, INPUT_PULLUP);
PCMSK1 |= 1<<SENSOR_PIN; // enables SENSOR_PIN to be a pin listening for interrupts
GIMSK |= 1<<INT0; // enables INT0 interrupt
MCUCR |= 1<<ISC01 | 1<<ISC00; // enables only rising edge to trigger INT0
SREG |= 1<<7; // enables interrupts
}
void estimateFlow() {
// if 1000ms have passed or the millis counter rolled over
if(millis()-lastTime > 1000 || millis() < lastTime) {
// rightshift to divide by two because isr is triggered on a change, but we want rising
int blips = blipsSeen;
flow = blips/4.8;
dtostrf(flow, 4, 2, flowString);
//sprintf(flowString, "%d", blips);
blipsSeen = 0;
lastTime = millis();
}
}
void setup() {
setupFlowSensor();
TinyWire.begin(mAddr);
TinyWire.onRequest(i2cISR);
}
void loop() {
estimateFlow();
} |
@RyanLoringCooper Would you mind enclosing your code with the following syntax?
(I've added spaces between each of the three french accents to prevent the parser from converting them into a block code.) |
I guess it is worth mentioning that to solve the problem that I mentioned I turned // This should be called STRING_LENGTH number of times in quick succession.
void i2cISR() {
TinyWire.send(flowString[i2cIndex++]);
if(i2cIndex == STRING_LENGTH)
i2cIndex = 0;
} The ATTiny84 that this code was running on was communicating with a Raspberry Pi using the i2c_smbus commands. And thanks for telling me about the c++ syntax highlighting. |
@lucullusTheOnly will probably be able to help you with that. Your approach works for me but using only 3 bytes. |
@tatobari so you are seeing the issue I mentioned, or you only tested sending 3 bytes? Which approach are you referring to? There is a chance that my problem was caused by |
@RuslanasMobOn : tatobari's approach should be the correct way of sending more than one byte for a request. The @RyanLoringCooper : I don't really know, what causes your issue and I couldn't find a problem, when I reviewed the corresponding part of the code just now. Maybe this is some weird timing issue with the interrupt for the flow sensor. Can you test your code with the flow sensors interrupt disabled? If the multiple EDIT: |
@lucullusTheOnly I'll give it a try when I can. I'm not totally convinced that my problem was caused by your library. |
I've spent some time looking at this problem because I was unable to make ANY TinyWire work. |
Hi, is it possible to send more than one byte of data per one request?
My current code is:
The text was updated successfully, but these errors were encountered: