;******************************************************************************
;   This file is a basic code template for object module code                 *
;   generation on the PIC12F675. This file contains the                       *
;   basic code building blocks to build upon.                                 *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on             *
;   features of the assembler and linker (Document DS33014).                  *
;                                                                             *
;   Refer to the respective PIC data sheet for additional                     *
;   information on the instruction set.                                       *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:      SwarmiTx.asm                                              *
;    Date:          22/8/12                                                   *
;    File Version:                                                            *
;                                                                             *
;    Author:        Mike Seiler                                               *
;    		                                                                  *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Files required: P12F675.INC                                              *
;                                                                             *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes:                                                                   *
;                                                                             *
;******************************************************************************

;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

     LIST      P=12F675              ; list directive to define processor
     #INCLUDE <P12F675.INC>          ; processor specific variable definitions

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the 
; .asm file. The lables following the directive are located in the respective 
; .inc file.  See the data sheet for additional information on configuration 
; word settings.
;
;------------------------------------------------------------------------------

    __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

	errorlevel -302
		
;------------------------------------------------------------------------------
; VARIABLE DEFINITIONS
;------------------------------------------------------------------------------

; example of using Shared Uninitialized Data Section
INT_VAR     UDATA_SHR   0x20   
W_TEMP      RES     1             ; variable used for context saving 
STATUS_TEMP RES     1             ; variable used for context saving
count		RES		3
counter		RES		1			  ; bit counter for message bits
message		RES		1			  ; storage for message value(0-255) corresponds to Ascii code
pulseCount	RES		1
dcount		RES		1
dcount2

#define	count2	count+1
#define	count3	count+2

;------------------------------------------------------------------------------
; OSCILLATOR CALIBRATION VALUE
;------------------------------------------------------------------------------

OSC       CODE    0x03FF

; Internal RC calibration value is placed at location 0x3FF by Microchip as
; a 0xADDLW K instruction, where the K is a literal value to be loaded into 
; the OSCCAL register.  

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RESET_VECTOR  CODE    0x0000  ; processor reset vector
        goto	START		  ; go to beginning of program
		
;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

INT_VECTOR    CODE    0x0004  ; interrupt vector location
        
        retfie                ; return from interrupt

;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

MAIN_PROG     CODE

START

;------------------------------------------------------------------------------
; OSCCAL RESTORE (not required if internal OSC is not used)
;------------------------------------------------------------------------------

        
        bsf     STATUS,RP0    ; set file register bank to 1 
        call    0x3FF         ; retrieve factory calibration value
        movwf   OSCCAL        ; update register with factory cal value 
        
        

;------------------------------------------------------------------------------

		
        clrf	ANSEL
		movlw	b'00011110'
		movwf	TRISIO
		bcf		STATUS,RP0
		clrf	GPIO
		movlw	b'00000111'		  ; setting bits 0-2 in CMCON disables comparator
		movwf	CMCON			  ; comparator config register			
loop		
		btfss	GPIO,1
		call	delay
		btfss	GPIO,1
		call	go.left
		btfss	GPIO,2
		call	delay
		btfss	GPIO,2
		call	go.right
		btfss	GPIO,3
		call	delay
		btfss	GPIO,3
		call	go.forward
		btfss	GPIO,4
		call	delay
		btfss	GPIO,4
		call	stop		
		goto	loop
		
go.left
		movlw	b'01001100'		  ; send the value 0x4C to turn left
		goto	load.message
go.right
		movlw	b'01010010'		  ; send the value 0x52 to turn right
		goto	load.message
go.forward
		movlw	b'01000110'		  ; send the value 0x46 to go forward
		goto	load.message
stop
		movlw	b'01010011'		  ; send the value 0x53 to stop
load.message		
		movwf	message

send.message
		movlw	.8
		movwf	counter			  ; init counter to send 8 bits
		movlw	.92				  ; a value of 92 gives us a 2400us burst
		call	send.burst		  ; generate start condition
		call 	do.space
		rlf		message,f		  
		btfsc	STATUS,C		  ; test first bit if 1 or zero
		call	do.one
		btfss	STATUS,C
		call	do.zero
		decfsz	counter,f
		goto	$-7
		call	do.space
		return

do.one							  ; 1200us space
		movlw	.46
		call	send.burst
		return		

do.zero
		movlw	.23
		call	send.burst
		return			

do.space							  ; 600us burst
		movlw	.10
		movwf	count
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
		movlw	.18
		movwf	count2
		decfsz	count2,f
		goto	$-1
		decfsz	count,f
		goto	$-5
		return
			  
send.burst
		movwf	pulseCount
pulse.hi
		bsf		GPIO,0
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
pulse.lo
		bcf		GPIO,0
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
		nop
		decfsz	pulseCount,f
		goto	pulse.hi
		return
delay
		movlw	.10
		movwf	dcount2
dloop		
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
		goto	$+1
		decfsz	dcount,f
		goto	dloop
		decfsz	dcount2,f
		goto	dloop
		return			
        END                       ; directive 'end of program'

