Termal Library 1 WIRE

General discussion on mikroBasic.
Post Reply
Author
Message
yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Termal Library 1 WIRE

#1 Post by yo2lio » 12 Jul 2007 17:53

Hello all.

I saw many articles and examples about DS18xxx termal sensor.
I use my library, in few industrial aplications ( 8 sensor / 1Wire ) and work very good.
My library was made and tested on BigPIC4 board, PIC18F8722 , GRAPH LCD 128x64, DS18B20 temp sensor and include functions and procedures for termal and humidity aplications.
About humidity, i tried a few sensors ( SHT11, SHT15, ..., HIH3605, HIH3610), but for my aplications (dust conditions) right choise was DRY BULB and WET BULB.

This library is only for P18 MCU. Examples are for this configuration.

My library include :

Code: Select all

sub function BusFree(dim port, pin as byte) as boolean
'return true if BUS is FREE

Code: Select all

sub function GetRomCode(dim port, pin as byte, dim byref romcode as byte[8]) as boolean
'put ROMCODE in romcode var and return true if CRC is valid

Code: Select all

sub procedure ConvertTempAll(dim port, pin as byte)
'start convesion of all sensors

Code: Select all

sub procedure ConvertTemp(dim port, pin as byte, dim byref romcode as byte[8])
'start conversion of sensor with specified ROMCODE

Code: Select all

sub function GetTemp(dim port, pin as byte, dim byref romcode as byte[8], dim byref temp as real) as boolean
'read temperature of sensor with specified ROMCODE (multiple sensor on 1WIRE BUS), put temperature in temp var and return true if CRC is valid

Code: Select all

sub function GetTempSkipCode(dim port, pin, family as byte, dim byref temp as real) as boolean
'read temperature of one sensor skip ROMCODE (one sensor on 1WIRE BUS),put temperature in temp var and return true if CRC is valid

Code: Select all

sub procedure GetHumidity(dim TempDryBulb, TempWetBulb as real, dim byref humidity as real)
'calculate HUMIDITY from TempDryBulb and TempWetBulb, return HUMIDITY in humidity var.

Code: Select all

sub procedure Byte2Hex(dim data as byte, dim byref hex as string[2])
'convert byte in HEX string

You can download code and examples of using this library from:

http://www.microelemente.ro/MikroBasic/ ... ibrary.zip

First copy termal_library_bas.mcl file in ../Uses/P18/
Source of this library is available only in MikroPascal language.

About HUMIDITY you found many articles on the net.

Best regards, Florin Medrea.

PaulC
Posts: 100
Joined: 03 Oct 2007 15:15
Location: Ireland

Thankyou

#2 Post by PaulC » 27 Nov 2007 20:48

Many thanks

:D

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 27 Nov 2007 21:12

My pleasure,

Read also :

http://www.mikroe.com/forum/viewtopic.p ... ght=termal

http://www.mikroe.com/forum/viewtopic.p ... hlight=one

You found some source code in Mikropascal language ....
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

silvano.cal@tin
Posts: 7
Joined: 14 Sep 2006 09:19
Location: Italy

DS 18B20

#4 Post by silvano.cal@tin » 05 Jan 2008 17:33

:D
I tried your library and it works well
Tested with PIC 18LF458 and two 18b20
Thanks.

Silvy

grange
Posts: 1
Joined: 20 Jan 2008 05:02

it won't work!!

#5 Post by grange » 20 Jan 2008 05:08

I have been referring to a lot of thread trying to display readings using nokia 3310 LCD from multiple 1-wire DS18B20s using PIC18LF4550. It still cannot detect to the sensors and read from the scratchpad. Two sensors are currently attached and what im trying is to detect one of the sensors and display the reading. Hope someone could give me some ideas. Thank you.

Below are the source codes:

[code]#include <18F4550.h>
#include <STDLIB.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#FUSES PUT //Power Up Timer
#FUSES BROWNOUT //Reset when brownout detected
#FUSES LVP

#use delay(clock=4000000) // setup for internal 4Mhz oscillator
#use fast_io(A)
#define LCD_DC PIN_A0 // setup pin names for easy coding
#define LCD_RST PIN_A1
#define LCD_DAT PIN_A4
#define LCD_CLK PIN_A5
#define DQ PIN_D0


void writecommand(int command); // declare function names for compiler
void writedata(int data);
void cursorxy (byte x, byte y);
void clearram(void);
void initpic(void);
void initlcd(void);
void putchar(char);
void putstr(char *);
unsigned char ow_reset(void);
unsigned char read_bit(void);
void write_bit(unsigned char bitval);
unsigned char read_byte(void);
void write_byte(unsigned char val);
void printbyte(unsigned char val);
void printdec(unsigned char val);
unsigned char Send_MatchRom(void);
void Read_ROMCode(void);
void display_temp(void);
unsigned char ow_reset1(void);
unsigned char read_bit1(void);
void write_bit1(unsigned char bitval);
unsigned char read_byte1(void);


