Page 1 of 2

OneWire Temperature Display

Posted: 15 May 2010 19:29
by mousepad
Hi all,

I tried to compile ME's "OneWire Library"
"Mikroelektronika\mikroBasic dsPIC\Examples\LV_24_33\Extra examples\One_Wire"
code just changing the MCU to PIC33FJ128GP710

but it gave "Identifier 'ADPCFG' was not declared " error .Is it ok to compile this code just commenting the error line or what are the necessary changes to Compile and work this

My tools are Lv 24-33 development board with PIC33FJ128GP710, Lcd 2x16 and ds1820,
mikroBasic compiler for Microchip dsPIC 30/33 and PIC 24 microcontrollers Version: 5.0.0.0

Help me please.

Code: Select all

' *
' * Project name:
'     Onewire_Test (Interfacing the DS1820 temperature sensor)
' * Target Platform:
'     dsPIC, PIC24;
' * Copyright:
'     (c) Mikroelektronika, 2006.
' * Revision History:
'     20060512:
'       - Initial release
' * Description:
'     This code demonstrates one-wire communication with temperature sensor DS1820.
'     After reset, dsPIC obtaines temperature from sensor and prints it on LCD.
' * Test configuration:
'     MCU:             PIC24FJ96GA010
'     Dev.Board:       LV 24-33
'     Oscillator:      XT-PLL4, 10.000 MHz
'     Modules:         DS18x20, Lcd_2x16
'     SW:              mikroBasic for dsPIC30/33 and PIC24 v4.0.0.0
' * NOTES:
'     - In order to have valid readings, the 1Wire pin must be connected to
'       pull-up resistor and 1Wire diode turned off! In this example, it is the PORTD.10 pin.
'     - Connect temp sensor to RD10 with jumper j2

program OneWire

' Set TEMP_RESOLUTION to the corresponding resolution of your DS18x20 sensoras
'  18S20as 9  (default setting can be 9,10,11,or 12)
'  18B20as 12
const TEMP_RESOLUTION as byte = 12

dim
  j as byte
  temp as word
  text as string[8]            ' "000.0000"

sub procedure Read_Temperature()
  Ow_Reset(PORTD,10)            ' Onewire reset signal
  Ow_Write(PORTD,10,0xCC)       ' Issue command SKIP_ROM
  Ow_Write(PORTD,10,0x44)       ' Issue command CONVERT_T
  Delay_us(120)

  Ow_Reset(PORTD,10)
  Ow_Write(PORTD,10,0xCC)       ' Issue command SKIP_ROM
  Ow_Write(PORTD,10,0xBE)       ' Issue command READ_SCRATCHPAD
  Delay_ms(400)

  j    = OW_Read(PORTD,10)     ' Get temperature LSB
  temp = OW_Read(PORTD,10)     ' Get temperature MSB
  temp = temp << 8
  temp = temp + j             ' Form the 2byte variable
end sub

sub procedure Write_Temp(dim temp2write as word)
  const
    RES_SHIFT as byte = TEMP_RESOLUTION - 8
  dim
    temp_whole as byte
    temp_fraction as word

  if ((temp and 0x8000) > 0) then
     text[0] = "-"
     temp2write = (not temp2write) + 1
  end if

  temp_whole = temp2write >> RES_SHIFT
  if (temp_whole div 100) then
     text[0] = temp_whole div 100  + 48
  end if

  text[1] =(temp_whole div 10) mod 10 + 48
  text[2] = temp_whole mod 10 + 48

  temp_fraction = temp2write << (4-RES_SHIFT)
  temp_fraction = temp_fraction and 0x000F
  temp_fraction = temp_fraction * 625
  text[4] =  temp_fraction div 1000 + 48
  text[5] = (temp_fraction div 100) mod 10 + 48
  text[6] = (temp_fraction div 10)  mod 10 + 48
  text[7] =  temp_fraction mod 10 + 48

  Lcd_Out(2,1,text)
end sub


' ----Main----
main:

  text   = "000.0000"
  ADPCFG = 0xFFFF

  Lcd_Init_LV_24_33()
  Lcd_Cmd(LCD_CLEAR)
  Lcd_Cmd(LCD_CURSOR_OFF)
  Lcd_Out(1,1,"temperature")

  '--- main loop
  while TRUE
    Read_Temperature()
    Write_Temp(temp)
    Delay_ms(1000)
  wend
end.


Re: OneWire Temperature Display

Posted: 15 May 2010 20:56
by NUFAN
If ADPCFG is a variable, you need to declare it (as word).

Re: OneWire Temperature Display

Posted: 15 May 2010 21:45
by mousepad
But i cant understand why that variable is Need

Re: OneWire Temperature Display

Posted: 15 May 2010 22:08
by NUFAN
I don't know too :) Does it work if you delete this variable?

Re: OneWire Temperature Display

Posted: 15 May 2010 22:10
by rmteo
It is referring to the ADC port configuration register that sets the port pin to Digital mode (1=digital, 0=analog). However, for your device, the name of this SFR may be different. It could be AD1PCFGH or something similar - refer to the ADC chapter of the data sheet and substitute the correct name in your code.

Re: OneWire Temperature Display

Posted: 15 May 2010 22:22
by NUFAN
Name for your SFR is AD1PCFG. Change ADPCFG to AD1PCFG and will work.

Re: OneWire Temperature Display

Posted: 15 May 2010 22:41
by mousepad
Hi Nufan "AD1PCFG" also gave the error , AD1PCFGH is Ok Its compiled Thanks rmteo. but now it displays -00.0625 still there is a problem! :cry:

Re: OneWire Temperature Display

Posted: 15 May 2010 22:49
by NUFAN
I assume that you use DS18B20 sensors. For these you need to set resolution to 9 bit.

Re: OneWire Temperature Display

Posted: 15 May 2010 22:54
by mousepad
my sensor is DS1820

Re: OneWire Temperature Display

Posted: 22 May 2010 08:44
by mousepad
HI
I set the resolution to 9 bit.Now LCD shows some values like
"temperature"
"-34.5000"

when I heat the Sensor the value go down. When I cooled it the value go Up :x
What explains "-" Symbol

Thanks

Re: OneWire Temperature Display

Posted: 22 May 2010 09:02
by NUFAN
The problem is only in "-" now if I understand you? Temperature value is correct, right?

Re: OneWire Temperature Display

Posted: 22 May 2010 09:13
by NUFAN
I think that the problem is here (in Write_Temp function).

Code: Select all

if ((temp and 0x8000) > 0) then
     text[0] = "-"
     temp2write = (not temp2write) + 1
  end if
Temperature value is transferred to the variable temp2write in that function. Change the temp variable to temp2write.

Code: Select all

if ((temp2write and 0x8000) > 0) then
     text[0] = "-"
     temp2write = (not temp2write) + 1
  end if

Re: OneWire Temperature Display

Posted: 22 May 2010 09:43
by mousepad
Dear NUFAN

I changed temp to temp2write,but same results.then I heated the sensor continuously it went about 160.5000 but with out "-" symbol

Any idea

Thanks

Re: OneWire Temperature Display

Posted: 22 May 2010 12:06
by NUFAN
mousepad wrote:I set the resolution to 9 bit
Are you using DS18B20 sensor now? If answer is yes than show us your procedure for setup resolution.

Re: OneWire Temperature Display

Posted: 22 May 2010 13:05
by mousepad
Sensor DALLAS DS1820 not DS18B20

Code: Select all

' *
' * Project name:
'     Onewire_Test (Interfacing the DS1820 temperature sensor)
' * Target Platform:
'     dsPIC, PIC24;
' * Copyright:
'     (c) Mikroelektronika, 2006.
' * Revision History:
'     20060512:
'       - Initial release
' * Description:
'     This code demonstrates one-wire communication with temperature sensor DS1820.
'     After reset, dsPIC obtaines temperature from sensor and prints it on LCD.
' * Test configuration:
'     MCU:             PIC24FJ96GA010
'     Dev.Board:       LV 24-33
'     Oscillator:      XT-PLL4, 10.000 MHz
'     Modules:         DS18x20, Lcd_2x16
'     SW:              mikroBasic for dsPIC30/33 and PIC24 v4.0.0.0
' * NOTES:
'     - In order to have valid readings, the 1Wire pin must be connected to
'       pull-up resistor and 1Wire diode turned off! In this example, it is the PORTD.10 pin.
'     - Connect temp sensor to RD10 with jumper j2

program OneWire

' Set TEMP_RESOLUTION to the corresponding resolution of your DS18x20 sensoras
'  18S20as 9  (default setting can be 9,10,11,or 12)
'  18B20as 12
const TEMP_RESOLUTION as byte = 9

dim
  j as byte
  temp as word
  text as string[8]            ' "000.0000"

sub procedure Read_Temperature()
  Ow_Reset(PORTD,10)            ' Onewire reset signal
  Ow_Write(PORTD,10,0xCC)       ' Issue command SKIP_ROM
  Ow_Write(PORTD,10,0x44)       ' Issue command CONVERT_T
  Delay_us(120)

  Ow_Reset(PORTD,10)
  Ow_Write(PORTD,10,0xCC)       ' Issue command SKIP_ROM
  Ow_Write(PORTD,10,0xBE)       ' Issue command READ_SCRATCHPAD
  Delay_ms(400)

  j    = OW_Read(PORTD,10)     ' Get temperature LSB
  temp = OW_Read(PORTD,10)     ' Get temperature MSB
  temp = temp << 8
  temp = temp + j             ' Form the 2byte variable
end sub

sub procedure Write_Temp(dim temp2write as word)
  const
    RES_SHIFT as byte = TEMP_RESOLUTION - 8
  dim
    temp_whole as byte
    temp_fraction as word

  if ((temp2write and 0x8000) > 0) then
     text[0] = "-"
     temp2write = (not temp2write) + 1
  end if

  temp_whole = temp2write >> RES_SHIFT
  if (temp_whole div 100) then
     text[0] = temp_whole div 100  + 48
  end if

  text[1] =(temp_whole div 10) mod 10 + 48
  text[2] = temp_whole mod 10 + 48

  temp_fraction = temp2write << (4-RES_SHIFT)
  temp_fraction = temp_fraction and 0x000F
  temp_fraction = temp_fraction * 625
  text[4] =  temp_fraction div 1000 + 48
  text[5] = (temp_fraction div 100) mod 10 + 48
  text[6] = (temp_fraction div 10)  mod 10 + 48
  text[7] =  temp_fraction mod 10 + 48

  Lcd_Out(2,1,text)
end sub


' ----Main----
main:

  text   = "000.0000"
  AD1PCFGh = 0xFFFF

  Lcd_Init_LV_24_33()
  Lcd_Cmd(LCD_CLEAR)
  Lcd_Cmd(LCD_CURSOR_OFF)
  Lcd_Out(1,1,"temperature")

  '--- main loop
  while TRUE
    Read_Temperature()
    Write_Temp(temp)
    Delay_ms(1000)
  wend
end.