>Hello everyone, > >How do we create time loops for 16C84? I want to measure >number of pulses in a given time, like in case of techometer. >Is it better use clock speed of Micro or internal timer? >I just need simple example. > >Thanks for your help, > >Vic Vic, Actually, you want to use both... If you are creating a Tachometer, and you want to measure the number of pulses in a given period of time you will use a forground loop to wait a set amount of time. Hooking up your input signal to TMR0 will give you a count of the number of pulses. Assuming you wanted to measure the number of pulses that happen in 1 msec, using a 16C84 running at 4 MHz, and the pulse input going into RA4/TOCK1; you could use the code: bsf STATUS, RP0 ; Go into Register Bank 1 movlw 0x0F0 ; Setup the Option Register to Use the RA4 movwf OPTION_REG & 0x07F ; for TMR0 Input with NO prescaler bcf STATUS, RP0 movlw 76 ; Have to Wait 1,000 Cycles movwf Count ; Count Loop will wait 227 Cycles movlw 2 ; Counthi Loop will wait 772 Cycles movwf Counthi ; Timer Setup will take 1 Cycles clrf TMR0 ; Restart the Timer (and clear the Prescaler) decfsz Count ; Wait 999 Cycles goto $ - 1 decfsz Counthi goto $ - 3 movf TMR0, w ; Save 1/2 the number of pulses encountered This code will only require two file registers (Count & Counthi). The basic operation of this loop is to use TMR0 to count the number of incoming pulses while the foreground operation waits 1,000 cycles (at 4 MHz, 1 cycle is 1 usec long) to get a 1 msec wait. The delay loop (in cycles) values are calculated by: cycles = 3 * ( Count - 1 ) + 2 + 2 + ( Counthi - 1 ) * ( 3 + 255 * 3 + 2 ) (The "Count" loop is pretty straight forward, but you have to remember that the "Counthi" Loop also encompasses the "Count" Loop). TMR0 still goes through a synchronization step, which means that it takes two pulses to increment it (and that's why I say "Save 1/2 the number of pulses encountered"). This example will safely work to up to 255 KHz. If the Overflow bit is set, you will want to run this again with a shorter delay. myke Avoiding precedents does not mean nothing should ever be done. It only means that nothing should ever be done for the first time - Sir Humphrey Appleby K.C.B.