Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
sinine
PostPosted: Apr 23, 2010 - 08:05 AM
Newbie


Joined: Apr 23, 2010
Posts: 4


Hi,
i am having a problem whit Atmega128 and ds1820 sensors.i am using AVR studio for programming. I have searched lots of materials for ds1820 sensors, but not founded right C code to modify.
I am using http://home.roboticlab.eu/en/hardware/homelab/controller Atmega128 board. I made a connection between RS232 (com port) and DS1820 sensors.
So i want to connect it to Atmega128 board and get the sensors reading the temperature and get it on LCD.

Anyone has some good ideas or sample codes please notice! Rolling Eyes
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Apr 23, 2010 - 08:37 AM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16544
Location: Wormshill, England

A DS18B20 is different to a DS18S20 chip. The DS1820 is obsolete (I think).

You can use any pin of your mega128 for the one-wire bus. You choose your physical pin and wire the chip according to the data sheet.
You tell the compiler / configure the library so that it knows this pin.

There are many programs. Google 'danni one wire AVR' or 'AVR DS18B20'

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
sinine
PostPosted: Apr 23, 2010 - 08:40 AM
Newbie


Joined: Apr 23, 2010
Posts: 4


david.prentice wrote:
A DS18B20 is different to a DS18S20 chip. The DS1820 is obsolete (I think).
David.


I know it is different, im not using "B"
 
 View user's profile Send private message  
Reply with quote Back to top
sherazi
PostPosted: Apr 25, 2010 - 01:59 PM
Wannabe


Joined: Mar 21, 2010
Posts: 83


u can get help from the tuxgraphics.com article..

although it is for webserver but u can extract ur code...

http://tuxgraphics.org/electronics/200906/home-climate-control.shtm is the artcicle

and the code files arelhttp://tuxgraphics.org/common/src2/article09061/

in the code files u can use the ds18x20.c leave the first block... thats for webserver... use the required code.... the article would also help[/quote]
 
 View user's profile Send private message  
Reply with quote Back to top
sherazi
PostPosted: Apr 25, 2010 - 02:04 PM
Wannabe


Joined: Mar 21, 2010
Posts: 83


u can get much help at the other similar post...

http://www.avrfreaks.net/index.php?name ... p;p=347550

hope it might have helped...
 
 View user's profile Send private message  
Reply with quote Back to top
sherazi
PostPosted: Apr 25, 2010 - 02:12 PM
Wannabe


Joined: Mar 21, 2010
Posts: 83


http://www.kmitl.ac.th/~kswichit%20/avrthermo/thermo.c

u can also get the avr lib for it...
http://www.sicklinger.com/en/microcontr ... -in-c.html
 
 View user's profile Send private message  
Reply with quote Back to top
sinine
PostPosted: May 04, 2010 - 07:17 AM
Newbie


Joined: Apr 23, 2010
Posts: 4


Thank you for the help! I tryed to do like this: http://www.sicklinger.com/en/microcontroller-avr/58-atmel-avr-atmega-ds18x20-library-in-c.html

I used only one sensor per ATMEGA Pin. So i connected first sensor to my Atmega128 PD3 (PORTD) and second one to PD5.
here is the datasheet of ds18s20: http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf
Here is the code, can someone check it out, some tips my be useful! I can get on my LCD only " 0,0 C " so something is wrong it wont measure temp Idea Question

Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include "pin.h"
#include "stdint.h"
#include "stdio.h"
#include <string.h>
#include "ds18x20lib.h"


// S T A R T  M A I N  P R O G R A M
int main (void)
{
// Init display
   lcd_init(LCD_DISP_ON);   
 
   // Clear screen
   lcd_clrscr();

//-----------------------------------------
// Allocate memory
//-----------------------------------------   
   unsigned char temp_po_str[8],temp_pm_str[8];//,temp_pu_str[8],temp_ke_str[8],temp_bo_str[8];
   float temp_po,temp_pm;//temp_pu,temp_ke,temp_bo;
//-----------------------------------------
// Init Stuff
//-----------------------------------------
   ds1820_init(DS1820_pin_po);              //Initialize DS1820
   ds1820_init(DS1820_pin_pm);              //Initialize DS1820
   
//-----------------------------------------
// Do only once
//-----------------------------------------      
   for(;;){
//-----------------------------------------
// Read temperature
//-----------------------------------------
       temp_po = ds1820_read_temp(DS1820_pin_po);//Get temperature from DS1820 puffer oben
      temp_pm = ds1820_read_temp(DS1820_pin_pm);//Get temperature from DS1820 puffer mitte
      

      sprintf(temp_po_str,"%.1f C",temp_po); //Convert temp. puffer oben to string
      lcd_gotoxy(1,0);
      lcd_puts(temp_po_str);
      
      sprintf(temp_pm_str,"%.1f C",temp_pm); //Convert temp. puffer mitte to string
         lcd_gotoxy(1,1);
      lcd_puts(temp_pm_str);      


   }
   
}
// E N D  M A I N  P R O G R A M


