#define LCD_I2C_asm      
#include	 <p18F14K22.inc>
#INCLUDE "LCD_I2C.inc"                   


;LCD control line bit definitions
#define	LCD_RS	LCDReg,	.0	;0=Command 1=Data
#define	LCD_RW	LCDReg,	.1	;R/W
#define	LCD_E	LCDReg,	.2	;1 to send data
#define	LCD_BL	LCDReg,	.3	;Backlight 1=off 0=on
#define	LCD_D4	LCDReg,	.4	;4 data lines
#define	LCD_D5	LCDReg, .5
#define	LCD_D6	LCDReg, .6
#define	LCD_D7	LCDReg, .7

#define LCDBL	TimerStat2, .3	;LCD Backlight status from main code
LED		equ	RC1			;Error LED

	extern	TimerStat2

tempdat access_ovr 

;delay
dc1    res 1      
dc2    res 1                   
dc3	   res 1

;LED Blink
bc1		res 1
	;LCD Temps	
d0 		res 1	;I2CWrite, I2cRead temp			      
d1 		res 1	;LCDSEND temp
d2		res 1 	;LCDADD	              
lcda.1	res	1	;LCDADD

LCDReg 	res	1	;Byte to store lcd data written to I2C
LCDRegTmp	res	1 ;temp to preserve W in LCDReg
LCDReadTmp	res	1 ;Save lcdreg state while reading LCD
d0I		res	1
d1I		res	1
d2I		res	1
LCDTmp	res 1	
LCD_RSTmp	res	1

LCD40x2_I2C_CODE code                   
 
SLAVEADDRESS    equ     040h ;I2C LCD device address 020h
;SLAVEADDRESS    equ     04Eh ;I2C LCD device address 027h


;***********************************************;
;                                                ;
;        EXTERN  I2CMPolInit                     ;
;        EXTERN  I2CMPolIsIdle                   ;
;        EXTERN  I2CMPolPut                      ;
;        EXTERN  I2CMPolGet                      ;
;        EXTERN  I2CMPolIsDataReady              ;
;        EXTERN  I2CMPolIsAckReceived            ;
;                                                ;
;        #ifdef  I2CM_MULTI_MASTER               ;
;                                                ;
;        EXTERN  I2CMPolIsBusCollision           ;
;                                                ;
;        #endif                                  ;
;***********************************************;
;        #endif                                  ;
                                                ;
;***********************************************;
;I2CMPolError bits                              ;
;***********************************************;
                                                ;
I2CMErrWriteCollision   EQU     01h             ;Write collision has occured
I2CMErrBusCollision     EQU     02h             ;Bus collision has occured
I2CMErrNoAck            EQU     03h             ;No Acknowledge received
I2CMErrRxDataOverFlow   EQU     04h             ;Received Data Over flow has occured
I2CMBusNotIdle          EQU     05h             ;Bus is not Idle
I2CMDataNotReady        EQU     06h             ;Data is not yet ready
                                                ;
;***********************************************;




  
;*******************************************************;
; This file is generated by Configurable Library Manager;
; Do Not Modify Manually                                ;
;*******************************************************;
;                                                       ;
#ifndef _I2CMPol_DEF                                    ;
#define _I2CMPol_DEF                                    ;
#ifndef CLOCK_FREQ                                      ;
#define CLOCK_FREQ             .40000000 				;
#endif                                                  ;
#define I2CM_BUS_FREQUENCY	   .100000 ;.300000     			    ;
#define I2CM_BLOCKING_FUNCTION                          ;
#define I2CM_SINGLE_MASTER                              ;
#define I2CM_NO_CONTROLED_SLEW_RATE                     ;
;#define I2CM_CONTROLED_SLEW_RATE
#define I2CM_NO_SMBUS                                   ;
#endif                                                  ;
;                                                       ;
;*******************************************************;
;***********************************************;
; Baud Rate Genarator Reload value              ;
;***********************************************;    
                                                ;
_QUOTIENT    EQU    CLOCK_FREQ / (4 * I2CM_BUS_FREQUENCY)-1;to convert the freq in 400KHz            
_REMAINDER   EQU    CLOCK_FREQ % (4 * I2CM_BUS_FREQUENCY)  ;
                                                ;
        #if     _REMAINDER != 0                 ;
                                                ;
