* BAROMETER APPLICATIONS PROJECT 	- Chris Winkler* Developed: October 1st, 1992* This code will be used to implement an MC68HC11 Micro-Controller*	as a processing unit for a simple barometer system.* The HC11 will interface with an MPX2100AP to monitor,store*	and display measured Barometric pressure via the 8-bit A/D channel* The sensor output (32mv max) will be amplified to .5 - 2.5 V dc* The processor will interface with a 4-digit LCD (FE202) via*	a Motorola LCD driver (MC145453) to display the pressure*	within +/- one tenth of an inch of mercury.*	The systems range is 15.0 - 30.5 in. Hg*	A/D  & CPU Register Assigment*	This code will use index addressing to access the*	important control registers.  All addressing will be*	indexed off of REGBASE, the base address for these registers.REGBASE		EQU		$1000				* register base assigment of control register ADCTL		EQU		$30					* offest of A/D conversion control registerADR2		EQU		$32					* offset of A/D results registerADOPT		EQU		$39					* offset for A/D option register locationPORTB		EQU		$04					* Location of PORTB used for conversionPORTD		EQU		$08					*  PORTD Data Register IndexDDRD		EQU		$09					* offset of Data Direction Reg.SPCR		EQU		$28					* offset of SPI Control Reg.SPSR		EQU		$29					* offset of SPI Status Reg.	SPDR		EQU		$2A					* offset of SPI Data Reg.*	User Variables*	The following locations are used to store important measurements*	and calculations used in determining the altitude.  They *	are located in the lower 256 bytes of user RAMDIGIT1		EQU		$0001				* BCD blank digit (not used)DIGIT2		EQU		$0002				* BCD tens digit for pressureDIGIT3		EQU		$0003				* BCD tenths digit for pressureDIGIT4		EQU		$0004				* BCD ones digit for pressureCOUNTER		EQU		$0005				* Variable to send 5 dummy bytesPOFFSET		EQU		$0010				* Storage Location for max pressure offsetSENSOUT		EQU		$0012				* Storage location for previous conversionRESULT		EQU		$0014				* Storage of Pressure(in Hg) in hex formatFLAG		EQU		$0016				* Determines if measurement is withing range*	MAIN PROGRAM*	The conversion process involves the following steps:**			1.		Set-Up SPI device-			SPI_CNFG*			2.		Set-Up A/D, Constants		SET_UP*			3.		Read A/D, store sample 		ADCONV*			4.		Convert into in. Hg			IN_HG*			5.		Determine FLAG condition	IN_HG*				a.	Display error 				ERROR*				b.	Continue Conversion			INRANGE*			6.		Convert hex to BCD format	BCDCONV*			7.		Convert LCD display digits	LOOKUP*			8.		Output via SPI to LCD		SPI2LCD*	This process is continually repeated as the loop CONVERT*	runs unconditionally through BRA (the BRANCH ALWAYS statement)*	Repeats to step 3 indefinitely.						ORG		$C000				* DESIGNATES START OF MEMORY MAP FOR USER CODE			LDX		#REGBASE			* Location of base register for indirect adr			BSR		SPI_CNFG			* Set-up SPI Module for data X-mit to LCD			BSR		SET_UP				* Power-Up A/D, initialize constantsCONVERT		BSR		ADCONV				* Calls subroutine to make an A/D conversion 			BSR		DELAY				* Delay routine to prevent LCD flickering			BSR		IN_HG				* Converts hex format to in of Hg			*	The value of FLAG passed from IN_HG is used to determine*	If a range error has occurred.  The following logical*	statements are used to either allow further conversion or jump*	to a routine to display a range error message.			LDAB	FLAG				* Determines if an range Error has occured			CMPB	#$80				* If No Error detected (FLAG=$80) then			BEQ		INRANGE				*   system will continue conversion process													BSR		ERROR				* If error occurs (FLAG<>80), branch to ERROR				BRA		OUTPUT				* Branches to output ERROR code to display*	No Error Detected, Conversion Process ContinuesINRANGE		JSR		BCDCONV				* Converts Hex Result to BCD			JSR		LOOKUP				* Uses Look-Up Table for BCD-Decimal	OUTPUT		JSR		SPI2LCD				* Output transmission to LCD			BRA		CONVERT				* Continually converts using Branch Always 			*	Subroutine SPI_CNFG	*		Purpose is to initiliaze SPI for transmission*	 	and clear the display before conversion.SPI_CNFG	BSET	PORTD,X #$20		* Set SPI SS Line High to prevent glitch			LDAA	#$38				* Initializing Data Direction for Port D			STAA	DDRD,X				* Selecting SS, MOSI, SCK as outputs only						LDAA	#$5D				* Initialize SPI-Control Register			STAA	SPCR,X				* selecting SPE,MSTR,CPOL,CPHA,CPRO						LDAA	#$5					* sets counter to X-mit 5 blank bytes			STAA	COUNTER			LDAA	SPSR,X				* Must read SPSR to clear SPIF Flag						CLRA						* Transmission of Blank Bytes to LCD			ERASELCD	JSR		TRANSFER			* Calls subroutine to transmit			DEC 	COUNTER			BNE		ERASELCD								RTS	*	Subroutine SET_UP	*		Purpose is to intialize constants and to power-up A/D*		and to initialize POFFSET used in conversion purposes.				SET_UP		LDAA	#$90				* selects ADPU bit in OPTION register			STAA	ADOPT,X				* Power-Up of A/D complete			LDD		#$0131+$001A		* Initialize POFFSET 			STD		POFFSET				* POFFSET = 305 - 25 in hex			LDAA	#$00				* or Pmax + offset voltage (5 V)			RTS*	Subroutine DELAY	*		Purpose is to delay the conversion process  *		to minimize LCD flickering.						DELAY		LDA	#$FF					* Loop for delay of display	OUTLOOP		LDB	#$FF					* Delay = clk/255*255INLOOP		DECB			BNE	INLOOP			DECA			BNE	OUTLOOP			RTS*	Subroutine ADCONV*		Purpose is to read the A/D input, store the conversion into*		SENSOUT. For conversion purposes later.ADCONV		LDX		#REGBASE			* loads base register for indirect addressing			LDAA	#$25							STAA	ADCTL,X				* initializes A/D cont. register SCAN=1,MULT=0			WTCONV		BRCLR	ADCTL,X	#$80 WTCONV	* Wait for completion of conversion flag			LDAB	ADR2,X				* Loads conversion result into Accumulator					CLRA			STD		SENSOUT				* Stores conversion as SENSOUT														RTS*	Subroutine IN_HG*		Purpose is to convert the measured pressure SENSOUT, into*		units of in. Hg, represented by a hex value of 305-150*		This represents the range 30.5 - 15.0 in. HgIN_HG		LDD		POFFSET				* Loads maximum offset for subtraction			SUBD	SENSOUT				* RESULT = POFFSET-SENSOUT in hex format					STD		RESULT				* Stores hex result for P, in Hg						CMPD	#305			BHI		TOHIGH						CMPD	#150			BLO		TOLOW					LDAB	#$80			STAB	FLAG			BRA		END_CONV			TOHIGH		LDAB	#$FF			STAB	FLAG			BRA		END_CONV			TOLOW		LDAB	#$00			STAB	FLAGEND_CONV	RTS*	Subroutine ERROR*			This subroutine sets the display digits to output*			an error message having detected an out of range *			measurement in the main program from FLAGERROR		LDAB	#$00				* Initialize digits 1,4 to blanks			STAB	DIGIT1							STAB	DIGIT4						LDAB	FLAG				* FLAG is used to determine			CMPB	#$00				* if above or below range.			BNE		SET_HI				* If above range GOTO SET_HI			LDAB	#$0E				* ELSE display LO on display			STAB	DIGIT2				* Set DIGIT2=L,DIGIT3=O			LDAB	#$7E			STAB	DIGIT3			BRA		END_ERR				* GOTO exit of subroutine			SET_HI		LDAB	#$37				* Set DIGIT2=H,DIGIT3=1			STAB	DIGIT2			LDAB	#$30			STAB	DIGIT3END_ERR		RTS												*	Subroutine BCDCONV	*			Purpose is to convert ALTITUDE from hex to BCD*			uses standard HEX-BCD conversion scheme*			Divide HEX/10 store Remainder, swap Q & R, repeat*			process until remainder = 0.BCDCONV		LDAA	#$00				* Default Digits 2,3,4 to 0			STAA	DIGIT2			STAA	DIGIT3			STAA	DIGIT4			LDY		#DIGIT4				* Conversion starts with lowest digit			LDD		RESULT				* Load voltage to be convertedCONVLP		LDX		#$A					* Divide hex digit by 10			IDIV						* Quotient in X, Remainder in D			STAB	0,Y					* stores 8 LSB's of remainder as BCD digit			DEY			CPX		#$0					* Determines if last digit stored			XGDX						* Exchanges remainder & quotient			BNE		CONVLP			LDX		#REGBASE			* Reloads BASE into main program			RTS			*	Subroutine LOOKUP	*			Purpose is to implement a Look-Up conversion*			The BCD is used to index off of TABLE*			where the appropriate hex code to display*			that decimal digit is contained.*			DIGIT4,3,2 are converted only.LOOKUP		LDX		#DIGIT1+4			* Counter starts at 5TABLOOP		DEX							* Start with Digit4			LDY		#TABLE				* Loads table base into Y-pointer					LDAB	0,X					* Loads current digit into B 			ABY							* Adds to base to index off TABLE			LDAA	0,Y					* Stores HEX segment result in A			STAA	0,X			CPX		#DIGIT2				* Loop condition complete, DIGIT2 Converted			BNE		TABLOOP			RTS			*	Subroutine SPI2LCD	Purpose is to output digits to LCD via SPI*						The format for this is to send a start byte,*						four digits, and a stop byte. This system*						will have 3 significant digits: blank digit*						and three decimal digits.*						Sending LCD Start ByteSPI2LCD		LDX		#REGBASE			LDAA	SPSR,X				* Reads to clear SPIF flag			LDAA	#$02				* Byte, no colon, start bit			BSR		TRANSFER			* Transmit byte			*						Initializing decimal point & blank digit					LDAA	DIGIT3				* Sets MSB for decimal pt.			ORA		#$80				* after digit 3			STAA	DIGIT3			LDAA	#$00				* Set 1st digit as blank			STAA	DIGIT1			*						Sending four decimal digits			LDY		#DIGIT1				* Pointer set to send 4 bytesDLOOP		LDAA	0,Y					* Loads digit to be x-mitted			BSR		TRANSFER			* Transmit byte			INY							* Branch until both bytes sent			CPY		#DIGIT4+1			BNE		DLOOP						*						Sending LCD Stop Byte			LDAA	#$00				* end byte requires all 0's			BSR		TRANSFER			* Transmit byte			RTS				*	Subroutine TRANSFER	*		Purpose is to send data bits to SPI*		and wait for conversion complete flag bit to be set.						TRANSFER	LDX		#REGBASE			BCLR	PORTD,X #$20		* Assert SS Line to start X-misssion			STAA	SPDR,X				* Load Data into Data Reg.,X-mitXMIT		BRCLR	SPSR,X  #$80 XMIT	* Wait for flag			BSET	PORTD,X #$20		* DISASSERT SS Line			LDAB	SPSR,X				* Read to Clear SPI Flag									RTS*	Location for FCB memory for look-up table*	There are 11 possible digits: blank, 0-9TABLE		FCB		$7E,$30,$6D,$79,$33,$5B,$5F,$70,$7F,$73,$00						END