CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

two DS18s20 (indoor and outdoor)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
linux123



Joined: 09 Oct 2006
Posts: 17

View user's profile Send private message

two DS18s20 (indoor and outdoor)
PostPosted: Mon Oct 16, 2006 4:58 pm     Reply with quote

Hy, i need check two temps, indoor and outdoor, i use DS1820, from Dallas, the protocol is 1-wire, whats read two sensor ?
My MCU is 18F452.
Thanks.
LazBoy



Joined: 17 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 5:35 am     Reply with quote

This is my solution. I know, not a optimal solution but it works.
I modified this code from http://www.vermontficks.org/pic.htm (Jon Fick)
This is working code but a litlle slow communication:

You may decide seperate 2 pin for Indoor and Outdoor sensors.
Include this two sensor files to your project:

Variables:
Code:

#define DS1820_SKIP_ROM 0xCC
#define DS1820_READ_SCRATCHPAD 0xBE
#define DS1820_CONVERT_T 0x44

#define INDOOR_PIN    PIN_A0
#define OUTDOOR_PIN    PIN_A1

static char cShiftBit,cDataOut;
static long iTemperature,iDataIn;

.
.
indoor.c
Code:

///////////// INDOOR ///////////////////////

void ResetDS1820_Indoor( void )
    {
    output_low ( INDOOR_PIN );
    delay_us ( 480 );         
    output_float ( INDOOR_PIN );
    delay_us ( 480 );
    }


void WriteDS1820_Indoor ( void )
    {
    for ( cShiftBit = 1; cShiftBit <= 8; ++cShiftBit )
        {
        output_low ( INDOOR_PIN );
        delay_us ( 5 );
        output_bit ( INDOOR_PIN, shift_right ( &cDataOut, 1, 0 ) );
        delay_us ( 60 );
        output_float ( INDOOR_PIN );
        delay_us ( 5 );
        }
    }



void ReadDS1820_Indoor ( void )
    {
    iDataIn = 0;
    for ( cShiftBit = 1; cShiftBit <= 16; ++cShiftBit )
       {
       output_low ( INDOOR_PIN );
       delay_us ( 5 );
       output_float ( INDOOR_PIN );
       delay_us ( 5 );
       shift_right ( &iDataIn, 2, input ( INDOOR_PIN ) );
       delay_us ( 55 );
       }
    }



void WaitForConversion_Indoor ( void )
    {
    while ( TRUE )
       {
       output_low ( INDOOR_PIN );
       delay_us ( 5 );
       output_float ( INDOOR_PIN );
       delay_us ( 5 );
       if ( input ( INDOOR_PIN ) == 1 )
           {
           break;
           }
       delay_us ( 55 );
       }
    }

.
.
.
.
outdoor.c
Code:

///////////// OUTDOOR ///////////////////////

void ResetDS1820_Outdoor( void )
    {
    output_low ( OUTDOOR_PIN );
    delay_us ( 480 );         
    output_float ( OUTDOOR_PIN );
    delay_us ( 480 );
    }


void WriteDS1820_Outdoor ( void )
    {
    for ( cShiftBit = 1; cShiftBit <= 8; ++cShiftBit )
        {
        output_low ( OUTDOOR_PIN );
        delay_us ( 5 );
        output_bit ( OUTDOOR_PIN, shift_right ( &cDataOut, 1, 0 ) );
        delay_us ( 60 );
        output_float ( OUTDOOR_PIN );
        delay_us ( 5 );
        }
    }



void ReadDS1820_Outdoor ( void )
    {
    iDataIn = 0;
    for ( cShiftBit = 1; cShiftBit <= 16; ++cShiftBit )
       {
       output_low ( OUTDOOR_PIN );
       delay_us ( 5 );
       output_float ( OUTDOOR_PIN );
       delay_us ( 5 );
       shift_right ( &iDataIn, 2, input ( OUTDOOR_PIN ) );
       delay_us ( 55 );
       }
    }



void WaitForConversion_Outdoor ( void )
    {
    while ( TRUE )
       {
       output_low ( OUTDOOR_PIN );
       delay_us ( 5 );
       output_float ( OUTDOOR_PIN );
       delay_us ( 5 );
       if ( input ( OUTDOOR_PIN ) == 1 )
           {
           break;
           }
       delay_us ( 55 );
       }
    }

.
.
.
Code:


        ResetDS1820_Indoor();
        cDataOut = DS1820_SKIP_ROM;
        WriteDS1820_Indoor();
        cDataOut = DS1820_CONVERT_T;
        WriteDS1820_Indoor();
        WaitForConversion_Indoor();
        ResetDS1820_Indoor();
        cDataOut = DS1820_SKIP_ROM;
        WriteDS1820_Indoor();
        cDataOut = DS1820_READ_SCRATCHPAD;
        WriteDS1820_Indoor();
        ReadDS1820_Indoor();
        iTemperature = iDataIn / 2;  // INDOOR Temperature
       // LCD or 7Seg Call routines here

        ResetDS1820_Outdoor();
        cDataOut = DS1820_SKIP_ROM;
        WriteDS1820_Outdoor();
        cDataOut = DS1820_CONVERT_T;
        WriteDS1820_Outdoor();
        WaitForConversion_Indoor();
        ResetDS1820_Outdoor();
        cDataOut = DS1820_SKIP_ROM;
        WriteDS1820_Outdoor();
        cDataOut = DS1820_READ_SCRATCHPAD;
        WriteDS1820_Outdoor();
        ReadDS1820_Outdoor();
        iTemperature = iDataIn / 2;  // OUTDOOR Temperature
       // LCD or 7Seg Call routines here

.
.
Bytheway,
I am not good with C. How can we use only one routine for both 2 sensors for code space optimization ?
Any suggestions wellcome.
.
.


Last edited by LazBoy on Thu Oct 19, 2006 1:21 pm; edited 1 time in total
linux123



Joined: 09 Oct 2006
Posts: 17

View user's profile Send private message

Stop
PostPosted: Wed Oct 18, 2006 4:46 pm     Reply with quote

Hy, the code stop begin call function, read temp_indoor, but, my code continue load, because call interrupt from serail RX, response, and i add call from SKIP ROM.
Thanks.
linux123



Joined: 09 Oct 2006
Posts: 17

View user's profile Send private message

Ok.
PostPosted: Thu Oct 19, 2006 7:04 am     Reply with quote

The code it's ok, but, have insert:
#define DS1820_SKIP_ROM 0xCC
#define DS1820_READ_SCRATCHPAD 0xBE
#define DS1820_CONVERT_T 0x44
I go job to add precision, because, the code return decimal value.
If anybody help-me, i thanks.
LazBoy



Joined: 17 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 6:09 am     Reply with quote

I added missed variables.

Yes, it hangs If there is no sensor on communication pin.
You may fix it for your needs.

Code:


void ResetDS1820_Indoor( void )
    {
     INDOOR_SENSOR_NOT_FOUND = FALSE;

    output_low ( INDOOR_PIN );
    delay_us ( 480 );         
    output_float ( INDOOR_PIN );

    delay_us ( 20 );

    if ( input ( INDOOR_PIN ) == 1 )
           {
           INDOOR_SENSOR_NOT_FOUND = TRUE;
           Break;
           }
    delay_us ( 460 );         
    }

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group