int8 temp;
int8 temp1;
int16 temp2;


union {
int16 W;
struct {
int8 L;
int8 H;
};
} temperature;

div_t idiv;
char tempchar[4];
unsigned char FoundROM[5][8]; // table of found ROM codes

const char table[] = { //removed for simplicity purposes
};


void main()
{
int value=0;
int j,i;
char pad[8];
//int count;

initpic();
initlcd();
clearram();

FoundROM[0][0]= 0x28;
FoundROM[0][1]= 0x31;
FoundROM[0][2]= 0x6A;
FoundROM[0][3]= 0x39;
FoundROM[0][4]= 0x01;
FoundROM[0][5]= 0x00;
FoundROM[0][6]= 0x00;
FoundROM[0][7]= 0x00;
FoundROM[1][0]= 0x28;
FoundROM[1][1]= 0x71;
FoundROM[1][2]= 0x79;
FoundROM[1][3]= 0x39;
FoundROM[1][4]= 0x01;
FoundROM[1][5]= 0x00;
FoundROM[1][6]= 0x00;
FoundROM[1][7]= 0x00;


do {

ow_reset();
write_byte(0xCC); // Skip ROM send cmd to all devices
Send_MatchRom();
delay_us(120);
write_byte(0x44); // Start Conversion
delay_ms(750);

ow_reset();
write_byte(0x55); // match ROM
Send_MatchRom();
delay_us(120);

write_byte(0xBE); // Read Scratch Pad
delay_us(120);

cursorxy(0,3);
for (j=0;j<9;j++)
{
pad[j]=read_byte();
printbyte(pad[j]);
}

cursorxy(0,0);
putchar("TEMP1=");
display_temp();


}while (1);
}



void writecommand(int command)
{
output_low(LCD_DC); // byte is a command it is read with the eight SCLK pulse
for (temp=8;temp>0;temp--)
{
output_low(LCD_CLK);
if ((command&0x80)==0)
{
output_low(LCD_DAT);
}
else
{
output_high(LCD_DAT);
}
output_high(LCD_CLK);
command=command<<1;
}
}

void writedata(int data)
{
output_high(LCD_DC);
for (temp=8;temp>0;temp--)
{
output_low(LCD_CLK);
if ((data&0x80)==0)
{
output_low(LCD_DAT);
}
else
{
output_high(LCD_DAT);
}
output_high(LCD_CLK);
data=data<<1;
}
}

void cursorxy (byte x, byte y) // Nokia LCD Position cursor
{
writecommand(0x40|(y&0x07));
writecommand(0x80|(x&0x7f));
}

void clearram(void)
{
long int lcdram;

cursorxy(0,0);
for (lcdram=600;lcdram>0;lcdram--)
writedata(0); // write all 504 LCDRAM addresses.
}

void initpic(void)
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_a(0);
}

void initlcd(void)
{
output_low(LCD_RST); // reset LCD
delay_ms(100); // Wait 100ms
output_high(LCD_RST);// release from reset

writecommand(0x21); // Activate Chip and H=1.
writecommand(0xC2); // Set LCD Voltage to about 7V.
writecommand(0x13); // Adjust voltage bias.
writecommand(0x20); // Horizontal addressing and H=0.
writecommand(0x09); // Activate all segments.
clearram(); // Erase all pixel on the lcdram.
writecommand(0x08); // Blank the Display.
writecommand(0x0C); // Display Normal.
cursorxy(0,0); // Cursor Home.
}

void putstr(char *s)
{
while(*s) {putchar(*s++);} // Points to one ASCII to be written one at a time.
}

void putchar(char character)
{
int tablept, count, char_column;

if((character<0x2b)||(character>0x5a)) {return;} // Exit function if character is not found.
tablept = ((5*character)-215); // Point to the columns of the character in the table.


for (count=5;count>0;count--) // Draw the columns to print the character.
{
char_column = table[tablept];
writedata(char_column);
tablept++;
}
writedata(0x00); // 1 pixel spacing per character.
}

unsigned char ow_reset(void)
{
unsigned char presence;

output_low(DQ); //pull DQ line low
delay_us(480); // leave it low for 480us
set_tris_d(0x01);
output_high(DQ); // allow line to return high
delay_us(70); // wait for presence
/*if (!input(DQ))
putchar('Y');
else
putchar('N');*/
presence = input(DQ);
delay_us(424); // wait for end of timeslot
set_tris_d(0x00);
return(presence);
}


