Please could someone provide some example code on how to read Pulse-Width-Modulation (PWM) for a pin? I want to trigger an operation once the incoming PWM signal hits a certain level.
thx
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No example code, but would use the CCPx module and interrupts to do this. So in pseudo code:
Set ServoPin as an input, and ServoPulse as a word. Also set CCP1CON register for capture on rising edge.
...
...
On Interrupt CCP1 call CheckEdge
...
...
Sub CheckEdge
CCP1IF = 0 ;Need to clear flag??
If CCPM0 = 1 Then ;Rising edge detected
TMR1On = 1 ;Start Timer
CCPM0 = 0 ;Set capture back to falling edge
Else ;Falling edge detected
TMR1On = 0 ;Stop timer
ServoPulse = CCPR1L
ServoPulse_H = CCPR1H
CCP1M0 = 1 ;Set capture back to rising edge
End if
end sub
Trigger event based on conditional statement and ServoPulse value.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Please could someone provide some example code on how to read Pulse-Width-Modulation (PWM) for a pin? I want to trigger an operation once the incoming PWM signal hits a certain level.
thx
No example code, but would use the CCPx module and interrupts to do this. So in pseudo code:
Set ServoPin as an input, and ServoPulse as a word. Also set CCP1CON register for capture on rising edge.
...
...
On Interrupt CCP1 call CheckEdge
...
...
Sub CheckEdge
CCP1IF = 0 ;Need to clear flag??
If CCPM0 = 1 Then ;Rising edge detected
TMR1On = 1 ;Start Timer
CCPM0 = 0 ;Set capture back to falling edge
Else ;Falling edge detected
TMR1On = 0 ;Stop timer
ServoPulse = CCPR1L
ServoPulse_H = CCPR1H
CCP1M0 = 1 ;Set capture back to rising edge
End if
end sub
Trigger event based on conditional statement and ServoPulse value.