Menu

PWM reading?

Help
2009-09-06
2013-05-30
  • Nobody/Anonymous

    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

     
    • kent_twt4

      kent_twt4 - 2009-09-06

      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.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.