Polling an Input more efficiently

Discussion in 'Embedded Systems and Microcontrollers' started by Tera-Scale, Mar 8, 2012.

  1. Tera-Scale

    Thread Starter Active Member

    Jan 1, 2011
    164
    5
    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 ( (Unknown Language)):
    1.  
    2. .text
    3. .global _asmFunction
    4.  
    5. _asmFunction:
    6.              
    7.  
    8.  
    9. clr.w w1               ;clear WREG1
    10. clr.w w3
    11. clr.w w4               ;clear WREG3
    12. clr.w TMR1                 ;clear TMR1
    13. mov.w w4, 0x2CA           ;clear PortB
    14. mov.w #0x8000, w1        ;move 0x8000 to w for t1con
    15.                                    
    16.  
    17. start: BTSS PORTB, #9    ;Test RB9 input if high skip next
    18. GOTO start  
    19.  
    20. mov.w w1, T1CON     ;timer 2 OFF  
    21.  
    22. falledge: BTSC 0x2CA, #9    ;test RA7 if not set skip next(low)
    23. GOTO falledge
    24.  
    25. stop: BTSS 0x2CA, #9    ;test RA7 if set skip(complete cycle)
    26. GOTO stop
    27.  
    28. mov.w w3, T1CON     ;timer 2 OFF
    29.  
    30. mov.w TMR1, w5       ;save
    31. mov.w w5, 0x800 ;timer value
    32.  
    33. return
    34.  
    35. .end
    36.  
     
Loading...