_I2CM_BAUD_COUNT    EQU     _QUOTIENT + 1       ;_I2CMBAUDCOUNT is nearest to ((fosc/4)/I2CMSPEED)-1,
;_I2CM_BAUD_COUNT    EQU	.12

                                                ; which has to be written in SSPADD.
        #else   ;_REMAINDER = 0                 ;
                                                ;
_I2CM_BAUD_COUNT    EQU    _QUOTIENT            ;_I2CMBAUDCOUNT = ((fosc/4)/I2CMSPEED)-1, which has to be written in SSPADD.
;_I2CM_BAUD_COUNT    EQU	.12

                                               ;which has to be written in SSPADD.
        #endif                                  ;
                                                ;
;-----------------------------------------------;



;*************************************************************
; Initialize LCD functions
;*************************************************************
LCDINIT

	call	I2CMPolInit			;Initialize I2C port


	
	; Forced LCD initialization
	bsf	LCD_BL			;Backlight on
	bcf	LCD_RS			; Set command mode
	bcf	LCD_E
	bcf	LCD_RW
	bsf	LCD_D4
	bsf	LCD_D5
	bcf	LCD_D6
	bcf	LCD_D7
	call	 LCDI2CTX
	call	PULSE
	
	
	call	PULSE	
	call	PULSE


	; Send the command to select 4-bit mode first
	bcf	LCD_D4 		
	call	 LCDI2CTX
	call	PULSE



	; Function set
	; Format: xxxDNFxx		  D: 1= 8-bit, 0= 4-bit (use 4-bit!)
	movlw	b'00101000'		; N: 1= 2 lines, 0= 1 line
	call	LCDSEND			; F: 1= 5x10 font, 0= 5x8 font


	; Display control
	; Format: xxxxxDCB		  D: Display on(1)/0ff(0) 
	movlw	b'00001100'		; C: Cursor on(1)/0ff(0) 
	call	LCDSEND			; B: Blink on(1)/0ff(0) 
	; Entry mode set
	; Format: xxxxxxIS		  I: 1= Increment counter, 0= Decrement counter
	movlw	b'00000110'		; S: 1= "Accompanies display shift"
	call	LCDSEND

	; Cursor or display shift
	; Format: xxxxSRxx		  S: 1= Display shift, 0= Cursor move
	movlw	b'00010000'		; R: 1= Shift right, 0= Shift left
	call	LCDSEND

	movlw	b'00000001'		; Clear screen
	call	LCDSEND	

	bsf	LCD_RS		        ; Set data mode

	RETURN










;*************************************************************
; Moves "cursor"
;*************************************************************
LCDADD
	btfss	LCDBL		;Set backlight status on every cursor move
	bcf		LCD_BL
	btfsc	LCDBL
	BSF		LCD_BL
	bcf	LCD_RS			; Command mode
	call	 LCDI2CTX
	iorlw	0x80			; Goto DDRAM adress
	call	LCDSEND
	bsf	LCD_RS			; Data mode
	call	 LCDI2CTX
;	call	delay_1ms		; Takes a couple of ms
	RETURN


;*************************************************************
; Sends contens of W to display
;*************************************************************
LCDSEND	; Sends character in W to lcd. Does not alter RS line!

	movwf	LCDTmp

	bcf	LCD_RSTmp,0	; need to save RS state
	btfsc	LCD_RS
	bsf	LCD_RSTmp,0

	bsf	LCD_RW		;Select read data
	bcf	LCD_RS		;Read data register
	call	LCDI2CTX	

bzwait
	bcf	LCD_E		; clear E if needed get ready to strobe
	call	LCDI2CTX

	bsf	LCD_D4		; pins high to select input. Prepare to read busy flag.
	bsf	LCD_D5
	bsf	LCD_D6
	bsf	LCD_D7

	bsf	LCD_E		;read data & hold

	call	LCDI2CTX

	call	I2CRead

	movwf LCDReadTmp	;Save LCD state before read
	btfss LCDReadTmp,7	; LCD_D7 high = busy
	bra	nbz


	bsf		LATC,LED
;	movlw	.5
;	call	adj_del_ms


	bcf	LCD_E
	call	LCDI2CTX

	bsf	LCD_E		;strobe out the low nibble	
	call	LCDI2CTX

	bra	bzwait		;busy, keep checking