unsigned char read_bit(void)
{
unsigned char v;
output_low(DQ); // pull DQ low to start timeslot
//set_tris_a(0x04);
output_high(DQ);
v = input(DQ);
delay_us(56);
//set_tris_a(0x00);
return (v); // return value of DQ line
}

void write_bit(unsigned char bitval)
{
output_low(DQ);
if(bitval==1)
output_high(DQ); // return DQ high if write 1
delay_us(60); // hold value for remainder of timeslot
output_high(DQ);
} // Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us


unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=8;i>0;i--)
{
value = value >> 1;
if (read_bit())
value|=0x80;
}
return(value);
}

void write_byte(unsigned char val)
{
unsigned char i;
for (i=8; i>0; i--) { // writes byte, one bit at a time
write_bit(val&1);
val = val >> 1;
}
}

void printbyte(unsigned char val)
{
if (val>=0xA0)
putchar((val>>4)+('A'-0x0A));
else
putchar((val>>4)+'0');
val &= 0x0F;
if (val>=0x0A)
putchar(val+('A'-0x0A));
else
putchar(val+'0');
}

void printdec(unsigned char val)
{
unsigned int8 i=0;
do {
idiv = div(val,10);
tempchar[i++] = idiv.rem + '0';
val=idiv.quot;
} while (idiv.quot>=10);
tempchar[i++] = idiv.quot + '0';
do {
putchar(tempchar[--i]);
} while (i!=0);
}


unsigned char Send_MatchRom(void)
{
unsigned char i;

if(ow_reset())
return (false);

write_byte(0x55); // match ROM
cursorxy(0,2);
for(i=0;i<8;i++)
{
write_byte(FoundROM[1][i]); //send ROM code
printbyte(FoundROM[1][i]);
}
return (true);
}

void Read_ROMCode(void)
{
int n;
char dat[9];

ow_reset();
write_byte(0x33); //
cursorxy(0,1);
for (n=0;n<8;n++)
{
dat[n]=read_byte();
printbyte(dat[n]);
}

}

void display_temp(void)
{

temperature.L = read_byte();
temperature.H = read_byte();

if (temperature.H&0x80)
{
putchar('-');
temperature.W = -temperature.W;
}
else {
putchar('+');
}

temp=(temperature.H<<4) | (temperature.L>>4);
temp&=0x7F;
printdec(temp);
putchar(".");

temp1=temperature.L&=0x0F;
temp2=temp1*6.25; //multiplication needs 2 byte
printdec(temp2);
writedata(0x07);
writedata(0x05);
writedata(0x07);
writedata(0);
putchar("C");
}[/code]

[color=#BF0000]Edited by Administrator: Added Code Tag![/color]

PaulC
Posts: 100
Joined: 03 Oct 2007 15:15
Location: Ireland

works great

#6 Post by PaulC » 22 Feb 2008 08:25

works for me
different micro's
fantastic job
helped me loads in learning

:D

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: works great

#7 Post by yo2lio » 22 Feb 2008 08:53

PaulC wrote:works for me
different micro's
fantastic job
helped me loads in learning

:D
Thank you !

Read also : http://www.mikroe.com/forum/viewtopic.php?p=63606#63606

only for P18.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#8 Post by yo2lio » 12 Mar 2008 23:08

After one hour of work, library was updated with source code in MikroBasic .

Added :

module Termal_Library_Bas_My
module OneWire_base1


Also you have old file Termal_Library.mcl, generated by MikroPascal.

New functions are available :

function GetTempDec, return temperature*100 as integer
function GetTempDecSkipCode, return temperature*100 as integer
procedure ConvertTempPullUp, parasite power mode
procedure ConvertTempAllPullUp, parasite power mode


http://www.microelemente.ro/MikroBasic/ ... ibrary.zip
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

cllow2020
Posts: 106
Joined: 06 Nov 2007 10:26

DS18B20_Parasite_Power for P16 lib ?

#9 Post by cllow2020 » 08 May 2008 07:44

yo2lio wrote:After one hour of work, library was updated with source code in MikroBasic .

Added :

module Termal_Library_Bas_My
module OneWire_base1

...
module not working for P16 MCU
error in Save_PRODH = PRODH....

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: DS18B20_Parasite_Power for P16 lib ?

#10 Post by yo2lio » 08 May 2008 10:02

cllow2020 wrote:module not working for P16 MCU
error in Save_PRODH = PRODH....
I don't write library for P16 ... Sorry .

P18 or greater ... P24, Atmel ....
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#11 Post by yo2lio » 22 Nov 2008 19:50

Hi,

Library updated :
http://www.microelemente.ro/MikroBasic/ ... ibrary.zip

Resolved small bug in negative temperature reading.

This library use custom One Wire functions, interrupt free.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Post Reply

Return to “mikroBasic General”