Go Down

Topic: INTERRUPT FOR EVERY 100MILLI SECONDS (Read 3745 times) previous topic - next topic

britto

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 ?

AWOL


britto

But, its not calling the function mentioned. :(

AWOL

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

Robin2

@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
Code: [Select]
if (millis - lastTriggerMillis >= 100) {
    lastTriggerMillis += 100;
    // other stuff
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

britto

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 ?

AWOL


UKHeliBob

Quote
I cant use millis() in every part of the code to compare and run the function.
Why not ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

britto

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("");
 }
}

Budvar10

Please, use code tags (the 1st button above edit window) like others.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

AWOL

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

Budvar10

In other words, move all Wire. and Serial. commands out of the ISR. :)
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Juraj

#12
Oct 11, 2017, 03:49 pm Last Edit: Oct 11, 2017, 03:50 pm by Juraj
Wire/I2C communication doesn't work in interrupt, because it uses interrupts

AWOL

Wire/I2C communication doesn't work in interrupt, because it uses interrupts
Psst - see reply #10 (Neither does Serial)

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up