Simple loop (reading temperature - MC68HC)

134 Views Asked by At

I was reading a chapter related to I/O using the MC68HC11; this book showed a suggested exercise (not really a hard one) but i was not able to solve it by using assembly:

image

I've been thinking and i can do it by using some basic logic (i do program in C and C++ ) but i got stuck while trying to do it in assembly.

Logic goes like this:

Loop:

value = ReadSensorValue()

if (value condition 1) // do action else if (value condition 2) // do something end if

go to Loop

Can help me to solve it but using real instructions in assembly?



EDIT:

ERROR

ERROR2

1

There are 1 best solutions below

14
tonypdmtr On BEST ANSWER

Here's one way to do it (using ASM11). I understand you're trying to learn but you don't show any effort. (This is the last one I'm doing without seeing some effort from you first.)

;*******************************************************************************
; MCU specific
;*******************************************************************************

REGS                equ       $1000               ;register base
PORTB               equ       REGS+$04            ;port B (output only)
PORTC               equ       REGS+$03            ;port C
STACKTOP            equ       $01FF               ;Top of Stack
ROM                 equ       $F800               ;beginning of ROM
Vreset              equ       $FFFE               ;reset vector

;*******************************************************************************
; Application specific
;*******************************************************************************

TEMPERATURE         equ       PORTC               ;temperature is available here

CONTROL             equ       PORTB
HEATER.             equ       1                   ;bit that controls heater
COMPRESSOR.         equ       2                   ;bit that controls cooler

MIN_TEMP            equ       20                  ;min allowed temp in C
MAX_TEMP            equ       22                  ;max allowed temp in C

;*******************************************************************************
                    org       ROM
;*******************************************************************************

;*******************************************************************************
; Purpose: Get temperature as Centigrade degrees
; Input  : None
; Output : A = temperature in whole degrees (fractional part discarded)
; Note(s): Formula is TEMPERATURE*5/10 using integer arithmetic
;        : Simplifies to TEMPERATURE/2

GetTemperature      proc
                    ldaa      TEMPERATURE         ;A = temperature
                    lsra                          ;A = temperature/2
                    adca      #0                  ;(optional) round up
                    rts

;*******************************************************************************

CoolIt              proc
                    pshx
                    ldx       #CONTROL
                    bclr      ,x,HEATER.
                    bset      ,x,COMPRESSOR.
                    pulx
                    rts

;*******************************************************************************

HeatIt              proc
                    pshx
                    ldx       #CONTROL
                    bclr      ,x,COMPRESSOR.
                    bset      ,x,HEATER.
                    pulx
                    rts

;*******************************************************************************

AllOff              proc
                    pshx
                    ldx       #CONTROL
                    bclr      ,x,COMPRESSOR.|HEATER.
                    pulx
                    rts

;*******************************************************************************

Start               proc
                    lds       #STACKTOP
                    bsr       AllOff

Loop@@              bsr       GetTemperature      ;A = temperature in degrees

                    cmpa      #MIN_TEMP           ;if below minimum
                    blo       HeatIt@@            ; go heat it up

                    cmpa      #MAX_TEMP           ;if above maximum
                    bhi       CoolIt@@            ; go cool it down

                    bsr       AllOff              ;if within range turn all off
                    bra       Loop@@              ;go check temperature again

CoolIt@@            bsr       CoolIt
                    bra       Loop@@              ;go check temperature again

HeatIt@@            bsr       HeatIt
                    bra       Loop@@              ;go check temperature again

;*******************************************************************************
                    org       Vreset
                    dw        Start
;*******************************************************************************