Go Down

Topic: 1 Wire DS18B20 Minimal Coding (Read 1 time) previous topic - next topic

transcendtient

#5
Jun 09, 2013, 03:17 am Last Edit: Jun 09, 2013, 08:19 am by transcendtient Reason: 1
http://www.adafruit.com/datasheets/DS18B20.pdf is a great resource...

I recoded the functions I need and I'm still trying to squeeze anything at all out I can. Here is what I'm using as of now.

Code: [Select]

float getTemp(){
  resetDS();
  writeByte(0x44); // start conversion, without parasite power on at the end
 
  resetDS();
  writeByte(0xBE); // Read Scratchpad
 
  return (readByte()|(readByte() << 8))*0.1125+32;//convert to deg f
}

void resetDS()
{
  pinMode(DS18S20_Pin, OUTPUT);
  digitalWrite(DS18S20_Pin, LOW);
  delayMicroseconds(255);//480u reqd
  pinMode(DS18S20_Pin, INPUT);
  delayMicroseconds(255);//480u reqd
  writeByte(0xCC);//ROM skip as only one sensor
}

void writeByte(byte data){
  pinMode(DS18S20_Pin, OUTPUT);
  for (byte mask = 00000001; mask>0; mask <<= 1){
    if (data & mask ){ // send 1
      digitalWrite(DS18S20_Pin,LOW);
      delayMicroseconds(10);//15u reqd
      digitalWrite(DS18S20_Pin,HIGH);
      delayMicroseconds(45);//60u total
    }
    else{ //if bitwise and resolves to false
      digitalWrite(DS18S20_Pin,LOW); // send 0
      delayMicroseconds(50);
      digitalWrite(DS18S20_Pin,HIGH);
      //delayMicroseconds(5);//60u total is slow enough to negate needing this
    }
  }
}

byte readByte(){
  byte r = 0;
  for (byte mask = 00000001; mask>0; mask <<= 1){
    pinMode(DS18S20_Pin, OUTPUT);
    digitalWrite(DS18S20_Pin, LOW);
    //delayMicroseconds(3);//1u reqd pinMode() is slow enough to negate needing this
    pinMode(DS18S20_Pin, INPUT); // let pin float, pull up will raise
    delayMicroseconds(10);//15u reqd
    if(digitalRead(DS18S20_Pin))r|=mask;
    delayMicroseconds(40);//60u complete cycle
  }
  return r;
}

aarondc

What is it compiling to, and how small does it need to be?
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

aarondc

Not a lot of help, however (compiled on a Mega, code untested, of course):

this saved 2 bytes:

Code: [Select]

//    if (digitalRead(DS18S20_Pin)) r |= mask;
r |= (mask & (digitalRead(DS18S20_Pin)));


This saved 8:

Code: [Select]
void writeByte(byte data){
  pinMode(DS18S20_Pin, OUTPUT);
  for (byte mask = 00000001; mask>0; mask <<= 1){
    digitalWrite(DS18S20_Pin,LOW);
    if (data & mask ){ // send 1
//    digitalWrite(DS18S20_Pin,LOW);
      delayMicroseconds(10);//15u reqd
      digitalWrite(DS18S20_Pin,HIGH);
      delayMicroseconds(45);//60u total
    }
    else{ //if bitwise and resolves to false
//    digitalWrite(DS18S20_Pin,LOW);
      delayMicroseconds(50);
      digitalWrite(DS18S20_Pin,HIGH);
      //delayMicroseconds(5);//60u total is slow enough to negate needing this
    }
  }
}


and I noticed in your first main sketch you had

Code: [Select]
//int DS18S20_Pin = 5; //DS18S20 thermometer pin
const int DS18S20_Pin = 5; //DS18S20 thermometer pin


which saved 24 bytes.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

aarondc

Another paltry 32 bytes (on the Mega 2560) but also catches the logic error if the the temperature == (78 || 72) ;)

Code: [Select]

void loop() {
  noInterrupts();
  float temperature = getTemp();//temperature data
  interrupts();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temperature,1);
  lcd.print("f"); 
 
  if(temperature > 78){//heat off
    setStage(0,     "Heating Off    ");
  else if(temperature < 72) {
    if (heatingStage!=q0+q2) //stage two heating
      setStage(q0+q2, "Stage 2 Heating ");
  }
  else if (heatingStage!=q0){//stage one heating
    setStage(q0, "Stage 1 Heating ");
  } 
}

void setStage(byte thisValue, char *thisMessage) {
    lcd._bitString=heatingStage=thisValue;
    lcd.spiSendOut();
    lcd.setCursor(0,1);
    lcd.print(thisMessage);
}
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

transcendtient

A paltry 32 bytes is the kind of thing I need. I've changed all my int's to unsigned ints... I'm tearing every library function I'm using to pieces...

I'm down to 5.2k, and I had dreams of putting this on an ATTiny45, but I'm afraid that's probably not possible.

I need to get another 1.2k to fit it, but I'll probably be switching to the ATTiny85.

Go Up