;**********************************************************************
;                                                                     *
;    Filename:     AFR_meter_main.asm                                 *
;    Date:         14 August 2010                                     *
;    File Version: 1.0.0                                              *
;                                                                     *
;    Author:  Dave Critchley                                          *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F876a.INC                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                  |--\/--|                                           *
;   ICSP VPP/MCLR -|1   28|- PGD ICSP                                 *
;          C1 IN- -|2   27|- PGC ICSP                                 *
;          C2 IN- -|3   26|- LCD R/W                                  *
;          C2 IN+ -|4   25|- LCD RS                                   *
;          C1 IN+ -|5   24|- LCD D7                                   *
;          C1 OUT -|6   23|- LCD D6                                   *
;          C2 OUT -|7   22|- LCD D5                                   *
;             VSS -|8   21|- LCD D4                                   *
;            XTAL -|9   20|- VDD                                      *
;            XTAL -|10  19|- VSS                                      *
;           LCD E -|11  18|- RX                                       *
;        PWM2 OUT -|12  17|- TX                                       *
;        PWM1 OUT -|13  16|-                                          *
;                 -|14  15|-                                          *
;                  --------                                           *
;  Serial data from Innovate LC1 is input on RX                       *
;  PWM1 (0-5V) outputs Lambda value 0v=7.35, 5v=22.xx                 *
;  PWM2 (1-0V) outputs Lambda reverse of above for std gauge          *
;                                                                     *
;  LCD (4 bit) shows warmup, error codes and lambda                   *
;                                                                     *
;  The 2 Comparators are used as buffer amps for the RC Filtered      *
;     pwm outputs                                                     *
;                                                                     *
;  XTAL = 4MHz Serial 19200 N 8 1                                     *
;                                                                     *
;**********************************************************************
;                                                                     ;
; Notes on operation							;                                        
;                                                                     ;
; LC1 packet frame is 6 bytes                                         ;
;   B2 82 <FN> 13 <LH> <LL>
;									;
; <FN> is one of 43,47,4B,4F,53,57,5B,5F - indicating data format	;
;	of lambda word							;
;									;
; <LH> is the high value of lambda word (x128) for lambda readings	;
;	below 22 only bottom 2 bits are of interest			;
;									;
; <LL> is lower 7 bits of Lambda word - 8th bit is 0 and not relevant	;                                                                     ;
;									;
;									;
; Function <FN> :							;
;
; Func2..0								;
; 000 Lambda valid and Aux data valid, normal operation			;.
; 001 Lambda value contains O2 level in 1/10%				;
; 010 Free air Calib in progress, Lambda data not valid			;
; 011 Need Free air Calibration Request, Lambda data not valid		;
; 100 Warming up, Lambda value is temp in 1/10% of operating temp.	;
; 101 Heater Calibration, Lambda value contains calibration countdown.	;
; 110 Error code in Lambda value					;
; 111 reserved								;
;
;
; 3rd Byte Indicates the mode						;
;
; 43 - Lambda								;
; 47 - O2 level 1/10 %	(L7 + L6-L0) 8 bit binary value (0x151=20.9%)   ;
; 4B - Free air Calib							;
; 4F - Need Free Air Calib						;
; 53 - Warmup - Lambda value is warmup in 1/10%				;
; 57 - Heater Calibration - Lambda value is countdown			;
; 5B - Error Code - Lambda value (low 4 bits) error code as table below	;
; 5F - N/A Reserved							;
;
; Error Table (lower 4 bits of <LL>					;
;
; 0 00h No error, last error cleared					;
; 1 01h Heater circuit shorted						;
; 2 02h Heater circuit open						;
; 3 03h Pump cell circuit shorted or too low an impedance		;
; 4 04h Pump cell circuit open or too high an impedance			;
; 5 05h Nernst cell circuit shorted or too low an impedance		;
; 6 06h Nernst cell circuit open or too high an impedance		;
; 7 07h For future use							;
; 8 08h Sensor timed out, possible damaged sensor			;
; 9 09h Battery voltage too low						;
;									;
;									;
;									;
;									;
; Lambda sets PWM1 duty cycle (0V = 0% to 5V = 100% )                 ;
;   PWM2 = FF-PWM1 value (so inverted)                                ;
;                                                                     ;
;                                                                     ;
;**********************************************************************

; *** Set Tabs to 14 when viewing the source code listing for nice formatting of output ***

	list	p=16f876a		; list directive to define processor
	include	"P16F876a.inc"		; processor specific variable definitions
	
	__CONFIG    _CP_OFF & _CPD_OFF & _BODEN_ON & _PWRTE_ON & _WDT_OFF & _XT_OSC & _LVP_OFF & _DEBUG_OFF
	
	errorlevel -302	; suppress banksel warning messages

; '__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.

; Load code release data in to EEPROM 
	org 0x2108
	de	" http://homoautomatus.co.uk "
	de	" Dave Critchley (c) 2010 "
	de	" AFR Meter for Innovate serial > PWM 1.0.0 "



;***** VARIABLE DEFINITIONS

	cblock 0x20
		SerFN			; Store Function Byte
		SerLH			; Lambda High Word
		SerLL			; Lambda Low Word
		Flags			; byte to store indicator flags
		LO_TEMP			; holds 8 bit binary for BCD Conversion
		TEMP1			; counter for BCD converter
		TEMP2			; working reg for BCD conversion
		temp			; temp storage
		ltmp			; lcd nibble holder
		cntmsec			; delay timer
		duty1
		duty2
		ldcnt
 	 
	endc

;****************************************************************************
; Defines and equates
;****************************************************************************
;Macros to select the register bank
;Many bank changes can be optimized when only one STATUS bit changes

Bank0		MACRO			;macro to select data RAM bank 0
		bcf	STATUS,RP0
		bcf	STATUS,RP1
		ENDM

Bank1		MACRO			;macro to select data RAM bank 1
		bsf	STATUS,RP0
		bcf	STATUS,RP1
		ENDM

Bank2		MACRO			;macro to select data RAM bank 2
		bcf	STATUS,RP0
		bsf	STATUS,RP1
		ENDM

Bank3		MACRO			;macro to select data RAM bank 3
		bsf	STATUS,RP0
		bsf	STATUS,RP1
		ENDM
		
EStrobe 	MACRO                   ;  Strobe the "E" Bit
  		bsf    LCD_E
  		nop
  		bcf    LCD_E
 		ENDM
		
;Constants

; base frequency
XTAL_FREQ	EQU	.4000000	; OSC freq in Hz

; BAUD_19200	EQU	(((10*XTAL_FREQ/(16*19200))+5)/10)-1

BAUD_19200	EQU	(XTAL_FREQ/(.16*.19200))-1

;----------------------------------------------------------------------------
;Bit Definitions

#define		BCflag	Flags,0x00	; blank checker for preceeding zeros
#define		RXflag	Flags,0x01	; Received Char Flag
#define		HiFlag	Flags,0x02	; Flag for High order bits of Lambda word
;
#define		LCD_RW	PORTB,5		; Bit 5 on PORTB
#define		LCD_RS	PORTB,4		; Bit 4 on PORTB
#define		LCD_E	PORTC,0		; Bit 0 on PORTC
#define		LED	PORTC,4		; Bit 4 on PORTC

;LCD_RW		equ    5    ; Bit 5 on PORTB
;LCD_RS		equ    4    ; Bit 4 on PORTB
;LCD_E 		equ    0    ; Bit 0 on PORTC

; string equates

CALIB		=	lCALIB-shomsg
_IN		=	l_IN-shomsg
PROGRESS	=	lPROGRESS-shomsg
REQUIRED	=	lREQUIRED-shomsg
WARMUP		=	lWARMUP-shomsg
HEATER		=	lHEATER-shomsg
BLANK		=	lBLANK-shomsg
BLANK3		=	lBLANK3-shomsg
LEAN		=	lLEAN-shomsg
RICH		=	lRICH-shomsg
AFR		=	lAFR-shomsg
ERRMSG		=	lERROR-shomsg
_O2		=	l_O2-shomsg
HELLO		=	l_HELLO-shomsg
AUTHOR		=	l_AUTHOR-shomsg


;**********************************************************************
		ORG	0x000		; processor reset vector
		include      "AFR_meter_code.asm"
	
		ORG	0x0020		; Strings start at address 20

shomsg:		addwf	PCL,f		; index into strings

lCALIB:		dt	"CALIB",0x00
l_IN:		dt	" IN",0x00
lPROGRESS:	dt	"PROGRESS",0x00
lREQUIRED:	dt	"REQUIRED",0x00
lWARMUP:	dt	"WARMUP  ",0x00	
lHEATER:	dt	"HEATER  ",0x00	
lBLANK:		dt	"        ",0x00	
lBLANK3		dt	"   ",0x00	
lLEAN:		dt	"LEAN",0x00
lRICH:		dt	"RICH",0x00	
lAFR:		dt	"AFR ",0x00
lERROR:		dt	"ERROR  ",0x00	
l_O2:		dt	"O2% ",0x00
l_HELLO:	dt	"AFR V1.0",0x00
l_AUTHOR:	dt	"DCX 2010",0x00


		ORG	0x0100		; AFR Table at address 100
		
getlambda:	addwf	PCL,f		; table contains AFR readings (7.3 to 22.3)


		include	      "LCD_AFR_Value_Table.inc"
	
		END           ; directive 'end of program'


