;--------------------------------------------- ;4-Digit Panel Meter Costs Less Than 6 Dollars ;Author: Jim Walker ;--------------------------------------------- errorlevel -302 ; ignore error when storing to Bank1 LIST P=16C711 #INCLUDE __CONFIG H'3FF1' ;MCLRE dis PWRTE dis int OS OSC IS XT ;Set AD-1 <1.2 volts for range of 5.10 volts in 20 mV steps (LEFT DIGIT BLANK) ;Set 1.3 < AD-1 <3.7 volts for range of 12.75 volts in 50 mv steps ;Set AD-1 > 3.8 volts for range of 25.5 volts in 100 mv steps (RIGHT DIGIT BLANK) ;-----------------EQUATES----------------------- ; in BANK 0 STAT EQU 0x03 ;Location of Status Register ; in BANK 2 OPT EQU 0x81 ;OPTION LOCATION ; general definitions W EQU 0 ;W is defined as "0" sends stuff to W register F EQU 1 ;F is defined as "1" sends stuff to F register C EQU 0 ;C is defined as "0" location in STATus Z EQU 2 ;Z is defined as "2" location in STATus ;----------------VARIABLES---------------------- MAX EQU 0x0C ;0, 1 or 2 MAX Digit of the LED 7-segment displays MID EQU 0x0D ;0 - 9 MID Digit of the LED 7-segment displays MIN EQU 0x0E ;0 - 9 MIN Digit of the LED 7-segment displays LOWD EQU 0x0F ;0 or 5 (if on 12-Volt scale) ) otherwise, MINIMUM DIGIT BIN EQU 0x10 ;location of ANALOG_0 variable DLYT EQU 0x11 ;location of DELAYT variable WORD EQU 0x12 ;WORD2 clear for 25.5 WORD,1 set for 12.75 WORD,0 for 5.10 TEM EQU 0x13 ;STORAGE for TEM variable RANGE EQU 0x14 ;store A/D one as RANGE ;----------------VECTORS------------------------ ORG 0x00 ;RESET vector GOTO RESET ;branch past table and subroutine ;----------------PIN OUT------------------------ ;RA2/AN2 LO = ENABLE LOWD pin 1 ;RA3/AN3/Vref LO = ENABLE MIN pin 2 ;RA4/TOCK1 LO = ENABLE MID pin 3 NOTE: OPEN-DRAIN OUTPUT ;~MCLR connect to +5 pin 4 ;GROUND pin 5 ;RB0/INT "A" pin 6 ;RB1 "B" pin 7 ;RB2 "C" pin 8 ;RB3 "D" pin 9 ;RB4 "E" pin 10 ;RB5 "F" pin 11 ;RB6 "G" pin 12 ;RB7 LO = ENABLE MAX pin 13 ;VCC +5 pin 14 ;OS2/CLKOUT CRYSTAL pin 15 ;OS1/CLKIN Or RESONATOR pin 16 ;RA0/AN0 VOLTAGE IN pin 17 ;RA1/AN1 0 V in = 5 volt range pin 18 2.5V = 12-volt range +5V = 25-volt range ;----------------TABLES----------------------- TABLE ADDWF PCL,1 ;PCL is now offset by imported value of W RETLW 0x3F ;data for 0011 1111 "0" RETLW 0x06 ;data for 0000 0110 "1" RETLW 0x5B ;data for 0101 1011 "2" RETLW 0x4F ;data for 0100 1111 "3" RETLW 0x66 ;data for 0110 0110 "4" RETLW 0x6D ;data for 0110 1101 "5" RETLW 0x7C ;data for 0111 1100 "6" RETLW 0x07 ;data for 0000 0111 "7" RETLW 0x7F ;data for 0111 1111 "8" RETLW 0x67 ;data for 0110 0111 "9" ;--------------------------------------------- DELAY CLRF DLYT ; DDDDD DECFSZ DLYT,F ;decrement DLYT, move back into self, check for ZERO GOTO DDDDD ;this takes about 770 uS RETURN ; ;--------------------------------------------- RESET BSF STATUS,5;Selects Page 1 MOVLW 0x00 ;Presets W register to 0000 0000 MOVWF TRISB ;loads W into TRISB = ALL OUT MOVLW 0x03 ;Presets W register to xxx0 0011 MOVWF TRISA ;loads W into TRISA bit 0,1 IN; all others=OUT MOVLW 0x02 ;presets W to 0000 0010 MOVWF ADCON1 ;A/D 0, A/D 1 IN; OTHERS=DIGITAL OUT BCF STATUS,5;Selects Page 0 MOVLW 0x41 ;presets W to 0100 0001 MOVWF ADCON0 ;Fosc/8 2 uS Use A/D 0 A/D ON CLRF PORTA ; CLRF PORTB ; CLRF MIN ; CLRF MID ; CLRF MAX ; CLRF LOWD ; CLRF WORD ; ;--------------------------------------------- DLOWD MOVF LOWD,W ;move LOWD to W CALL TABLE ;return from table with 7-seg drive ready MOVWF PORTB ;sets 7-seg drive in place BTFSC WORD,1 ;skip next if not 12 volts GOTO QQQ ;if 12 volts, skip to QQQ BTFSC WORD,0 ;skip next if not 5 volts GOTO QQQ ;if 5 volts, skip to QQQ CLRF PORTB ;blanks right most digit if 25.5-volt scale QQQ BSF PORTA,2 ;enable the LOWD driver CLRF WORD ; ;NEXT 24 lines GO OUT AND READ A/D1 and put result to RANGE ;Converts to proper WORD setting ;WORD,0 set = 5 volt WORD,1 set = 12-volt No WORD SET = 25-volt range SAM1 BSF ADCON0,3;select A/D channel ONE BSF INTCON,6;enable A/D interrupt BCF INTCON,7;Disable all interrupts (if set, "A/D DONE" goes to 04h) MOVLW 0x03 ;sets W to 0000 0011 MOVWF DLYT ;sets DLYT to 0000 0011 D12 DECFSZ DLYT,F ;decrement DLYT, move back into self, check for ZERO GOTO D12 ;this takes 11 uS BSF ADCON0,2;Start the A/D conversion (2 = GO) XXX1 BTFSC ADCON0,2;skip next if GO (ADCON,2) has cleared GOTO XXX1 ;do this until Conversion is done MOVF ADRES,W ;move ADRES to W MOVWF RANGE ; SELECT MOVLW 0xC0 ;sets W to 1100 0000 SUBWF RANGE,W ;RANGE minus W into W BTFSC STATUS,C;skip if C0h > RANGE (skip if not 25-volt) GOTO RRR ; MOVLW 0x40 ;sets W to 0100 0000 SUBWF RANGE,W ;RANGE minus W into W BTFSS STATUS,C;skip if 40h < RANGE (skip if not 5-volt) GOTO FIVE ; BSF WORD,1 ;set for 12 VOLT RANGE GOTO RRR ; FIVE BSF WORD,0 ;both upper bits of RANGE are 0; SET AS FIVE GOTO RRR ; RRR BCF ADCON0,3;select A/D channel ZERO CALL DELAY ; CALL DELAY ; BCF PORTA,2 ;disable the LOWD driver DMIN MOVF MIN,W ;move MIN to W CALL TABLE ;return from table with 7-seg drive ready MOVWF PORTB ;sets 7-seg drive in place BSF PORTA,3 ;enable the MIN driver ;---next 10 lines do an A/D sample of AD0 and store results in BIN--- SAMPLE BSF INTCON,6;enable A/D interrupt BCF INTCON,7;Disable all interrupts (if set, "A/D DONE" goes to 04h) MOVLW 0x03 ;sets W to 0000 0011 MOVWF DLYT ;sets DLYT to 0000 0011 D11 DECFSZ DLYT,F ;decrement DLYT, move back into self, check for ZERO GOTO D11 ;this takes 11 uS BSF ADCON0,2;Start the A/D conversion (2 = GO) XXX BTFSC ADCON0,2;skip next if GO (ADCON,2) has cleared GOTO XXX ;do this until conversion is done MOVF ADRES,W ;move ADRES to W MOVWF BIN ; CALL DELAY ; CALL DELAY ; BCF PORTA,3 ;disable the MIN driver DMID MOVF MID,W ;move MID to W CALL TABLE ;return from table with 7-seg drive ready MOVWF PORTB ;sets 7-seg drive in place BSF PORTA,4 ;enable the DMID driver CONVERT CLRF MAX ; CLRF MID ; CLRF MIN ; CLRF LOWD ; BTFSS WORD,1 ;skip next if 12-volt GOTO BBB ; V12.5 CLRF LOWD ; BTFSS BIN,0 ;tests lowest bit of BIN GOTO AAA ;keep LOWD as 0000 0000 MOVLW 0x05 ;sets W to 0000 0101 MOVWF LOWD ;set LOWD to "5" AAA BCF STATUS,C;sets CARRY BIT LOW RRF BIN,F ;was ABCD EFGH is now 0ABC DEFG BBB MOVLW 0x0A ;Set W to "10" decimal SUBWF BIN,W ;BIN minus W into W may affect STATUS BTFSS STATUS,C;if C is 1 skip next GOTO STUFF ;about 15 lines down MOVLW 0x0A ;Set W to "10" decimal SUBWF BIN,F ;BIN minus W into BIN INCF MID,F ;up the MID count TEMP123 BTFSC WORD,0 ;WORD 0 clear, skip next INCF MID,F ;do this for 5.10 count MOVLW 0x0A ;Set W to "10" decimal SUBWF MID,W ;MID minus W into W may affect STATUS BTFSS STATUS,Z;if Z is 1 skip next GOTO BBB ;about 15 lines up CLRF MID ;sets MID to 0000 0000 INCF MAX,F ;increment the MAX register GOTO BBB ;do this until BIN is less than 10 STUFF MOVF BIN,W ;move BIN to W BTFSS WORD,0 ;if set for 5-volts, skip next GOTO TTT ; MOVWF TEM ;move W to TEM MOVLW 0x05 ;sets W to 0000 0101 SUBWF TEM,W ;TEM minus 5 into W may affect STATUS BTFSS STATUS,C;not set means F is 5 or more, skip next GOTO SHIFT ;about 10 lines ahead MOVLW 0x05 ;sets W to 0000 0101 SUBWF TEM,F ;TEM minus 5 into F may affect Status INCF MID,F ;increments the MID register SHIFT BCF STATUS,C;Clear CARRY RLF TEM,F ;TEMP was 0000 0ABC (4 or less) is now 0000 ABC0 (8 or less) MOVF TEM,W ;move TEm to W TTT MOVWF MIN ;move W to MIN BTFSS WORD,0 ;IS 5-volt skip next GOTO VVV ;shift for 5.10-volt scale MOVF MIN,W ;move MIN to W MOVWF LOWD ;was MIN now is LOWD MOVF MID,W ;move MIN to W MOVWF MIN ;was MID now is MIN MOVF MAX,W ;move MAX to W MOVWF MID ;was MAX now is MID CLRF MAX ;only in 5-volt position VVV CALL DELAY ; CALL DELAY ; BCF PORTA,4 ;disable the MID driver DMAX MOVF MAX,W ;move MAX to W CALL TABLE ;return from table with 7-seg drive ready MOVWF PORTB ;sets 7-seg drive in place BTFSS WORD,0 ;if 5-volt skip next GOTO NNN ; CLRF PORTB ;blanks left most digit if 5.10-volt scale NNN BSF PORTB,7 ;enable the MAX driver CALL DELAY ; CALL DELAY ; BCF PORTB,7 ;disable the MAX driver GOTO DLOWD ; ;--------------------------------------------- END ;