nbz    


	bcf		LATC,LED



	bcf	LCD_E
	call	LCDI2CTX


	bsf	LCD_E		;strobe out the low nibble	
	call	LCDI2CTX

	bcf	LCD_E
	call	LCDI2CTX



	bcf	LCD_RW
	call	 LCDI2CTX

	bcf	LCD_RS		; need to restore RS state
	btfsc	LCD_RSTmp,0
	bsf	LCD_RS

	bcf	LCD_D4
	bcf	LCD_D5
	bcf	LCD_D6
	bcf	LCD_D7

	btfsc	LCDTmp, 7
		bsf	LCD_D7
	btfsc	LCDTmp, 6
		bsf	LCD_D6
	btfsc	LCDTmp, 5
		bsf	LCD_D5
	btfsc	LCDTmp, 4
		bsf	LCD_D4
	call	 LCDI2CTX
	call	PULSE


	bcf	LCD_D4
	bcf	LCD_D5
	bcf	LCD_D6
	bcf	LCD_D7

	btfsc	LCDTmp, 3
		bsf	LCD_D7
	btfsc	LCDTmp, 2
		bsf	LCD_D6
	btfsc	LCDTmp, 1
		bsf	LCD_D5
	btfsc	LCDTmp, 0
		bsf	LCD_D4
	call	 LCDI2CTX
	call	PULSE
	return


;*************************************************************
; Pulse enable LCD Enable pin
;*************************************************************
PULSE ; Pulse Enable.
	bsf	LCD_E
	call	 LCDI2CTX
	bcf	LCD_E
	call	LCDI2CTX
	return

;*************************************************************
; Clears display
;*************************************************************
LCDCLR ; clears the entire display
	bcf	LCD_RS			; Set command mode
	call	 LCDI2CTX
	movlw	b'00000001'		; Clear screen
	call	LCDSEND	
	bsf	LCD_RS			; Set data mode
	call	 LCDI2CTX

	return



;*******************************************************************
;		read byte from I2C into W.
I2CRead:
		call	I2CStartRead
		call    I2CMPolIsIdle

;getblock = enablerec,isidle, get

		bsf     SSPCON2,RCEN  ;mI2CMPolEnableReceiver ;Enable the receiver

		call    I2CMPolIsIdle

		call	I2CMPolGet


		movwf	d0				;Save received data

;stopblock = stop, isidle

		call 	I2CStop
		bcf     SSPCON2,RCEN  ;Disable the receiver


		movf	d0,w			;Restore received data		
		return

;******************************************************************


I2CStartRead:  ;Initiate I2C receive, send address

        bsf     SSPCON2,SEN                     ;Send START bit
        call    I2CMPolIsIdle                   ;Wait till START is sent
		 movlw   SLAVEADDRESS+1                    ;SlaveAddress(with read bit)
       	call    I2CMPolPut                      ;
 		tstfsz  WREG                            ;Collision?
        bra     ofailr                         ;
         call    I2CMPolIsIdle                   ;Wait till Byte is transmitted
         call    I2CMPolIsAckReceived            ;
		tstfsz	WREG								;No ACK?
		bra		ofailr
		return



		movwf	d0	;save err code
		movlw	.20
ofailr:	movf	d0,w
	 call BlinkLED
		movlw	.254
		call	adj_del_ms
		bra		ofailr
;--------------------------------------------------------------------------
;I2CReStart:                           ;
 ;                                               ;
 ;       BANKSEL SSPCON2                         ;
 ;       bsf     SSPCON2,RSEN                    ;Send RepeatedSTART bit
 ;                                               ;
 ; return                                    ;
     
;*************************************************************
LCDI2CTX	;Send lcd data register to I2C port (LCDReg)

		movwf	LCDRegTmp			;Save W
		call	I2CStart			;Initiate connection
		movf	LCDReg,W			;Get  lcd port values
		call 	I2CWrite			;Send to port
		call	I2CStop				;close connection
		movf	LCDRegTmp,w			;Restore W
		return	
;=================================================================
;Write W to I2C bus
;WToI2C:
;		movwf	d0
;		call	I2CStart			;Initiate connection
;		movf	d0,w
;		call	 I2CWrite
;		call	I2CStop				;close connection
;		return
;*************************************************************

I2CStart:  ;Initiate I2C transmit, send address


        bsf     SSPCON2,SEN                     ;Send START bit
        call    I2CMPolIsIdle                   ;Wait till START is sent
                                               ;
        movlw   SLAVEADDRESS                    ;SlaveAddress(with write bit)
       	call    I2CMPolPut                      ;
 		tstfsz  WREG                            ;Collision?
        bra     ofail                         ;
         call    I2CMPolIsIdle                   ;Wait till Byte is transmitted
          call    I2CMPolIsAckReceived            ;
		tstfsz	WREG								;No ACK?
		bra		ofail
		return


		movwf	d0	;save err code
ofail:	movf	d0,w
	 call BlinkLED
		movlw	.254
		call	adj_del_ms

		bra ofail
;******************************************************************


I2CStop:  ;Close I2C Connection                                        ;

        bsf     SSPCON2,PEN                     ;Send STOP bit
        call    I2CMPolIsIdle                   ;Wait till STOP is sent
		return
;*****************************************************************
; write string to I2C. Must call I2CStart before and I2CStop after
prtstrI2C:

		tblrd*+			;read from table and inc ptr
		movf	TABLAT,W	; table data to w

		andlw	b'11111111'	
		bz		prtst1l	;Last character 0?		
		call	I2CWrite	;Write W to I2C
		bra		prtstrI2C
prtst1l:	return


;*******************************************************************
;		send byte in W to I2C Must call I2CStart first
I2CWrite:

;		movwf	d0		;save w
       	call    I2CMPolPut                      ;
 		tstfsz  WREG                            ;Collision?
        bra     wfail                         ;
         call    I2CMPolIsIdle                   ;Wait till Byte is transmitted
          call    I2CMPolIsAckReceived            ;
		tstfsz	WREG								;No ACK?
		bra		wfail
		return


		movwf	d0	;save err code
wfail:	movf	d0,w
	 call BlinkLED
		movlw	.254
		call	adj_del_ms
		bra wfail


;******************************************************************


;Toggle RB0 to blink LED

BlinkLED:    
		movlw   .2	           ; 2 blinks for all I2C errors
		movwf   bc1            
blinkc:
		BsF     LATC,LED        ; Turn on LED connected to RC7
		movlw	.20	       ; 100 ms delay
        CALL adj_del_ms
        BcF     LATC,LED        ; Turn off LED connected to RC7
        movlw	.20
        CALL    adj_del_ms 
        decfsz  bc1,f           
        goto    blinkc

		return


;----------------------------------------------------------------------

adj_del_ms:
        movwf   dc3             
dly2    movlw   .130 ; 40MHz.;.13             ; 65 20mhz ; repeat inner loop 13 times 4mhz
        movwf   dc2             ; -> 13x(767+3)-1 = 10009 cycles
        clrf    dc1             ; inner loop = 256x3-1 = 767 cycles
dly1    decfsz  dc1,f           
        goto    dly1
        decfsz  dc2,f           ; end middle loop
        goto    dly1            
        decfsz  dc3,f           ; end outer loop
        goto    dly2
        return


;***********************************************************************;
; Function: I2CMPolInit                                                 ;
;                                                                       ;
; PreCondition: None                                                    ;
;                                                                       ;
; Overview:                                                             ;
;       This routine is used for MSSP Module Initialization.            ;
;       It initializes Module according to compile time selection       ;
;                                                                       ;
; Input: MpAM options                                                   ;
;                                                                       ;
;                                                                       ;
; Output: None                                                          ;
;                                                                       ;
; Side Effects: 'W' register is changed                                 ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;
I2CMPolInit:                                    ;
                                                ;
;        GLOBAL  I2CMPolInit                     ;
                                                ;
        movlw   028h                            ;Enable MSSP module and Master mode
        movwf   SSPCON1                         ;
                                                ;
        movlw   _I2CM_BAUD_COUNT                ;
        movwf   SSPADD                          ;loading SSPADD with baudrate generator
                                                ;reloading value
        #ifdef  I2CM_CONTROLED_SLEW_RATE        ; 
                                                ;
        bsf     SSPSTAT,SMP                     ;
                                                ;
        #endif                                  ;
                                                ;
        #ifdef  I2CM_SMBUS                      ; 
                                                ;
        bsf     SSPSTAT,CKE                     ;
                                                ;
        #endif                                  ;
                                                ;
        return                                  ;
                                                ;
;***********************************************;10




;***********************************************************************;
; Function: I2CMPolPut                                                  ;
;                                                                       ;
; PreCondition: I2CMPolStart should be called.                          ;
;                                                                       ;
; Overview:                                                             ;
;       This sends data byte over I2C Bus and checks write collision.   ;
;                                                                       ;
; Input: 'W' Register                                                   ;
;                                                                       ;
; Output: 'W' Register                                                  ;
;                                                                       ;
; Side Effects: 'W' register is changed                                 ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;
I2CMPolPut:                                     ;
                                                ;
;        GLOBAL  I2CMPolPut                      ;
                                                ;
        movwf   SSPBUF                          ;
                                                ;
        btfss   SSPCON1,WCOL                    ;
        retlw   000h                            ;
                                                ;
        bcf     SSPCON1,WCOL                    ;
        retlw   I2CMErrWriteCollision           ;indicates Write Collision Error
                                                ;
;***********************************************;8        



;***********************************************************************;
; Function: I2CMPolIsAckReceived                                        ;
;                                                                       ;
; PreCondition: I2CMPolPut should have called.                          ;
;                                                                       ;
; Overview:                                                             ;
;       This checks acknowledge has received.                           ;
;                                                                       ;
; Input: None                                                           ;
;                                                                       ;
; Output: 'W' Register                                                  ;
;                                                                       ;
; Side Effects: 'W' register is changed                                 ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;
I2CMPolIsAckReceived:                           ;
                                                ;
;        GLOBAL  I2CMPolIsAckReceived            ;
                                                ;
        btfsc   SSPCON2,ACKSTAT                 ;
        retlw   I2CMErrNoAck                    ;indicates No Acknowledge received
                                                ;
        retlw   000h                            ;
                                                ;
;***********************************************;6        



;***********************************************************************;
; Function: I2CMPolGet                                                  ;
;                                                                       ;
; PreCondition: I2CMPolPut should have called.                          ;
;                                                                       ;
; Overview:                                                             ;
;       This reads a data byte from the buffer.                         ;
;                                                                       ;
; Input: None                                                           ;
;                                                                       ;
; Output: 'W' Register                                                  ;
;                                                                       ;
; Side Effects: 'W' register is changed                                 ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;
I2CMPolGet:                                     ;
                                                ;
;        GLOBAL  I2CMPolGet                      ;
                                                ;
        movf    SSPBUF,w                        ;
                                                ;
        return                                  ;
                                                ;
;***********************************************;5        



;***********************************************************************;
; Function: I2CMPolIsDataReady                                          ;
;                                                                       ;
; PreCondition: SPIMPOlPut should have been called.                     ;
;                                                                       ;
; Overview:                                                             ;
;       This tells weather the Data is received                         ;
;        If data over flow has occurred it indicates it.                ; 
;                                                                       ;
; Input: None                                                           ;
;                                                                       ;
; Output: 'W' Register                                                  ;
;                                                                       ;
; Side Effects: Bank selection bits and 'W' register are changed        ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;        
I2CMPolIsDataReady:                             ;
;        GLOBAL  I2CMPolIsDataReady              ;
                                                ;
        btfsc   SSPCON1,SSPOV                   ;Checks is over flow has occured
        bra     I2COverRxErr                    ;
                                                ;
        #ifndef I2CM_BLOCKING_FUNCTION          ;
                                                ;
        btfss   SSPSTAT,BF                      ;Checks transmission/reception in progress
        retlw   I2CMDataNotReady                ;Indicates In progress
                                                ;
        retlw   000h                            ;Indicates Over
                                                ;
        #else                                   ;
                                                ;
I2CMWaitRxOver                                  ;
        btfss   SSPSTAT,BF                      ;
        bra     I2CMWaitRxOver                  ;Wait for a data byte reception
                                                ;
        return                                  ;
                                                ;
        #endif                                  ;
                                                ;
I2COverRxErr                                    ;
        bcf     SSPCON1,SSPOV                   ;
        retlw   I2CMErrRxDataOverFlow           ;Indicates Over flow
                                                ;
;***********************************************;6



