* Filename      : MAIN.ASM
* Programmer    : Michael Hattermann
* Date          : February 22, 2002
* Version       : 1.0
* Description   : This file contains the main routine to
*                   control the rest of the robot.  It
*                   includes all the other control files.
*                   The followong functions are available:
*
*                   MAIN - start of program, inits/uses systems
*                   MAINOUT - end of program, kills systems
*

#include "hc12.asm"

*
************************************************************
* Main Equates
************************************************************
*
PROGSTART       EQU         $B000       ; start of the program
STACKPTR        EQU         $0A00       ; bottom of internal RAM for stack
GLBLVARS        EQU         $0900       ; top of internal RAM for global variables

OAPROCRATE      EQU         10          ; how often to execute object avoidance (in ms)
FPROCRATE       EQU         10          ; how often to execute following (in ms)
*
************************************************************
* Global Variables
************************************************************
*
                ORG     GLBLVARS
UPPERTIMER      DC.W    $0000           ; 16-bit extension of TCNT
LEFTSPDTIME     DC.W    $0000           ; Time left break beam was broken
RIGHTSPDTIME    DC.W    $0000           ; Time right break beam was broken
SPDTIMEFLG      DC.B    $00             ; Flags to indicate which beams were broken
SPDCAUGHT       DC.B    $00             ; Flags to indicate speeder caught
FLASHTIMER      DC.B    $00             ; Timer for flashing lights
FLASHPREV       DC.B    $00             ; Previous status of lights
SIRENFREQ       DC.W    $0000           ; Frequency of the siren
SIRENTIMER      DC.W    $0000           ; Timer for siren

OLEFTSPD        DC.W    $0000           ; current speed of left motor
NLEFTSPD        DC.W    $0000           ; next speed of left motor
ORIGHTSPD       DC.W    $0000           ; current speed of right motor
NRIGHTSPD       DC.W    $0000           ; next speed of right motor

MAXFOWARDSPD    DC.W    $0000           ; maximum foward speed for motors
MAXBACKSPD      DC.W    $0000           ; maximum reverse speed for motors
LVALUE          DC.B    $00             ; value of left IR
LREACT          DC.B    $00             ; reaction to left IR value
RVALUE          DC.B    $00             ; value of right IR
RREACT          DC.B    $00             ; reaction to right IR value
CVALUE          DC.B    $00             ; value of center IR
CREACT          DC.B    $00             ; reaction to center IR value
PREVRAND        DC.B    $00             ; previous random turning direction
PREVREACT       DC.B    $00             ; previous reaction value
CURRMAN         DC.W    $0000           ; current manuever being performed

LFVALUE         DC.B    $00             ; left follow sensor value
CFVALUE         DC.B    $00             ; center follow sensor value
RFVALUE         DC.B    $00             ; right follow sensor value
FTBLIDX         DC.B    $00             ; index into reaction table
LASTFOLLOW      DC.B    $00             ; last follow direction
OLDCFAVG        DC.W    $0000           ; old average of center follow sensor values
NEWCFAVG        DC.W    $0000           ; new average of center follow sensor values
AVGCNT          DC.B    $0000           ; number of items in new average

BUMPVALUE       DC.B    $00             ; A/D bumper value from wait function

*
************************************************************
* Pseudointerrupt vectors
************************************************************
*
            ORG     T0_PVECT
            JMP     LEFTSTS

            ORG     T1_PVECT
            JMP     RIGHTSTS

            ORG     TMR_OVER_PVECT
            JMP     TIMEEXT

            ORG     RTI_PVECT
            JMP     UPDMOTORS

            ORG     T6_PVECT
            JMP     SIREN

            ORG     USERPROG_PVECT
            JMP     MAIN

            ORG     PROGSTART
*
************************************************************
* Main Constants
************************************************************
*
WELCOME     DC.B    CR,LF
            DC.B    'STEVE - Speed Trap Enforcement VehiclE'
            DC.B    CR,LF
            DC.B    'Michael Hattermann'
            DC.B    CR,LF
            DC.B    'IMDL - Spring 2002'
            DC.B    CR,LF,EOS
SHUTDOWN    DC.B    CR,LF
            DC.B    'STEVE - Program ended...shutting down systems'
            DC.B    CR,LF,EOS
LEFTIRSTR   DC.B    'Left IR value = '
            DC.B    EOS
RIGHTIRSTR  DC.B    'Right IR value = '
            DC.B    EOS
NEWLINE     DC.B    CR,LF,EOS
LEFTSPSTR   DC.B    'Left beam broken'
            DC.B    CR,LF,EOS
RIGHTSPSTR  DC.B    'Right beam broken'
            DC.B    CR,LF,EOS
SPEED       DC.B    CR,LF
            DC.B    'SPEED = '
            DC.B    EOS


*
*******************************************************************************
*                       SUBROUTINE -  MAIN
* Description: Main program.  Inits the robots sub-systems and begins the robot
*               behavior code.
* Input         : None.
* Output        : None.
* Destroys      : None.
* Calls         : None.
*******************************************************************************
*
MAIN        MOVB    #$00,COPCTL         ; turn off COP watchdog timer
            LDS     #STACKPTR           ; load stack pointer

            JSR     INITSCI             ; init SCI system
            JSR     INITATD             ; init A/D system
            JSR     INITTIME            ; init timer system
            MOVB    #$00,LED1PORT       ; turn off flashing lights
            MOVB    #$00,SEG7PORT       ; reset speed capture to zero

            LDX     #WELCOME            ; print welcome message
            JSR     OUTSTR              ;

            CLI                         ; turn on interrupts

            JSR     WAITSPEED           ; wait for a speeder
            JSR     INITPWM             ; init PWM system
            JSR     FLASHON             ; turn on flashing lights
            JSR     SIRENON             ; turn on siren
            JSR     PULLOUT             ; pull out and prepare to follow

MAIN2
            JSR     FOLLOW              ; follow speeder
            LDX     #FPROCRATE          ; wait designated amount of time
            JSR     WAIT                ;
            BRA     MAIN2               ; do it again

*
*******************************************************************************
*                       SUBROUTINE -  MAINOUT
* Description: Exits the program.  Stops motors, signals end of program with
*               the lights, then shuts down the subsystems and enters a
*               never ending loop
* Input         : None.
* Output        : None.
* Destroys      : None.
* Calls         : STEER,OUTSTR,WAIT,KILLATD,KILLPWM,KILLTIME.
*******************************************************************************
*
MAINOUT     LDX     #STOP               ; stop the motors
            JSR     STEER               ;
            LDX     #SHUTDOWN           ; print shutdown message
            JSR     OUTSTR              ;

            LDAA    #8                  ; load loop counter
MAINOUT1    MOVB    #$3C,LED1PORT       ; turn off lights
            JSR     SIRENOFF            ; turn off siren
            LDX     #250                ; wait
            JSR     WAIT                ;
            MOVB    #$C3,LED1PORT       ; turn on lights
            JSR     SIRENON             ; turn on siren
            LDX     #250                ; wait
            JSR     WAIT                ;
            DBNE    A,MAINOUT1          ; continue looping until done
            MOVB    #$00,LED1PORT       ; turn on lights

            JSR     KILLATD             ; shutdown atd system
            JSR     KILLPWM             ; shutdown pwm system
            JSR     KILLTIME            ; shutdown timer system
            MOVB    #$00,LED1PORT       ; turn off flashing lights
            MOVB    #$00,SEG7PORT       ; clear 7 segment display
MAINOUTX    BRA     MAINOUTX            ; end of program
*
*******************************************************************************

#include "wait.asm"
#include "sci.asm"
#include "atd.asm"
#include "time.asm"
#include "pwm.asm"
#include "objavoid2.asm"
#include "follow.asm"
