 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
|
Posted: Sun Aug 21, 2005 8:37 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:44 pm |
|
|
i am so sorry to post so many time, my internet is too slow today!!!???
peter  |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Aug 21, 2005 9:50 pm |
|
|
| If you would have registered and logged in then you could have deleted them! |
|
 |
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Sun Mar 26, 2006 4:41 am |
|
|
Hi,
I am think of using the ds18b20>> (of 3 to 5 v power and 1.5ma) one wire for a tempature sensor, I want to utilize the parasite power setup. As I dont have a spare pin on my micro (18f1320)>> that has High current sink/source of 25 mA/25 mA as per the spec.
would you thing there be enoufe power from the micro (if we hold the OW pin high to parisite the power for the one wire temp sensor and then do the Rx or TX at intervauls.
I would try this out however if some one has done this before, I would like to know did it work, if not, I may need to consider a different micro and new design rather than a code change.
Andrew _________________ What has been learnt if you make the same mistake?  |
|
 |
|
|
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
|