Polling an Input more efficiently

Thread Starter

Tera-Scale

Joined Jan 1, 2011
164
Hi,

I wrote this code for measuring period of an input squarewave with a PIC24F. First I implemented an interrupt routine but then I realize that I would be better off by polling, as the operation would be executed only once for one cycle. I am measuring from rising edge to rising edge. My clock frequency would be 16Mhz with a crystal of 32Mhz (fosc/2). My input period would vary from 2µs to 200µs.

I have 32cycles available. With this polling, I am using 14 clock cycle and I what to know I can lower them more as I will be doing an ADC conversion (conversion only - no sampling - requires 12 clock cycles) and some 4 or 5 simple tasks like mov etc... my code is below:

Rich (BB code):
.text
.global _asmFunction

_asmFunction:
              


clr.w w1			   ;clear WREG1
clr.w w3
clr.w w4			   ;clear WREG3
clr.w TMR1		           ;clear TMR1
mov.w w4, 0x2CA           ;clear PortB
mov.w #0x8000, w1        ;move 0x8000 to w for t1con
                                   

start: BTSS PORTB, #9    ;Test RB9 input if high skip next
GOTO start   

mov.w w1, T1CON 	;timer 2 OFF   

falledge: BTSC 0x2CA, #9 	;test RA7 if not set skip next(low) 
GOTO falledge

stop: BTSS 0x2CA, #9 	;test RA7 if set skip(complete cycle) 
GOTO stop

mov.w w3, T1CON 	;timer 2 OFF

mov.w TMR1, w5       ;save 
mov.w w5, 0x800	;timer value 

return
 
.end
 
Top