Pages: [1]   Go Down
Author Topic: mstimer2 + I2C bug ?  (Read 868 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am trying to use the library msTimer but I think there is a bug when usinf the "Wire.endTransmission" function of the wire.h library.

Wire.endTransmission freezes my application when it is used in an interrupt function.

I am using an Arduino Duemilanove / ATmega328

Best regards,

Florent

---------- here is my debug pg -----------

#include <Wire.h>
#include <MsTimer2.h>
int ID_pcf=0;
int Id_an=0;
int Id_in=0;
int tmps=0;

int mesure[8];  // tab des dernieres mesures
int id_an=0;    // num entrée ana à lire

  
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Start Calibration");
  MsTimer2::set(400, myinterupt); // 500ms period
  MsTimer2::start();
}
void loop() {
  Serial.print(ID_pcf);
  Serial.print("\t id an= ");
  Serial.print(id_an);
  Serial.print(" \tmesure= ");
Serial.println(tmps);
  }
 
void myinterupt(){
  MsTimer2::stop() ;
  if  (id_an<4) ID_pcf= B1001001;
    else ID_pcf=B1001011;
 
  int Id_in=id_an%4;
  Wire.beginTransmission(ID_pcf);
  Wire.send(Id_in);
  // Wire.endTransmission();
  //Wire.requestFrom(ID_pcf, smiley-cool;
  //tmps++=Wire.receive();
  // mesure[id_an]=tmps;
 
if  (id_an==7) { id_an= 0;tmps++;}
    else {id_an ++;tmps++;}
MsTimer2::start();  
}
Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In order for I2C to work, interrupts must be enabled. Interrupts are  enabled by default, but not so within your timer interrupt code. This may not be what you wanted to hear, but nevertheless this is by definition how interrupt handlers are designed on most (if not all) platforms.

The nature of your application suggests that you do not need interrupts at all. Rather you may consider the following:

Code:
void loop() {
  static word time_last = 0;

  if ((int)((word)millis()-time_last)>=500) {
    // code that needs to execute every 500 millisecond goes here
    time_last+=500;
  }
}

Logged

Pages: [1]   Go Up
Jump to: