1 Wire DS18B20 Minimal Coding


i'm working on paring sketch space i'm placing on attiny45, , i've been deleting code left , right, replacing function calls hard coded data.

also... data byte array in example says needs 9 bytes, utilizes 2? chopped down 2 byte array , worked, don't know if i'm somehow wrong doing that.

one more thing... when first plug in sensor (doesn't happen when it's reset) junk reading of 185.0f first reading. not big problem, annoying.

i've come point i'm not sure can more. me chop!
here gettemp function:
code: [select]
float gettemp(){
  resetds();
  writebyte(0x44); // start conversion, without parasite power on @ end
 
  resetds();
  writebyte(0xbe); // read scratchpad
 
  return (readbyte()|(readbyte() << 8))*0.1125+32;//convert 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 1 sensor
}

void writebyte(byte data){
  pinmode(ds18s20_pin, output);
  (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 , resolves false
      digitalwrite(ds18s20_pin,low); // send 0
      delaymicroseconds(50);
      digitalwrite(ds18s20_pin,high);
      //delaymicroseconds(5);//60u total slow enough negate needing this
    }
  }
}

byte readbyte(){
  byte r = 0;
  (byte mask = 00000001; mask>0; mask <<= 1){
    pinmode(ds18s20_pin, output);
    digitalwrite(ds18s20_pin, low);
    //delaymicroseconds(3);//1u reqd pinmode() slow enough negate needing this
    pinmode(ds18s20_pin, input); // let pin float, pull raise
    delaymicroseconds(10);//15u reqd
    if(digitalread(ds18s20_pin))r|=mask;
    delaymicroseconds(40);//60u complete cycle
  }
  return r;
}


here rest of program...
code: [select]
#include <spi.h>
#include <myliquidcrystal.h>
int ds18s20_pin = 5; //ds18s20 thermometer pin*/

byte heatingstage=0;

const int q0 = 1;//first unused 74hc595 pin
const int q2 = 4;//second unused 74hc595 pin

liquidcrystal lcd(0);// initialize lcd number of ss/latch
     
void setup() { 
  lcd.begin(16, 2);// set lcd's number of columns , rows:
}
     
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 < 72 && heatingstage!=q0+q2){//stage 2 heating
    lcd._bitstring=heatingstage=q0+q2;
    lcd.spisendout();
    lcd.setcursor(0,1);
    lcd.print("stage 2 heating ");
  }
  if(temperature < 78 && temperature > 72 && heatingstage!=q0){//stage 1 heating
    lcd._bitstring=heatingstage=q0;
    lcd.spisendout();
    lcd.setcursor(0,1);
    lcd.print("stage 1 heating"); 
  } 
  if(temperature > 78){//heat off
    lcd._bitstring=heatingstage=0;
    lcd.spisendout();
    lcd.setcursor(0,1);
    lcd.print("heating off    ");
  }
}

if there 1 device on onewire bus can tell respond without requiring address. eliminate address array.


Arduino Forum > Using Arduino > Sensors > 1 Wire DS18B20 Minimal Coding


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62