Ok, I found the example on the EasyPIC5 CD (I know it should of been the first place to look, but as it was called one-wire and not temp sensor you can understand why I missed it - I said I was rusty !)
This works and gives a real temp of 25 degree C which rises when I touch the sensor. I apologise now as the following is not PBP - but hopefully someone can tell me why I was having so much trouble with PBP
Code:
' *
' * Project name
' Onewire_Test (Interfacing the DS18x20 temperature sensor - all versions)
' * Copyright
' (c) MikroElektronika, 2005-2008
' * Description
' After reset, PIC reads temperature from the sensor and prints it on the Lcd.
' The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
' results, the 18x20's temperature resolution has to be adjusted (constant
' TEMP_RESOLUTION)
' * Test configuration
' MCU PIC16F887
' Dev.Board EasyPIC5
' Oscillator HS, 08.0000 MHz
' Ext. Modules DS18x20 connected to RE2 pin, LCD_2x16
' SW mikroC v8.0
' * NOTES
' - Pull up and turning off diode on pin used for one wire bus may be required.
' *
program Onewire_Test
' Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor
' 18S20 9 (default setting can be 9,10,11,or 12)
' 18B20 12
const TEMP_RESOLUTION as byte = 9
dim text as string[8]
temp as word
sub procedure Display_Temperature(dim temp2write as Word)
const RES_SHIFT as byte = TEMP_RESOLUTION - 8
dim temp_whole as byte
temp_fraction as Word
text = "000.0000"
' check if temperature is negative
if (temp2write and $8000) = 0x8000 then
text[0] = "-"
temp2write = not temp2write + 1
end if
' extract temp_whole 0
temp_whole = temp2write >> RES_SHIFT
' convert temp_whole to characters
if (temp_whole/100) then
text[0] = temp_whole/100 + 48
end if
text[1] = (temp_whole/10) mod 10 + 48 ' extract tens digit
text[2] = temp_whole mod 10 + 48 ' extract ones digit
' extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT)
temp_fraction = temp_fraction and $000F
temp_fraction = temp_fraction * 625
' convert temp_fraction to characters
text[4] = temp_fraction/1000 + 48 ' extract thousands digit
text[5] = (temp_fraction/100) mod 10 + 48 ' extract hundreds digit
text[6] = (temp_fraction/10) mod 10 + 48 ' extract tens digit
text[7] = temp_fraction mod 10 + 48 ' extract ones digit
' print temperature on LCD
Lcd_Out(2, 5, text)
end sub'~
main:
ANSEL = 0 ' Configure AN pins as digital I/O
ANSELH = 0
Lcd_Config(PORTB,3,2,1,0,PORTB,4,7,5) ' Lcd_Init_EP5, see Autocomplete
Lcd_Cmd(LCD_CURSOR_OFF)
Lcd_Out(1, 1, " Temperature ")
' Print degree character, 'C' for Centigrades
Lcd_Chr(2,13,223)
Lcd_Chr(2,14,"C")
'--- main loop
while TRUE
'--- perform temperature reading
Ow_Reset(PORTE,2) ' Onewire reset signal
Ow_Write(PORTE,2,$CC) ' Issue command SKIP_ROM
Ow_Write(PORTE,2,$44) ' Issue command CONVERT_T
Delay_us(120)
Ow_Reset(PORTE,2)
Ow_Write(PORTE,2,$CC) ' Issue command SKIP_ROM
Ow_Write(PORTE,2,$BE) ' Issue command READ_SCRATCHPAD
temp = Ow_Read(PORTE,2)
temp = (Ow_Read(PORTE,2) << 8) + temp
'--- Format and display result on Lcd
Display_Temperature(temp)
Delay_ms(500)
wend
end.
Maybe its time to learn a new language
Bookmarks