PDA

View Full Version : DS1820 Negative Numbers and OffSet



gadelhas
- 23rd April 2011, 03:09
Hi everyone once again;

I made a program to read a DS1820 showing positive and negative temperatures. I found a piece of code in this forum to handle that, and its workinf fine. Now i want to put an offset in the temperature that was readed, for instance if the sensor is at 18ºC and the OFFSETTEMP variable is 2, then the LCD shows 20ºC, if the OFFSETTEMP variable is -3 than the lcd shows 15ºC.

In the code you will see that i allready have a menu to change the OFFSETTEMP variable, however i don't know now how to interect with the temperature that i'm reading.

Attached is the Proteus file with the simulation of the code, the code, and the HEX file.

The "MENU" button its to enter the the menu, and the "+" and "-" button is to change the value of the OFFSETTEMP variable.

Can somebody give me a clue?

Here is the code:

'************************************************* ***************
'* Name : TESTE DS1820.BAS *
'* Author : GADELHAS *
'* Notice : Copyright (c) *
'* : All Rights Reserved *
'* Date : 23-04-2011 *
'* Version : 1.0 *
'* Notes : BreadBoard + EASYPICV6 *
'* : PIC16F877A *
'************************************************* ***************
DEFINE OSC 20
@ __config _HS_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF & _BODEN_OFF & _PWRTE_OFF
' VARIABLES
' ================================================== ==================

TEMPC Var Word
OFFSETTEMP VAR BYTE
SIGN var bit
FLAG VAR BYTE

' PINOUT 1 = IN; 0 = OUT
' ================================================== ==================
'76543210
TRISA = %00000000
TRISB = %00000000
TRISC = %10010000
TRISD = %00001100
TRISE = %00000000
ADCON1 = 7

' ALIAS
' ================================================== ==================
LED VAR PORTB.7
BMENOS var PORTD.2
BMAIS var PORTD.3
BMENU VAR PORTC.4
OUT Var PORTC.2
DQ VAR PORTE.2

' DEFINITIONS
' ================================================== ==================

DEFINE LCD_DREG PORTB ' LCD Data bits on PORTB
DEFINE LCD_DBIT 0 ' PORTB starting address
DEFINE LCD_RSREG PORTB ' LCD RS bit on PORTB
DEFINE LCD_RSBIT 4 ' LCD RS bit address
DEFINE LCD_EREG PORTB ' LCD E bit on PORTB
DEFINE LCD_EBIT 5 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 2 ' LCD has 2 rows
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

' MAIN
' ================================================== ==================
PAUSE 100

INICIO: '01234567890123456789
Lcdout $fe, 1, " DS1820 Teste "
Lcdout $fe, $C0," HUGO OLIVEIRA "
Pause 1000

MAIN:
if Bmenu = 0 then
Pause 200
goto MENU
endif

gosub SENSOR

IF SIGN=1 THEN
Lcdout $fe, $C0, "Temp: -",DEC ABS (TEMPC/10), 223,"C "
else
Lcdout $fe, $C0, "Temp: ",DEC ABS (TEMPC/10), 223,"C "
endif
Goto MAIN

' BUTTON ROUTINE
' ================================================== ==================
MENU:
flag=1
while flag <> 0
pause 100
if flag = 1 then
Lcdout $fe, $1, "Menu 01-OffSet Temp."
if OFFSETTEMP.7 = 0 THEN
Lcdout $fe, $C0,"OffSet: ", DEC abs(OFFSETTEMP)
ELSE
Lcdout $fe, $C0,"OffSet: -", DEC abs(OFFSETTEMP)
ENDIF
gosub PLUSBUTTON
gosub MINUSBUTTON
endif

if Bmenu =0 then
pause 200
if flag = 1 then
flag =0
else
flag = flag + 1
endif
endif
wend
GOTO MAIN

PLUSBUTTON:
if Bmais = 0 then
pause 200
if flag = 1 then
if OFFSETTEMP = 5 then
OFFSETTEMP = 5
else
OFFSETTEMP=OFFSETTEMP + 1
endif
endif
ENDIF
RETURN
MINUSBUTTON:
if BMENOS = 0 then
pause 200
if flag = 1 then
if OFFSETTEMP = 251 then
OFFSETTEMP = 251
else
OFFSETTEMP=OFFSETTEMP - 1
endif
endif
ENDIF
RETURN
' TEMPERATURE ROUTINE
' ================================================== ==================
SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]
sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
return
END

gadelhas
- 23rd April 2011, 04:03
Hi again;

I allready found it out.

Here is the code:


'************************************************* ***************
'* Name : TESTE DS1820.BAS *
'* Author : GADELHAS *
'* Notice : Copyright (c) *
'* : All Rights Reserved *
'* Date : 23-04-2011 *
'* Version : 1.0 *
'* Notes : BreadBoard + EASYPICV6 *
'* : PIC16F877A *
'************************************************* ***************
DEFINE OSC 20
@ __config _HS_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF & _BODEN_OFF & _PWRTE_OFF
' VARIABLES
' ================================================== ==================

TEMPC Var Word
OFFSETTEMP VAR BYTE
SIGN var bit
FLAG VAR BYTE
temp var byte

' PINOUT 1 = IN; 0 = OUT
' ================================================== ==================
'76543210
TRISA = %00000000
TRISB = %00000000
TRISC = %10010000
TRISD = %00001100
TRISE = %00000000
ADCON1 = 7

' ALIAS
' ================================================== ==================
LED VAR PORTB.7
BMENOS var PORTD.2
BMAIS var PORTD.3
BMENU VAR PORTC.4
OUT Var PORTC.2
DQ VAR PORTE.2

' DEFINITIONS
' ================================================== ==================

DEFINE LCD_DREG PORTB ' LCD Data bits on PORTB
DEFINE LCD_DBIT 0 ' PORTB starting address
DEFINE LCD_RSREG PORTB ' LCD RS bit on PORTB
DEFINE LCD_RSBIT 4 ' LCD RS bit address
DEFINE LCD_EREG PORTB ' LCD E bit on PORTB
DEFINE LCD_EBIT 5 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 2 ' LCD has 2 rows
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

' MAIN
' ================================================== ==================
PAUSE 100

INICIO: '01234567890123456789
Lcdout $fe, 1, " DS1820 Teste "
Lcdout $fe, $C0," HUGO OLIVEIRA "
Pause 1000

MAIN:
if Bmenu = 0 then
Pause 200
goto MENU
endif

gosub SENSOR
IF SIGN=1 THEN
Lcdout $fe, $C0, "Temp: -",DEC ABS (TEMPC/10), 223,"C "
else
Lcdout $fe, $C0, "Temp: ",DEC ABS (TEMPC/10), 223,"C "
endif

Goto MAIN

' BUTTON ROUTINE
' ================================================== ==================
MENU:
flag=1
while flag <> 0
pause 100
if flag = 1 then
Lcdout $fe, $1, "Menu 01-OffSet Temp."
if OFFSETTEMP.7 = 0 THEN
Lcdout $fe, $C0,"OffSet: ", DEC abs(OFFSETTEMP)
ELSE
Lcdout $fe, $C0,"OffSet: -", DEC abs(OFFSETTEMP)
ENDIF
gosub PLUSBUTTON
gosub MINUSBUTTON
endif

if Bmenu =0 then
pause 200
if flag = 1 then
flag =0
else
flag = flag + 1
endif
endif
wend
GOTO MAIN

PLUSBUTTON:
if Bmais = 0 then
pause 200
if flag = 1 then
if OFFSETTEMP = 5 then
OFFSETTEMP = 5
else
OFFSETTEMP=OFFSETTEMP + 1
endif
endif
ENDIF
RETURN
MINUSBUTTON:
if BMENOS = 0 then
pause 200
if flag = 1 then
if OFFSETTEMP = 251 then
OFFSETTEMP = 251
else
OFFSETTEMP=OFFSETTEMP - 1
endif
endif
ENDIF
RETURN
' TEMPERATURE ROUTINE
' ================================================== ==================
SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]

temp=abs(OFFSETTEMP)
IF OFFSETTEMP.7=1 then
temp =(temp<<1)
tempc = TEMPC-temp
else
temp =(temp<<1)
tempc = TEMPC+temp
ENDIF

sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
return
END

In the aquisition and temperature conversion routine i change from this:



SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]
sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
return


to this;



SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]

temp=abs(OFFSETTEMP)
IF OFFSETTEMP.7=1 then
temp =(temp<<1)
tempc = TEMPC-temp
else
temp =(temp<<1)
tempc = TEMPC+temp
ENDIF

sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
return
END

The new code and hex file is attached if some one want to give it a try.

Thank you.

scrwld
- 23rd April 2011, 04:14
Hi everyone once again;

I made a program to read a DS1820 showing positive and negative temperatures. I found a piece of code in this forum to handle that, and its workinf fine. Now i want to put an offset in the temperature that was readed, for instance if the sensor is at 18ºC and the OFFSETTEMP variable is 2, then the LCD shows 20ºC, if the OFFSETTEMP variable is -3 than the lcd shows 15ºC.

In the code you will see that i allready have a menu to change the OFFSETTEMP variable, however i don't know now how to interect with the temperature that i'm reading.

Attached is the Proteus file with the simulation of the code, the code, and the HEX file.

The "MENU" button its to enter the the menu, and the "+" and "-" button is to change the value of the OFFSETTEMP variable.

Can somebody give me a clue?

Here is the code:

'************************************************* ***************
'* Name : TESTE DS1820.BAS *
'* Author : GADELHAS *
'* Notice : Copyright (c) *
'* : All Rights Reserved *
'* Date : 23-04-2011 *
'* Version : 1.0 *
'* Notes : BreadBoard + EASYPICV6 *
'* : PIC16F877A *
'************************************************* ***************
DEFINE OSC 20
@ __config _HS_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF & _BODEN_OFF & _PWRTE_OFF
' VARIABLES
' ================================================== ==================

TEMPC Var Word
OFFSETTEMP VAR BYTE
SIGN var bit
FLAG VAR BYTE

' PINOUT 1 = IN; 0 = OUT
' ================================================== ==================
'76543210
TRISA = %00000000
TRISB = %00000000
TRISC = %10010000
TRISD = %00001100
TRISE = %00000000
ADCON1 = 7

' ALIAS
' ================================================== ==================
LED VAR PORTB.7
BMENOS var PORTD.2
BMAIS var PORTD.3
BMENU VAR PORTC.4
OUT Var PORTC.2
DQ VAR PORTE.2

' DEFINITIONS
' ================================================== ==================

DEFINE LCD_DREG PORTB ' LCD Data bits on PORTB
DEFINE LCD_DBIT 0 ' PORTB starting address
DEFINE LCD_RSREG PORTB ' LCD RS bit on PORTB
DEFINE LCD_RSBIT 4 ' LCD RS bit address
DEFINE LCD_EREG PORTB ' LCD E bit on PORTB
DEFINE LCD_EBIT 5 ' LCD E bit address
DEFINE LCD_BITS 4 ' LCD in 4-bit mode
DEFINE LCD_LINES 2 ' LCD has 2 rows
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

' MAIN
' ================================================== ==================
PAUSE 100

INICIO: '01234567890123456789
Lcdout $fe, 1, " DS1820 Teste "
Lcdout $fe, $C0," HUGO OLIVEIRA "
Pause 1000

MAIN:
if Bmenu = 0 then
Pause 200
goto MENU
endif

gosub SENSOR

IF SIGN=1 THEN
Lcdout $fe, $C0, "Temp: -",DEC ABS (TEMPC/10), 223,"C "
else
Lcdout $fe, $C0, "Temp: ",DEC ABS (TEMPC/10), 223,"C "
endif
Goto MAIN

' BUTTON ROUTINE
' ================================================== ==================
MENU:
flag=1
while flag <> 0
pause 100
if flag = 1 then
Lcdout $fe, $1, "Menu 01-OffSet Temp."
if OFFSETTEMP.7 = 0 THEN
Lcdout $fe, $C0,"OffSet: ", DEC abs(OFFSETTEMP)
ELSE
Lcdout $fe, $C0,"OffSet: -", DEC abs(OFFSETTEMP)
ENDIF
gosub PLUSBUTTON
gosub MINUSBUTTON
endif

if Bmenu =0 then
pause 200
if flag = 1 then
flag =0
else
flag = flag + 1
endif
endif
wend
GOTO MAIN

PLUSBUTTON:
if Bmais = 0 then
pause 200
if flag = 1 then
if OFFSETTEMP = 5 then
OFFSETTEMP = 5
else
OFFSETTEMP=OFFSETTEMP + 1
endif
endif
ENDIF
RETURN
MINUSBUTTON:
if BMENOS = 0 then
pause 200
if flag = 1 then
if OFFSETTEMP = 251 then
OFFSETTEMP = 251
else
OFFSETTEMP=OFFSETTEMP - 1
endif
endif
ENDIF
RETURN
' TEMPERATURE ROUTINE
' ================================================== ==================
SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]
sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
return
END
************************************************** *************
hi(hola) gadelhas,
i read you program try this:

SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]
sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
' if OFFSETTEMP = -3 and temperature = 30 tempc=tempc+(-3)=27
' if OFFSETTEMP = 3 and temperature = 30 tempc=tempc+(3)=33
' put this line in you code look the picture
----> Tempc=Tempc+OFFSETTEMP
return

gadelhas
- 23rd April 2011, 19:19
************************************************** *************
hi(hola) gadelhas,
i read you program try this:

SENSOR:
owout DQ,1,[$CC, $44]
Pause 200
owout DQ,1,[$CC, $BE]
OWIn DQ, 2, [TempC.byte0, TempC.byte1]
sign = tempc.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
' if OFFSETTEMP = -3 and temperature = 30 tempc=tempc+(-3)=27
' if OFFSETTEMP = 3 and temperature = 30 tempc=tempc+(3)=33
' put this line in you code look the picture
----> Tempc=Tempc+OFFSETTEMP
return


Hi scrwld ;

it dosen't work like should be.

But still trying.

EDIT:

After trying other things with your line of code, it doesn't work like should be.

However thanks for watching and answering my post.

aratti
- 23rd April 2011, 21:03
Why don't you use two different offsets? If you use N_OffSet for the negative number and P_OffSet for the positive number the operation will be:

LCD_Temperature = Sensor_Temperature + P_OffSet - N_OffSet

You have simply to set to zero the value not used.


Cheers

Al.

gadelhas
- 23rd April 2011, 21:53
Why don't you use two different offsets? If you use N_OffSet for the negative number and P_OffSet for the positive number the operation will be:

LCD_Temperature = Sensor_Temperature + P_OffSet - N_OffSet

You have simply to set to zero the value not used.


Cheers

Al.


I Al,

It's a good idea, but i allready resolved the problem just with one variable, and it works like a charm. You can see my solution in the second post of this thread.

Thanks.