Shop OBEX P1 Docs P2 Docs Learn Events
Checking Code — Parallax Forums

Checking Code

ArchiverArchiver Posts: 46,084
edited 2004-01-16 06:56 in General Discussion
Hi, im new in this.

Can you guys give me some second opinion on my code, maybe the logic
is flawed or even buggy.
What im doing is basically a temperature fan control.

If temperature crosses a certain limit, case fans will automatically
on, and if temp goes down to a certain limit, fans will off. This is
done in a loop. And there's a LED that lights up whenever the fans
turn off/on.

Here's the code:

'{$STAMP BS2}
' Program Info:
' 1) Programming the EEPROM DS1620 Digital Thermometer chip .
' 2) Automatic control of the cooling fans, constant reading of the
DS1620 IC and
' turns it on/off in accordance to the temperature.
'
'
'

' ===================== Define Pins and Variables ================
DQ CON 10 ' Pin 10 => DQ.
CLK CON 9 ' Pin 9 => CLK.
RST CON 8 ' Pin 8 => RST (high = active).
LED CON 2 ' Pin 2 => LED
Relay CON 3 ' Pin 3 => Relay
DSdata VAR Word ' Word variable to hold 9-bit data.
Sign VAR DSdata.BIT8 ' Sign bit of raw temperature data.
T_sign VAR Bit ' Saved sign bit for converted
temperature.

' ===================== Define DS1620 Constants ===================

' >>> Constants for configuring the DS1620
Rconfig CON $AC ' Protocol for 'Read Configuration.'
Wconfig CON $0C ' Protocol for 'Write Configuration.'
CPU CON %10 ' Config bit: serial thermometer mode.
NoCPU CON %00 ' Config bit: standalone thermostat
mode.
OneShot CON %01 ' Config bit: one conversion per
start request.
Cont CON %00 ' Config bit: continuous conversions
after start.
' >>> Constants for serial thermometer applications.
StartC CON $EE ' Protocol for 'Start Conversion.'
StopC CON $22 ' Protocol for 'Stop Conversion.'
Rtemp CON $AA ' Protocol for 'Read Temperature.'
' >>> Constants for programming thermostat functions.
RhiT CON $A1 ' Protocol for 'Read High-Temperature
Setting.'
WhiT CON $01 ' Protocol for 'Write High-
Temperature Setting.'
RloT CON $A2 ' Protocol for 'Read Low-Temperature
Setting.'
WloT CON $02 ' Protocol for 'Write Low-Temperature
Setting.'


' ===================== Begin Program ============================
LOW RST ' Deactivate '1620 for now.
HIGH CLK ' Put clock in starting state.
PAUSE 100 ' Let things settle down a moment.
HIGH RST ' Activate the '1620 and set it for
continuous..
SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]Wconfig,CPU+Cont] ' ..temp conversions.
LOW RST ' Done--deactivate.
PAUSE 50 ' Wait for the EEPROM
to self-program.
HIGH RST ' Now activate it again
and send the..
SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]StartC] ' Send start-conversion
protocol.
LOW RST ' Done--deactivate.

' The loop below continuously reads the latest temperature from
' the DS1620.

again:
PAUSE 1000 ' Wait a second between
readings.
HIGH RST ' Activate the '1620.
SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]Rtemp] ' Request to read temperature.
SHIFTIN DQ,CLK,LSBPRE,[noparse][[/noparse]DSdata\9] ' Get the temperature reading.
LOW RST
T_sign = Sign ' Save the sign bit of the
reading.



DSdata = (DSdata / 2) ' Convert the data to degrees C.
IF DSdata >= 45 THEN ONFAN ' Jump to ONFAN if temp is high.
IF DSdata <= 29 THEN OFFFAN ' Jump to OFFFAN if temp is
bearable.




DEBUG SDEC DSdata," degrees C",CR ' Show signed temperature in C.
PAUSE 1000
GOTO again



ONFAN:

DEBUG SDEC DSdata," degrees C",CR ' Show signed temperature in C.

HIGH LED
LOW LED
HIGH Relay

GOTO again

OFFFAN:

DEBUG SDEC DSdata," degrees C",CR ' Show signed temperature in C.

LOW LED
HIGH LED
LOW Relay

GOTO again

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-01-16 06:56
    Have you tried the code that you are asking our opinion about? If
    so, what happened that leads you to believe it might be buggy or
    flawed? What did you try in order to isolate the problem?

    -- Tracy

    >Can you guys give me some second opinion on my code, maybe the logic
    >is flawed or even buggy.
Sign In or Register to comment.