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 support@ccsinfo.com

Thermostat with ds18b20 and pic16f628a

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



Joined: 23 Mar 2017
Posts: 9
Location: Brazil

View user's profile Send private message

Thermostat with ds18b20 and pic16f628a
PostPosted: Thu Mar 23, 2017 8:57 am     Reply with quote

Hello everyone! I'm a beginner in this forum and a beginner in electronics. Any help will be welcome! I need to interface the sensor with Pic to activate a cooling system with a DC motor after the temperature >= X degrees Celsius. In the tutorial site, the project integrates an LCD; However, for my use I will not need it. These codes fall like a glove for what I need to do, requiring only a few modifications. The problem: Proteus runs the code without displaying errors in the log, but nothing happens when the temperature exceeds the limit temperature imposed in the code; I went to see what it was, I went back to the sections that the LCD showed, I made the Proteus schematic, and that's it ... Here's the problem: Whatever the temperature on the ds18b20, the LCD always accounted "-0.5C". Is it a Proteus bug or would it also go wrong in practice?

The codes:

main.c
Code:

#include <16F628A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=20000000)

#include <1wire.c>
#include <ds1820.c>

void alarma();

void main(){
float temperatura;
float minimo;
float maximo;

minimo=26;
maximo=28;

while (1){
  temperatura = ds1820_read();
  if (temperatura<=minimo){
      output_high(PIN_A1);}
  if (temperatura>=maximo){
      output_low(PIN_A1);
  alarma();}
  if (temperatura==minimo){
      output_low(PIN_A1);}
  }
}

void alarma(){
output_high(PIN_B3);
delay_ms(100);
output_low(PIN_B3);
delay_ms(100);
}


ds1820.c
Code:

float ds1820_read()
{
 int8 busy=0, temp1, temp2;
 signed int16 temp3;
 float result;

 onewire_reset();
 onewire_write(0xCC);
 onewire_write(0x44);

 while (busy == 0)
  busy = onewire_read();

 onewire_reset();
 onewire_write(0xCC);
 onewire_write(0xBE);
 temp1 = onewire_read();
 temp2 = onewire_read();
 temp3 = make16(temp2, temp1);
 
 result = (float) temp3 / 2.0;   //Calculation for DS18S20 with 0.5 deg C resolution
// result = (float) temp3 / 16.0;  //Calculation for DS18B20 with 0.1 deg C resolution
 
 delay_ms(200);
 return(result);
}


1wire.c
Code:

// (C) copyright 2003 j.d.sandoz / jds-pic !at! losdos.dyndns.org

// released under the GNU GENERAL PUBLIC LICENSE (GPL)
// refer to http://www.gnu.org/licenses/gpl.txt

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

/***********************1Wire Class***********************/
/*Description: This class handles all communication */
/* between the processor and the 1wire */
/* sensors.
/*********************************************************/

/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_A0

/*******************1-wire communication functions********************/

/************onewire_reset*************************************************/
/*This function initiates the 1wire bus */
/* */
/*PARAMETERS: */
/*RETURNS: */
/*********************************************************************/

void onewire_reset()  // OK if just using a single permanently connected device
{
 output_low(ONE_WIRE_PIN);
 delay_us( 500 ); // pull 1-wire low for reset pulse
 output_float(ONE_WIRE_PIN); // float 1-wire high
 delay_us( 500 ); // wait-out remaining initialisation window.
 output_float(ONE_WIRE_PIN);
}

/*********************** onewire_write() ********************************/
/*This function writes a byte to the sensor.*/
/* */
/*Parameters: byte - the byte to be written to the 1-wire */
/*Returns: */
/*********************************************************************/

void onewire_write(int data)
{
 int count;

 for (count=0; count<8; ++count)
 {
  output_low(ONE_WIRE_PIN);
  delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
  output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire
  delay_us( 60 ); // wait until end of write slot.
  output_float(ONE_WIRE_PIN); // set 1-wire high again,
  delay_us( 2 ); // for more than 1us minimum.
 }
}

