
;                            FILE COUNTER1.ASM
;
;              Sample Program Demonstrating Setup and Use of
;              a Timer and Counter Using Timers 0 and 1.
;              Timer 0 is used as a Timer in Mode 1 to generate
;              pulses on Port 1.0.  Timer 1 is used as a counter
;              to count the number of pulses in a fixed time
;              interval.
;
;                     Written By S.C.T.  1/23/99

  $MOD52                        ; Include 8052 Definitions
  
  SendChar      EQU     3F06h   ; Entry Point of Monitor Routine
  SendStr       EQU     3F09h   ; Entry Point of Monitor Routine

  TimerLow      EQU     0FEh    ; Setup Timer to Gate Counter for 
  TimerHigh     EQU     4Bh     ; a 50 millisecond window
                                ; (NOTE-We are not correcting for
                                ; jump routine latency so this is
                                ; an approximate value)

  CounterLow    EQU     00h     ; Reset Counter Count Value 
  CounterHigh   EQU     00h

  ORG  4000h                    ; Start Program at SDK Code Space Base

  MOV   SP,#7Fh                 ; Move Stack to Upper RAM (8052 ONLY)
  CLR   EA                      ; Disable Global Interrupts
  MOV   TMOD, #51h              ; Set Timer 0 in Mode 1 as a Timer
                                ; and Timer 1 in Mode 1 as a Counter
  SETB  P3.5                    ; Set Timer 1 input

startmainloop:
  MOV   TL0, #TimerLow          ; Load Timer 0 for 50 millisecond pulses
  MOV   TH0, #TimerHigh

  MOV   TL1, #CounterLow        ; Load Counter setup Value
  MOV   TH1, #CounterHigh

  CLR   TF0                     ; Clear Timer 0 Overflow flag
  CLR   TF1                     ; Clear Timer 1 Overflow flag

  SETB  TR0                     ; Start Timer 0
  SETB  TR1                     ; Start Counter

loop1:
  JB    TF0,measurecount        ; Loop Until Timeout
  JMP   loop1                   ; repeat loop until bit set

measurecount:
  CLR   TR1                     ; Stop Count
  CLR   TR0                     ; Stop Timer
  CLR   TF0                     ; Clear Timeout Flag
  JB    TF1,countoverflow       ; Jump if Counter Overflowed

  MOV   DPTR,#CRLF              ; Send a CR/LF
  CALL  SENDSTR

  MOV   A, TH1                  ; Retrieve Count High Byte
  CALL  hex2ascii               ; Send Count High Byte

  MOV   A, TL1                  ; Retrieve Count Low Byte
  CALL  hex2ascii               ; Send Count Low Byte

  CALL  waitabit                ; Call Time Delay Routine

  JMP   startmainloop           ; Repeat Main Loop Forever


countoverflow:
  MOV   DPTR,#OVMessage         ; Point to Overflow Message
  CALL  SENDSTR                 ; Print It
  JMP   startmainloop           ; Go Back to Main Loop

;  *******************   HEX2ASCII Routine   *******************
;  This routine converts a hex value in the accumulator to ASCII
;  then sends it out using the monitor sendchar routine
;  *************************************************************

hex2ascii:
  PUSH ACC                      ; Save accumulator on stack
  SWAP A                        ; Swap Nibbles
  ANL  A,#0Fh                   ; Mask out LSB
  MOV  DPTR,#HexTable           ; Point to Conversion Table
  MOVC A,@A+DPTR                ; Retrieve Byte
  CALL SENDCHAR                 ; Send First Character

  POP   ACC                     ; Retrieve Accumulator from stack
  ANL  A,#0Fh                   ; Mask out LSB
  MOV  DPTR,#HexTable           ; Point to Conversion Table
  MOVC A,@A+DPTR                ; Retrieve Byte
  CALL SENDCHAR                 ; Send Second Character

  RET                           ; Return to Calling Code
; --------------------------------------------------------


;  *******************    WAITABIT Routine   *******************
;  This routine uses a nested loop to generate a time delay
;  *************************************************************

WaitABit:
  MOV   R0,#04h
delay1:
  MOV   R1,#0FFh
delay2:
  MOV   R2,#0FFh
delay3:
  DJNZ  R2,delay3
  DJNZ  R1,delay2
  DJNZ  R0,delay1

  RET                   ; Return to Calling Code
; -----------------------------------------------------------


; ******************  TEXT AND LOOKUP TABLES ****************

hextable:                       ; Hex2ASCII Lookup Table
  DB    30h,31h,32h,33h,34h,35h,36h,37h,38h,39h
  DB    41h,42h,43h,44h,45h,46h

OVMessage:
  DB    0Dh,0Ah,' Data Counter Overflow ',0Dh,0Ah,'$'

CRLF:
  DB    0Dh,0Ah,'$'

  END
