1  | program DS18B20_Test
  | 
2  | 
  | 
3  | dim LED as sbit at RA4_Bit
  | 
4  | dim OneWire as sbit at RA5_Bit
  | 
5  | 
  | 
6  | sub procedure InitHardware
  | 
7  |     PORTA = %00000000         'Reset all IO bits
  | 
8  |     TRISA  = %00101010        'Select inputs/outputs
  | 
9  |     SLRCONA = %00000000       'Disable slewrate limit
  | 
10  |     'TRISA.0  = 0             'RA0 = Output = TX
  | 
11  |     'TRISA.1  = 1             'RA1 = Input = RX
  | 
12  |     'TRISA.2  = 0             'RA2 = Output = Reserved
  | 
13  |     'TRISA.3  = 1             'RA3 = Input = Reserved
  | 
14  |     'TRISA.4  = 0             'RA4 = Output = LED
  | 
15  |     'TRISA.5  = 1             'RA5 = Input = 1 Wire
  | 
16  |     'TRISA.6  = 0             'Not implemented
  | 
17  |     'TRISA.7  = 0             'Not implemented
  | 
18  |     ANSELA  = %00000000       'All pins are digital
  | 
19  | end sub
  | 
20  | 
  | 
21  | sub procedure ReadTemperature
  | 
22  |     dim temp as byte
  | 
23  |     Ow_Reset(PortA, 5)        'Reset Onewire
  | 
24  |     Ow_Write(PortA, 5, 0xCC)  'Send SKIP_ROM
  | 
25  |     Ow_Write(PortA, 5, 0x44)  'Send CONVERT_T
  | 
26  |     Delay_ms(750)             'Wait 750ms for conversion end
  | 
27  |     Ow_Reset(PortA, 5)        'Reset Onewire
  | 
28  |     Ow_Write(PortA, 5, 0xCC)  'Send SKIP_ROM
  | 
29  |     Ow_Write(PortA, 5, 0xBE)  'Send READ_SCRATCHPAD
  | 
30  |     temp = Ow_Read(PortA, 5)  'Read first byte
  | 
31  |     UART1_Write(temp)         'Write to UART
  | 
32  |     temp = Ow_Read(PortA, 5)  'Read second byte
  | 
33  |     UART1_Write(temp)         'Write to UART
  | 
34  | end sub
  | 
35  | 
  | 
36  | main:
  | 
37  |      InitHardware             'Setup the hardware
  | 
38  |      UART1_Init(19200)        'Setup the UART
  | 
39  |      while 1                  'Mainloop
  | 
40  |            clrwdt             'Clear watchdog timer
  | 
41  |            LED = LED xor 1    'Toggle LED
  | 
42  |            delay_ms(250)      'Wait 1 second (~750ms from DS18B20)
  | 
43  |            ReadTemperature    'Read temperature
  | 
44  |      wend
  | 
45  | end.
  |