INTERRUPT FOR EVERY 100MILLI SECONDS

I am using arduino mega 2560.

I need some data transmission have to happen for every 100ms.

I have found that, the function can work in that, and found is error.

void loop()
{
attachInterrupt(data_transfer,1000);

}

Can anyone help me in this ?

Can anyone help me in this ?

Certainly

But, its not calling the function mentioned. :frowning:

My guess is it isn't even compiling, so any discussion about functions being called or not are completely off the table.

@britto, you need to post your complete program.

If you only want something to happen at 100 millisec intervals it is hard to see why an interrupt would be necessary. Just use millis - something like this

if (millis - lastTriggerMillis >= 100) {
    lastTriggerMillis += 100;
    // other stuff
}

...R

That is exactly correct. I had used millis() in the first stage of this code.

The part I am working on is, I2C communication. It should transmit and receive data for every 100ms.

I cant use millis() in every part of the code to compare and run the function. Is that makes sense?

I2C communication is a part of code, main code is water level control.

I want to send the data to HMI via I2C for evey 100ms, So I have thought of using interrupts. Is it possible to use millis(0 still in this concept ?

Is that makes sense?

No.

I cant use millis() in every part of the code to compare and run the function.

Why not ?

This is simple try out of the main code. I don’t know which library to include to compile this code. Can you help me

#include <Wire.h>
char send_data[8] = {‘A’,‘R’,‘D’,‘U’,‘U’,‘I’,‘N’,‘O’};
char receive_data[8] = {‘1’,‘1’,‘1’,‘1’,‘1’,‘1’,‘1’,‘1’};
unsigned long time_value = 0;

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
// set up Timer 1
TCCR1A = 0; // normal operation
TCCR1B = bit(WGM12) | bit(CS10); // CTC, no pre-scaling
OCR1A = 999; // compare A register value (1000 * clock speed)
TIMSK1 = bit (OCIE1A); // interrupt on Compare A Match
Serial.begin(9600);
}

ISR(TIMER1_COMPA_vect)
{
Wire.beginTransmission(8); // transmit to device #8
Serial.println(“Data sent”);
for(int i=0; i<8; i++)
{
Wire.write(send_data*); // sends five bytes*
Serial.print(send_data*);
_
}_
_
Serial.println(" ");_
_
Wire.endTransmission(); // stop transmitting*_

* Wire.requestFrom(8, 8);*
* while(Wire.available() > 1)*
* {*
* Serial.println(“Data received”);*
* for(int j =0; j<8; j++)*
* {*
* receive_data[j] = Wire.read();
Serial.print(receive_data[j]);
_
}_
_
Serial.println("");_
_
}_
_
}*_

Please, use code tags (the 1st button above edit window) like others.

Don't try to use it interrupt-driven functions in interrupt context, unless you really know what you're doing.

In other words, move all Wire. and Serial. commands out of the ISR. :slight_smile:

Wire/I2C communication doesn't work in interrupt, because it uses interrupts

Juraj:
Wire/I2C communication doesn't work in interrupt, because it uses interrupts

Psst - see reply #10 (Neither does Serial)

britto:
This is simple try out of the main code.

Why have you ignored the question in Replies #7 which was also implicit in Reply #6 and my Reply #4

Using millis() will be a great deal easier.

…R

Hi Arduino ppl,

In this code, I have tried using I2C protocol with timer interrupt. The Transmission form the master(Arduino) is successful. The issue comes when,

Wire.endTransmission(); // stop transmitting

Wire.requestFrom(8, 8);

in these lines. The receiver code is standard, That receives and transmits when the master asks for it. Kindly check and say your thoughts.

This is the code

#include <Wire.h> //included i2c library
#include <TimerOne.h> //included timer library for interrupt

#define LED0 13 //Led definition for changing state at every time the interrupt occurs
#define TIMER_US 1000000 // 100mS set timer duration in microseconds

char send_data[8] = {‘F’,‘F’,‘F’,‘F’,‘F’,‘F’,‘F’,‘F’}; //data to be sent
char receive_data[8] = {‘1’,‘1’,‘1’,‘1’,‘1’,‘1’,‘1’,‘1’}; //buffer character array to store the incoming characters

void setup()
{
pinMode(LED0, OUTPUT); // Set the pins as outputs
Timer1.initialize(TIMER_US); // Initialise timer 1
Wire.begin();
Serial.begin(9600);
}

void loop()
{
Timer1.attachInterrupt( timerIsr ); // interrupt occurs for every time the timer gets 100ms
}

void timerIsr()
{
digitalWrite( LED0, digitalRead( LED0 ) ^ 1 ); // Toggle LED 0
message_transfer(); //calling i2c message transfer function.
}

void message_transfer()
{
Wire.beginTransmission(8); // transmit to device #8
Serial.println(“Arduino to HMI”);
for(int i=0; i<8; i++)
{
Wire.write(send_data*); *
Serial.print(send_data*);
_
}_
_
Serial.println(" ");_
_
// Wire.endTransmission(); // stop transmitting*_

// Wire.requestFrom(8, 8); //master asks slave to send data
* while(Wire.available() > 1)*
* {*
* Serial.println(“HMI to Arduino”);*
* for(int j =0; j<8; j++)*
* {*
* receive_data[j] = Wire.read();
Serial.print(receive_data[j]);
_
}_
_
Serial.println("");_
_
} _
_
}*_
Please say your suggestions soon…

Now can you see why we ask you to post code in code tags?

The Wire library uses interrupts. Interrupts are disabled in your function which is called by your ISR.

Then how can I implement the same function using Arduino ? I cant use the millis() function as I have limitations. What will be the other way to reach it ?

when I include these 2 lines in the main code, the interrupt occurrence stops.

  1. Wire.endTransmission();

  2. Wire.requestFrom(8, 8);

what might be the problem?..