![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 08, 2004 9:19 am |
|
|
Quote: |
#include <16F877A.h>
#use delay(clock=2000000)
#fuses HS, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT, NOLVP |
I see a problem in the code above. The #use delay() statement
is set for only 2 MHz. But you're using the HS fuse, so I suspect
that you really using a 20 MHz crystal.
The effect of this mistake would be for all delay_ms() and delay_us()
statements to run 10x faster than expected. ie., delay_us(420);
would only delay for 42 us. This could cause everything to fail.
The statement should be changed to this:
Code: | #use delay(clock=20000000) |
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Jul 08, 2004 9:40 am |
|
|
Thank uou PCM:
I do changed to 20mHz, but the reading is still same as before. 10 degree c |
|
![](templates/subSilver/images/spacer.gif) |
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Thu Jul 08, 2004 5:51 pm |
|
|
Code: |
if (temperatureMSB & 0b11111000) // if temp is negative rtn 0
return(0);
else { // else rtn the positive temp
temperatureLSB=((temperatureLSB & 0b11110000)>>4);
temperatureMSB=((temperatureMSB & 0b00000111)<<4);
}
return(temperatureMSB | temperatureLSB); // OR msb&lsb
|
The problem is probably that you have not modified the part of jds-pic's code above to suit the DS1820/DS18S20. This part has a fixed 9 bit resolution with 0.5 deg. C per bit compared to the DS1822/DS18B20 which has 12 bit resolution and 1/16 deg C per bit. The other difference is the one jds-pic mentioned ie the latter ones can have the resolution changed by setting the config reg bits. In other respects the code is the same.
Bytes 6 and 7 in the scratchpad can be used to improve resolution (see data sheet).
With the DS1820 the twos complement data is contained completely in the LSB and the MSB contains the sign bits (see data sheet), so the code needs to be changed to something like the following (if not using signed arithmetic):
if (temperatureMSB > 0) // if temp is negative rtn 0
return(0);
else { // else rtn the positive temp
temperatureLSB=(temperatureLSB + 1)/2; // 0.5 deg C/bit, apply rounding by adding half denom.
}
return(temperatureLSB); |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 08, 2004 10:39 pm |
|
|
You're having so much trouble making this work.
Peter Anderson has some code for the DS1820 on his website.
He uses the CCS compiler:
http://www.phanderson.com/PIC/PICC/CCS_PCM/ds1820.html
Here's the main page:
http://www.phanderson.com/PIC/PICC/index.html
The only problem with his code is that he doesn't always
use CCS functions. For example, he writes his own
delay functions, in order to make his code be more
"universal". But by doing so, he makes it more
difficult for CCS users who run their PIC at a different
crystal frequency than he uses. So I suggest that
when you see his code like this
then you should change it to use the CCS function, like this:
Code: | delay_us(500); // 10 us x 50 = 500 us |
Also, delete his delay_ms() function, since CCS has its own
delay_ms() function.
At the end of his DS1820 code, he has some LCD code which
you don't need. Just delete it. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Fri Jul 09, 2004 6:29 am |
|
|
Hi PCM:
thank you for you answer.
I did make 1820 working using Peter Anderson's program after make a little modification on parasite power situation, when I change the connection to non-parasite power situation, the reading is consistantly 85 degree c. could you tell me why. and where should I changed. I tried to remove _1w_strong_pull_up(sensor); in the program, but there is no change.
Code: |
#if defined(__PCM__)
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#include <lcd420_1.c>
#include <math.h>
#byte PORTA =0x05
#byte TRISA =0x85
#define MAX_SENSORS 4
// 1-wire prototypes
void _1w_init(int sensor);
int _1w_in_byte(int sensor);
void _1w_out_byte(int d, int sensor);
void _1w_pin_hi(int sensor);
void _1w_pin_low(int sensor);
void _1w_strong_pull_up(int sensor); // not used in this routine
void main(void)
{
int sensor, n;
float buff;
sensor=5;
while(1)
// for (sensor=0; sensor<MAX_SENSORS; sensor++)
{
_1w_init(sensor);
_1w_out_byte(0xcc, sensor); // skip ROM
_1w_out_byte(0x44, sensor); // perform temperature conversion
_1w_strong_pull_up(sensor);
_1w_init(sensor);
_1w_out_byte(0xcc, sensor); // skip ROM
_1w_out_byte(0xbe, sensor); // read the result
buff=(float)(_1w_in_byte(sensor))/(2.0);
lcd_init();
printf(lcd_putc," ");
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"* Temperature *");
lcd_gotoxy(1,2);
printf(lcd_putc,"Tem=%4.2f%1cC",buff,0xdf);
}
}
// The following are standard 1-Wire routines.
void _1w_init(int sensor)
{
_1w_pin_hi(sensor);
_1w_pin_low(sensor);
delay_us(50);
_1w_pin_hi(sensor);
delay_us(50);
}
int _1w_in_byte(int sensor)
{
int n, i_byte, temp, mask;
mask = 0xff & (~(0x01<<sensor));
for (n=0; n<8; n++)
{
PORTA=0x00;
TRISA=mask;
TRISA=0xff;
#asm
CLRWDT
NOP
NOP
#endasm
temp=PORTA;
if (temp & ~mask)
{
i_byte=(i_byte>>1) | 0x80; // least sig bit first
}
else
{
i_byte=i_byte >> 1;
}
delay_us(6);
}
return(i_byte);
}
void _1w_out_byte(int d, int sensor)
{
int n, mask;
mask = 0xff & (~(0x01<<sensor));
for(n=0; n<8; n++)
{
if (d&0x01)
{
PORTA=0;
TRISA=mask; // momentary low
TRISA=0xff;
delay_us(6);
}
else
{
PORTA=0;
TRISA=mask;
delay_us(6);
TRISA=0xff;
}
d=d>>1;
}
}
void _1w_pin_hi(int sensor)
{
TRISA = 0xff;
}
void _1w_pin_low(int sensor)
{
PORTA = 0x00;
TRISA = 0xff & (~(0x01 << sensor));
}
void _1w_strong_pull_up(int sensor) // bring DQ to strong +5VDC
{
PORTA = 0x01 << sensor;
TRISA = 0xff & (~(0x01 << sensor));
delay_ms(120);
TRISA = 0xff;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Jerry I
Joined: 14 Sep 2003 Posts: 96 Location: Toronto, Ontario, Canada
|
|
Posted: Fri Jul 09, 2004 4:14 pm |
|
|
Do you have more than one sensor.
This happened to me also, and I found out that sensor was dead.
Try that first.
Jerry
I did make 1820 working using Peter Anderson's program after make a little modification on parasite power situation, when I change the connection to non-parasite power situation, the reading is consistantly 85 degree c. could you tell me why. and where should I changed. I tried to remove _1w_strong_pull_up(sensor); in the program, but there is no change. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Jul 12, 2004 12:27 pm |
|
|
Hi Jerrt:
No I just have one 1820 sensor. I believe my sensor is working. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|