; * ; Filename: Lin_mtr.asm * ; Date: * ; File Version: Linear Scale * ; * ; Author: jr7dut * ; Company: * ; * ; * ;********************************************************************** ; * ; Files Required: P16F818.INC * ; * ;********************************************************************** ; * ; Notes: * ; * ;********************************************************************** list p=16f818 ; list directive to define processor #include ; processor specific variable definitions errorlevel -302 ; suppress message 302 from list file __CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _DEBUG_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO ; '__CONFIG' directive is used to embed configuration word within .asm file. ; The lables following the directive are located in the respective .inc file. ; See data sheet for additional information on configuration word settings. ;***** VARIABLE DEFINITIONS w_temp EQU 0x07E ; variable used for context saving status_temp EQU 0x07F ; variable used for context saving nAcq EQU 0x020 ADL0 EQU 0x030 ;********************************************************************** ORG 0x000 ; processor reset vector goto main ; go to beginning of program ;================================================================================ ORG 0x004 ORG 0x010 main clrf PORTA clrf PORTB ; INTOSC set bcf STATUS,RP1 bsf STATUS,RP0 ;Select Bank1 movlw b'01110000' ;8MHz clock movwf OSCCON ;Initialize TMR0 ;TMR0 for abt 0.125*4*256us=0.128ms (abt 0.1ms) timer movlw b'11010111' ;Set OPTION_REG ; RBPU Pull up Disable=1 bit7 ; INTEDG inc or dec...no concern bit6 ; TMR0 Clock source 0 = Internal Clock bit5 ; TMR0 edge inc or dec...no concern bit4 ; PSA=0 Prescaler to TMR0 bit3 ; PS2-0=1,1,1 Prescaler 1:256 bit2-0 movwf OPTION_REG clrf TRISB ; Set PORTB to output bcf TRISA,6 ; Set PORTA<6,7> to output bcf TRISA,7 ; Initialize AD converter movlw b'10001110' ; movwf ADCON1 ; ADCON1 ; bit<7>=1 ADRESH2bits/ADRESL8bits (Right justified) ; bit<6>=0 Tad=2,8or32*Fosc ; bit<5,4> vacant ; bit<3,0>=<1110> ; RA0 to Analog Input ; RA1,RA2,RA3,RA4 to Digital Input ; Vref+:Vdd, Vref-:Vss bcf STATUS,RP0 ; select Bank0 ;Initialize TMR1 ; TMR1 for AD conversion interval (abt 20ms) clrf T1CON ; T1CON ; bit<7,6> vacant ; bit<5,4>=<0,0> Set Prescalor 1/1 ; bit<3>=0 Oscrator shut-off (T1OSCEN) ; bit<2>=0 ignored where TMRCS=0 (!T1SYNC) ; bit<1>=0 Select Internal clock as clock source (TMRCS) ; bit<0>=0 Stop TMR1 (TMR1ON) movlw b'10000001' ; movwf ADCON0 ; ADCON0 ; bit<7,6>=<0,1> Set Conversion clock to Fosc/32 (4us) ; bit<5,3>=<0,0,0> Slect channel '0' ; bit<2>=0 AD-conversion Stop "GO" ; bit<1> vacant ; bit<0>=0 AD-module Stop ; bsf ADCON0,ADON ; wait for acquisition time loop call wAcq ; AD conversion bsf ADCON0,GO ; Start AD conversion w_cnv btfsc ADCON0,GO goto w_cnv ; bcf ADCON0,ADON bsf STATUS,RP0 ; Select Bank1 movf ADRESL,W bcf STATUS,RP0 ; Select Bank0 movwf ADL0 ; ADRESL to Bank0 'ADL0' ; LED display btfss ADRESH,1 goto L_half ; If MSB is 'Clear', it means lower half. btfss ADRESH,0 goto b7 ; ADRESH<1,0>, lower side of higher half. ; b9 0000000000 1110011010 d'922' ; b8 x000000000 1100110011 d'819' ; b7 xx00000000 1011001100 d'716' ; b6 xxx0000000 1001100110 d'614' ; b5 xxxx000000 1000000000 d'512' ; b4 xxxxx00000 0110011010 d'410' ; b3 xxxxxx0000 0100110011 d'307' ; b2 xxxxxxx000 0011001101 d'205' ; b1 xxxxxxxx00 0001101001 d'102' ; b0 xxxxxxxxx0 0000001000 d'16' ; b_null xxxxxxxxxx b9 movlw b'10011010' ; ADRESH<1,1> ; Check if [ADcon value > d'922'] subwf ADL0,W ; 922-ADRESL(eq) btfss STATUS,C ; result zero or positive, ADcon value is higher than 922 goto b8 bsf PORTA,7 bsf PORTA,6 movlw b'11111111' movwf PORTB goto b_out b8 movlw b'00110011' ; ADRESH<1,1> ; Check if [ADcon value > d'819'] subwf ADL0,W ; 819-ADRESL(eq) btfss STATUS,C ; result zero or positive, ADcon value is higher than 819 goto b7x ; result negative, lower than 819 ; higher than b7 threshold bcf PORTA,7 bsf PORTA,6 movlw b'11111111' movwf PORTB goto b_out b7 movlw b'11001100' ; ADRESH<1,0> subwf ADL0,W btfss STATUS,C goto b6 b7x bcf PORTA,7 bcf PORTA,6 movlw b'11111111' movwf PORTB goto b_out b6 movlw b'01100110' subwf ADL0,W btfss STATUS,C goto b5 bcf PORTA,7 bcf PORTA,6 movlw b'01111111' movwf PORTB goto b_out b5 bcf PORTA,7 ; b5 b'1000000000' is least value of ADRESH<1,0> bcf PORTA,6 ; value lower than b'01100110'(b6) automaticlly higher than b5 threshold movlw b'00111111' movwf PORTB goto b_out L_half btfss ADRESH,0 goto b2 b4 movlw b'10011010' ; ADRESH<0,1> subwf ADL0,W btfss STATUS,C goto b3 bcf PORTA,7 bcf PORTA,6 movlw b'00011111' movwf PORTB goto b_out b3 movlw b'00110011' subwf ADL0,W btfss STATUS,C goto b2x bcf PORTA,7 bcf PORTA,6 movlw b'00001111' movwf PORTB goto b_out b2 movlw b'11001101' ; ADRESH<0,0> subwf ADL0,W btfss STATUS,C goto b1 b2x bcf PORTA,7 bcf PORTA,6 movlw b'00000111' movwf PORTB goto b_out b1 movlw b'01100110' subwf ADL0,W btfss STATUS,C goto b0 bcf PORTA,7 bcf PORTA,6 movlw b'00000011' movwf PORTB goto b_out b0 movlw b'00001000' subwf ADL0,W btfss STATUS,C goto b_null bcf PORTA,7 bcf PORTA,6 movlw b'00000001' movwf PORTB goto b_out b_null bcf STATUS,RP0 ; Select Bank0 bcf PORTA,7 bcf PORTA,6 clrf PORTB b_out ; Disp finished ; wait next conversion clrf TMR1L movlw d'100' movwf TMR1H bcf PIR1,TMR1IF bsf T1CON,TMR1ON w_int btfss PIR1,TMR1IF ; Wait interval goto w_int bcf T1CON,TMR1ON goto loop ;;;;;Subroutine;;;;;; ; for AD con********************************************************************************************* ; wait acquitition time wAcq movlw d'30' ; 0.4*2*30=24micro_sec(about) movwf nAcq w_lp decfsz nAcq,F goto w_lp return END ; directive 'end of program'