
#define	API_SOURCE
	#include <p18F258.inc>
	#include "APIdef.inc"
	#include "CANDef.inc"
	;#include "APImacro.inc"
	
	
	
API_start CODE



;*******************************************************
;
;	Routines for treatment of the information
;	coming from Bus CAN
;
;*******************************************************

;*******************************************************
; 
;	This procedure read the stored information
;	in the address specific in Add and it returns
;	in TempRead
;
;*******************************************************

READFunc
	local	Reads
	
Reads:	
	movff	POSTINC0,POSTINC1
	banksel	TempSize
	decfsz	TempSize,1
	bra	Reads
	
	return
	
;*******************************************************
; 
;	This procedure write the  information
;	in the address specific in Add
;
;*******************************************************

WRITEFunc
	local	Writes
	
Writes:	
	movff	POSTINC0, POSTINC1
	decfsz	TempSize, 1
	bra	Writes
		
	return

;*******************************************************
; 
;	This procedure processes the coming information
;	of the CAN Bus
;
;*******************************************************

PROCESSFun
	GLOBAL	PROCESSFun	
	local isWrite, isActivate, isDefuse, isData, GoBack		
	
	call	COPYvalFun
	
	banksel	TempCommand
	movlw	0x00
	cpfseq	TempCommand
	bra	isWrite
	banksel	TempAdd
	READ	TempAdd, TempComp
	bra	GoBack

isWrite:	
	movlw	0x01
	cpfseq	TempCommand
	bra	isActivate
	banksel	TempData
	WRITE	TempData, TempAdd, TempComp
	bra	GoBack
	
isActivate:	
	movlw	0x02
	cpfseq	TempCommand
	bra	isDefuse
	banksel	TempAdd
	EnableBIT	TempAdd, TempComp
	bra	GoBack
	
isDefuse:
	movlw	0x03
	cpfseq	TempCommand
	bra	isData
	DisableBIT	TempAdd, TempComp
	bra	GoBack
	
isData:
	movlw	0x04
	cpfseq	TempCommand
	bra	GoBack
	setf	0x004
	bra	GoBack		
		
GoBack:	
	return
	
	
;*******************************************************
; 
;	This procedure copy the coming information
;	recived in NewMessData
;
;*******************************************************
	
COPYvalFun
	
	movlw	low(NewMessageData)
	movwf	FSR0L
	movlw	high(NewMessageData)
	movwf	FSR0H
	banksel	TempCommand
	movff	POSTINC0,TempCommand
	banksel	TempAdd
	movff	POSTINC0,TempAdd
	movff	POSTINC0,TempAdd+1
	banksel	TempComp
	movff	POSTINC0,TempComp
	banksel	TempData
	movff	POSTINC0,TempData
	movff	POSTINC0,TempData+1
	movff	POSTINC0,TempData+2
	movff	POSTINC0,TempData+3

	return

;*******************************************************
; 
; 	This function allows to activate a bit	
;
;*******************************************************
	
EnableBitFun
	local is1, is2, is3, is4, is5, is6, is7, GoBack_2
	
	
	banksel	TempComp
	movlw	0x00
	cpfseq	TempComp
	bra	is1
	bsf	INDF0,0
	bra	GoBack_2
is1:	movlw	0x01
	cpfseq	TempComp
	bra	is2
	bsf	INDF0,1
	bra	GoBack_2
is2:	movlw	0x02
	cpfseq	TempComp
	bra	is3
	bsf	INDF0,2
	bra	GoBack_2
is3:	movlw	0x03
	cpfseq	TempComp
	bra	is4
	bsf	INDF0,3
	bra	GoBack_2
is4:	movlw	0x04
	cpfseq	TempComp
	bra	is5
	bsf	INDF0,4
	bra	GoBack_2
is5:	movlw	0x05
	cpfseq	TempComp
	bra	is6
	bsf	INDF0,5
	bra	GoBack_2
is6:	movlw	0x06
	cpfseq	TempComp
	bra	is7
	bsf	INDF0,6
	bra	GoBack_2
is7:	movlw	0x07
	cpfseq	TempComp
	bra	GoBack_2
	bsf	INDF0,7
	bra	GoBack_2
	
GoBack_2:

	return

;*******************************************************
; 
; 	This function allows to disable a bit
;
;*******************************************************

DisableBitFun
	local to1, to2, to3, to4, to5, to6, to7, GoBack_3
	
	
	banksel	TempComp
	movlw	0x00
	cpfseq	TempComp
	bra	to1
	bcf	INDF0,0
	bra	GoBack_3
to1:	movlw	0x01
	cpfseq	TempComp
	bra	to2
	bcf	INDF0,1
	bra	GoBack_3
to2:	movlw	0x02
	cpfseq	TempComp
	bra	to3
	bcf	INDF0,2
	bra	GoBack_3
to3:	movlw	0x03
	cpfseq	TempComp
	bra	to4
	bcf	INDF0,3
	bra	GoBack_3
to4:	movlw	0x04
	cpfseq	TempComp
	bra	to5
	bcf	INDF0,4
	bra	GoBack_3
to5:	movlw	0x05
	cpfseq	TempComp
	bra	to6
	bcf	INDF0,5
	bra	GoBack_3
to6:	movlw	0x06
	cpfseq	TempComp
	bra	to7
	bcf	INDF0,6
	bra	GoBack_3
to7:	movlw	0x07
	cpfseq	TempComp
	bra	GoBack_3
	bcf	INDF0,7
	bra	GoBack_3
	
GoBack_3:

	return	

	END