DS18B20

Discuss the Firewing language

DS18B20

Postby AndrewB » Sun Feb 28, 2016 7:21 pm

I have multiple DS18B20 on the 1wire and sometime these may be removed or changed so having fixed ID's will not work.
The routines I use are below and work until I use an interrupt with an analogue sensor check.
As soon as the analogue signal changes too fast it effects the routines and devices can change to more or less than on the bus.
Any suggestions.

It starts main with Getsensors and then Getsensorvalues.
it check \getsensorvalues in a while loop with a check of Getsensors every 58 seconds.
If the alanlogue level is changing fast in the 58 sec mark then var devices is incorrect.

Sub OnDeviceFound(command As Byte, family As Byte, ByRef romID() As Byte, ByRef abort As Boolean) Handles OW.OnSearch
Console.Write(13,10)
if devices = 0 then
romID1 = romID
Console.Write("romID1 : ")
for index as byte = 0 to ubound(romID1)
Console.Write("&H", Hex(romID1(index),2)," ")
next
end if
if devices = 1 Then
romID2 = romID
Console.Write("romID2 : ")
for index as byte = 0 to ubound(romID2)
Console.Write("&H", Hex(romID2(index),2)," ")
next

end if
if devices = 2 Then
romID3 = romID
Console.Write("romID3 : ")
for index as byte = 0 to ubound(romID3)
Console.Write("&H", Hex(romID3(index),2)," ")
next
end if
devices = devices + 1

End Sub

Sub Getsensors()
OW.Search()
if devices > 0 then
DS18B20_Available = True
end if
Console.Write(Str(devices))
If DS18B20_Available Then
DS18B20.SetResolution(Resolution.Is9Bit)
end if
End sub

Sub Getsensorvalues()
if DS18B20_Available and devices >=1 then
'Get Led Temp1
DS18B20.ROMID = romID1
DS18B20.SetResolution(Resolution.Is9Bit)
DS18B20.Convert()
DS18B20.GetTemp(Temp1,TempFrac)
end if
'Get Abient Temp2
if DS18B20_Available and devices >=2 then
DS18B20.ROMID = romID2
DS18B20.SetResolution(Resolution.Is9Bit)
DS18B20.Convert()
DS18B20.GetTemp(Temp2,TempFrac)
end if
'Get Abient Temp3
if DS18B20_Available and devices >=3 then
DS18B20.ROMID = romID3
DS18B20.SetResolution(Resolution.Is9Bit)
DS18B20.Convert()
DS18B20.GetTemp(Temp3,TempFrac)
end if
End Sub
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: DS18B20

Postby Jerry Messina » Sun Feb 28, 2016 10:04 pm

The issue you're having here is probably related to your other post (timers and PWM).

The OneWire bus protocol is very timing sensitive and can get upset if there's stuff going on in the background (like interrupts) that interfere with the timing.
You could probably get away with some very simple interrupt handlers but the timing in the OW.bas module is generated using software-based 'delayus()' calls and there's not a lot of leeway.

The simplest thing? Shut off the interrupts while you're doing OW stuff. If you really need to you could generate the OW interface using a UART, but that's a lot more involved.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: DS18B20

Postby AndrewB » Sun Feb 28, 2016 10:48 pm

How do I stop timers.bas? Is is just a .stop?

The DS1307 also has a problem in the same situation.
Saving data into the DS1307 memory location 1 and 2 and reading seems to be a problem too.
I'm guessing that stopping and starting the timer interrupt would help here.

if you could explain how I may use the Uart as a 1wire that would be great.
I could use a Maxim DS2480B
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: DS18B20

Postby Jerry Messina » Mon Feb 29, 2016 11:18 am

You can enable/disable the timer interrupt by using Timers.Start() and Timers.Halt()

If you're having issues with the DS1307 as well then there may be more to this. That chip uses an I2C interface and normally that's not timing sensitive at all.
What, exactly, do you have in the timer OnTimeout subroutine that handles Timers.OnTimeout?
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: DS18B20

Postby AndrewB » Mon Feb 29, 2016 11:50 am

Sub OnTimeout(ByRef timer As TimerItem) Handles Timers.OnTimeout
Dim Item As TimerItem = timer ' create a copy for more efficient processing
Select Item.ID ' get the timer ID
Case 0 : Item.Interval = 50 : Getsensor2
End Select

