read a pwm


Closed Thread
Results 1 to 3 of 3

Thread: read a pwm

  1. #1
    Join Date
    Jan 2006
    Posts
    8

    Default read a pwm

    hello,
    I need to increase a frequency of 120Hz to 10kHz PWM so
    I have a measure of a pwm dutycyle has 120hz and take its value to send in another PWM at 10kHz with a pic 18F2420

    can you give me a procedure?

    thank you

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,810


    Did you find this post helpful? Yes | No

    Default

    I can give you an idea to start. Use the Pulsin command to measure the width of High and Low pulses. Or use timer.Start an stop the timers on each edge of the 120Hz pulse and then find the frequency or period.

    Ioannis

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hello

    I think Darrel already has published something ( of course nice !!! ) about that ...

    Alain

    Here it is ...

    Code:
    '****************************************************************
    '*  Name    : FREQx2.bas                                        *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 3/2/2007                                          *
    '*  Version : 3.0                                               *
    '*  Notes   : Doubles the frequency of input pulses,            *
    '*          : and maintains the same DutyCycle as the Input.    *
    '*          : Range = .1hz to 500 hz  (0-40% max DutyCyle)      *
    '*          : Target = PIC12F629/675                            *
    '****************************************************************
    @  __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    DEFINE  NO_CLRWDT 1
    Clear
    SpeedPin   VAR GPIO.2                       ; Input from speed sensor
    SpeedX2Pin VAR GPIO.4                       ; Output to Speedometer
    IdleState  VAR GPIO.0                       ; State of input when sensor
                                                ;    is not on a "Slot"
    Invert     VAR GPIO.1                       ; Inverts output if = 1
    OPTION_REG.7 = 0                            ; enable weak Pull-ups
    WPU = 3                                     ; weak Pull-up on GP0 and GP1      
    ;_____________________________________________________________________________
    LastState  VAR BIT                          ; Last state the Input changed to
    LoopCount  VAR byte                         ; Counts the loops of each pulse
    LoopCount1 VAR byte
    LoopCount2 VAR byte
    PulseWidth  VAR byte  BANK0                 ; Counts the loops of output pulse
    PulseWidth1 VAR byte  BANK0
    PulseWidth2 VAR byte  BANK0
    LastPulseWidth  VAR byte                    ; saved width of last pulse
    LastPulseWidth1 VAR byte                    ;    so it can duplicate it
    LastPulseWidth2 VAR byte
    CycleWidth  VAR byte  BANK0                 ; Counts the loops of output cycle
    CycleWidth1 VAR byte  BANK0
    CycleWidth2 VAR byte  BANK0
    OutPulseCount VAR BIT
     
    CMCON = 7
    'ANSEL = 0    ; for 12F675
    SpeedX2Pin = IdleState
    OUTPUT SpeedX2Pin
    Main:
        LoopCount = LoopCount + 1               ; 24-bit counter
        If LoopCount = 0 Then
            LoopCount1 = LoopCount1 + 1         
            if LoopCount1 = 0 then
                LoopCount2 = LoopCount2 + 1
            endif
        endif
        IF SpeedPin <> LastState then           ; If Input State Changed?
            LastState = SpeedPin
     
            if LastState = (IdleState ^ 1) then         ; Start of Input Pulse
                CycleWidth  = LoopCount
                CycleWidth1 = LoopCount1
                CycleWidth2 = LoopCount2
                ASM
                    bcf   STATUS,C              ; CycleWidth = LoopCount / 2
                    rrf   _CycleWidth2, F       ; Output Cycle is half of Input
                    rrf   _CycleWidth1, F
                    rrf   _CycleWidth, F
                ENDASM
                LoopCount  = 0                  ; reset LoopCount
                LoopCount1 = 0
                LoopCount2 = 0
            else                                ; End of Input Pulse
                PulseWidth  = LoopCount
                PulseWidth1 = LoopCount1
                PulseWidth2 = LoopCount2
                ASM
                    bcf   STATUS,C              ; PulseWidth = PulseWidth / 2
                    rrf   _PulseWidth2, F       ; Output Pulse is half of Input
                    rrf   _PulseWidth1, F
                    rrf   _PulseWidth, F
                ENDASM
                LastPulseWidth = PulseWidth
                LastPulseWidth1 = PulseWidth1
                LastPulseWidth2 = PulseWidth2
                SpeedX2Pin = ((IdleState ^ 1) ^ Invert) ; start First Output Pulse
                OutPulseCount = 0
            endif
        else
            if SpeedX2Pin = ((IdleState ^ 1) ^ Invert) then
                if PulseWidth = 0 then          ; countdown output time (24bit)
                    if PulseWidth1 = 0 then
                        if PulseWidth2 = 0 then
                            SpeedX2Pin = (IdleState ^ Invert) ; end of pulse, 
                        else                       ; wait for next transition
                            PulseWidth2 = PulseWidth2 - 1
                            PulseWidth1 = 255
                            PulseWidth  = 255
                        endif
                    else
                        PulseWidth1 = PulseWidth1 - 1
                        PulseWidth  = 255
                    endif
                else
                    PulseWidth = PulseWidth - 1
                endif
            else
              @ NOP                             ; Keep the loop symmetrical
              @ NOP
              @ NOP
              @ NOP
              @ NOP
              @ NOP
            endif
     
            if OutPulseCount = 0 then
                if CycleWidth = 0 then          ; countdown to Midpoint of cycle
                    if CycleWidth1 = 0 then
                        if CycleWidth2 = 0 then
                            SpeedX2Pin = ((IdleState ^ 1) ^ Invert) ; Start second pulse
                            OutPulseCount = 1
                            PulseWidth  = LastPulseWidth
                            PulseWidth1 = LastPulseWidth1
                            PulseWidth2 = LastPulseWidth2
                        else
                            CycleWidth2 = CycleWidth2 - 1
                            CycleWidth1 = 255
                            CycleWidth  = 255
                        endif
                    else
                        CycleWidth1 = CycleWidth1 - 1
                        CycleWidth  = 255
                    endif
                else
                    CycleWidth = CycleWidth - 1
                endif
            else
              @ NOP                             ; Keep the loop symmetrical
              @ NOP
              @ NOP
              @ NOP
              @ NOP
            endif
        endif
    goto Main
    end
    ...

    Just for the idea ... !
    Last edited by Acetronics2; - 20th January 2011 at 10:41.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts