Here is some code that will measure an RC receiver pulse width coming in on GPIO.2, perform a little math on the value, and put out the wave form out on GPIO.1, using DT Interrupts.
It's on a little 12F683 and was written for 8mhz internal. Looks pretty good on the scope. I need to test it on a motor controller though. But time for bed.....
Code:<html> <head></head> <body><!--StartFragment--><pre><code><font color="#000080"><i> </i></font><b>DEFINE </b>OSC 8 <b>INCLUDE </b><font color="#FF0000">"DT_INTS-14.bas"</font><font color="#000080"><i>;interrupt routines </i></font><b>INCLUDE </b><font color="#FF0000">"sub16.inc" </font><font color="#000080"><i>; subtract 16 bit macro </i></font><b>DEFINE </b>DEBUG_REG GPIO <b>DEFINE </b>DEBUG_BIT 0 <b>DEFINE </b>DEBUGIN_BIT 1 <b>DEFINE </b>DEBUG_BAUD 38400 <b>DEFINE </b>DEBUG_MODE 0 INTCON = %10101000 <font color="#000080"><i>' </i></font>OSCCON = %01110000 <font color="#000080"><i>'set for 8mhz internal </i></font>CMCON0 = 7 <font color="#000080"><i>'TURN COMPARITORS OFF </i></font>ANSEL = %00000000 <font color="#000080"><i>'Set A/D OFF </i></font>ADCON0 = %00000000 <font color="#000080"><i>'Analog converter OFF </i></font>TRISIO = %010100 <font color="#000080"><i>'Set GSIO 4 and 2 to INPUT, others to OUTPUT </i></font>OPTION_REG = %11000010 T1CON = %00110001 T2CON = %01001110 CCP1CON = %00000101 risetime <b>VAR WORD </b><font color="#000080"><i>;used for pulse width measure start of pw </i></font>falltime <b>VAR WORD </b><font color="#000080"><i>;time at end of pulse width </i></font>falltime_l <b>VAR </b>falltime.Byte0 falltime_h <b>VAR </b>falltime.Byte1 risetime_l <b>VAR </b>risetime.Byte0 risetime_h <b>VAR </b>risetime.Byte1 pulsewidth <b>VAR WORD </b>pulsewidth_l <b>VAR </b>pulsewidth.Byte0 pulsewidth_h <b>VAR </b>pulsewidth.Byte1 ServoOut <b>VAR </b>GPIO.1 ServoOut = 0 pulse <b>VAR BYTE </b>TMR0 = 0 pulse = 255 bittest <b>VAR BIT ASM </b><font color="#008000">INT_LIST macro </font><font color="#000080"><i>; IntSource, Label, Type, ResetFlag? </i></font><font color="#008000">INT_Handler TMR0_INT, PulseOut, ASM, yes INT_Handler TMR2_INT, period, ASM, yes INT_Handler CCP1_INT, PWMeasure, ASM, yes endm INT_CREATE </font><font color="#000080"><i>; Creates the interrupt processor </i></font><font color="#008000">INT_ENABLE TMR0_INT </font><font color="#000080"><i>; Enable pulseout interrupts </i></font><font color="#008000">INT_ENABLE TMR2_INT </font><font color="#000080"><i>; enable period interrupt (20ms) </i></font><font color="#008000">INT_ENABLE CCP1_INT </font><font color="#000080"><i>; enable interrupt to increment pulse width (test) </i></font><b>ENDASM PAUSE </b>200 Main: <b>PAUSE </b>20 <font color="#000080"><i>;debug dec pulsewidth," ",10,13 </i></font>pulse = ((pulsewidth * 8)/10)-167 <font color="#000080"><i>;scale to fit into a byte </i></font>pulse = 255 - pulse <font color="#000080"><i>;reverse the stick </i></font><b>GOTO </b>Main <font color="#000080"><i>'---[TMR0_INT - interrupt handler]------------------------------------------ </i></font><b>ASM </b><font color="#008000">PulseOut btfsc _bittest </font><font color="#000080"><i>;test status of bittest bit </i></font><font color="#008000">goto $+7 </font><font color="#000080"><i>;if high skip 7 lines </i></font><font color="#008000">bcf INTCON,5 </font><font color="#000080"><i>;stop timer </i></font><font color="#008000">movf _pulse,w </font><font color="#000080"><i>;load TMR0 with value of pulse variable </i></font><font color="#008000">movwf TMR0 bsf _bittest </font><font color="#000080"><i>;set bittest bit, indicating we are on second half </i></font><font color="#008000">bsf INTCON,5 </font><font color="#000080"><i>;start timer again for second interrupt </i></font><font color="#008000">goto $+3 </font><font color="#000080"><i>;skip to return from interrupt </i></font><font color="#008000">bcf INTCON,5 </font><font color="#000080"><i>;stop timer </i></font><font color="#008000">bcf _ServoOut </font><font color="#000080"><i>;bring ServoOut pin low for end of pulse width </i></font><font color="#008000">INT_RETURN </font><b>ENDASM </b><font color="#000080"><i>'---[TMR2_INT - interrupt handler]------------------------------------------ </i></font><b>ASM </b><font color="#008000">period movlw d'72' </font><font color="#000080"><i>;fine tune timer2 period </i></font><font color="#008000">movwf TMR2 bcf INTCON,2 </font><font color="#000080"><i>;clear timer flag if set </i></font><font color="#008000">movlw d'17' </font><font color="#000080"><i>;set timer0 first 1ms time base </i></font><font color="#008000">movwf TMR0 bsf INTCON,5 </font><font color="#000080"><i>;enable timer0 interrupt </i></font><font color="#008000">bsf _ServoOut </font><font color="#000080"><i>;set ServoOut pin high (begin pulse width) </i></font><font color="#008000">bcf _bittest INT_RETURN </font><b>ENDASM </b><font color="#000080"><i>'---[CCP1_INT - interrupt handler]------------------------------------------ </i></font><b>ASM </b><font color="#008000">PWMeasure BTFSS CCP1CON, CCP1M0 </font><font color="#000080"><i>; Check for falling edge watch </i></font><font color="#008000">GOTO FALL_EDGE </font><font color="#000080"><i>; Go pick up the falling edge </i></font><font color="#008000">MOVF CCPR1L,W </font><font color="#000080"><i>; else store leading edge value </i></font><font color="#008000">MOVWF _risetime_l </font><font color="#000080"><i>; into 16 bit word risetime </i></font><font color="#008000">MOVF CCPR1H,W MOVWF _risetime_h BCF CCP1CON, CCP1M0 </font><font color="#000080"><i>; Now capture the trailing edge </i></font><font color="#008000">GOTO ISR_2 </font><font color="#000080"><i>; Exit the interrupt service routine </i></font><font color="#008000">FALL_EDGE: BSF CCP1CON, CCP1M0 </font><font color="#000080"><i>; Re-set for trailing edge capture </i></font><font color="#008000">MOVF CCPR1L,W </font><font color="#000080"><i>; Store the captured value into </i></font><font color="#008000">MOVWF _falltime_l </font><font color="#000080"><i>; falltime_l and ... </i></font><font color="#008000">MOVF CCPR1H,W MOVWF _falltime_h </font><font color="#000080"><i>; ... falltime_h ; ; 16 bit subtract ; falltime = falltime - risetime ; </i></font><font color="#008000">SUB16 _falltime, _risetime </font><font color="#000080"><i>;do 16 bit subtraction to find width </i></font><font color="#008000">movf _falltime_l,w movwf _pulsewidth_l movf _falltime_h,w movwf _pulsewidth_h ISR_2 INT_RETURN </font><b>ENDASM </b></code></pre><!--EndFragment--></body> </html>




Bookmarks