' re-enable the timer and copy back into the timer
' which has been passed by reference...
Item.Enabled = True
timer = Item
End Sub

sub Getsensor2()
save (pwmcontrol)
dim sensor2 as ushort = Adc.Read(A2) ' read sensor
'Set output PWM
if sensor2 <= pwmcontrol then
Dogm.SetBacklight(sensor2)
else
Dogm.SetBacklight(pwmcontrol)
end if
restore
end sub
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: DS18B20

Postby David John Barker » Mon Feb 29, 2016 12:46 pm

By default, I2C is bit bashed to provide Arduino pin compatibility. So again, timing issue is more than likely. Given that refreshing isn't time critical (that is, within a few ms) I would just either

(1) check the timer in my main code block for expiry or
(2) just set a flag in the timeout ISR, then process in my main loop. For example,

Code: Select all
Sub OnTimeout(ByRef timer As TimerItem) Handles Timers.OnTimeout
   Dim Item As TimerItem = timer ' create a copy for more efficient processing
   Select Item.ID ' get the timer ID
   Case 0 : Item.Interval = 50 : NeedToGetSensor2 = true
   End Select

   ' re-enable the timer and copy back into the timer
   ' which has been passed by reference...
   Item.Enabled = True
   timer = Item
End Sub

Sub Main()
   ...
   while true
      if NeedToGetSensor2 then
         ' halt isrs...
         NeedToGetSensor2 = false
         GetSensor2()
        ' enable isrs...
      end if
   end while
end sub
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: DS18B20

Postby Jerry Messina » Mon Feb 29, 2016 12:56 pm

Off the top of my head it looks like that would be ok, so I don't know what the conflict might be.

I looked at using the UART to do the OW interface, and unfortunately the default OW pin (A3) is RB2 on the pic24HJ128GP502,
and that one can't be set to open-drain output which is what you would need to use it without any addtl hardware. Bummer.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: DS18B20

Postby AndrewB » Mon Feb 29, 2016 2:40 pm

Thanks I will add that version of the timer.

I really need to have the analogue to pwm conversion to run smoothly. The problem I guess is that the polling of the 1wire and logging the three sensors takes a fair bit of time.

\Do you have any suggestions on how to improve the 1wire multiple sensor logging?

Thanks
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: DS18B20

Postby AndrewB » Mon Feb 29, 2016 4:23 pm

OK, I've kept to my original setup with the timer checking analogue and updating the PWM as this is the smoothest route.
Added the Timers.Halt() and Timers.start() before and after the DS1307 memory read&write cmds and the OW.search().
The data for the DS1307 is good no loss.
Lowered the timer to 20 with a really smooth conversion.
The sensors are reading well with no problems so far.

It did seem to reset once when really hammering the analogue input up and down but has not since.
Not sure what happened but the display went blank the backlight went off then all went back to normal?

Do you have any views on the reading of the 1wire sensors as I guess my routine is pretty basic.
AndrewB
 
Posts: 94
Joined: Thu Jan 02, 2014 3:38 pm

Re: DS18B20

Postby Jerry Messina » Mon Feb 29, 2016 4:35 pm

David's method is probably the simplest, but if you want to try with your method you might want to see if changing the Save()/Restore() helps any...
Code: Select all
Sub OnTimeout(ByRef timer As TimerItem) Handles Timers.OnTimeout
  save (Getsensor2)

  Dim Item As TimerItem = timer ' create a copy for more efficient processing
  Select Item.ID ' get the timer ID
  Case 0 : Item.Interval = 50 : Getsensor2
  End Select

  ' re-enable the timer and copy back into the timer
  ' which has been passed by reference...
  Item.Enabled = True
  timer = Item
 
  Restore
End Sub

sub Getsensor2()
 dim sensor2 as ushort = Adc.Read(A2) ' read sensor
 'Set output PWM
 if sensor2 <= pwmcontrol then
   Dogm.SetBacklight(sensor2)
 else
   Dogm.SetBacklight(pwmcontrol)
 end if
 end sub


There's quite a number of things getting clobbered in the process of doing Getsensor2. I'm not sure if that's enough to protect it or not.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Next

Return to Language

Who is online

Users browsing this forum: No registered users and 2 guests

cron

x