Tuesday, August 25, 2009

Simple frequency meter- pic16f886








Protel 98 is used






Microsoft Word is used



I’m using Mplab 7.60 compiler and microchip pickit2 programmer



list p=16f886
; list directive to define processor
#include ; processor specific variable definitions
; processor specific variable definitions


__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

__CONFIG _CONFIG2, _WRT_OFF & _BOR21V





errorlevel -302 ;Suppress bank warning
errorlevel -203 ;Suppress bank warning

; I’m using internal oscillator—see? INTRC_OSC
; internal oscillator by default after reset is 4 MHz

;**************** Label Definition ********************

w EQU 0
f EQU 1

counter_unit EQU H'41'
counter_decimal EQU H'42'
counter_hundred EQU H'43'
counter_thousand EQU H'44'
counter_tenthousand EQU H'45'
sec EQU H'46'
min EQU H'47'
cnt1m EQU H'48'
cnt500u EQU H'49'
cnt0m EQU H'4A'
cnt600u EQU H'4B'
cnt601u EQU H'4C'
;**************** Program Start ***********************

ORG H'0000' ;Reset Vector
goto START

;**************** Initial Process *********************

START
bsf STATUS,RP0
movlw B'11010111'
movwf OPTION_REG
bcf STATUS,RP0
clrf PORTB
movlw B'11100000'
movwf PORTA
movlw B'00000000'
movwf PORTC
bcf PORTC,0
clrf TMR1H ;Clear TMR1H register
clrf TMR1L ;Clear TMR1L register
bsf STATUS,RP0
clrf TRISA ; outputs
clrf TRISB ; outputs
movlw B'00000001'
movwf TRISC
bsf TRISC,0 ; input
BCF STATUS,RP0
clrf counter_unit
clrf counter_decimal
clrf counter_hundred
clrf counter_thousand
clrf counter_tenthousand
clrf cnt0m
clrf cnt1m
clrf cnt500u
movlw b'00000111'
;Pre=1:1 tmr1-external source, TMR1=Int TMR1=ON
MOVWF T1CON ;Set T1CON register

;*********************************************
; 1 sec gate open
;*********************************************
movlw B'01111111'
movwf PORTA
call t0m
movlw B'11111111'
movwf PORTA
;*********************************************
; TMR1H---> min
; TMR1L---> sec
;*********************************************
movf TMR1H,w ; move TMR1H in min
movwf min ;
movf TMR1L,w ; move TMR1L in sec
movwf sec ;
;*********************************************
check_sec
movlw D'0'
xorwf sec,w
btfss STATUS,Z ; =0 ?
goto decrease_sec ; no
goto check_min ; yes
check_min
movlw D'0'
xorwf min,w
btfss STATUS,Z ; =0 ?
goto decrease_min ; no
goto DESIGNATION ; yes
;*********************************************
decrease_min
decf min,f
decrease_sec
decf sec,f
incf counter_unit,f
nop
nop
movlw d'10'
xorwf counter_unit,w
nop
nop
btfss STATUS,Z ; =10 ?
goto check_sec ; no
clrf counter_unit
goto increase_decimal
;*********************************************
increase_decimal
incf counter_decimal,f
nop
nop
movlw d'10'
xorwf counter_decimal,w
nop
nop
btfss STATUS,Z ; =10 ?
goto check_sec ; no
clrf counter_decimal
;*********************************************
goto increase_hundred
increase_hundred
incf counter_hundred,f
nop
nop
movlw d'10'
xorwf counter_hundred,w
nop
nop
btfss STATUS,Z ; =10 ?
goto check_sec ; no
clrf counter_hundred
goto increase_thousand
;*********************************************
increase_thousand
incf counter_thousand,f
nop
nop
movlw d'10'
xorwf counter_thousand,w
nop
nop
btfss STATUS,Z ; =10 ?
goto check_sec ; no
clrf counter_thousand
goto increase_tenthousand
;*********************************************
increase_tenthousand
incf counter_tenthousand,f
goto check_sec
;****************************************
;DESIGNATION and time delay
;****************************************
DESIGNATION
tm1lp61
movlw d'250'
movwf cnt601u
tm1lp71 nop
;****************************************
unit
clrf PORTB
movlw B'11111110'
movwf PORTA
movf counter_unit,w
movwf PORTB
call tm1lp6
nop
nop
nop
;****************************************
decimal
clrf PORTB
movlw B'11111101'
movwf PORTA
movf counter_decimal,w
movwf PORTB
call tm1lp6
nop
nop
nop
;****************************************
hundred
clrf PORTB
movlw B'11111011'
movwf PORTA
movf counter_hundred,w
movwf PORTB
call tm1lp6
nop
nop
nop
;****************************************
thousand
clrf PORTB
movlw B'11110111'
movwf PORTA
movf counter_thousand,w
movwf PORTB
call tm1lp6
nop
nop
nop
;****************************************
tenthousand
clrf PORTB
movlw B'11101111'
movwf PORTA
movf counter_tenthousand,w
movwf PORTB
call tm1lp6
nop
nop
nop
;****************************************
decfsz cnt601u,f
goto tm1lp71
goto START
;*********************************************
; Measuring time- open gate
;************* 1 sec Timer Subroutine *****************
t0m
movlw d'10'
movwf cnt0m
t1m
movlw d'40'
movwf cnt1m
tm1lp1
movlw d'250'
movwf cnt500u
tm1lp2
nop
nop
nop
nop
nop
nop
nop
decfsz cnt500u,f
goto tm1lp2
decfsz cnt1m,f
goto tm1lp1
decfsz cnt0m,f
goto t1m
clrf cnt0m
clrf cnt1m
clrf cnt500u
return
;Total -1uS x 10 x 250 x 40 x 10=1second
; Fosc=4Mhz (Tosc=1/Fosc =250 nS )
; one instructions takes Tic=4 x 250 nS=1 uS
; exceptions for- goto , decfsz they need 2 uS
;*********************************************
; Time between flashing 7 seg
;*********************************************
tm1lp6
movlw d'250'
movwf cnt600u
tm1lp7 nop
nop
nop
nop
nop
nop
nop
decfsz cnt600u,f
goto tm1lp7
return
;Total – 1uS x 10 x 250=2 500uS = 2.5mS
END





HEX file

:020000040000FA
:1000000001288316D730810083128601E0308500F5
:100010000030870007108F018E018316850186014D
:100020000130870007148312C101C201C301C4015A
:10003000C501CA01C801C901073090007F308500A1
:100040009420FF3085000F08C7000E08C60000305E
:100050004606031D32282C2800304706031D312890
:100060006128C703C603C10A000000000A30410628
:1000700000000000031D2728C1013E28C20A00001D
:1000800000000A30420600000000031D2728C201BC
:100090004928C30A000000000A304306000000009F
:1000A000031D2728C3015428C40A000000000A3099
:1000B000440600000000031D2728C4015F28C50A6C
:1000C0002728FA30CC0000008601FE308500410868
:1000D0008600AB200000000000008601FD30850096
:1000E00042088600AB200000000000008601FB30C3
:1000F000850043088600AB20000000000000860158
:10010000F730850044088600AB20000000000000A6
:100110008601EF30850045088600AB200000000016
:100120000000CC0B632801280A30CA002830C80020
:10013000FA30C900000000000000000000000000CC
:100140000000C90B9A28C80B9828CA0B9628CA0128
:10015000C801C9010800FA30CB000000000000000F
:0E0160000000000000000000CB0BAD280800DE
:02400E00E428A4
:02401000FF3E71
:00000001FF