Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Is there room for improvement in this polling routine?

Status
Not open for further replies.

jonnybgood

Full Member level 4
Joined
Dec 28, 2010
Messages
214
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,298
Activity points
3,011
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:

Code:
.text
.global _asmFunction

_asmFunction:
mov.w #0x0, w4
mov.w w4, 0x2CA

clr.w w1			   ;clear WREG1
clr.w w3			   ;clear WREG3
clr.w TMR1		   ;clear TMR1

mov.w #0x8000, w1 ;move 0x8000 to w for t1con
                                   

start: BTSS PORTB, #9  
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
 

wouldn't using timers and Input Capture be a better option to measure the period of your square wave?
 
That's what I just did yesterday, but with with the latency of ISR it cost me 16 clock cycles, now it costs me 14, not a lot of improvement but not bad. Can I use input capture with out going to ISR? Just polling?
 

Can I use input capture with out going to ISR? Just polling?
Yes, of course.

Utilizing the capture buffer, you can also start a measurement and read it later. No time-critical code is involved.
 
So I can eliminate the ISR and the Latency that come with it?

Can you show me an example with just simple pseudocode please?
 

Referring to 15.6 CAPTURE BUFFER OPERATION in the PIC24F family refernce manual:
- reset the input capture module
- setup input capture and associated tímer to the intended mode
- enable input capture
- wait for two consecutive capture events by polling ICBNE and read their results
 
thanks, therefore, for what I need to do, I can first wait for the first rising edge by polling the ICBNE and then wait for the second rising edge by polling the ICIF am I right? Because unless I read the first capture value in the IC1BUF, the ICBNE will not turn 0 and therefore I need to relay on another flag.

Regarding the mnemonic "BTSS.b", is it the most efficient mnemonic I can use, as it takes 3 machine cycles...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top