Split from: Reading 12-bit temperature from sensor

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hallo,

i am doing small project by using onewire temperatur Sensor in Atmelstudio. How can  i see Temperatur  at Atmelstudio 7 without using Temperatur reading Monitor . May be i must to add any Library.

 

Best Regards

Nrc

Last Edited: Wed. Jan 8, 2020 - 10:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nrc wrote:
i am doing small project by using onewire temperatur Sensor

First Welcome to AVRFreaks!

 

Is this your first project? What experience do you have with AVR's?

Dallas One Wire temp sensors do not make for an easy first project using bare metal mpu's.

Have you worked with Arduino, shields and lib's?

 

Tell us more about your hardware setup, or post a picture of your setup.

 

Happy holidays.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hallo,

yes this is my fist project. i have connected attiny85 to dallas sensor. Actually i have no experience with AVR,s.  I need to read temp by using like UART at Atmel studio or suggest me easier way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why have you posted this in a year-old thread about an entirely different sensor, and a different processor?

 

* Split *

 

was originally here:  https://www.avrfreaks.net/forum/reading-12-bit-temperature-sensor

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jan 9, 2020 - 12:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well try out this Arduino sketch to verify your hardware is wired ok and working.


#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,4); // RX, TX

#define TEMP_PIN  2 //See Note 1, sheepdogguides..ar3ne1tt.htm

void OneWireReset(int Pin);//See Note 2
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);

void setup() {

    mySerial.begin(4800);
    mySerial.println("Hello, world?");
    digitalWrite(TEMP_PIN, LOW);
    pinMode(TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)

//9600 to match the data rate being used by the
//serial monitor on my system, which is set to
//the Arduino default. (Sample code published
//by nuelectronics used a faster baud rate.)
    delay(100);
    mySerial.print("temperature measurement:\n");
}

void loop(){
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0xbe);

  LowByte = OneWireInByte(TEMP_PIN);
  HighByte = OneWireInByte(TEMP_PIN);
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;


  if (SignBit) // If its negative
  {
     mySerial.print("-");
  }
  mySerial.print(Whole);
  mySerial.print(".");
  if (Fract < 10)
  {
     mySerial.print("0");
  }

  mySerial.println(Fract);

 //     mySerial.print("\n");
  delay(5000);      // 5 second delay.  Adjust as necessary
}

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
     digitalWrite(Pin, LOW);
     pinMode(Pin, OUTPUT); // bring low for 500 us
     delayMicroseconds(500);
     pinMode(Pin, INPUT);
     delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
   byte n;

   for(n=8; n!=0; n--)
   {
      if ((d & 0x01) == 1)  // test least sig bit
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(5);
         pinMode(Pin, INPUT);
         delayMicroseconds(60);
      }
      else
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(60);
         pinMode(Pin, INPUT);
      }

      d=d>>1; // now the next bit is in the least sig bit position.
   }

}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
    byte d, n, b;

    d=0;//This critical line added 04 Oct 16
    //I hate to think how many derivatives of
    //this code exist elsewhere on my web pages
    //which have NOT HAD this. You may "get away"
    //with not setting d to zero here... but it
    //is A Very Bad Idea to trust to "hidden"
    //initializations!
    //The matter was brought to my attention by
    //a kind reader who was THINKING OF YOU!!!
    //If YOU spot an error, please write in, bring
    //it to my attention, to save the next person
    //grief.

    for (n=0; n<8; n++)
    {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        pinMode(Pin, INPUT);
        delayMicroseconds(5);
        b = digitalRead(Pin);
        delayMicroseconds(50);
        d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
    }
    return(d);
}

You can find just about any example code by googling "Ardunio and what ever device you have"!

That was how I found this.

Note: one wire (ie. dallas devices) usually need a pull up resistor on the data line in order to work.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nrc wrote:
i have no experience with AVR

Do you have any experience with any other microcontroller(s) ?

 

Do you have any experience with 'C' programming in general ?

 

As Jim says, this is not really suitable as a first project - there's a lot of basics that need to be in place before you're ready to be reading a 1-Wire sensor!

 

See: https://www.avrfreaks.net/commen...

 

And: https://www.avrfreaks.net/forum/just-general-question-embedded-c

 

See also   Tip #6 - in my signature, below (may not be visible on mobile)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...