Using Interrupts


hi all,

i'm new arduino (and reasonably new "proper" programming well!) , i've run issue attachinterrupt i'm sure purely down lack of knowledge!

i have dallas ds18s20 , trying put device performs action based on when temperature changes.

the issue have run need have constant polling of other devices occurring @ same time, thought attachinterrupt work

the ds18s20 wired in usual way (4.7k pull-up resistor, pin 2 on sensor -> pin2 on arduino (or seeeduino in case)) , current code follows:

code: [select]

#include <onewire.h>
onewire ds(2);

void setup(void){
serial.begin(9600);
attachinterrupt(0,readtemp, change);
}

void readtemp(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }

  if (onewire::crc8(addr, 7) != addr[7]) {
    serial.println("crc not valid!");
    return;
  }

  // first rom byte indicates chip
  switch (addr[0]) {
  case 0x10:
    //      serial.println("  chip = ds18s20");  // or old ds1820
    type_s = 1;
    break;
  case 0x28:
    //    serial.println("  chip = ds18b20");
    type_s = 0;
    break;
  case 0x22:
    //  serial.println("  chip = ds1822");
    type_s = 0;
    break;
  default:
    //serial.println("device not ds18x20 family device.");
    return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, parasite power on @ end

  delay(1000);     // maybe 750ms enough, maybe not
  // might ds.depower() here, reset take care of it.

  present = ds.reset();
  ds.select(addr);   
  ds.write(0xbe);         // read scratchpad

  ( = 0; < 9; i++) {           // need 9 bytes
    data[i] = ds.read();
  }
  // convert data actual temperature

  unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xfff0) + 12 - data[6];
    }
  }
  else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  serial.println(celsius);
}
void loop(void){
}


as can see, "dallas temperature" example comes onewire library, think i'm "doing wrong" when comes configuring interrupts.

any can provided (especially links further reading etc.) more welcome.

kind regards,

prof

the attachinterrupt() function registers event handler invoked when event occurs. need event occur cause event handler called.

if interrupt handler invoked, need fast. fast, not cause other interrupts ignored while handler dinking around taking it's time doing something.

code: [select]
    delay(250);
that's hardly fast, it. delay() function relies on interrupts (the clock ticking generates interrupt). interrupts disabled during function. so, absolutely can not use delay() in isr. , don't try around other time wasting technique.

code: [select]
    serial.println("crc not valid!");
this isn't fast, either.

code: [select]
  delay(1000);     // maybe 750ms enough, maybe not
you got part fast, right?

code: [select]
  serial.println(celsius);
not in isr...

is ds18s20 generating interrupt?


Arduino Forum > Using Arduino > Programming Questions > Using Interrupts


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial