MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 1 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00001 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00002 ; 00003 ; Project: Interfacing PICs 00004 ; Source File Name: INSTAMP.ASM 00005 ; Devised by: MPB 00006 ; Date: 21-12-05 00007 ; Status: Final 00008 ; 00009 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00010 ; 00011 ; Demonstrates simple analogue input 00012 ; using an external reference voltage of 2.56V 00013 ; The 8-bit result is converted to BCD for display 00014 ; as a voltage using the standard LCD routines. 00015 ; 00016 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00017 00018 PROCESSOR 16F877 00019 ; Clock = XT 4MHz, standard fuse settings 2007 3731 00020 __CONFIG 0x3731 00021 00022 ; LABEL EQUATES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 00023 00024 INCLUDE "P16F877A.INC" 00001 LIST 00002 ; P16F877A.INC Standard Header File, Version 1.00 Microchip Technology, Inc. Message[301]: MESSAGE: (Processor-header file mismatch. Verify selected processor.) 00398 LIST 00025 ; standard register labels 00026 00027 ;---------------------------------------------------------- 00028 ; User register labels 00029 ;---------------------------------------------------------- 00030 ; GPR 20 - 2F allocated to included LCD display routine 00031 00000030 00032 count EQU 30 ; Counter for ADC setup delay 00000031 00033 ADbin EQU 31 ; Binary input value 00000032 00034 huns EQU 32 ; Hundreds digit in decimal value 00000033 00035 tens EQU 33 ; Tens digit in decimal value 00000034 00036 ones EQU 34 ; Ones digit in decimal value 00037 00038 ;---------------------------------------------------------- 00039 ; PROGRAM BEGINS 00040 ;---------------------------------------------------------- 00041 0000 00042 ORG 0 ; Default start address 0000 0000 00043 NOP ; required for ICD mode 00044 00045 ;---------------------------------------------------------- 00046 ; Port & display setup 00047 0001 1683 1303 00048 BANKSEL TRISC ; Select bank 1 Message[302]: Register in operand not in bank 0. Ensure that bank bits are correct. MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 2 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0003 0188 00049 CLRF TRISD ; Display port is output 0004 3003 00050 MOVLW B'00000011' ; Analogue input setup code Message[302]: Register in operand not in bank 0. Ensure that bank bits are correct. 0005 009F 00051 MOVWF ADCON1 ; Left justify result, 00052 ; Port A = analogue inputs 00053 0006 1283 1303 00054 BANKSEL PORTC ; Select bank 0 0008 0188 00055 CLRF PORTD ; Clear display outputs 0009 3041 00056 MOVLW B'01000001' ; Analogue input setup code 000A 009F 00057 MOVWF ADCON0 ; f/8, RA0, done, enable 00058 000B 2067 00059 CALL inid ; Initialise the display 00060 00061 ;---------------------------------------------------------- 00062 ; MAIN LOOP 00063 ;---------------------------------------------------------- 00064 000C 2010 00065 start CALL getADC ; read input 000D 2019 00066 CALL condec ; convert to decimal 000E 2030 00067 CALL putLCD ; display input 000F 280C 00068 GOTO start ; jump to main loop 00069 00070 ;----------------------------------------------------------- 00071 ; SUBROUTINES 00072 ;----------------------------------------------------------- 00073 ; Read ADC input and store ................................. 00074 0010 3007 00075 getADC MOVLW 007 ; load counter 0011 00B0 00076 MOVWF count Message[305]: Using default destination of 1 (file). 0012 0BB0 00077 down DECFSZ count ; and delay 20us 0013 2812 00078 GOTO down 00079 0014 151F 00080 BSF ADCON0,GO ; start ADC.. 0015 191F 00081 wait BTFSC ADCON0,GO ; ..and wait for finish 0016 2815 00082 GOTO wait 00083 0017 081E 00084 MOVF ADRESH,W ; store result, high 8 bits 0018 0008 00085 RETURN 00086 00087 ;----------------------------------------------------------- 00088 ; Convert input to decimal 00089 0019 00B1 00090 condec MOVWF ADbin ; get ADC result 001A 01B2 00091 CLRF huns ; zero hundreds digit 001B 01B3 00092 CLRF tens ; zero tens digit 001C 01B4 00093 CLRF ones ; zero ones digit 00094 00095 ; Calclulate hundreds digit................................. 00096 001D 1403 00097 BSF STATUS,C ; set carry for subtract 001E 3064 00098 MOVLW D'100' ; load 100 Message[305]: Using default destination of 1 (file). MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 3 LOC OBJECT CODE LINE SOURCE TEXT VALUE 001F 02B1 00099 sub1 SUBWF ADbin ; and subtract from result Message[305]: Using default destination of 1 (file). 0020 0AB2 00100 INCF huns ; count number of loops 0021 1803 00101 BTFSC STATUS,C ; and check if done 0022 281F 00102 GOTO sub1 ; no, carry on 00103 Message[305]: Using default destination of 1 (file). 0023 07B1 00104 ADDWF ADbin ; yes, add 100 back on Message[305]: Using default destination of 1 (file). 0024 03B2 00105 DECF huns ; and correct loop count 00106 00107 ; Calculate tens and ones digit............................. 00108 0025 1403 00109 BSF STATUS,C ; repeat process for tens 0026 300A 00110 MOVLW D'10' ; load 10 Message[305]: Using default destination of 1 (file). 0027 02B1 00111 sub2 SUBWF ADbin ; and subtract from result Message[305]: Using default destination of 1 (file). 0028 0AB3 00112 INCF tens ; count number of loops 0029 1803 00113 BTFSC STATUS,C ; and check if done 002A 2827 00114 GOTO sub2 ; no, carry on 00115 Message[305]: Using default destination of 1 (file). 002B 07B1 00116 ADDWF ADbin ; yes, add 100 back on Message[305]: Using default destination of 1 (file). 002C 03B3 00117 DECF tens ; and correct loop count 002D 0831 00118 MOVF ADbin,W ; load remainder 002E 00B4 00119 MOVWF ones ; and store as ones digit 00120 002F 0008 00121 RETURN ; done 00122 00123 ;----------------------------------------------------------- 00124 ; Output to display 00125 0030 10F4 00126 putLCD BCF Select,RS ; set display command mode 0031 3080 00127 MOVLW 080 ; code to home cursor 0032 2057 00128 CALL send ; output it to display 0033 14F4 00129 BSF Select,RS ; and restore data mode 00130 00131 ; Convert digits to ASCII and display.......................... 00132 0034 3030 00133 MOVLW 030 ; load ASCII offset Message[305]: Using default destination of 1 (file). 0035 07B2 00134 ADDWF huns ; convert hundreds to ASCII Message[305]: Using default destination of 1 (file). 0036 07B3 00135 ADDWF tens ; convert tens to ASCII Message[305]: Using default destination of 1 (file). 0037 07B4 00136 ADDWF ones ; convert ones to ASCII 00137 0038 0832 00138 MOVF huns,W ; load hundreds code 0039 2057 00139 CALL send ; and send to display 003A 0833 00140 MOVF tens,W ; load tens code 003B 2057 00141 CALL send ; and output MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 4 LOC OBJECT CODE LINE SOURCE TEXT VALUE 003C 302E 00142 MOVLW '.' ; load point code 003D 2057 00143 CALL send ; and output 003E 0834 00144 MOVF ones,W ; load ones code 003F 2057 00145 CALL send ; and output 0040 3020 00146 MOVLW ' ' ; load space code 0041 2057 00147 CALL send ; and output 0042 306D 00148 MOVLW 'm' ; load volts code 0043 2057 00149 CALL send ; and output 0044 3056 00150 MOVLW 'V' ; load volts code 0045 2057 00151 CALL send ; and output 00152 0046 0008 00153 RETURN ; done 00154 00155 ;---------------------------------------------------------- 00156 ; INCLUDED ROUTINES 00157 ;---------------------------------------------------------- 00158 ; Include LCD driver routine 00159 ; 00160 INCLUDE "LCDIS.INC" 00001 ; LCDIS.INC MPB 19-12-05 00002 ; 00003 ; Include file to operate 16x2 LCD display 00004 ; Uses GPR 70 - 75 00005 ; 00006 ; Final version 00007 ;------------------------------------------------------------------------------------------- 00008 00000070 00009 Timer1 EQU 70 ; 1ms count register 00000071 00010 TimerX EQU 71 ; Xms count register 00000072 00011 Var EQU 72 ; Output variable 00000073 00012 Point EQU 73 ; Program table pointer 00000074 00013 Select EQU 74 ; Used to set or clear RS bit 00000075 00014 OutCod EQU 75 ; Temp store for output code 00015 00000001 00016 RS EQU 1 ; Register select output bit 00000002 00017 E EQU 2 ; Enable output clocks display input 00018 00019 00020 ;-------------------------------------------------------------------------------------------- 00021 ; 1ms delay with 1us cycle time (1000 cycles) 00022 ;-------------------------------------------------------------------------------------------- 0047 30F9 00023 onems MOVLW D'249' ; Count for 1ms delay 0048 00F0 00024 MOVWF Timer1 ; Load count 0049 0000 00025 loop1 NOP ; Pad for 4 cycle loop Message[305]: Using default destination of 1 (file). 004A 0BF0 00026 DECFSZ Timer1 ; Count 004B 2849 00027 GOTO loop1 ; until Z 004C 0008 00028 RETURN ; and finish 00029 00030 ;--------------------------------------------------------------------------------------------- 00031 ; Delay Xms 00032 ; Receives count in W, uses Onems 00033 ;--------------------------------------------------------------------------------------------- MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 5 LOC OBJECT CODE LINE SOURCE TEXT VALUE 004D 00F1 00034 xms MOVWF TimerX ; Count for X ms 004E 2047 00035 loopX CALL onems ; Delay 1ms Message[305]: Using default destination of 1 (file). 004F 0BF1 00036 DECFSZ TimerX ; Repeat X times 0050 284E 00037 GOTO loopX ; until Z 0051 0008 00038 RETURN ; and finish 00039 00040 ;--------------------------------------------------------------------------------------------- 00041 ; Generate data/command clock siganl E 00042 ;--------------------------------------------------------------------------------------------- 0052 1508 00043 pulseE BSF PORTD,E ; Set E high 0053 2047 00044 CALL onems ; Delay 1ms 0054 1108 00045 BCF PORTD,E ; Reset E low 0055 2047 00046 CALL onems ; Delay 1ms 0056 0008 00047 RETURN ; done 00048 00049 ;--------------------------------------------------------------------------------------------- 00050 ; Send a command byte in two nibbles from RB4 - RB7 00051 ; Receives command in W, uses PulseE and Onems 00052 ;--------------------------------------------------------------------------------------------- 0057 00F5 00053 send MOVWF OutCod ; Store output code 0058 39F0 00054 ANDLW 0F0 ; Clear low nybble 0059 0088 00055 MOVWF PORTD ; Output high nybble 005A 18F4 00056 BTFSC Select,RS ; Test RS bit 005B 1488 00057 BSF PORTD,RS ; and set for data 005C 2052 00058 CALL pulseE ; and clock display register 005D 2047 00059 CALL onems ; wait 1ms for display to complete 00060 Message[305]: Using default destination of 1 (file). 005E 0EF5 00061 SWAPF OutCod ; Swap low and high nybbles 005F 0875 00062 MOVF OutCod,W ; Retrieve output code 0060 39F0 00063 ANDLW 0F0 ; Clear low nybble 0061 0088 00064 MOVWF PORTD ; Output low nybble 0062 18F4 00065 BTFSC Select,RS ; Test RS bit 0063 1488 00066 BSF PORTD,RS ; and set for data 0064 2052 00067 CALL pulseE ; and clock display register 0065 2047 00068 CALL onems ; wait 1ms for display to complete 0066 0008 00069 RETURN ; done 00070 00071 ;--------------------------------------------------------------------------------------------- 00072 ; Initialise the display 00073 ; Uses Send 00074 ;--------------------------------------------------------------------------------------------- 0067 3064 00075 inid MOVLW D'100' ; Load count for 100ms delay 0068 204D 00076 CALL xms ; and wait for display start 0069 30F0 00077 MOVLW 0F0 ; Mask for select code 006A 00F4 00078 MOVWF Select ; High nybble not masked 00079 006B 3030 00080 MOVLW 0x30 ; Load initial nibble 006C 0088 00081 MOVWF PORTD ; and output it to display 006D 2052 00082 CALL pulseE ; Latch initial code 006E 3005 00083 MOVLW D'5' ; Set delay 5ms 006F 204D 00084 CALL xms ; and wait MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 6 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0070 2052 00085 CALL pulseE ; Latch initial code again 0071 2047 00086 CALL onems ; Wait 1ms 0072 2052 00087 CALL pulseE ; Latch initial code again 0073 1208 00088 BCF PORTD,4 ; Set 4-bit mode 0074 2052 00089 CALL pulseE ; Latch it 00090 0075 3028 00091 MOVLW 0x28 ; Set 4-bit mode, 2 lines 0076 2057 00092 CALL send ; and send code 0077 3008 00093 MOVLW 0x08 ; Switch off display 0078 2057 00094 CALL send ; and send code 0079 3001 00095 MOVLW 0x01 ; Code to clear display 007A 2057 00096 CALL send ; and send code 007B 3006 00097 MOVLW 0x06 ; Enable cursor auto inc 007C 2057 00098 CALL send ; and send code 007D 3080 00099 MOVLW 0x80 ; Zero display address 007E 2057 00100 CALL send ; and send code 007F 300C 00101 MOVLW 0x0C ; Turn on display 0080 2057 00102 CALL send ; and send code 00103 0081 0008 00104 RETURN ; Done 00105 00161 ; Contains routines: 00162 ; init: Initialises display 00163 ; onems: 1 ms delay 00164 ; xms: X ms delay 00165 ; Receives X in W 00166 ; send: sends a character to display 00167 ; Receives: Control code in W (Select,RS=0) 00168 ; ASCII character code in W (RS=1) 00169 ; 00170 ;---------------------------------------------------------- 00171 END ; of source code MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 7 SYMBOL TABLE LABEL VALUE ACKDT 00000005 ACKEN 00000004 ACKSTAT 00000006 ADCON0 0000001F ADCON1 0000009F ADCS0 00000006 ADCS1 00000007 ADDEN 00000003 ADFM 00000007 ADIE 00000006 ADIF 00000006 ADON 00000000 ADRESH 0000001E ADRESL 0000009E ADbin 00000031 BCLIE 00000003 BCLIF 00000003 BF 00000000 BRGH 00000002 C 00000000 C1INV 00000004 C1OUT 00000006 C2INV 00000005 C2OUT 00000007 CCP1CON 00000017 CCP1IE 00000002 CCP1IF 00000002 CCP1M0 00000000 CCP1M1 00000001 CCP1M2 00000002 CCP1M3 00000003 CCP1X 00000005 CCP1Y 00000004 CCP2CON 0000001D CCP2IE 00000000 CCP2IF 00000000 CCP2M0 00000000 CCP2M1 00000001 CCP2M2 00000002 CCP2M3 00000003 CCP2X 00000005 CCP2Y 00000004 CCPR1H 00000016 CCPR1L 00000015 CCPR2H 0000001C CCPR2L 0000001B CHS0 00000003 CHS1 00000004 CHS2 00000005 CIS 00000003 CKE 00000006 CKP 00000004 CM0 00000000 MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 8 SYMBOL TABLE LABEL VALUE CM1 00000001 CM2 00000002 CMCON 0000009C CMIE 00000006 CMIF 00000006 CREN 00000004 CSRC 00000007 CVR0 00000000 CVR1 00000001 CVR2 00000002 CVR3 00000003 CVRCON 0000009D CVREN 00000007 CVROE 00000006 CVRR 00000005 D 00000005 DATA_ADDRESS 00000005 DC 00000001 D_A 00000005 E 00000002 EEADR 0000010D EEADRH 0000010F EECON1 0000018C EECON2 0000018D EEDATA 0000010C EEDATH 0000010E EEIE 00000004 EEIF 00000004 EEPGD 00000007 F 00000001 FERR 00000002 FSR 00000004 GCEN 00000007 GIE 00000007 GO 00000002 GO_DONE 00000002 I2C_DATA 00000005 I2C_READ 00000002 I2C_START 00000003 I2C_STOP 00000004 IBF 00000007 IBOV 00000005 INDF 00000000 INTCON 0000000B INTE 00000004 INTEDG 00000006 INTF 00000001 IRP 00000007 NOT_A 00000005 NOT_ADDRESS 00000005 NOT_BO 00000000 NOT_BOR 00000000 NOT_DONE 00000002 MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 9 SYMBOL TABLE LABEL VALUE NOT_PD 00000003 NOT_POR 00000001 NOT_RBPU 00000007 NOT_RC8 00000006 NOT_T1SYNC 00000002 NOT_TO 00000004 NOT_TX8 00000006 NOT_W 00000002 NOT_WRITE 00000002 OBF 00000006 OERR 00000001 OPTION_REG 00000081 OutCod 00000075 P 00000004 PCFG0 00000000 PCFG1 00000001 PCFG2 00000002 PCFG3 00000003 PCL 00000002 PCLATH 0000000A PCON 0000008E PEIE 00000006 PEN 00000002 PIE1 0000008C PIE2 0000008D PIR1 0000000C PIR2 0000000D PORTA 00000005 PORTB 00000006 PORTC 00000007 PORTD 00000008 PORTE 00000009 PR2 00000092 PS0 00000000 PS1 00000001 PS2 00000002 PSA 00000003 PSPIE 00000007 PSPIF 00000007 PSPMODE 00000004 Point 00000073 R 00000002 RBIE 00000003 RBIF 00000000 RC8_9 00000006 RC9 00000006 RCD8 00000000 RCEN 00000003 RCIE 00000005 RCIF 00000005 RCREG 0000001A RCSTA 00000018 RD 00000000 MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 10 SYMBOL TABLE LABEL VALUE READ_WRITE 00000002 RP0 00000005 RP1 00000006 RS 00000001 RSEN 00000001 RX9 00000006 RX9D 00000000 R_W 00000002 S 00000003 SEN 00000000 SMP 00000007 SPBRG 00000099 SPEN 00000007 SREN 00000005 SSPADD 00000093 SSPBUF 00000013 SSPCON 00000014 SSPCON2 00000091 SSPEN 00000005 SSPIE 00000003 SSPIF 00000003 SSPM0 00000000 SSPM1 00000001 SSPM2 00000002 SSPM3 00000003 SSPOV 00000006 SSPSTAT 00000094 STATUS 00000003 SYNC 00000004 Select 00000074 T0CS 00000005 T0IE 00000005 T0IF 00000002 T0SE 00000004 T1CKPS0 00000004 T1CKPS1 00000005 T1CON 00000010 T1INSYNC 00000002 T1OSCEN 00000003 T1SYNC 00000002 T2CKPS0 00000000 T2CKPS1 00000001 T2CON 00000012 TMR0 00000001 TMR0IE 00000005 TMR0IF 00000002 TMR1CS 00000001 TMR1H 0000000F TMR1IE 00000000 TMR1IF 00000000 TMR1L 0000000E TMR1ON 00000000 TMR2 00000011 MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 11 SYMBOL TABLE LABEL VALUE TMR2IE 00000001 TMR2IF 00000001 TMR2ON 00000002 TOUTPS0 00000003 TOUTPS1 00000004 TOUTPS2 00000005 TOUTPS3 00000006 TRISA 00000085 TRISB 00000086 TRISC 00000087 TRISD 00000088 TRISE 00000089 TRISE0 00000000 TRISE1 00000001 TRISE2 00000002 TRMT 00000001 TX8_9 00000006 TX9 00000006 TX9D 00000000 TXD8 00000000 TXEN 00000005 TXIE 00000004 TXIF 00000004 TXREG 00000019 TXSTA 00000098 Timer1 00000070 TimerX 00000071 UA 00000001 Var 00000072 W 00000000 WCOL 00000007 WR 00000001 WREN 00000002 WRERR 00000003 Z 00000002 _BODEN_OFF 00003FBF _BODEN_ON 00003FFF _CPD_OFF 00003FFF _CPD_ON 00003EFF _CP_ALL 00001FFF _CP_OFF 00003FFF _DEBUG_OFF 00003FFF _DEBUG_ON 000037FF _HS_OSC 00003FFE _LP_OSC 00003FFC _LVP_OFF 00003F7F _LVP_ON 00003FFF _PWRTE_OFF 00003FFF _PWRTE_ON 00003FF7 _RC_OSC 00003FFF _WDT_OFF 00003FFB _WDT_ON 00003FFF _WRT_1FOURTH 00003BFF MPASM 03.70.01 Released INSTAMP.ASM 3-28-2006 22:38:49 PAGE 12 SYMBOL TABLE LABEL VALUE _WRT_256 00003DFF _WRT_HALF 000039FF _WRT_OFF 00003FFF _XT_OSC 00003FFD __16F877 00000001 condec 00000019 count 00000030 down 00000012 getADC 00000010 huns 00000032 inid 00000067 loop1 00000049 loopX 0000004E onems 00000047 ones 00000034 pulseE 00000052 putLCD 00000030 send 00000057 start 0000000C sub1 0000001F sub2 00000027 tens 00000033 wait 00000015 xms 0000004D MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0080 : XX-------------- ---------------- ---------------- ---------------- 2000 : -------X-------- ---------------- ---------------- ---------------- All other memory blocks unused. Program Memory Words Used: 130 Program Memory Words Free: 8062 Errors : 0 Warnings : 0 reported, 0 suppressed Messages : 18 reported, 0 suppressed Error[173] : source file path exceeds 62 characters (C:\A PIC INTERFACING BOOK PROJECT\PIC INTERFACING BOOK APPLICATIONS MARCH 06\A PPLICATIONS\CHAP07\INSTAMP\INSTAMP.ASM) Error[173] : source file path exceeds 62 characters (C:\A PIC INTERFACING BOOK PROJECT\PIC INTERFACING BOOK APPLICATIONS MARCH 06\A PPLICATIONS\CHAP07\INSTAMP\P16F877A.INC) Error[173] : source file path exceeds 62 characters (C:\A PIC INTERFACING BOOK PROJECT\PIC INTERFACING BOOK APPLICATIONS MARCH 06\A PPLICATIONS\CHAP07\INSTAMP\LCDIS.INC)