list p=16f877 include __CONFIG _PWRTE_ON&_HS_OSC&_LVP_ON&_WDT_OFF&_DEBUG_OFF ;a/d channels in a round robin format ;rc oscillator ;reference(vdd) ;one channel is selected and 10 bit result is converted to bcd and then to ascii and is displayed on LCD LCD_DATA EQU PORTB LCD_DATA_TRIS EQU TRISB LCD_CNTL EQU PORTD E EQU 5 RS EQU 6 RW EQU 7 ovflw equ 0 PC equ 0x02 tenk equ 0x20 onek equ 0x21 hund equ 0x22 ten equ 0x23 one equ 0x24 ms equ 0x25 ls equ 0x26 index equ 0x27 dig_ctr equ 0x28 lsb1 equ 0x29 msb1 equ 0x2a lsb2 equ 0x2b msb2 equ 0x2c flags equ 0x2d temp equ 0x2e s1 equ 30h s2 equ 31h s3 equ 32h s4 equ 33h s5 equ 34h char equ 35h charpos equ 36h TEMP equ 38h s6 equ 37h adif equ 1 adgo equ 2 org 00h banksel TRISB clrf TRISA clrf TRISB clrf TRISD movlw 0xff movwf TRISA banksel PORTB clrf PORTB clrf PORTD clrf PORTA clrf index movlw 0x04 movwf charpos call INITLCD call initad banksel ADCON1 bsf ADCON1,adgo loop1 btfsc ADCON1,adgo goto loop1 goto update update call HEX2BCD call bcdtoa call LCD_OUT NextAd call NextChannel call Delay banksel ADCON0 bcf ADCON0,adif bsf ADCON0,adgo loop banksel ADCON0 btfsc ADCON0,adgo goto loop goto update HEX2BCD movlw ADRESH movwf ms bsf STATUS,RP1 movlw ADRESL banksel PORTA movwf l movf ms,w movwf msb2 movf ls,w movwf lsb2 call dblb2d return tbl_lo addwf PC,f ; add index to program counter retlw 0x10 ; low byte for 10000 retlw 0xe8 ; low byte for 1000 retlw 0x64 ; low byte for 100 retlw 0x0a ; low byte for 10 tbl_hi addwf PC,f ; add index to program counter retlw 0x27 ; high byte for 10000 retlw 0x03 ; high byte for 1000 retlw 0x00 ; high byte for 100 retlw 0x00 ; high byte for 10 dblb2d clrf tenk ; clear the 10000's output register clrf onek ; clear the 1000's output register clrf hund ; clear the 100's output register clrf ten ; clear the 10's output register clrf one ; clear the 1's output register clrf index clrf dig_ctr ; clear counter bcf flags,ovflw ; clear the overflow bit in flags register subtr movf index,w ; get current index into W call tbl_lo ; get LS chunk for subtraction movwf lsb1 movf index,w ; get current index into W call tbl_hi ; get MS chunk for subtraction movwf msb1 call dblsub ; go to double precision subtraction btfsc STATUS,C ; test carry flag (-ve number) goto incdig movlw 0x20 ; move base address of table of the output values movwf FSR movf index,w ;get index addwf FSR,f ; add offset movf dig_ctr,w ; get digit counter contents movwf INDF ; store at digit loc (indexed) movf index,w ; get index call tbl_lo ; get LS chunk for addition (to restore a +ve number) movwf lsb1 movf index,w ; get index call tbl_hi ; get MS chunk for addition movwf msb1 bcf flags,ovflw call dblplus ; call the double precision addition movf index,w ; get index sublw 0x03 ; test if index=3 btfsc STATUS,Z ; if index=3, we are done goto finish incf index,f ; if index!=3, increment index clrf dig_ctr ; clear digit count for the next output goto subtr incdig incf dig_ctr,f ; increment digit counter goto subtr finish movf lsb2,w ;get the ones "reminder after getting tens, hund. ..." movwf one ;save in the one register return dblsub bcf flags,ovflw ; clear the overflow flag comf lsb1,f ; calculate the 1's complement of the LSB for subtraction comf msb1,f ; calculate the 1's complement of the MSB for subtraction movf lsb1,w addlw 0x01 ; add 1 to the LSB 1st complement to get the 2's complement movwf lsb1 btfsc STATUS,C ; test the carry flag, if a carry occurred incf msb1,f ; add 1 to the MSB register dblplus movf lsb1,w addwf lsb2,f ; add the low byte btfsc STATUS,C ; test the carry flag bsf flags,ovflw ; indicate overflow movf msb1,w addwf msb2,f ; add high bytes btfss flags,ovflw goto dblx btfsc STATUS,C goto dbll movlw 0x01 addwf msb2,f goto dblx dbll incf msb2,f dblx return bcdtoa movlw 0x30 addwf tenk,1 addwf onek,1 addwf hund,1 addwf ten,1 addwf one,1 return INITLCD movlw d'100' movwf s5 c5 call delay_1ms decfsz s5 goto c5 movlw 0x030 movwf LCD_DATA bsf LCD_CNTL,E bcf LCD_CNTL,E movlw d'6' movwf s5 c1 call delay_1ms decfsz s5 goto c1 movlw 0x030 movwf LCD_DATA bsf LCD_CNTL,E bcf LCD_CNTL,E call delay_1ms movlw 0x030 movwf LCD_DATA bsf LCD_CNTL,E bcf LCD_CNTL,E movlw 0x38 ;8-bit,2 line 5X7 dots call writeins movlw 0x00f ;display-on,cursor-on,blinking cursor call writeins movlw 0x01 ;clear display call writeins movlw 0x06 ;increment mode call writeins return LCD_OUT movlw 80 call writeins movlw 0x04 movwf charpos movlw 0x21 movwf FSR rpt1 movf INDF,w call writedata incf FSR decfsz charpos goto rpt1 return writedata movwf char call busy_check movf char,w movwf LCD_DATA bcf LCD_CNTL,RW bsf LCD_CNTL,RS bsf LCD_CNTL,E bcf LCD_CNTL,E return writeins movwf char call busy_check movf char,w movwf LCD_DATA bcf LCD_CNTL,RW bcf LCD_CNTL,RS bsf LCD_CNTL,E bcf LCD_CNTL,E return busy_check bsf STATUS,RP0 movlw 0xff movwf LCD_DATA_TRIS bcf STATUS,RP0 bcf LCD_CNTL,RS bsf LCD_CNTL,RW bsf LCD_CNTL,E bcf LCD_CNTL,E movf LCD_DATA,w movwf temp btfsc temp,7 goto busy_check bcf LCD_CNTL,RW bsf STATUS,RP0 movlw 0x00 movwf LCD_DATA_TRIS bcf STATUS,RP0 return delay_1ms movlw d'5' movwf s1 c4 movlw d'111' movwf s2 c3 decfsz s2 goto s3 decfsz s1 goto c4 return initad bsf STATUS,RP0 movlw b'00000000' movwf ADCON1 bcf STATUS,RP0 movlw b'11000001' movwf ADCON0 clrf ADRESH banksel TRISA clrf ADRESL banksel PORTA return NextChannel banksel ADCON0 movlw 0x08 addwf ADCON0,F bcf ADCON0,5 return Delay movlw d'29' movwf s6 t1 decfsz s6 goto t1 return end