(HCMODU0013 or HCMODU0038) to display the value of the a DS18B20 sensor to a serial terminal using the hardware module for RS232 communications.
Code: Select all
' FILE: PIC_GCB_16F877A_DS18B20_with_HardwareSerial_Example.gcb
' DATE: 07/08/14
' VERSION: 0.1a
' AUTHOR: Anobium
'
'This program was written using the Great Cow Basic IDE for use with the PIC
'16F8777A development board (HCDVBD0003) and a 1602 parallel LCD module
'(HCMODU0013 or HCMODU0038) to display the value of the a DS18B20 sensor to a serial terminal using the hardware module for RS232 communications.
'Chip Settings. Assumes the development board with a 16F877A
#chip 16F877A,4
#include <DS18B20.h>
'USART settings
#define USART_BAUD_RATE 9600
Dir PORTC.6 Out
Dir PORTC.7 In
#define USART_DELAY 1 ms
#define USART_BLOCKING
' DS18B20 port settings - this is required
#define DQ PortC.3
dim TempC_100 as word ' a variabler to handle the temperature calculations
ccount = 0
do forever
' The function readtemp returns the integer value of the sensor
DSdata = readtemp
' Display the integer value of the sensor on the Terminal, ANSI codes.
HSerPrint hex(ccount)
HSerPrint " Ceil"
HSerSend 9
HSerPrint DSdata
HSerPrint chr(223)+"C"
HSerPrintCRLF (1)
' Display the integer and decimal value of the sensor on the terminal
' The function readtemp12 returns the raw value of the sensor.
' The sensor is read as a 12 bit value therefore each unit equates to 0.0625 of a degree
DSdata = readtemp12
SignBit = DSdata / 256 / 128
If SignBit = 0 Then goto Positive
' its negative!
DSdata = ( DSdata # 0xffff ) + 1 ' take twos comp
Positive:
' Convert value * 0.0625. Mulitple value by 6 then add result to multiplication of the value with 25 then divide result by 100.
TempC_100 = DSdata * 6
DSdata = ( DSdata * 25 ) / 100
TempC_100 = TempC_100 + DSdata
Whole = TempC_100 / 100
Fract = TempC_100 % 100
If SignBit = 0 Then goto DisplayTemp
HSerPrint "-"
DisplayTemp:
HSerPrint hex(ccount)
HSerPrint " Real"
HSerSend 9
HSerPrint str(Whole)
HSerPrint "."
' To ensure the decimal part is two digits
Dig = Fract / 10
HSerPrint Dig
Dig = Fract % 10
HSerPrint Dig
HSerPrint chr(223)
HSerPrint "C"
HSerPrintCRLF (2)
wait 2 s
ccount++
loop