For pulse width measurement, I ran across an interesting page.
http://www.mcmanis.com/chuck/robotic...e_measure.html
I borrowed a little bit of the code, and then added it to some DT_Interrupts. I am just measuring a servo pulse width, and sending raw tmr1 result to the serial port here, but you can change it to make it do other things. This was done with a PIC18f2520 in a LAB-X2. But the above gentleman used a PIC16F628, so it should be pretty flexible if you chose the corresponding DT_INTS include file. I would like to see more examples of pulse width measurement as well as pulse generation using interrupts, so if you have some, please share!
Code:DEFINE OSC 20 DEFINE LOADER_USED 1 DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1 DEFINE HSER_CLROERR 1 ' Clear overflow automatically DEFINE HSER_SPBRG 42 ' 115200 Baud @ 20MHz, 0.94% SPBRGH = 0 BAUDCON.3 = 1 ' Enable 16 bit baudrate generator LED0 VAR portb.0 LED1 VAR portb.1 LED2 VAR portb.2 adcon1=15 ;sets all to digital TRISA=%00000000 ' Set PORTA TRISB=%01110000 ' Set PortB TRISC=%10000100 ' Set PortC bit.2 for input (ccp) and bit.7 for ser input INCLUDE "DT_INTS-18.bas" ; Base Interrupt System INCLUDE "sub16.inc" ; subtract 16 bit macro LED0=0 LED2=0 risetime VAR WORD falltime VAR WORD falltime_l VAR falltime.Byte0 falltime_h VAR falltime.Byte1 risetime_l VAR risetime.Byte0 risetime_h VAR risetime.Byte1 ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? INT_Handler TMR0_INT, ToggleLED0, ASM, yes INT_Handler CCP1_INT, PulseWidth, ASM, yes endm INT_CREATE ; Creates the interrupt processor ENDASM T0CON = %10000100 T1CON = %00000001 T2CON = %00111101 CCP1CON = %00000101 @ INT_ENABLE TMR0_INT ; Enable Timer 0 Interrupts @ INT_ENABLE CCP1_INT ; Enable Capture Compare for pulse width measurement Main: PAUSE 1000 HSEROUT [DEC falltime,10] GOTO Main ASM ToggleLED0 btg _LED0 ;blinky light INT_RETURN ENDASM ASM PulseWidth BTFSS CCP1CON, CCP1M0 ; Check for falling edge watch GOTO FALL_EDGE ; Go pick up the falling edge MOVF CCPR1L,W ; else store leading edge value MOVWF _risetime_l ; into 16 bit word risetime MOVF CCPR1H,W MOVWF _risetime_h BCF CCP1CON, CCP1M0 ; Now capture the trailing edge GOTO ISR_2 ; Exit the interrupt service routine FALL_EDGE: BSF CCP1CON, CCP1M0 ; Re-set for trailing edge capture MOVF CCPR1L,W ; Store the captured value into MOVWF _falltime_l ; falltime_l and ... MOVF CCPR1H,W MOVWF _falltime_h ; ... falltime_h ; ; 16 bit subtract ; falltime = falltime - risetime ; SUB16 _falltime, _risetime ;this subroutine performs 16 bit subtraction ISR_2 INT_RETURN ENDASM




Bookmarks