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

ds18b20 pic16f876A
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vmetal



Joined: 27 Jul 2018
Posts: 8

View user's profile Send private message

ds18b20 pic16f876A
PostPosted: Fri Jul 27, 2018 10:29 am     Reply with quote

Hello, I am trying to use a pic16f876A with a Ds18b20 but it doesn't work. I found an example in this website using pic16f877a. https://www.ccsinfo.com/forum/viewtopic.php?t=28425. It works in proteus but when I change "#include <16f877a.h>" to #include <16f876a.h> it doesn't work. I receive -0.5ºC. I tried to use a lot of codes of other website but nothing... Can you explain me what it's wrong?
I attached code.

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 );
}


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);
}

main.c
Code:
//#include <16F877A.h>
#include <16F876A.h>
#device *=16
#device adc=8

#FUSES NOWDT, HS, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT     
#use delay(clock=20000000)

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


void main()
{
float temperature;

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
//setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW|-2);

lcd_init();
lcd_putc("\f");

while (1)
{
  temperature = ds1820_read();

  lcd_gotoxy(1,1);
  printf(lcd_putc,"TEMP: %3.1f ", temperature);
  lcd_putc(223);
  lcd_putc("C    ");

  lcd_gotoxy(1,2);
  if(temperature >= 29.0)
   printf(lcd_putc,"Hot!    ");
  else if( temperature >= 20 && temperature < 29.0)
   printf(lcd_putc,"Comfort!");
  else
   printf(lcd_putc,"Cold!   ");
 }

}


Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 11:02 am     Reply with quote

Seriously the 876a, and the 877a are exactly the same chip. Just a smaller package with some pins not available.
Big question though is do you have the -20 version?. If not it is only rated to run to 4MHz. Won't work at 20MHz.
If it is the -20 version, then you need to triple check your connections. Particularly the crystal.
Both of these chips are 'not recommended for new designs', and are basically obsolete. The 886 is the newer replacement.
vmetal



Joined: 27 Jul 2018
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 12:34 pm     Reply with quote

Thank for your reply. When I started with this design, I used a pic18f25k50. It didn't work. Then I looked for on internet about this problem and I found a lot of information about pic16f877A. I don't have a pic16f877a but I have a pic16f876A. I know that both pic are same less PORTD. In conclusion, I don't know what is wrong but I used both pic and It doesn't work. I prefer to use pic18f25k50 if it is possible.

Thank you so much.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: ds18b20 pic16f876A
PostPosted: Fri Jul 27, 2018 12:44 pm     Reply with quote

vmetal wrote:
It works in proteus but when I change "#include <16f877a.h>" to #include <16f876a.h> it doesn't work. I receive -0.5ºC.


The first two posts in this thread discuss how to fix the -0.5ºC problem
in proteus:
http://www.ccsinfo.com/forum/viewtopic.php?t=28425&start=38
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 12:45 pm     Reply with quote

Question still applies. You need the pic16f877A-20 to run at 20MHz. If you have the -4, it won't run.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 12:47 pm     Reply with quote

Does Proteus offer a selection between -4 and -20 units ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 2:07 pm     Reply with quote

I think he is saying it works in Proteus, but not in the real chip.
vmetal



Joined: 27 Jul 2018
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 2:47 pm     Reply with quote

In proteus doesn't work.
temtronic



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

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 3:15 pm     Reply with quote

Please read 'sticky' PIC101.
Proteus is NOT 100% working !! It is a defective piece of software!!
I've yet to see a Proteus schematic that WILL work in the real World.

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 4:17 pm     Reply with quote

vmetal wrote:
In proteus doesn't work

Is all testing being done in Proteus ?

Is any testing being done in real hardware ?
vmetal



Joined: 27 Jul 2018
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 4:50 pm     Reply with quote

I tested in real board with pic18f25k50 and 16f876A and doesn't work.
temtronic



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

View user's profile Send private message

PostPosted: Fri Jul 27, 2018 5:37 pm     Reply with quote

I've had DS18B20s working with PIC16F877, 16F84, 16F648, 18F46K22 for several years so I suspect it's either a wiring or timing issue.
you use a DS1820 driver but have ds18B20 devices. I seem to recall that there may be timing differences between the devices, so check the datasheets to confirm you've got the correct driver.
What value of pullup resistor do you have ?
How long a wire from PIC to temp sensor ?

Important question, have you confirmed the PIC IS operating at the required speed ?
I don't use that driver, rather one from the code library. Even cloned it 4 times for my remote greenhouse system as each sensor had it's own PIC pin for communication. Far simpler to design hardware.

Jay
vmetal



Joined: 27 Jul 2018
Posts: 8

View user's profile Send private message

PostPosted: Sat Jul 28, 2018 1:57 am     Reply with quote

The wire is 1 meter and the pullup resistor is 4k7 between VCC(5V) and Pin_Data. The crystal is 4MHz with two 32pF capacitor and other 100nF capacitor between VDD and VSS. I don't understand that I use PIC16f877A in proteus and it works and I change to pic16f876A only changing the library and it doesn't work :( .
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Jul 28, 2018 1:59 am     Reply with quote

Quote:

The crystal is 4MHz


Er. The code posted is for a crystal at 20MHz.
vmetal



Joined: 27 Jul 2018
Posts: 8

View user's profile Send private message

PostPosted: Sat Jul 28, 2018 2:09 am     Reply with quote

yes because in proteus used 20Mhz but I used 4 Mhz in proteus too and nothing.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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