;***********************************************************************;
; Function: I2CMPolIsIdle                                               ;
;                                                                       ;
; PreCondition: Must be after every I2CMPol functions and macros        ;
;        except I2CMPolGet                                              ;
;                                                                       ;
; Overview:                                                             ;
;       This checks whether the I2C Bus is Idle or not.                 ;
;                                                                       ;
; Input: None                                                           ;
;                                                                       ;
; Output: 'W' Register                                                  ;
;                                                                       ;
; Side Effects: 'W' register is changed                                 ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;
I2CMPolIsIdle:                                  ;
;        GLOBAL  I2CMPolIsIdle                   ;
                                                ;
        #ifndef I2CM_BLOCKING_FUNCTION          ;
                                                ;
        movf    SSPCON2,w                       ;
        andlw   01fh                            ;Checks SEN,PEN,RSEN,RCEN,ACKEN
                                                ;
        btfss   STATUS,Z                        ;
        retlw   I2CMBusNotIdle                  ;Indicates Bus is not Idle
                                                ;
        btfsc   SSPSTAT,R_W                     ;Checks transmission in progress
        retlw   I2CMBusNotIdle                  ;Indicates Bus is not Idle
                                                ;
        retlw   000h                            ;Indicates Bus is Idle
                                                ;
                                                ;
        #else                                   ;
                                                ;
I2CMSPRxANotOver                                ;
        movf    SSPCON2,w                       ;
        andlw   01fh                            ;Checks SEN,PEN,RSEN,RCEN,ACKEN
                                                ;
        btfss   STATUS,Z                        ;
        bra     I2CMSPRxANotOver                ;Indicates Bus is not Idle
                                                ;
I2CMTxNotOver                                   ;
;		BcF     LATC,LED        ; Turn on LED 
        btfsc   SSPSTAT,R_W                     ;Checks transmission in progress
        bra     I2CMTxNotOver                   ;Indicates Bus is not Idle
 ;		BsF     LATC,LED        ; Turn off LED                                                ;
        return                                  ;
                                                ;
        #endif                                  ;
                                                ;
;***********************************************;10

I2CMPolAck:                             ;
                                                ;
        bcf     SSPCON2,ACKDT                   ;
        bsf     SSPCON2,ACKEN                   ;Acknowledge
                                                ;
		return                           ;
   

        #ifdef  I2CM_MULTI_MASTER

;***********************************************************************;
; Function: I2CMPolIsBusCollision                                       ;
;                                                                       ;
; PreCondition: Must be called after every I2CMPol functions and macros ;        
;                in multimater mode. This portion of the code will      ;
;                be assembled only for multi-master mode                ;
; Overview:                                                             ;
;       This checks whether the I2C Bus Collision has occured or not.   ;
;                                                                       ;
; Input: None                                                           ;
;                                                                       ;
; Output: 'W' Register                                                  ;
;                                                                       ;
; Side Effects: 'W' register is changed                                 ;
;                                                                       ;
; Stack requirement: 1 level deep                                       ;
;                                                                       ;
;***********************************************;***********************;
                                                ;
I2CMPolIsBusCollision:                          ;
;        GLOBAL  I2CMPolIsBusCollision           ;
                                                ;
        btfss   PIR2,BCLIF                      ;
        retlw   000h                            ;
                                                ;
        bcf     PIR2,BCLIF                      ;
        retlw   I2CMErrBusCollision             ;Indicates Bus Collision has occured
                                                ;
;***********************************************;7

        #endif


;*************************************************************
; Calls the delay_1ms W times
;*************************************************************
;delay
;	movwf	d0I
dly_loop
;	call	delay_1ms
;	decfsz	d0I, F
;	goto	dly_loop
;	return


;*************************************************************	
; 1ms delay.
; Modify this to match your processor speed.
; http://www.piclist.org/techref/piclist/codegen/delay.htm
;*************************************************************
;delay_1ms
;	movlw	0xfe ;0xF3		;2498 cycles
;	movwf	d1I
;	movlw	0x09 ;0x0c ;0x09 ;0x02
;	movwf	d2I
;Delay_0
;	decfsz	d1I, f
;	goto	$+4
;	decfsz	d2I, f
;	goto	Delay_0
;	goto	$+2		;2 cycles
;	return			;4 cycles (including call)


;*************************************************************	
; 40us delay.
; Modify this to match your processor speed.
; http://www.piclist.org/techref/piclist/codegen/delay.htm
;*************************************************************
;short_dly
;	movlw	.143 ;.183 ;.143 ;0x1F		;94 cycles
;	movwf	d1I
;short_dly_0
;	decfsz	d1I, f
;	goto	short_dly_0
;	goto	$+2		;2 cycles
;	return			;4 cycles (including call)

	end