/*********************** read1wire() *********************************/
/*This function reads the 8 -bit data via the 1-wire sensor. */
/* */
/*Parameters: */
/*Returns: 8-bit (1-byte) data from sensor */
/*********************************************************************/

int onewire_read()
{
 int count, data;

 for (count=0; count<8; ++count)
 {
  output_low(ONE_WIRE_PIN);
  delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
  output_float(ONE_WIRE_PIN); // now let 1-wire float high,
  delay_us( 8 ); // let device state stabilise,
  shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result.
  delay_us( 120 ); // wait until end of read slot.
 }

 return( data );
}


The links that I followed:
https://www.ccsinfo.com/forum/viewtopic.php?t=28425
https://picadicto.wordpress.com/2009/08/22/controlado-la-temperatura-con-ds18b20/


Can someone help?
(Sorry for bad english)
Thank you!
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 23, 2017 1:32 pm     Reply with quote

OK some comments
1) do NOT waste your time with Proteus. Read PIC101 sticky at top please. I could(should ?) write a book on what is wrong with Proteus but I won't live long enough....

2) the driver is for a DS1820 device your subject says DS18B20. You need to compare both datasheets to see IF the 'timing' is the same. Be sure the pulse widths for 1s,0s, and cycel times are identical.

Now I have used DS18B20s and 16F628s for a few projects(smart thermostats, greenhouse controllers, etc.) so I know they will work

3) motor. Be sure it is properly driven( MOSFET ?),control back EMF, and th ePSu has enough current(5x run) to handle the motor.

Jay
lucke



Joined: 23 Mar 2017
Posts: 9
Location: Brazil

View user's profile Send private message

PostPosted: Fri Mar 24, 2017 1:33 pm     Reply with quote

Hello, Jay! Firstly, thanks for replying to the topic, I've put this in several forums here in Brazil and nobody helps me...

Quote:
do NOT waste your time with Proteus.

Ok kkkk

Quote:

the driver is for a DS1820 device your subject says DS18B20.

Actually, I do not dominate the C language, I'm just following tutorials and trying to learn yet Embarassed Crying or Very sad

Quote:

You need to compare both datasheets to see IF the 'timing' is the same. Be sure the pulse widths for 1s,0s, and cycel times are identical.

I looked at the datasheet but did not know how to identify the information ...

Quote:

motor. Be sure it is properly driven( MOSFET ?),control back EMF, and th ePSu has enough current(5x run) to handle the motor.

I would use some transistors or the L293D to activate the motor ... Would it work?

Quote:

Now I have used DS18B20s and 16F628s for a few projects

Could you give me the codes you used? Because I have been reading about this subject for days, trying to solve it, I almost gave up ...

Thank you!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Thermostat with ds18b20 and pic16f628a
PostPosted: Fri Mar 24, 2017 2:03 pm     Reply with quote

lucke wrote:

Whatever the temperature on the ds18b20, the LCD always accounted "-
0.5C". Is it a Proteus bug or would it also go wrong in practice?

Read these posts about the "-0.5C" problem:
http://www.ccsinfo.com/forum/viewtopic.php?t=28425&start=34
lucke



Joined: 23 Mar 2017
Posts: 9
Location: Brazil

View user's profile Send private message

Re: Thermostat with ds18b20 and pic16f628a
PostPosted: Fri Mar 24, 2017 9:13 pm     Reply with quote

PCM programmer wrote:
lucke wrote:

Whatever the temperature on the ds18b20, the LCD always accounted "-
0.5C". Is it a Proteus bug or would it also go wrong in practice?

Read these posts about the "-0.5C" problem:
http://www.ccsinfo.com/forum/viewtopic.php?t=28425&start=34


Many thanks temtronic and PCM programmer! I did the same procedures as the user ishmael and xuxa, and it worked! Of all the forums I posted, you were the only ones who helped me ... Problem solved!
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