//----------------------------------------
// Reset DS18S20
//----------------------------------------
uint8_t ds1820_reset(uint8_t used_pin)
{
  uint8_t err=100;
  DS1820_DDR |= 1<<used_pin;                  // define as ouput
  DS1820_PORT &= ~(1<<used_pin);               //Pull low
  _delay_us(480);            ;               // 480 us
  DS1820_DDR &= ~(1<<used_pin);                  // define as input
  DS1820_PORT |= 1<<used_pin;                  //Pullup on
  _delay_us(66);                              // 66 us
  err = (DS1820_PIN & (1<<used_pin)) >> used_pin;   // no presence detect --> err=1 otherwise err=0
  _delay_us(240);                           // 240 us
  if( (DS1820_PIN & (1<<used_pin)) == 0 ){         // short circuit --> err=2
    err = 2;
   }
  return err;
}
//-----------------------------------------
// Write one bit to DS18S20
//-----------------------------------------
void ds1820_wr_bit(uint8_t wrbit,uint8_t used_pin)
{
   if (wrbit ==0)   {
      DS1820_DDR |= 1<<used_pin;               // define as ouput
        DS1820_PORT &= ~(1<<used_pin);            //Pull low
      _delay_us(60);
      DS1820_DDR &= ~(1<<used_pin);            // define as input
        DS1820_PORT |= 1<<used_pin;               //Pullup on
      _delay_us(4);
   }
   if (wrbit ==1)   {
      DS1820_DDR |= 1<<used_pin;               // define as ouput
        DS1820_PORT &= ~(1<<used_pin);            //Pull low
      _delay_us(10);
      DS1820_DDR &= ~(1<<used_pin);            // define as input
        DS1820_PORT |= 1<<used_pin;               //Pullup on
      _delay_us(54);
   }
}
//-----------------------------------------
// Read one bit from DS18S20
//-----------------------------------------
uint8_t ds1820_re_bit(uint8_t used_pin)
{
   uint8_t rebit;
   DS1820_DDR |= 1<<used_pin;                     // define as ouput
     DS1820_PORT &= ~(1<<used_pin);                  //Pull low
   _delay_us(1);
   DS1820_DDR &= ~(1<<used_pin);                  // define as input
   DS1820_PORT |= 1<<used_pin;                     //Pullup on
   _delay_us(10);
   rebit = (DS1820_PIN & (1<<used_pin)) >> used_pin;    //Read bit
   _delay_us(50);
   return rebit;                        
}
//-----------------------------------------
// Read 1 byte from DS18S20
//-----------------------------------------
uint8_t ds1820_re_byte(uint8_t used_pin)
{
   uint8_t rebit;
   uint8_t rebyte =0x00;
   
   uint8_t i;

   for (i=0;i<8;i++)
   {
      rebit=ds1820_re_bit(used_pin);
      //_delay_us(2);                           //be on the save side
      if (rebit==1){
         rebyte|=(1<<i);
      }
   }
   return(rebit);
}
//-----------------------------------------
// Write 1 byte to DS18S20
//-----------------------------------------
void ds1820_wr_byte(uint8_t wrbyte,uint8_t used_pin)
{
   uint8_t i;
   for (i=0; i<8; i++) // writes byte, one bit at a time
   {      
      ds1820_wr_bit((wrbyte & 0b00000001),used_pin);
      wrbyte = wrbyte >> 1;
   }
   _delay_us(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
float  ds1820_read_temp(uint8_t used_pin)   
{
   uint8_t error,i;
   uint16_t j=0;
    uint8_t scratchpad[9];
   float temp=0;
   scratchpad[0]=0;
   scratchpad[1]=0;
   scratchpad[2]=0;
   scratchpad[3]=0;
   scratchpad[4]=0;
   scratchpad[5]=0;
   scratchpad[6]=0;
   scratchpad[7]=0;
   scratchpad[8]=0;
   error=ds1820_reset(used_pin);                           //1. Reset
   if (error==0){
       ds1820_wr_byte(0xCC,used_pin);                       //2. skip ROM
       ds1820_wr_byte(0x44,used_pin);                       //3. ask for temperature conversion
       while (ds1820_re_byte(used_pin)==0xFF && j<1000){         //4. wait until conversion is finished
         _delay_us(1);
         j++;
      }                           
       error=ds1820_reset(used_pin);                        //5. Reset
       ds1820_wr_byte(0xCC,used_pin);                       //6. skip ROM
       ds1820_wr_byte(0xBE,used_pin);                       //7. Read entire scratchpad 9 bytes
   
       for (i=0; i<9; i++)                                //8. Get scratchpad byte by byte
       {
          scratchpad[i]=ds1820_re_byte(used_pin);                //9. read one DS18S20 byte
       }
   }
   if(scratchpad[1]==0x00 && scratchpad[7]!=0){               //Value pos.
      scratchpad[0]=scratchpad[0] >> 1;
      temp=(scratchpad[0]-0.25f+(((float)scratchpad[7]-(float)scratchpad[6])/(float)scratchpad[7]));
      temp = (floor(temp*10.0+0.5)/10);                     //Round value .x

   }
   if(scratchpad[1]!=0x00){                              //Value negative
      uint8_t tmp;
      tmp =scratchpad[0];                                 //Save Kommabit
      tmp= ~ tmp;
      tmp= tmp >> 1;
      temp = (-1)*(tmp+1);
      if ((scratchpad[0]&0b00000001)==1){
         temp=temp+0.5;
      }

   }

   return temp;
}
//-----------------------------------------
// Initialize DS18S20
//-----------------------------------------
void  ds1820_init(uint8_t used_pin)   
{
   uint8_t error;
   uint16_t i =0;
   error=ds1820_reset(used_pin);                           //1. Reset
   if (error==0){
       ds1820_wr_byte(0xCC,used_pin);                       //2. skip ROM
       ds1820_wr_byte(0x44,used_pin);                       //3. ask for temperature conversion
       while (ds1820_re_byte(used_pin)==0xFF && i<1000){         //4. wait until conversion is finished
         _delay_us(1);
         i++;
      }
      error=ds1820_reset(used_pin);                        //5. Reset
       ds1820_wr_byte(0xCC,used_pin);                       //6. skip ROM
       ds1820_wr_byte(0xBE,used_pin);                       //7. Read entire scratchpad 9 bytes
      }
   
      
}
 
 View user's profile Send private message  
Reply with quote Back to top
sinine
PostPosted: May 17, 2010 - 06:31 PM
Newbie


Joined: Apr 23, 2010
Posts: 4


Code updated, help still needed!
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: May 17, 2010 - 06:55 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

Why not put some LCD output routines (followed by a delay to allow you to see the result) within the ds1820_read_temp() starting by outputting the raw bytes read from the chip. I'm kind of guessing they might all be 0x00 suggesting that the ds1820_re and ds1820_wr primitives themselves are not working.

Don't expect to be able to debug something this complex by writing the entire thing then wondering why the display shows "0.0 C". You need to start at the lowest level primitives, prove those and only move on to debugging the code that relies on them when they are proven 100%

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Bingo600
PostPosted: May 17, 2010 - 07:34 PM
Raving lunatic


Joined: Apr 25, 2004
Posts: 3817
Location: Denmark

I still recommend this library
Web
http://www.siwawi.arubi.uni-kl.de/avr_p ... index.html

Code
http://www.siwawi.arubi.uni-kl.de/avr_p ... 091001.zip

/Bingo
 
 View user's profile Send private message  
Reply with quote Back to top
sherazi
PostPosted: Jul 12, 2010 - 10:15 AM
Wannabe


Joined: Mar 21, 2010
Posts: 83


the ds18x20 are one wire sensors, that means you can connect multiple sensors on a single pin...
 
 View user's profile Send private message  
Reply with quote Back to top
titan5451
PostPosted: Mar 22, 2012 - 06:41 PM
Newbie


Joined: Mar 22, 2012
Posts: 9


hey I have written code for interfacing ds18s20 with avr128.can anyone tell me what is the error and how can i rectify it.I want to display output on port B.I am using winavr.Please help at earliest.

Code:
#include <stdio.h>
#include <delay.h>
#include <avr/io.h>

#define SKIP_ROM   0xCC

#define CONVERT_T   0x44      // DS18S20 commands
#define READ      0xBE
#define WRITE      0x4E
#define EE_WRITE   0x48
#define READ_PS      0xB4

unsigned short w1_init(unsigned short);
unsigned short w1_read(unsigned short);
void w1_write( unsigned short, unsigned short);
void w1_writebyte(unsigned short, unsigned short);
unsigned short w1_readbyte(unsigned short);


void _delay_ms (double __ms);
void _delay_us (double __us);


   unsigned short w1_init(unsigned short pin)
   {

     unsigned short detect;
     DDRE  |= (1<<pin);     // pull bus low  (output logical 0)
     PORTE &= ~(1<<pin);
     _delay_us(480);
     DDRE  &= ~(1<<pin);    // Release bus (input, pull-up)
     PORTE |= (1<<pin);
     _delay_us(70);
     detect = !(w1_read(pin));       // if a 0 is detected, sensor is detected
     _delay_us(410);
     return detect;
   }   
/*************************************************************/
      
      
   unsigned short w1_read(unsigned short pin) {

  unsigned short result;
  DDRE  |= (1<<pin);
  PORTE &= ~(1<<pin);      // initiate communication
  _delay_us(6);
  DDRE  &= ~(1<<pin);
  PORTE |= (1<<pin);      // release the bus (pull-up)
  _delay_us(9);
  result = PINE & (1<<pin); //leftshift once
  _delay_us(55);
  return result;
}
/*************************************************************/
   
   
   void w1_write( unsigned short pin, unsigned short thebit) {
  if(thebit == 1) {
    DDRE    |= (1<<pin);
    PORTE  &= ~(1<<pin);    // initiate communication
    _delay_us(6);
    DDRE    &= ~(1<<pin);
    PORTE  |= (1<<pin);     // release the bus (pull-up)
    _delay_us(64);
  }
  else {
    DDRE    |= (1<<pin);
    PORTE  &= ~(1<<pin);    // initiate communication
    _delay_us(60);             // maintain pull down during 60us
    DDRE    &= ~(1<<pin);
    PORTE  |= (1<<pin);     // release the bus (pull-up)
    _delay_us(10);
  }
}

   
/*************************************************************/
void w1_writebyte(unsigned short pin, unsigned short command) {

  unsigned short temp,i;

  for(i=0;i<8;i++) {
    temp = (command & 0x01); // temp is equal to the value of the first bit of command LSB
    if(temp)             //if true
       w1_write(pin,1);    //write bit1
    else
      w1_write(pin,0);       //write bit0
    command = command >> 1; //right shift by 1 so next bit comes in LSB
  }
}
/*************************************************************/
unsigned short w1_readbyte(unsigned short pin) {

  unsigned short temp;
  int i;
  temp = 0x00;
  for(i=0;i<8;i++)
    temp |= ((w1_read(pin)) << i); //read bit by bit and keep shifting left to reach MSB so
                           //when i=7 MSB will have bit 7
  return temp;
}   

/********************************************************************/
int main(void)
{                        //referred from page 19 of DS18S20 data sheet
   unsigned short x;
   DDRE = 0x00;              // 1wire defined as input by default 
   PORTE = 0x01;
   DDRB = 0xFF;             //make output port
   w1_init(0);
   w1_writebyte(0,SKIP_ROM);
   w1_writebyte(0,CONVERT_T);
   _delay_ms (10);
   w1_init(0);
   w1_writebyte(0,SKIP_ROM);
   x=w1_readbyte(READ);
   PORTB=x;
   w1_writebyte(0,WRITE);
   w1_writebyte(0,EE_WRITE);      //copy to SPD
   PORTE=0x01;            //enable internal pull up for input
   _delay_ms (10);
   
}

$ Fixed code tags - JS $

Confused Confused Confused Confused
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Mar 22, 2012 - 07:36 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20626
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
what is the error
What error? What is happening and what do you expect? What happens after the last _delay_ms (10); statement?

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
titan5451
PostPosted: Mar 23, 2012 - 07:19 AM
Newbie


Joined: Mar 22, 2012
Posts: 9


js wrote:
Quote:
what is the error
What error? What is happening and what do you expect? What happens after the last _delay_ms (10); statement?


I have written according to the datasheet of ds18s20.My main objective is"to get the temperature from the sensor,convert it to integer value and store it in the micro controller and then display it on lcd" so what changes do I have to make in the code?
 
 View user's profile Send private message  
Reply with quote Back to top
titan5451
PostPosted: Mar 23, 2012 - 07:57 AM
Newbie


Joined: Mar 22, 2012
Posts: 9


sinine wrote:
Thank you for the help! I tryed to do like this: http://www.sicklinger.com/en/microcontroller-avr/58-atmel-avr-atmega-ds18x20-library-in-c.html

I used only one sensor per ATMEGA Pin. So i connected first sensor to my Atmega128 PD3 (PORTD) and second one to PD5.
here is the datasheet of ds18s20: http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf
Here is the code, can someone check it out, some tips my be useful! I can get on my LCD only " 0,0 C " so something is wrong it wont measure temp Idea Question

Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include "pin.h"
#include "stdint.h"
#include "stdio.h"
#include <string.h>
#include "ds18x20lib.h"


// S T A R T  M A I N  P R O G R A M
int main (void)
{
// Init display
   lcd_init(LCD_DISP_ON);   
 
   // Clear screen
   lcd_clrscr();

//-----------------------------------------
// Allocate memory
//-----------------------------------------   
   unsigned char temp_po_str[8],temp_pm_str[8];//,temp_pu_str[8],temp_ke_str[8],temp_bo_str[8];
   float temp_po,temp_pm;//temp_pu,temp_ke,temp_bo;
//-----------------------------------------
// Init Stuff
//-----------------------------------------
   ds1820_init(DS1820_pin_po);              //Initialize DS1820
   ds1820_init(DS1820_pin_pm);              //Initialize DS1820
   
//-----------------------------------------
// Do only once
//-----------------------------------------      
   for(;;){
//-----------------------------------------
// Read temperature
//-----------------------------------------
       temp_po = ds1820_read_temp(DS1820_pin_po);//Get temperature from DS1820 puffer oben
      temp_pm = ds1820_read_temp(DS1820_pin_pm);//Get temperature from DS1820 puffer mitte
      

      sprintf(temp_po_str,"%.1f C",temp_po); //Convert temp. puffer oben to string
      lcd_gotoxy(1,0);
      lcd_puts(temp_po_str);
      
      sprintf(temp_pm_str,"%.1f C",temp_pm); //Convert temp. puffer mitte to string
         lcd_gotoxy(1,1);
      lcd_puts(temp_pm_str);      


   }
   
}
// E N D  M A I N  P R O G R A M


//----------------------------------------
// Reset DS18S20
//----------------------------------------
uint8_t ds1820_reset(uint8_t used_pin)
{
  uint8_t err=100;
  DS1820_DDR |= 1<<used_pin;                  // define as ouput
  DS1820_PORT &= ~(1<<used_pin);               //Pull low
  _delay_us(480);            ;               // 480 us
  DS1820_DDR &= ~(1<<used_pin);                  // define as input
  DS1820_PORT |= 1<<used_pin;                  //Pullup on
  _delay_us(66);                              // 66 us
  err = (DS1820_PIN & (1<<used_pin)) >> used_pin;   // no presence detect --> err=1 otherwise err=0
  _delay_us(240);                           // 240 us
  if( (DS1820_PIN & (1<<used_pin)) == 0 ){         // short circuit --> err=2
    err = 2;
   }
  return err;
}
//-----------------------------------------
// Write one bit to DS18S20
//-----------------------------------------
void ds1820_wr_bit(uint8_t wrbit,uint8_t used_pin)
{
   if (wrbit ==0)   {
      DS1820_DDR |= 1<<used_pin;               // define as ouput
        DS1820_PORT &= ~(1<<used_pin);            //Pull low
      _delay_us(60);
      DS1820_DDR &= ~(1<<used_pin);            // define as input
        DS1820_PORT |= 1<<used_pin;               //Pullup on
      _delay_us(4);
   }
   if (wrbit ==1)   {
      DS1820_DDR |= 1<<used_pin;               // define as ouput
        DS1820_PORT &= ~(1<<used_pin);            //Pull low
      _delay_us(10);
      DS1820_DDR &= ~(1<<used_pin);            // define as input
        DS1820_PORT |= 1<<used_pin;               //Pullup on
      _delay_us(54);
   }
}
//-----------------------------------------
// Read one bit from DS18S20
//-----------------------------------------
uint8_t ds1820_re_bit(uint8_t used_pin)
{
   uint8_t rebit;
   DS1820_DDR |= 1<<used_pin;                     // define as ouput
     DS1820_PORT &= ~(1<<used_pin);                  //Pull low
   _delay_us(1);
   DS1820_DDR &= ~(1<<used_pin);                  // define as input
   DS1820_PORT |= 1<<used_pin;                     //Pullup on
   _delay_us(10);
   rebit = (DS1820_PIN & (1<<used_pin)) >> used_pin;    //Read bit
   _delay_us(50);
   return rebit;                        
}
//-----------------------------------------
// Read 1 byte from DS18S20
//-----------------------------------------
uint8_t ds1820_re_byte(uint8_t used_pin)
{
   uint8_t rebit;
   uint8_t rebyte =0x00;
   
   uint8_t i;

   for (i=0;i<8;i++)
   {
      rebit=ds1820_re_bit(used_pin);
      //_delay_us(2);                           //be on the save side
      if (rebit==1){
         rebyte|=(1<<i);
      }
   }
   return(rebit);
}
//-----------------------------------------
// Write 1 byte to DS18S20
//-----------------------------------------
void ds1820_wr_byte(uint8_t wrbyte,uint8_t used_pin)
{
   uint8_t i;
   for (i=0; i<8; i++) // writes byte, one bit at a time
   {      
      ds1820_wr_bit((wrbyte & 0b00000001),used_pin);
      wrbyte = wrbyte >> 1;
   }
   _delay_us(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
float  ds1820_read_temp(uint8_t used_pin)   
{
   uint8_t error,i;
   uint16_t j=0;
    uint8_t scratchpad[9];
   float temp=0;
   scratchpad[0]=0;
   scratchpad[1]=0;
   scratchpad[2]=0;
   scratchpad[3]=0;
   scratchpad[4]=0;
   scratchpad[5]=0;
   scratchpad[6]=0;
   scratchpad[7]=0;
   scratchpad[8]=0;
   error=ds1820_reset(used_pin);                           //1. Reset
   if (error==0){
       ds1820_wr_byte(0xCC,used_pin);                       //2. skip ROM
       ds1820_wr_byte(0x44,used_pin);                       //3. ask for temperature conversion
       while (ds1820_re_byte(used_pin)==0xFF && j<1000){         //4. wait until conversion is finished
         _delay_us(1);
         j++;
      }                           
       error=ds1820_reset(used_pin);                        //5. Reset
       ds1820_wr_byte(0xCC,used_pin);                       //6. skip ROM
       ds1820_wr_byte(0xBE,used_pin);                       //7. Read entire scratchpad 9 bytes
   
       for (i=0; i<9; i++)                                //8. Get scratchpad byte by byte
       {
          scratchpad[i]=ds1820_re_byte(used_pin);                //9. read one DS18S20 byte
       }
   }
   if(scratchpad[1]==0x00 && scratchpad[7]!=0){               //Value pos.
      scratchpad[0]=scratchpad[0] >> 1;
      temp=(scratchpad[0]-0.25f+(((float)scratchpad[7]-(float)scratchpad[6])/(float)scratchpad[7]));
      temp = (floor(temp*10.0+0.5)/10);                     //Round value .x

   }
   if(scratchpad[1]!=0x00){                              //Value negative
      uint8_t tmp;
      tmp =scratchpad[0];                                 //Save Kommabit
      tmp= ~ tmp;
      tmp= tmp >> 1;
      temp = (-1)*(tmp+1);
      if ((scratchpad[0]&0b00000001)==1){
         temp=temp+0.5;
      }

   }

   return temp;
}
//-----------------------------------------
// Initialize DS18S20
//-----------------------------------------
void  ds1820_init(uint8_t used_pin)   
{
   uint8_t error;
   uint16_t i =0;
   error=ds1820_reset(used_pin);                           //1. Reset
   if (error==0){
       ds1820_wr_byte(0xCC,used_pin);                       //2. skip ROM
       ds1820_wr_byte(0x44,used_pin);                       //3. ask for temperature conversion
       while (ds1820_re_byte(used_pin)==0xFF && i<1000){         //4. wait until conversion is finished
         _delay_us(1);
         i++;
      }
      error=ds1820_reset(used_pin);                        //5. Reset
       ds1820_wr_byte(0xCC,used_pin);                       //6. skip ROM
       ds1820_wr_byte(0xBE,used_pin);                       //7. Read entire scratchpad 9 bytes
      }
   
      
}


Which complier are you using winavr or codevision?
 
 View user's profile Send private message  
Reply with quote Back to top
titan5451
PostPosted: Mar 23, 2012 - 09:50 AM
Newbie


Joined: Mar 22, 2012
Posts: 9


sinine wrote:
Thank you for the help! I tryed to do like this: http://www.sicklinger.com/en/microcontroller-avr/58-atmel-avr-atmega-ds18x20-library-in-c.html

I used only one sensor per ATMEGA Pin. So i connected first sensor to my Atmega128 PD3 (PORTD) and second one to PD5.
here is the datasheet of ds18s20: http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf
Here is the code, can someone check it out, some tips my be useful! I can get on my LCD only " 0,0 C " so something is wrong it wont measure temp Idea Question

Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include "pin.h"
#include "stdint.h"
#include "stdio.h"
#include <string.h>
#include "ds18x20lib.h"


// S T A R T  M A I N  P R O G R A M
int main (void)
{
// Init display
   lcd_init(LCD_DISP_ON);   
 
   // Clear screen
   lcd_clrscr();

//-----------------------------------------
// Allocate memory
//-----------------------------------------   
   unsigned char temp_po_str[8],temp_pm_str[8];//,temp_pu_str[8],temp_ke_str[8],temp_bo_str[8];
   float temp_po,temp_pm;//temp_pu,temp_ke,temp_bo;
//-----------------------------------------
// Init Stuff
//-----------------------------------------
   ds1820_init(DS1820_pin_po);              //Initialize DS1820
   ds1820_init(DS1820_pin_pm);              //Initialize DS1820
   
//-----------------------------------------
// Do only once
//-----------------------------------------      
   for(;;){
//-----------------------------------------
// Read temperature
//-----------------------------------------
       temp_po = ds1820_read_temp(DS1820_pin_po);//Get temperature from DS1820 puffer oben
      temp_pm = ds1820_read_temp(DS1820_pin_pm);//Get temperature from DS1820 puffer mitte
      

      sprintf(temp_po_str,"%.1f C",temp_po); //Convert temp. puffer oben to string
      lcd_gotoxy(1,0);
      lcd_puts(temp_po_str);
      
      sprintf(temp_pm_str,"%.1f C",temp_pm); //Convert temp. puffer mitte to string
         lcd_gotoxy(1,1);
      lcd_puts(temp_pm_str);      


   }
   
}
// E N D  M A I N  P R O G R A M


//----------------------------------------
// Reset DS18S20
//----------------------------------------
uint8_t ds1820_reset(uint8_t used_pin)
{
  uint8_t err=100;
  DS1820_DDR |= 1<<used_pin;                  // define as ouput
  DS1820_PORT &= ~(1<<used_pin);               //Pull low
  _delay_us(480);            ;               // 480 us
  DS1820_DDR &= ~(1<<used_pin);                  // define as input
  DS1820_PORT |= 1<<used_pin;                  //Pullup on
  _delay_us(66);                              // 66 us
  err = (DS1820_PIN & (1<<used_pin)) >> used_pin;   // no presence detect --> err=1 otherwise err=0
  _delay_us(240);                           // 240 us
  if( (DS1820_PIN & (1<<used_pin)) == 0 ){         // short circuit --> err=2
    err = 2;
   }
  return err;
}
//-----------------------------------------
// Write one bit to DS18S20
//-----------------------------------------
void ds1820_wr_bit(uint8_t wrbit,uint8_t used_pin)
{
   if (wrbit ==0)   {
      DS1820_DDR |= 1<<used_pin;               // define as ouput
        DS1820_PORT &= ~(1<<used_pin);            //Pull low
      _delay_us(60);
      DS1820_DDR &= ~(1<<used_pin);            // define as input
        DS1820_PORT |= 1<<used_pin;               //Pullup on
      _delay_us(4);
   }
   if (wrbit ==1)   {
      DS1820_DDR |= 1<<used_pin;               // define as ouput
        DS1820_PORT &= ~(1<<used_pin);            //Pull low
      _delay_us(10);
      DS1820_DDR &= ~(1<<used_pin);            // define as input
        DS1820_PORT |= 1<<used_pin;               //Pullup on
      _delay_us(54);
   }
}
//-----------------------------------------
// Read one bit from DS18S20
//-----------------------------------------
uint8_t ds1820_re_bit(uint8_t used_pin)
{
   uint8_t rebit;
   DS1820_DDR |= 1<<used_pin;                     // define as ouput
     DS1820_PORT &= ~(1<<used_pin);                  //Pull low
   _delay_us(1);
   DS1820_DDR &= ~(1<<used_pin);                  // define as input
   DS1820_PORT |= 1<<used_pin;                     //Pullup on
   _delay_us(10);
   rebit = (DS1820_PIN & (1<<used_pin)) >> used_pin;    //Read bit
   _delay_us(50);
   return rebit;                        
}
//-----------------------------------------
// Read 1 byte from DS18S20
//-----------------------------------------
uint8_t ds1820_re_byte(uint8_t used_pin)
{
   uint8_t rebit;
   uint8_t rebyte =0x00;
   
   uint8_t i;

   for (i=0;i<8;i++)
   {
      rebit=ds1820_re_bit(used_pin);
      //_delay_us(2);                           //be on the save side
      if (rebit==1){
         rebyte|=(1<<i);
      }
   }
   return(rebit);
}
//-----------------------------------------
// Write 1 byte to DS18S20
//-----------------------------------------
void ds1820_wr_byte(uint8_t wrbyte,uint8_t used_pin)
{
   uint8_t i;
   for (i=0; i<8; i++) // writes byte, one bit at a time
   {      
      ds1820_wr_bit((wrbyte & 0b00000001),used_pin);
      wrbyte = wrbyte >> 1;
   }
   _delay_us(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
float  ds1820_read_temp(uint8_t used_pin)   
{
   uint8_t error,i;
   uint16_t j=0;
    uint8_t scratchpad[9];
   float temp=0;
   scratchpad[0]=0;
   scratchpad[1]=0;
   scratchpad[2]=0;
   scratchpad[3]=0;
   scratchpad[4]=0;
   scratchpad[5]=0;
   scratchpad[6]=0;
   scratchpad[7]=0;
   scratchpad[8]=0;
   error=ds1820_reset(used_pin);                           //1. Reset
   if (error==0){
       ds1820_wr_byte(0xCC,used_pin);                       //2. skip ROM
       ds1820_wr_byte(0x44,used_pin);                       //3. ask for temperature conversion
       while (ds1820_re_byte(used_pin)==0xFF && j<1000){         //4. wait until conversion is finished
         _delay_us(1);
         j++;
      }                           
       error=ds1820_reset(used_pin);                        //5. Reset
       ds1820_wr_byte(0xCC,used_pin);                       //6. skip ROM
       ds1820_wr_byte(0xBE,used_pin);                       //7. Read entire scratchpad 9 bytes
   
       for (i=0; i<9; i++)                                //8. Get scratchpad byte by byte
       {
          scratchpad[i]=ds1820_re_byte(used_pin);                //9. read one DS18S20 byte
       }
   }
   if(scratchpad[1]==0x00 && scratchpad[7]!=0){               //Value pos.
      scratchpad[0]=scratchpad[0] >> 1;
      temp=(scratchpad[0]-0.25f+(((float)scratchpad[7]-(float)scratchpad[6])/(float)scratchpad[7]));
      temp = (floor(temp*10.0+0.5)/10);                     //Round value .x

   }
   if(scratchpad[1]!=0x00){                              //Value negative
      uint8_t tmp;
      tmp =scratchpad[0];                                 //Save Kommabit
      tmp= ~ tmp;
      tmp= tmp >> 1;
      temp = (-1)*(tmp+1);
      if ((scratchpad[0]&0b00000001)==1){
         temp=temp+0.5;
      }

   }

   return temp;
}
//-----------------------------------------
// Initialize DS18S20
//-----------------------------------------
void  ds1820_init(uint8_t used_pin)   
{
   uint8_t error;
   uint16_t i =0;
   error=ds1820_reset(used_pin);                           //1. Reset
   if (error==0){
       ds1820_wr_byte(0xCC,used_pin);                       //2. skip ROM
       ds1820_wr_byte(0x44,used_pin);                       //3. ask for temperature conversion
       while (ds1820_re_byte(used_pin)==0xFF && i<1000){         //4. wait until conversion is finished
         _delay_us(1);
         i++;
      }
      error=ds1820_reset(used_pin);                        //5. Reset
       ds1820_wr_byte(0xCC,used_pin);                       //6. skip ROM
       ds1820_wr_byte(0xBE,used_pin);                       //7. Read entire scratchpad 9 bytes
      }
   
      
}




temp = (floor(temp*10.0+0.5)/10);

what is floor..?what does this do?

and i am getting this warning



main.c:86: warning: implicit declaration of function 'floor'
main.c:86: warning: incompatible implicit declaration of built-in function 'floor'
main.c: In function 'main':
main.c:227: warning: pointer targets in passing argument 1 of 'sprintf' differ in signedness
main.c:227: warning: format '%.1f' expects type 'double', but argument 3 has type 'float'

for

temp = (floor(temp*10.0+0.5)/10); //Round value .x

and
sprintf(temp_po_str,"%.1f C",temp_po); //Convert temp. puffer oben to string
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 23, 2012 - 10:08 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

floor:

http://www.nongnu.org/avr-libc/user-man ... d89bd4cb85

which means you need:
Code:
#include <math.h>

at the top of your code and to be sure you link with libm.a

and PLEASE don't quote entire posts just to add a few lines in future!

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
titan5451
PostPosted: Mar 26, 2012 - 05:20 PM
Newbie


Joined: Mar 22, 2012
Posts: 9


clawson wrote:
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 26, 2012 - 05:26 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

The entire main() code of that program does:
Code:
int main (void)
{
//-----------------------------------------
// Allocate memory
//-----------------------------------------   
   unsigned char temp_x_str[8];
   float temp_x;
//-----------------------------------------
// Initialization Stuff
//-----------------------------------------
   w1_init(DS1820_pin);              //Initialize DS1820 Buffer oben
//-----------------------------------------
// Do only once
//-----------------------------------------      
   for(;;){
//-----------------------------------------
// Read temperature
//-----------------------------------------
       temp_x = ds1820_read_temp(DS1820_pin);//Get temperature from DS1820 puffer oben
      sprintf(temp_x_str,"%.1f C",temp_x); //Convert temp. puffer oben to string
   }
   
}

Exactly which line in that do you think will write anything on the LCD?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
titan5451
PostPosted: Mar 26, 2012 - 05:36 PM
Newbie


Joined: Mar 22, 2012
Posts: 9


clawson wrote:
The entire main() code of that program does:
[code]int main (void)
{...


sorry...my mistake.but the temperature does not come.
 
 View user's profile Send private message  
Reply with quote Back to top
vinzrule
PostPosted: Dec 28, 2012 - 06:09 AM
Newbie


Joined: Dec 28, 2012
Posts: 2


clawson wrote:
The entire main() code of that program does:
Code:
int main (void)
{
//-----------------------------------------
// Allocate memory
//-----------------------------------------   
   unsigned char temp_x_str[8];
   float temp_x;
//-----------------------------------------
// Initialization Stuff
//-----------------------------------------
   w1_init(DS1820_pin);              //Initialize DS1820 Buffer oben
//-----------------------------------------
// Do only once
//-----------------------------------------      
   for(;;){
//-----------------------------------------
// Read temperature
//-----------------------------------------
       temp_x = ds1820_read_temp(DS1820_pin);//Get temperature from DS1820 puffer oben
      sprintf(temp_x_str,"%.1f C",temp_x); //Convert temp. puffer oben to string
   }
   
}

Exactly which line in that do you think will write anything on the LCD?
 
 View user's profile Send private message  
Reply with quote Back to top
vinzrule
PostPosted: Dec 28, 2012 - 06:09 AM
Newbie


Joined: Dec 28, 2012
Posts: 2


tried running main program. an error-- ds18x20lib.h coming tell me what to do
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits