One wire bus (DS1820) Control For Basic PRO Compiler

Include "modedefs.bas" 
@ Device PIC16F877,XT_OSC,WDT_off,PWRT_off,PROTECT_off ,BOD_Off
define  adc_bits 8
define  osc 4
DEFINE  LCD_DREG PORTD
DEFINE  LCD_DBIT 4
DEFINE  LCD_RSREG PORTD
DEFINE  LCD_RSBIT 1
DEFINE  LCD_EREG PORTD
DEFINE  LCD_EBIT 0
low     portD.2 

trisa  = $ff
trisb  = $00
TRISD  = $00

adcon1 = 7

temperature   VAR WORD  
count_remain  VAR BYTE  
count_per_c   VAR BYTE  
sV            var byte
PV            var byte
mode          var porta.0
up            var porta.1
down          var porta.2
DQ            VAR PORTA.3 ' One-wire data pin
heater        VAR PORTB.0

High heater
sV =  25 
 LCDOut $fe, 1,"  TEMPS  SENSOR  "
 LCDOut $fe, $c0,"DS1820 1WIRE BUS" 
pause 2000
main: 
 gosub read_pv
 gosub calculate
 gosub display
 read 0,sv
 if mode = 0 then pause 200 : gosub set_temp
 pause 10
GoTo main  
End

read_pv:   
 OWOut DQ, 1,[$CC, $44]  
loop: 
 OWIn DQ, 4, [count_remain]  
 IF count_remain = 0 Then loop 
 OWOut DQ, 1, [$CC, $BE]  
 OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE, Skip 4, count_remain, count_per_c]
 temperature = (((temperature >> 1) * 100) - 25) + (((count_per_c - count_remain) * 100) / count_per_c)
 LCDOut $fe, 1, DEC (temperature / 100), ".", DEC2 temperature, " C"
 PV =  (temperature / 100)
 return

 calculate: 
 If (PV >= (SV - 1/2))  then   
  gosub display
  write 0,sv 
  high heater 'off heater 
  Pause 100
  endif  
If (PV < sV ) then    
  gosub display
  write 0,sv    
  low heater 'on  heater 
  Pause 100
  endif
return     

display:
 LCDOut $fe, 1,"PV=", DEC (temperature / 100)+1,"C"
 LCDOut $fe, $c0,"SV=" ,dec SV,"C" 
 pause 10
 return 

set_temp:
 LCDOut $fe, 1,"settemp "
 pause 2000
 LCDOut $fe, 1 
 pause 1000
loop2:
if mode = 1 then  
IF (Up=0)THEN
  PAUSE 100
  SV = SV+1
  IF (SV>=100)THEN SV = 100
  write 0,sv  
ENDIF

IF(DOWn=0)THEN
  PAUSE 100
  SV = Sv-1
  IF(SV<=0)THEN SV = 0
  write 0,sv 
ENDIF
pause 100
LCDOut $fe, 1,"setpoint temp "
LCDOut $fe, $c0,"SV=", dec SV,"C" 
goto loop2
endif
pause 100
goto main
RETURN

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter