;**********************************************************************
;                                                                     *
;    Filename:	    main.asm                                          *
;    Date: May 13th, 2007                                             *
;    File Version: 0.1                                                *
;                                                                     *
;    Author: s1axter                                                  *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************


	list      p=16f872            ; list directive to define processor
	#include <p16f872.inc>        ; processor specific variable definitions
	
	__CONFIG _XT_OSC & _PWRTE_OFF & _BODEN_OFF & _CP_OFF & _WDT_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.


;**********************************************************************
RESET_VECTOR  CODE    0x000       ; processor reset vector
	   	movlw  high  start        ; load upper byte of 'start' label
        movwf  PCLATH             ; initialize PCLATH
        goto   start              ; go to beginning of program


INT_VECTOR   CODE    0x004        ; interrupt vector location

;------------------------------------------------------------
;------------------- Variable Definition
;------------------------------------------------------------
;NOTE: 0x20 is start of general purpose registers

	;Varibles for delay routine
	cblock 	0x20
			delayCount1
			delayCountA
			delayCountB
	endc

	;Variables for motor control
;	cblock 	0x25
;		FWD EQU b'10100000'
;		REV EQU b'01010000'
;		RTE EQU b'01100000'
;		LFT EQU b'10010000'
;		RUN EQU b'000000001'
;		OFF EQU b'000000000'
;	endc

	;Variables for timing
	cblock 	0x30
		ONESEC
		STEPRUN
	endc

MAIN    CODE
start
;------------------------------------------------------------
;------------------- Initial Setup for PIC
;------------------------------------------------------------
		movlw 0x00       ; all port pins = low
        movwf PORTA
        movwf PORTB
        movwf PORTC

        bsf STATUS,RP0  ; set RAM Page 1 for TRIS registers

	; INITIALISE PORTS
	; binary used to see individual pin IO status

        movlw 0x00       ; PORTA and PORTB outputs
        movwf TRISA
        movwf TRISB
		movwf TRISC

        movlw b'00000110'       ; all analog pins = digital
        movwf ADCON1

        bcf STATUS,RP0  ; back to RAM page 0
;------------------------------------------------------------
;------------------- Main Program Code
;------------------------------------------------------------
DemoRun

;Forward, 4 sec
	call STOP    
		movlw 0x04
		movwf STEPRUN
		movlw b'01010000'
		movwf PORTC
	call RUN
	call Delay
;Right, 2 sec
	call STOP    
		movlw 0x02
		movwf STEPRUN
		movlw b'01100000'
		movwf PORTC
	call RUN
	call Delay
;Reverse, 4 sec
	call STOP    
		movlw 0x04
		movwf STEPRUN
		movlw b'10100000'
		movwf PORTC
	call RUN
	call Delay
;Left, 2 sec
	call STOP    
		movlw 0x02
		movwf STEPRUN
		movlw b'10010000'
		movwf PORTC
	call RUN
	call Delay
 goto DemoRun

;------------------------------------------------------------
;------------------- Run Bot
;------------------------------------------------------------
RUN
	movlw 0x01
	movwf PORTB
retlw 0x00
;------------------------------------------------------------
;------------------- Stop Bot
;------------------------------------------------------------
STOP
	movlw 0x00
	movwf PORTB
retlw 0x00
;------------------------------------------------------------
;------------------- Delay X seconds
;------------------------------------------------------------
Delay
	movlw d'4'
	movwf ONESEC
	insideDelay
		call Delay250
		decfsz ONESEC, F
		goto insideDelay
	decfsz STEPRUN
	goto Delay
retlw	0x00

;------------------------------------------------------------
;------------------- Delay 250 ms
;------------------------------------------------------------
Delay250
	movlw	d'250'
	movwf	delayCount1
 d1	movlw	0xC7
	movwf	delayCountA
	movlw	0x01
	movwf	delayCountB
 Delay_0
	decfsz	delayCountA, f
	goto	$+2
	decfsz	delayCountB, f
	goto	Delay_0

	decfsz	delayCount1	,f
	goto	d1
	retlw	0x00

END                       ; directive 'end of program'


