PDA

View Full Version : Using A Ds18s20 And Servo



CrazyCooter
- 10th September 2007, 12:41
Hi everyone,
I am having difficulties using the ds18s20 and servo together on the pic16f88.
I find that i can't keep the 50Hz req for the servo except when its only running the servo code alone. The ds18s20 is working very well.
What i am hoping to achieve here is when the temp moves either up or down the servo will move to try and maintain the correct temp.
This also needs to be fast as there is more code to come.

Regards
Crazy Cooter

Acetronics2
- 10th September 2007, 13:05
Hi, Cooter

Just find the conversion time of the 18B20 and that's all ...

I committed that some years ago ... the servo opens and closes air vents for a "Corsair" model plane ...

might suit your needs ... I wonder !

Alain

CrazyCooter
- 10th September 2007, 13:40
Thanks ACETRONICS for a speedy response. I will have a look at it tomorrow as its late here in Australia.

Regards
Crazy Cooter

BrianT
- 11th September 2007, 05:26
I have some code that refreshes servos at 50 Hz and reads some DS1620 slow temperature sensors - they take 400 - 1000 mSecs about the same as the DS1820 I think. I do it with a loopcounter and issue the conversion request in one pass then 1000 mSecs later read the result. SOmething like

MainLoop:
LoopCtr + 1
if LoopCtr > 50 then loopCtr = 0
if LoopCtr = 1 then TempConversion
if LoopCtr = 0 then ReadTemperature

ServoCodeHere:
Low ServoPin
Pulsout ServoPin, ServoWidth
Delay to give 20 mSec looptime
Goto MainLoop

TempConversion:
Issue code to start the temperature conversion - don't wait for result
Goto MainLoop

ReadTemperature:
Issue code to read most recent temperature conversion
Goto MainLoop

HTH
Brian

CrazyCooter
- 12th September 2007, 09:41
Thanks Brian your code idea works a treat. So how come yours does and mine doesn't????? I just don't understand why not. The only issue now i have is the servo keeps resetting itself(matter of speach) goes in the direction i want holds for a second then rotates all the way back then rotates back to where it should be. I require the servo to stay put how do i go about this as my code does not do this when in my head it should. what am i doing wrong

the code..



OSCCON = %01101000 'INTRC = 4 MHz
TRISA = %11111111
ANSEL = 0 'DISABLE ALL ADC
CMCON = 7 'DISABLE ALL ANALOG COMPARATOR
WHILE OSCCON.2=0:WEND 'STABILISE THE CLOCK
ADCON1 = 1
DEFINE OSC 4
PAUSE 1000
'-------------------------------------------------------------------------------

'-------------------------------------------------------------------------------
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4 '4,5,6,7
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
pause 1000
lcdout $fe, 1

'-------------------------------------------------------------------------------
ServoWidth var byte
ServoPin Var PORTB.0
ServoWidth = 1500
DQ VAR PORTA.1 ' One-wire data pin
b3 var byte
b3 = 1200
temperature VAR WORD ' Temperature storage
count_remain VAR BYTE ' Count remaining
count_per_c VAR BYTE ' Count per degree C
TEMP VAR WORD
LoopCtr var byte
'-------------------------------------------------------------------------------

MainLoop:

LoopCtr = LoopCtr + 1
if LoopCtr > 50 then loopCtr = 0
if LoopCtr = 1 then TempConversion
IF TEMP > 24 THEN ServoWidth = ServoWidth - 1
IF TEMP < 22 THEN ServoWidth = ServoWidth + 1
if TEMP = 23 THEN ServoWidth = ServoWidth + 0
if LoopCtr = 0 then ReadTemperature

ServoCodeHere:
Low ServoPin
if ServoWidth = 1105 then high ServoPin ' servo stop
if ServoWidth = 1895 then high ServoPin ' servo stop

Pulsout ServoPin, ServoWidth
'Delay to give 20 mSec looptime
PAUSE 18
Goto MainLoop

TempConversion:
'Issue code to start the temperature conversion - don't wait for result
OWOut DQ, 1, [$CC, $44] ' Start temperature conversion
Goto MainLoop

ReadTemperature:
'Issue code to read most recent temperature conversion
DS18S20WAITLOOP: OWIn DQ, 4, [count_remain] ' Check for still busy converting
IF count_remain = 0 Then DS18S20WAITLOOP
OWOut DQ, 1, [$CC, $BE] ' Read the temperature
OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE, Skip 4, count_remain, count_per_c]
temperature = (((temperature >> 1) * 100) - 25) + (((count_per_c - count_remain) * 100) / count_per_c)
TEMP = temperature / 100
LCDOut $fe, 1, DEC (temperature / 100)," C"


Goto MainLoop



Regards
Crazy Cooter