|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
needs ds1820 working c codes |
Posted: Sat Aug 13, 2005 9:39 pm |
|
|
Hi all,
i am new to pic, i read few of previous post topics about ds1820 or ds18b20, and ds1822, but there is no complete working codes for ds1820, i found there is a complete codes for ds18b20, but ds1820 and ds18b20 are not the same.
can anyone help me with a complete working codes for ds1820? thanks!!
peter |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
|
Guest
|
|
Posted: Mon Aug 15, 2005 8:45 pm |
|
|
hi, jds-pic!
first thanks the info!.. i tried your codes(DS1822), but i got some error after compliting the codes. the first error: ""A #DEVICE required before this line""
before " void onewire_disable_interrupts(int disable)"
ok... then i added a line: #include <16f877.h>
then the other error comes up: "Undefined indentifier ONE_WIRE_PIN
is from: putput_low(ONE_WIRE_PIN);
i know those errors are not from your codes, but i don't know where i did wrong or not properly setup!!
thanks for the help..
peter |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Tue Aug 16, 2005 7:30 am |
|
|
Anonymous wrote: | thanks for the help..
|
we have to know where to start...
this is your first time C programming or your first time using CCS?
jds-pic |
|
|
pom
Joined: 25 Nov 2004 Posts: 30 Location: Germany
|
|
Posted: Tue Aug 16, 2005 7:45 am |
|
|
I know your problem.
The error that there is missing the ONE_WIRE_PIN means, that you first have to say, which pin your ds1820 is connected to. You have to add a line
#define ONE_WIRE_PIN PIN_C2
PIN_C2 must be the name of the pin, where your ds1820 is. The line has to be put before including the one-wire-library.
Good luck with one-wire, pom |
|
|
Guest
|
|
Posted: Tue Aug 16, 2005 8:41 am |
|
|
hi all,
i learn c (turbo c) long time ago, i can do simple program but i have to look up the books for reference, i am not a programmer just an auto parts man, i don't have much c coding experience.
i learn pic from examples. thanks!!
peter |
|
|
Guest
|
|
Posted: Wed Aug 17, 2005 10:21 pm |
|
|
hi everyone,
can anyone post any working c codes for ds1820 thanks.....
peter |
|
|
pom
Joined: 25 Nov 2004 Posts: 30 Location: Germany
|
|
Posted: Thu Aug 18, 2005 12:32 am |
|
|
I think you already have a complete working code.
Did you try to define a ONE_WIRE_PIN in your code when you got the error:
Undefined indentifier ONE_WIRE_PIN ?
I got the same error and I just had to define the pin, the ds1820 is connected to, as ONE_WIRE_PIN.
Try this and say if it does not work. |
|
|
Guest
|
|
Posted: Sun Aug 21, 2005 9:45 am |
|
|
pom wrote: | I think you already have a complete working code.
Did you try to define a ONE_WIRE_PIN in your code when you got the error:
Undefined indentifier ONE_WIRE_PIN ?Try this and say if it does not work. |
i defined the pin, and got no error, but i download to pic and run, the temperature was not right and temperature didn't change even i heated it up!!! any help!
thanks
peter |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sun Aug 21, 2005 10:18 am |
|
|
Did you check the power required to sense the temperature? You'll need to look at the ds1820 specs but it may require more power than it can get parasitically from the PIC pin. |
|
|
Guest
|
|
Posted: Sun Aug 21, 2005 10:39 am |
|
|
Douglas Kennedy wrote: | Did you check the power required to sense the temperature? You'll need to look at the ds1820 specs but it may require more power than it can get parasitically from the PIC pin. |
hi, thanks for the replay!
DQ is connected to pic
a 4.7k resistor pulled up to +5v
VDD is to +5V
peter |
|
|
Guest
|
|
Posted: Sun Aug 21, 2005 2:56 pm |
|
|
Anonymous wrote: | hi everyone,
can anyone post any working c codes for ds1820 thanks.....
peter |
Hey Peter, go write the code yourself first
(Then post it later if you have mistakes.) |
|
|
Guest
|
|
Posted: Sun Aug 21, 2005 8:24 pm |
|
|
Code: |
// 16F877A Pin 33 (RB0) connected to DS1820 Pin2 (DQ).
// 16F877A Pin 33 (RB0) also connect to 4.7K resistor pulled up to +5V.
// 16F877A Pin 25 (RC6/Tx) connected to MAX232 which is then connected to PC serial Port.
// ds1822 scratchpad registers
#define DS1822_SP_TLSB 0
#define DS1822_SP_TMSB 1
#define DS1822_SP_HLIM 2
#define DS1822_SP_LLIM 3
#define DS1822_SP_CFG 4
#define DS1822_SP_RES0 5
#define DS1822_SP_RES1 6
#define DS1822_SP_RES2 7
#define DS1822_SP_CRC 8
// ds1822 rom registers
#define DS1822_ROM_DEVTYPE 0
#define DS1822_ROM_SERIAL1 1
#define DS1822_ROM_SERIAL2 2
#define DS1822_ROM_SERIAL3 3
#define DS1822_ROM_SERIAL4 4
#define DS1822_ROM_SERIAL5 5
#define DS1822_ROM_SERIAL6 6
#define DS1822_ROM_CRC 7
// ds1822 command set
#define DS1822_CMD_READROM 0x33
#define DS1822_CMD_SKIPROM 0xCC
#define DS1822_CMD_CONVERTTEMP 0x44
#define DS1822_CMD_WRITESCRATCHPAD 0x4E
#define DS1822_CMD_READSCRATCHPAD 0xBE
#define DS1822_CMD_COPYSCRATCHPAD 0x48
// note that not all applications need to disable interrupts when
// performing onewire transactions. but, if unmasked interrupts
// cause onewire timing violations, returned data will be suspect
// or there may be other, hard-to-reproduce problems.
#define ONE_WIRE_PIN PIN_B0
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <stdlib.h>
void onewire_disable_interrupts(int disable) {
if (disable)
disable_interrupts(GLOBAL);
else
enable_interrupts(GLOBAL);
}
short int onewire_init_with_error_check() {
onewire_disable_interrupts(TRUE);
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( 5 ); // allow pin to stabilize
if (!input(ONE_WIRE_PIN)) {
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (1-wire leads shorted)
}
delay_us( 80 ); // wait for presence pulse, allowing for device variation
if (input(ONE_WIRE_PIN)) {
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (no 1-wire devices present)
}
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
//printf(debug_putc,"<>ok >onewire_init\n\r");
onewire_disable_interrupts(FALSE);
return ( TRUE ); // device(s) present and initialised.
}
void onewire_init() { // OK if just using a single permanently connected device
onewire_disable_interrupts(TRUE);
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( 80 ); // wait for presence pulse, allowing for device variation
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
onewire_disable_interrupts(FALSE);
}
void onewire_sendbyte(int data) {
int count;
//static int debugS;
//printf(debug_putc,"0x%x >onewire_sendbyte(%u)\n\r",data,debugS++);
onewire_disable_interrupts(TRUE);
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.
}
onewire_disable_interrupts(FALSE);
}
int onewire_readbyte() {
int count, data;
//static int debugR;
onewire_disable_interrupts(TRUE);
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.
}
//printf(debug_putc,"0x%x >onewire_readbyte(%u)\n\r",data,debugR++);
onewire_disable_interrupts(FALSE);
return( data );
}
int onewire_ds1822_read_scratchpad(int field) { /* returns config bitfield */
int data[9];
onewire_init();
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_READSCRATCHPAD);
data[DS1822_SP_TLSB]=onewire_readbyte(); // 0 Tlsb
data[DS1822_SP_TMSB]=onewire_readbyte(); // 1 Tmsb
data[DS1822_SP_HLIM]=onewire_readbyte(); // 2 Thlim
data[DS1822_SP_LLIM]=onewire_readbyte(); // 3 Tllim
data[DS1822_SP_CFG]=onewire_readbyte(); // 4 Config
data[DS1822_SP_RES0]=onewire_readbyte(); // 5 RES0
data[DS1822_SP_RES1]=onewire_readbyte(); // 6 RES1
data[DS1822_SP_RES2]=onewire_readbyte(); // 7 RES2
data[DS1822_SP_CRC]=onewire_readbyte(); // 8 CRC
if (field > 8) {
//printf(debug_putc,"ERR! >scratchpad field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else {
//printf(debug_putc,"0x%x >scratchpad field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}
}
int onewire_ds1822_read_rom(int field) { /* rtns ROM info, one byte at a time */
int data[8];
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_READROM);
data[DS1822_ROM_DEVTYPE]=onewire_readbyte(); // 0 family code
data[DS1822_ROM_SERIAL1]=onewire_readbyte(); // 1 serial number LSB
data[DS1822_ROM_SERIAL2]=onewire_readbyte(); // 2 serial number
data[DS1822_ROM_SERIAL3]=onewire_readbyte(); // 3 serial number
data[DS1822_ROM_SERIAL4]=onewire_readbyte(); // 4 serial number
data[DS1822_ROM_SERIAL5]=onewire_readbyte(); // 5 serial number
data[DS1822_ROM_SERIAL6]=onewire_readbyte(); // 6 serial number MSB
data[DS1822_ROM_CRC]=onewire_readbyte(); // 7 CRC
if (field > 7) {
//printf(debug_putc,"ERR! >rom field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else {
//printf(debug_putc,"0x%x >rom field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}
}
int onewire_crc(int oldcrc, int newbyte) {
// see http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf
int shift_reg, data_bit, sr_lsb, fb_bit, j;
shift_reg=oldcrc;
for(j=0; j<8; j++) { // for each bit
data_bit = (newbyte >> j) & 0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
shift_reg = shift_reg ^ 0x8c;
}
return(shift_reg);
}
int onewire_ds1822_set_temperature_resolution(int resolution) { /* set up for nbit resolution */
int resolution_cfgfield;
if ((resolution < 9) || (resolution > 12))
resolution=9;
resolution_cfgfield=((resolution-9)<<5); // see DS1822 datasheet page 7
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_WRITESCRATCHPAD);
onewire_sendbyte(0b01111101); // set max TH threshold (125'C)
onewire_sendbyte(0b11001001); // set min TL threshold (-55'C)
onewire_sendbyte(resolution_cfgfield); // Temp resolution, set to nbit
onewire_init(); // reset
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_COPYSCRATCHPAD);
delay_ms(15); // allow time for flash memory write.
return( (onewire_ds1822_read_scratchpad(DS1822_SP_CFG) & 0b01100000)>>5 );
}
int onewire_ds1822_read_temp_c_lite() { /* 0 to 125'C byte rtnd, nonparasitic mode */
int temperatureLSB, temperatureMSB, config, delaymult;
config=((onewire_ds1822_read_scratchpad(DS1822_SP_CFG) && 0b01100000)>>5);
// each addn'l resolution bit needs twice the base conversion time!
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_CONVERTTEMP);
delaymult=1<<config;
//printf(debug_putc,"0x%x >delay_mult (ds1822)\n\r",delaymult);
while (delaymult--)
delay_ms(100); // allow worst case time for temp. conversion.
temperatureLSB = onewire_ds1822_read_scratchpad(DS1822_SP_TLSB);
temperatureMSB = onewire_ds1822_read_scratchpad(DS1822_SP_TMSB);
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
}
void main(){
float temperature_f,temperature_c;
printf("connecting to DS1820 -----\r\n");
while (1){
delay_ms(2000);
if (onewire_init_with_error_check()) {
// set the number of bits of resolution on the ds1822.
// note that this affects the conversion time,
// RTFM and see the called function.
onewire_ds1822_set_temperature_resolution(9);
// read the temperature from the ds1822.
// note that the returned value is in deg C,
// and the "lite" function returns only positive temps.
temperature_c = onewire_ds1822_read_temp_c_lite();
printf( "Temperature C= %10.2f degrees C \r\n", temperature_c ); // print temperature C
temperature_f = (temperature_c* 9)/5 + 32;
printf( "Temperature F= %10.2f degrees F \r\n", temperature_f ); // print temperature F
delay_ms (1000);
}
else
printf("onewire device not found\r\n");
}//end while(1)
}// end of main()
|
i used most of jds-pic's codes, the temperature is not correct(if i change different ds1820 sensor, it reads different temperature and stays the same temperature!)
peter |
|
|
Guest
|
|
Posted: Sun Aug 21, 2005 8:26 pm |
|
|
Code: |
// 16F877A Pin 33 (RB0) connected to DS1820 Pin2 (DQ).
// 16F877A Pin 33 (RB0) also connect to 4.7K resistor pulled up to +5V.
// 16F877A Pin 25 (RC6/Tx) connected to MAX232 which is then connected to PC serial Port.
// ds1822 scratchpad registers
#define DS1822_SP_TLSB 0
#define DS1822_SP_TMSB 1
#define DS1822_SP_HLIM 2
#define DS1822_SP_LLIM 3
#define DS1822_SP_CFG 4
#define DS1822_SP_RES0 5
#define DS1822_SP_RES1 6
#define DS1822_SP_RES2 7
#define DS1822_SP_CRC 8
// ds1822 rom registers
#define DS1822_ROM_DEVTYPE 0
#define DS1822_ROM_SERIAL1 1
#define DS1822_ROM_SERIAL2 2
#define DS1822_ROM_SERIAL3 3
#define DS1822_ROM_SERIAL4 4
#define DS1822_ROM_SERIAL5 5
#define DS1822_ROM_SERIAL6 6
#define DS1822_ROM_CRC 7
// ds1822 command set
#define DS1822_CMD_READROM 0x33
#define DS1822_CMD_SKIPROM 0xCC
#define DS1822_CMD_CONVERTTEMP 0x44
#define DS1822_CMD_WRITESCRATCHPAD 0x4E
#define DS1822_CMD_READSCRATCHPAD 0xBE
#define DS1822_CMD_COPYSCRATCHPAD 0x48
// note that not all applications need to disable interrupts when
// performing onewire transactions. but, if unmasked interrupts
// cause onewire timing violations, returned data will be suspect
// or there may be other, hard-to-reproduce problems.
#define ONE_WIRE_PIN PIN_B0
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <stdlib.h>
void onewire_disable_interrupts(int disable) {
if (disable)
disable_interrupts(GLOBAL);
else
enable_interrupts(GLOBAL);
}
short int onewire_init_with_error_check() {
onewire_disable_interrupts(TRUE);
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( 5 ); // allow pin to stabilize
if (!input(ONE_WIRE_PIN)) {
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (1-wire leads shorted)
}
delay_us( 80 ); // wait for presence pulse, allowing for device variation
if (input(ONE_WIRE_PIN)) {
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (no 1-wire devices present)
}
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
//printf(debug_putc,"<>ok >onewire_init\n\r");
onewire_disable_interrupts(FALSE);
return ( TRUE ); // device(s) present and initialised.
}
void onewire_init() { // OK if just using a single permanently connected device
onewire_disable_interrupts(TRUE);
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( 80 ); // wait for presence pulse, allowing for device variation
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
onewire_disable_interrupts(FALSE);
}
void onewire_sendbyte(int data) {
int count;
//static int debugS;
//printf(debug_putc,"0x%x >onewire_sendbyte(%u)\n\r",data,debugS++);
onewire_disable_interrupts(TRUE);
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.
}
onewire_disable_interrupts(FALSE);
}
int onewire_readbyte() {
int count, data;
//static int debugR;
onewire_disable_interrupts(TRUE);
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.
}
//printf(debug_putc,"0x%x >onewire_readbyte(%u)\n\r",data,debugR++);
onewire_disable_interrupts(FALSE);
return( data );
}
int onewire_ds1822_read_scratchpad(int field) { /* returns config bitfield */
int data[9];
onewire_init();
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_READSCRATCHPAD);
data[DS1822_SP_TLSB]=onewire_readbyte(); // 0 Tlsb
data[DS1822_SP_TMSB]=onewire_readbyte(); // 1 Tmsb
data[DS1822_SP_HLIM]=onewire_readbyte(); // 2 Thlim
data[DS1822_SP_LLIM]=onewire_readbyte(); // 3 Tllim
data[DS1822_SP_CFG]=onewire_readbyte(); // 4 Config
data[DS1822_SP_RES0]=onewire_readbyte(); // 5 RES0
data[DS1822_SP_RES1]=onewire_readbyte(); // 6 RES1
data[DS1822_SP_RES2]=onewire_readbyte(); // 7 RES2
data[DS1822_SP_CRC]=onewire_readbyte(); // 8 CRC
if (field > 8) {
//printf(debug_putc,"ERR! >scratchpad field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else {
//printf(debug_putc,"0x%x >scratchpad field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}
}
int onewire_ds1822_read_rom(int field) { /* rtns ROM info, one byte at a time */
int data[8];
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_READROM);
data[DS1822_ROM_DEVTYPE]=onewire_readbyte(); // 0 family code
data[DS1822_ROM_SERIAL1]=onewire_readbyte(); // 1 serial number LSB
data[DS1822_ROM_SERIAL2]=onewire_readbyte(); // 2 serial number
data[DS1822_ROM_SERIAL3]=onewire_readbyte(); // 3 serial number
data[DS1822_ROM_SERIAL4]=onewire_readbyte(); // 4 serial number
data[DS1822_ROM_SERIAL5]=onewire_readbyte(); // 5 serial number
data[DS1822_ROM_SERIAL6]=onewire_readbyte(); // 6 serial number MSB
data[DS1822_ROM_CRC]=onewire_readbyte(); // 7 CRC
if (field > 7) {
//printf(debug_putc,"ERR! >rom field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else {
//printf(debug_putc,"0x%x >rom field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}
}
int onewire_crc(int oldcrc, int newbyte) {
// see http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf
int shift_reg, data_bit, sr_lsb, fb_bit, j;
shift_reg=oldcrc;
for(j=0; j<8; j++) { // for each bit
data_bit = (newbyte >> j) & 0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
shift_reg = shift_reg ^ 0x8c;
}
return(shift_reg);
}
int onewire_ds1822_set_temperature_resolution(int resolution) { /* set up for nbit resolution */
int resolution_cfgfield;
if ((resolution < 9) || (resolution > 12))
resolution=9;
resolution_cfgfield=((resolution-9)<<5); // see DS1822 datasheet page 7
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_WRITESCRATCHPAD);
onewire_sendbyte(0b01111101); // set max TH threshold (125'C)
onewire_sendbyte(0b11001001); // set min TL threshold (-55'C)
onewire_sendbyte(resolution_cfgfield); // Temp resolution, set to nbit
onewire_init(); // reset
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_COPYSCRATCHPAD);
delay_ms(15); // allow time for flash memory write.
return( (onewire_ds1822_read_scratchpad(DS1822_SP_CFG) & 0b01100000)>>5 );
}
int onewire_ds1822_read_temp_c_lite() { /* 0 to 125'C byte rtnd, nonparasitic mode */
int temperatureLSB, temperatureMSB, config, delaymult;
config=((onewire_ds1822_read_scratchpad(DS1822_SP_CFG) && 0b01100000)>>5);
// each addn'l resolution bit needs twice the base conversion time!
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_CONVERTTEMP);
delaymult=1<<config;
//printf(debug_putc,"0x%x >delay_mult (ds1822)\n\r",delaymult);
while (delaymult--)
delay_ms(100); // allow worst case time for temp. conversion.
temperatureLSB = onewire_ds1822_read_scratchpad(DS1822_SP_TLSB);
temperatureMSB = onewire_ds1822_read_scratchpad(DS1822_SP_TMSB);
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
}
void main(){
float temperature_f,temperature_c;
printf("connecting to DS1820 -----\r\n");
while (1){
delay_ms(2000);
if (onewire_init_with_error_check()) {
// set the number of bits of resolution on the ds1822.
// note that this affects the conversion time,
// RTFM and see the called function.
onewire_ds1822_set_temperature_resolution(9);
// read the temperature from the ds1822.
// note that the returned value is in deg C,
// and the "lite" function returns only positive temps.
temperature_c = onewire_ds1822_read_temp_c_lite();
printf( "Temperature C= %10.2f degrees C \r\n", temperature_c ); // print temperature C
temperature_f = (temperature_c* 9)/5 + 32;
printf( "Temperature F= %10.2f degrees F \r\n", temperature_f ); // print temperature F
delay_ms (1000);
}
else
printf("onewire device not found\r\n");
}//end while(1)
}// end of main()
|
i used most of jds-pic's codes, the temperature is not correct(if i change different ds1820 sensor, it reads different temperature and stays the same temperature!)
peter |
|
|
Guest
|
|
Posted: Sun Aug 21, 2005 8:30 pm |
|
|
Code: |
// 16F877A Pin 33 (RB0) connected to DS1820 Pin2 (DQ).
// 16F877A Pin 33 (RB0) also connect to 4.7K resistor pulled up to +5V.
// 16F877A Pin 25 (RC6/Tx) connected to MAX232 which is then connected to PC serial Port.
// ds1822 scratchpad registers
#define DS1822_SP_TLSB 0
#define DS1822_SP_TMSB 1
#define DS1822_SP_HLIM 2
#define DS1822_SP_LLIM 3
#define DS1822_SP_CFG 4
#define DS1822_SP_RES0 5
#define DS1822_SP_RES1 6
#define DS1822_SP_RES2 7
#define DS1822_SP_CRC 8
// ds1822 rom registers
#define DS1822_ROM_DEVTYPE 0
#define DS1822_ROM_SERIAL1 1
#define DS1822_ROM_SERIAL2 2
#define DS1822_ROM_SERIAL3 3
#define DS1822_ROM_SERIAL4 4
#define DS1822_ROM_SERIAL5 5
#define DS1822_ROM_SERIAL6 6
#define DS1822_ROM_CRC 7
// ds1822 command set
#define DS1822_CMD_READROM 0x33
#define DS1822_CMD_SKIPROM 0xCC
#define DS1822_CMD_CONVERTTEMP 0x44
#define DS1822_CMD_WRITESCRATCHPAD 0x4E
#define DS1822_CMD_READSCRATCHPAD 0xBE
#define DS1822_CMD_COPYSCRATCHPAD 0x48
// note that not all applications need to disable interrupts when
// performing onewire transactions. but, if unmasked interrupts
// cause onewire timing violations, returned data will be suspect
// or there may be other, hard-to-reproduce problems.
#define ONE_WIRE_PIN PIN_B0
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <stdlib.h>
void onewire_disable_interrupts(int disable) {
if (disable)
disable_interrupts(GLOBAL);
else
enable_interrupts(GLOBAL);
}
short int onewire_init_with_error_check() {
onewire_disable_interrupts(TRUE);
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( 5 ); // allow pin to stabilize
if (!input(ONE_WIRE_PIN)) {
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (1-wire leads shorted)
}
delay_us( 80 ); // wait for presence pulse, allowing for device variation
if (input(ONE_WIRE_PIN)) {
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (no 1-wire devices present)
}
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
//printf(debug_putc,"<>ok >onewire_init\n\r");
onewire_disable_interrupts(FALSE);
return ( TRUE ); // device(s) present and initialised.
}
void onewire_init() { // OK if just using a single permanently connected device
onewire_disable_interrupts(TRUE);
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( 80 ); // wait for presence pulse, allowing for device variation
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
onewire_disable_interrupts(FALSE);
}
void onewire_sendbyte(int data) {
int count;
//static int debugS;
//printf(debug_putc,"0x%x >onewire_sendbyte(%u)\n\r",data,debugS++);
onewire_disable_interrupts(TRUE);
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.
}
onewire_disable_interrupts(FALSE);
}
int onewire_readbyte() {
int count, data;
//static int debugR;
onewire_disable_interrupts(TRUE);
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.
}
//printf(debug_putc,"0x%x >onewire_readbyte(%u)\n\r",data,debugR++);
onewire_disable_interrupts(FALSE);
return( data );
}
int onewire_ds1822_read_scratchpad(int field) { /* returns config bitfield */
int data[9];
onewire_init();
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_READSCRATCHPAD);
data[DS1822_SP_TLSB]=onewire_readbyte(); // 0 Tlsb
data[DS1822_SP_TMSB]=onewire_readbyte(); // 1 Tmsb
data[DS1822_SP_HLIM]=onewire_readbyte(); // 2 Thlim
data[DS1822_SP_LLIM]=onewire_readbyte(); // 3 Tllim
data[DS1822_SP_CFG]=onewire_readbyte(); // 4 Config
data[DS1822_SP_RES0]=onewire_readbyte(); // 5 RES0
data[DS1822_SP_RES1]=onewire_readbyte(); // 6 RES1
data[DS1822_SP_RES2]=onewire_readbyte(); // 7 RES2
data[DS1822_SP_CRC]=onewire_readbyte(); // 8 CRC
if (field > 8) {
//printf(debug_putc,"ERR! >scratchpad field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else {
//printf(debug_putc,"0x%x >scratchpad field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}
}
int onewire_ds1822_read_rom(int field) { /* rtns ROM info, one byte at a time */
int data[8];
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_READROM);
data[DS1822_ROM_DEVTYPE]=onewire_readbyte(); // 0 family code
data[DS1822_ROM_SERIAL1]=onewire_readbyte(); // 1 serial number LSB
data[DS1822_ROM_SERIAL2]=onewire_readbyte(); // 2 serial number
data[DS1822_ROM_SERIAL3]=onewire_readbyte(); // 3 serial number
data[DS1822_ROM_SERIAL4]=onewire_readbyte(); // 4 serial number
data[DS1822_ROM_SERIAL5]=onewire_readbyte(); // 5 serial number
data[DS1822_ROM_SERIAL6]=onewire_readbyte(); // 6 serial number MSB
data[DS1822_ROM_CRC]=onewire_readbyte(); // 7 CRC
if (field > 7) {
//printf(debug_putc,"ERR! >rom field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else {
//printf(debug_putc,"0x%x >rom field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}
}
int onewire_crc(int oldcrc, int newbyte) {
// see http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf
int shift_reg, data_bit, sr_lsb, fb_bit, j;
shift_reg=oldcrc;
for(j=0; j<8; j++) { // for each bit
data_bit = (newbyte >> j) & 0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
shift_reg = shift_reg ^ 0x8c;
}
return(shift_reg);
}
int onewire_ds1822_set_temperature_resolution(int resolution) { /* set up for nbit resolution */
int resolution_cfgfield;
if ((resolution < 9) || (resolution > 12))
resolution=9;
resolution_cfgfield=((resolution-9)<<5); // see DS1822 datasheet page 7
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_WRITESCRATCHPAD);
onewire_sendbyte(0b01111101); // set max TH threshold (125'C)
onewire_sendbyte(0b11001001); // set min TL threshold (-55'C)
onewire_sendbyte(resolution_cfgfield); // Temp resolution, set to nbit
onewire_init(); // reset
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_COPYSCRATCHPAD);
delay_ms(15); // allow time for flash memory write.
return( (onewire_ds1822_read_scratchpad(DS1822_SP_CFG) & 0b01100000)>>5 );
}
int onewire_ds1822_read_temp_c_lite() { /* 0 to 125'C byte rtnd, nonparasitic mode */
int temperatureLSB, temperatureMSB, config, delaymult;
config=((onewire_ds1822_read_scratchpad(DS1822_SP_CFG) && 0b01100000)>>5);
// each addn'l resolution bit needs twice the base conversion time!
if (!onewire_init_with_error_check())
return (0);
onewire_sendbyte(DS1822_CMD_SKIPROM);
onewire_sendbyte(DS1822_CMD_CONVERTTEMP);
delaymult=1<<config;
//printf(debug_putc,"0x%x >delay_mult (ds1822)\n\r",delaymult);
while (delaymult--)
delay_ms(100); // allow worst case time for temp. conversion.
temperatureLSB = onewire_ds1822_read_scratchpad(DS1822_SP_TLSB);
temperatureMSB = onewire_ds1822_read_scratchpad(DS1822_SP_TMSB);
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
}
void main(){
float temperature_f,temperature_c;
printf("connecting to DS1820 -----\r\n");
while (1){
delay_ms(2000);
if (onewire_init_with_error_check()) {
// set the number of bits of resolution on the ds1822.
// note that this affects the conversion time,
// RTFM and see the called function.
onewire_ds1822_set_temperature_resolution(9);
// read the temperature from the ds1822.
// note that the returned value is in deg C,
// and the "lite" function returns only positive temps.
temperature_c = onewire_ds1822_read_temp_c_lite();
printf( "Temperature C= %10.2f degrees C \r\n", temperature_c ); // print temperature C
temperature_f = (temperature_c* 9)/5 + 32;
printf( "Temperature F= %10.2f degrees F \r\n", temperature_f ); // print temperature F
delay_ms (1000);
}
else
printf("onewire device not found\r\n");
}//end while(1)
}// end of main()
|
i used most of jds-pic's codes, the temperature is not correct(if i change different ds1820 sensor, it reads different temperature and stays the same temperature!)
peter |
|
|
|
|
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
|