; Frequency Counter program for PIC16F84 ; Assembler : PICer ; ------------ SPEC. ------------------------------- ; cycle 32Hz - 0.5MHz ; sampling 16sec - 0.98msec ; Voltage 50mVpp - 5Vpp ; Comsumption 7mA, 6V ; ; (C)All rights reserved by Einstein. ; Edit by pulgrim http://www.pulgrim.com ; ; PIC16F84 Register #DEF _indf 0 ; 16F84 Special-purpose register #DEF _tmr0 1 ; 16F84 Special-purpose register #DEF _rtcc 1 ; _rtcc = _tmr0 #DEF _option 1 ; 16F84 Special-purpose register #DEF b_ps0 0 #DEF b_ps1 1 #DEF b_ps2 2 #DEF b_psa 3 #DEF b_tose 4 #DEF b_tocs 5 #DEF b_intedg 6 #DEF b_~rbpu 7 #DEF _pcl 2 ; 16F84 Special-purpose register #DEF _status 3 ; 16F84 Special-purpose register #DEF b_c 0 #DEF b_dc 1 #DEF b_z 2 #DEF b_~pd 3 #DEF b_~to 3 #DEF b_rp0 5 #DEF b_rp1 6 #DEF b_irp 7 #DEF _fsr 4 ; 16F84 Special-purpose register #DEF _trisa 5 ; 16F84 Special-purpose register #DEF _porta 5 ; 16F84 Special-purpose register #DEF _trisb 6 ; 16F84 Special-purpose register #DEF _portb 6 ; 16F84 Special-purpose register #DEF _adcon0 8 ; 16F84 Special-purpose register #DEF b_pcfg0 0 #DEF b_pcfg1 1 #DEF _adcon1 8 ; 16F84 Special-purpose register #DEF b_adon 0 #DEF b_adif 1 #DEF b_go 2 ; in _adcon0 #DEF b_~done 2 ; in _adcon0 #DEF b_chs0 3 #DEF b_chs1 4 #DEF b_adcs0 6 #DEF b_adcs1 7 #DEF _adres 9 ; 16F84 Special-purpose register #DEF _pclath A ; 16F84 Special-purpose register #DEF _intcon B ; 16F84 Special-purpose register #DEF b_rbif 0 #DEF b_intf 1 #DEF b_t0if 2 ; in _intcon #DEF b_rbie 3 #DEF b_inte 4 #DEF b_t0ie 5 ; in _intcon #DEF b_adie 6 #DEF b_gie 7 ; General #DEF TRUE 1 #DEF FALSE 0 #DEF YES 1 #DEF NO 0 ; #FUSE 3FF2 ; Configuration Word #DEF w 00 ; destination w or f #DEF f 01 ; destination w or f ; Memory #DEF Command 0C #DEF Data 0D #DEF M0 0E #DEF M1 0F #DEF M2 10 #DEF M3 11 #DEF A3 12 #DEF A2 13 #DEF A1 14 #DEF A0 15 #DEF B3 16 #DEF B2 17 #DEF B1 18 #DEF B0 19 #DEF C3 1A #DEF C2 1B #DEF C1 1C #DEF C0 1D #DEF AA3 1E #DEF AA2 1F #DEF AA1 20 #DEF AA0 21 #DEF BB3 22 #DEF BB2 23 #DEF BB1 24 #DEF BB0 25 #DEF CC3 26 #DEF CC2 27 #DEF CC1 28 #DEF CC0 29 #DEF T3 2A #DEF T2 2B #DEF T1 2C #DEF T0 2D #DEF D0 2E #DEF D1 2F #DEF D2 30 #DEF D3 31 #DEF D4 32 #DEF D5 33 #DEF D6 34 #DEF D7 35 #DEF D8 36 #DEF D9 37 #DEF CF 38 #DEF D 39 #DEF CC 3A #DEF TCC 3B #DEF PRE 3C #DEF PREcode 3D ; ;******************************************************************************************** ; Start ;******************************************************************************************** #ORG 00 GOTO Start #ORG 04 goto Interrupt ;******************************************************************************************** ; Sub routine ;******************************************************************************************** ;####### #LBL InitLCD ; port A setting bsf _status,b_rp0 ; Select Page 1 movlw #F0 ; E=RA2, RS=RA1, R/W=RA0 movwf _trisa ; RA4=input, others=output bcf _status,b_rp0 ; Select Page 0 return ;####### #LBL WaitLCD ; port B setting movlw #00 movwf _portb bsf _status,b_rp0 ; Select Page 1 movlw #FF movwf _trisb ; all input, BF=RB7 bcf _status,b_rp0 ; Select Page 0 ; bcf _porta,#1 ; RS=0 bcf _porta,#0 ; R/W=0 #LBL WaitLCDloop bcf _porta,#2 ; E=0 ; 1 clock= 0.4us bcf _porta,#1 ; RS=0 bsf _porta,#0 ; R/W=1 ; 140ns nop bsf _porta,#2 ; E=1 ; 320ns nop nop btfsc _portb,#7 ; BF=0 then, Skip goto WaitLCDloop bcf _porta,#2 ; E=0 return ;####### #LBL WriteLCDCommand ; Command call WaitLCD ; port B setting movlw #00 movwf _portb bsf _status,b_rp0 ; Select Page 1 movlw #0 movwf _trisb ; all output bcf _status,b_rp0 ; Select Page 0 ; bcf _porta,#1 ; RS=0 bcf _porta,#0 ; R/W=0 movf Command,w movwf _portb ; 140ns nop bsf _porta,#2 ; E=1 ; 320ns nop nop bcf _porta,#2 ; E=0 return ;####### #LBL WriteLCDData ; Data call WaitLCD ; port B setting movlw #00 movwf _portb bsf _status,b_rp0 ; Select Page 1 movlw #0 movwf _trisb ; all output bcf _status,b_rp0 ; Select Page 0 ; bsf _porta,#1 ; RS=1 bcf _porta,#0 ; R/W=0 movf Data,w movwf _portb ; 140ns nop bsf _porta,#2 ; E=1 ; 320ns nop nop bcf _porta,#2 ; E=0 return ;####### #LBL Start call InitLCD ; movlw #30 ; Function Set movwf Command call WriteLCDCommand ; movlw #01 ; Clear Display movwf Command call WriteLCDCommand ; movlw #0C ; Display On/Off Control movwf Command call WriteLCDCommand ; movlw #06 ; Entry Mode Set movwf Command call WriteLCDCommand ;initial display clrf CC3 clrf CC2 clrf CC1 clrf CC0 call conv call disp ;####### ;TMR0 setting movlw #2 movwf PRE movlw #F0 movwf PREcode bcf _status,b_rp0 ; Bank 0 clrf _tmr0 ; Clear TMR0 clrwdt bsf _status,b_rp0 ; Bank 1 movf PREcode,w movwf _option ; rising edge, prescaler bcf _status,b_rp0 ; Bank 0 bsf _intcon,b_t0ie ; TMR0 interrupt enable bsf _intcon,b_gie ; interrupt enable ;####### #LBL idlingloop incfsz M0,f goto idlingloop incfsz M1,f goto idlingloop incfsz M2,f goto idlingloop incf M3,f goto idlingloop ;####### #LBL Interrupt clrf A3 movf M2,w movwf A2 movf M1,w movwf A1 movf M0,w movwf A0 call calc call conv call disp ; Clear counter clrf M0 clrf M1 clrf M2 clrf M3 clrf _tmr0 ; Clear TMR0 bcf _intcon,b_t0if ; Clear flag retfie ;####### ; [C3,C2,C1,C0]=[C3,C2,C1,C0]+[A3,A2,A1,A0] #LBL add clrf CF clrf D ; C0=C0+A0 ... w=Carry movf A0,w addwf C0,f rlf D,w ;w=C ; C1=C1+w ... CF=Carry addwf C1,f rlf CF,f ;CF=C ; C1=C1+A1 ... w=Carry movf A1,w addwf C1,f rlf D,w ;w=C ; w=CF or w iorwf CF,w clrf CF ; C2=C2+w ... CF=Carry addwf C2,f rlf CF,f ; CF=C ; C2=C2+A2 ... w=Carry movf A2,w addwf C2,f rlf D,w ; w=C ; w=CF or w iorwf CF,w clrf CF ; C3=C3+w ... CF=Carry addwf C3,f rlf CF,f ; CF=C ; C3=C3+A3 ... w=Carry movf A3,w addwf C3,f rlf D,w ; w=C ; w=CF or w iorwf CF,f return ; [C3,C2,C1,C0]=[C3,C2,C1,C0]-[A3,A2,A1,A0] #LBL sub clrf D comf D,f clrf CF comf CF,f ; C0=C0-A0 ... w=Carry movf A0,w subwf C0,f rlf D,f ; w=C comf D,w ; C1=C1-w ... CF=Carry subwf C1,f rlf CF,f ; CF=C comf CF,f clrf D comf D,f ; C1=C1-A1 ... w=Carry movf A1,w subwf C1,f rlf D,f ; w=C comf D,w ; w=CF or w iorwf CF,w clrf CF comf CF,f ; C2=C2-w ... CF=Carry subwf C2,f rlf CF,f ; CF=C comf CF,f clrf D comf D,f ; C2=C2-A2 ... w=Carry movf A2,w subwf C2,f rlf D,f ; w=C comf D,w ; w=CF or w iorwf CF,w clrf CF comf CF,f ; C3=C3-w ... CF=Carry subwf C3,f rlf CF,f ; CF=C comf CF,f clrf D comf D,f ; C3=C3-A3 ... w=Carry movf A3,w subwf C3,f rlf D,f ; w=C comf D,w ; w=CF or w iorwf CF,f return ; [C3,C2,C1,C0]=[A3,A2,A1,A0]x[B3,B2,B1,B0] #LBL mul clrf C3 clrf C2 clrf C1 clrf C0 clrf CF #LBL mulloop movf B3,w iorwf B2,w iorwf B1,w iorwf B0,w btfsc _status,b_z goto mulend #LBL mulnext bcf _status,b_c rrf B3,f rrf B2,f rrf B1,f rrf B0,f btfsc _status,b_c ; [C3,C2,C1,C0]=[C3,C2,C1,C0]+[A3,A2,A1,A0] call add bcf _status,b_c rlf A0,f rlf A1,f rlf A2,f rlf A3,f btfss _status,b_c goto mulloop rlf CF,f ; CF=C #LBL mulend return ; [CC3,CC2,CC1,CC0]=[AA3,AA2,AA1,AA0]/[BB3,BB2,BB1,BB0] ...[C3,C2,C1,C0] #LBL div clrf CC3 clrf CC2 clrf CC1 clrf CC0 clrf T3 clrf T2 clrf T1 clrf T0 bsf _status,b_c rrf T3,f #LBL divloop movf T3,w iorwf CC3,f movf T2,w iorwf CC2,f movf T1,w iorwf CC1,f movf T0,w iorwf CC0,f movf CC3,w movwf A3 movf CC2,w movwf A2 movf CC1,w movwf A1 movf CC0,w movwf A0 movf BB3,w movwf B3 movf BB2,w movwf B2 movf BB1,w movwf B1 movf BB0,w movwf B0 ; [C3,C2,C1,C0]=[A3,A2,A1,A0]x[B3,B2,B1,B0] call mul movf CF,f btfss _status,b_z goto divnext movf C3,w movwf A3 movf C2,w movwf A2 movf C1,w movwf A1 movf C0,w movwf A0 movf AA3,w movwf C3 movf AA2,w movwf C2 movf AA1,w movwf C1 movf AA0,w movwf C0 ; [C3,C2,C1,C0]=[C3,C2,C1,C0]-[A3,A2,A1,A0] call sub movf CF,f btfsc _status,b_z goto divend #LBL divnext movf T3,w subwf CC3,f movf T2,w subwf CC2,f movf T1,w subwf CC1,f movf T0,w subwf CC0,f movf BB3,w movwf A3 movf BB2,w movwf A2 movf BB1,w movwf A1 movf BB0,w movwf A0 ; [C3,C2,C1,C0]=[C3,C2,C1,C0]+[A3,A2,A1,A0] call add #LBL divend bcf _status,b_c rrf T3,f rrf T2,f rrf T1,f rrf T0,f btfss _status,b_c goto divloop return ; answer=256*pre/(A2A1A0*3+A1*2+A2*4)/0.4E-6 ; clk = (A2*0x10000 + A1*0x100 + A0)*3 + A1*2 + A2*4 ; t=0x2625A000 * pre ; answer=t/clk #LBL calc ; clk = (A2*0x10000 + A1*0x100 + A0)*3 + A1*2 + A2*4 movf A3,w movwf AA3 movf A2,w movwf AA2 movf A1,w movwf AA1 movf A0,w movwf AA0 clrf B3 clrf B2 clrf B1 movlw #3 movwf B0 call mul movf C3,w movwf CC3 movf C2,w movwf CC2 movf C1,w movwf CC1 movf C0,w movwf CC0 clrf A3 clrf A2 clrf A1 movf AA1,w movwf A0 clrf B3 clrf B2 clrf B1 movlw #2 movwf B0 call mul movf CC3,w movwf A3 movf CC2,w movwf A2 movf CC1,w movwf A1 movf CC0,w movwf A0 call add movf C3,w movwf CC3 movf C2,w movwf CC2 movf C1,w movwf CC1 movf C0,w movwf CC0 clrf A3 clrf A2 clrf A1 movf AA2,w movwf A0 clrf B3 clrf B2 clrf B1 movlw #4 movwf B0 call mul movf CC3,w movwf A3 movf CC2,w movwf A2 movf CC1,w movwf A1 movf CC0,w movwf A0 call add movf C3,w movwf BB3 movf C2,w movwf BB2 movf C1,w movwf BB1 movf C0,w movwf BB0 ; t=0x2625A000 * pre movlw #26 movwf C3 movlw #25 movwf C2 movlw #A0 movwf C1 movlw #00 movwf C0 btfsc PRE,#0 goto calc3 movlw #26 movwf A3 movlw #25 movwf A2 movlw #A0 movwf A1 movlw #00 movwf A0 btfsc PRE,#1 goto calc2 clrf A3 movlw #26 movwf A2 movlw #25 movwf A1 movlw #A0 movwf A0 #LBL calc2 clrf B3 clrf B2 clrf B1 movf PRE,w movwf B0 call mul #LBL calc3 ; answer movf C3,w movwf AA3 movf C2,w movwf AA2 movf C1,w movwf AA1 movf C0,w movwf AA0 call div btfsc PRE,#1 goto calc4 movf CC2,w movwf CC3 movf CC1,w movwf CC2 movf CC0,w movwf CC1 clrf CC0 #LBL calc4 return ;KHz Hex Pre Time[Sec] _option ;0.032 0x0020 2 16 #F0 ;0.064 0x0040 2 8 #F0 ;0.128 0x0080 2 4 #F0 ;0.256 0x0100 2 2 #F0 ;0.512 0x0200 2 1 #F0 ;1 0x0400 2 0.5 #F0 ;2 0x0800 2 0.25 #F0 ;4 0x1000 2 0.125 #F0 ;8 0x2000 2 0.0625 #F0 ;16 0x4000 2 0.03125 #F0 ;32 0x8000 2 0.015625 #F0 ;64 0x10000 2 0.0078125 #F0 ;128 0x20000 2 0.00390625 #F0 ;256 0x40000 2 0.001953125 #F0 ;512 0x80000 2 0.0009765625 #F0 ; [CC3,CC2,CC1,CC0]->[D9,D8,D7,D6,D5,D4,D3,D2,D1,D0] #LBL conv clrf D9 clrf D8 clrf D7 clrf D6 clrf D5 clrf D4 clrf D3 clrf D2 clrf D1 clrf D0 movlw #A movwf CC clrf BB3 clrf BB2 clrf BB1 movlw #A movwf BB0 #LBL convloop movf CC3,w movwf AA3 movf CC2,w movwf AA2 movf CC1,w movwf AA1 movf CC0,w movwf AA0 call div movf CC,w movwf TCC movf C0,w decfsz TCC,f goto conv0 movwf D0 #LBL conv0 decfsz TCC,f goto conv1 movwf D1 #LBL conv1 decfsz TCC,f goto conv2 movwf D2 #LBL conv2 decfsz TCC,f goto conv3 movwf D3 #LBL conv3 decfsz TCC,f goto conv4 movwf D4 #LBL conv4 decfsz TCC,f goto conv5 movwf D5 #LBL conv5 decfsz TCC,f goto conv6 movwf D6 #LBL conv6 decfsz TCC,f goto conv7 movwf D7 #LBL conv7 decfsz TCC,f goto conv8 movwf D8 #LBL conv8 decfsz TCC,f goto conv9 movwf D9 #LBL conv9 decfsz CC,f goto convloop return #LBL disp movlw #30 addwf D0,f addwf D1,f addwf D2,f addwf D3,f addwf D4,f addwf D5,f addwf D6,f addwf D7,f addwf D8,f addwf D9,f movlw #01 ; Clear Display movwf Command call WriteLCDCommand movf D0,w movwf Data call WriteLCDData movf D1,w movwf Data call WriteLCDData movf D2,w movwf Data call WriteLCDData movf D3,w movwf Data call WriteLCDData movf D4,w movwf Data call WriteLCDData movf D5,w movwf Data call WriteLCDData movf D6,w movwf Data call WriteLCDData movf D7,w movwf Data call WriteLCDData movf D8,w movwf Data call WriteLCDData movf D9,w movwf Data call WriteLCDData movlw #20 ; Data " " movwf Data call WriteLCDData movlw #48 ; Data "H" movwf Data call WriteLCDData movlw #7A ; Data "z" movwf Data call WriteLCDData return