PWM to Frequency


Closed Thread
Results 1 to 4 of 4
  1. #1

    Default PWM to Frequency

    Currently using a 16F886 with any internal or external OSC setting. I am flexible there. I need to read in a square wave that is 300Hz. Based on the incoming duty cycle I need to output a frequency that varies from 0-1500hz and keeping the duty cycle at 50%. What I am doing is trying to fool an electric motor controller into thinking there is a motor connected to it. The controller outputs a PWM for the speed and uses feedback based on a 1500ppr encoder to control the speed. So I want to make a feedback simulator in essence. On the board I have rigged up a couple flip flops so I can divide the frequency out of the PortC.2 by 2,4,8,16 if needed.

    Using the Mister E Pic calculator is looks like if I go the CCP route I am going to have to change pre-scalers and the duty cycle on the fly. Will using HPWM be ok for this? I am going to give that route a go first but looking for some input.

    Thanks in advance.

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: PWM to Frequency

    How you go about this depends a lot on the resolution you need. Do you have some idea of that?
    Charles Linquist

  3. #3
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: PWM to Frequency

    Also, will the pic be deticated to this or are you going to want/need other things frm it also.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: PWM to Frequency

    The PIC is dedicated to doing this alone, nothing else.

    As far as resolution, the more the better of course. I just don't know how much I can bullshit the controller until I hook it up.

    I am playing around with the PULSIN for the incoming PWM and HPWM for the outgoing signal. It seems to be working pretty good.

    Here is the code.

    Code:
    Include "modedefs.bas"    ' Mode definitions for Serout
    Yellow          Var     PortC.5
    Red             Var     PortC.4
    Green           Var     PortC.3
    Rate            var     PortC.1
    LCD             var     PortB.5 
    
    VoltIn          var     word
    Flasher         var     word 
    Speed           var     word
    Numerator       var     word
    Divisor         var     word
    Total           var     word
    
    Prefix          con     $FE             ' needed before each command
    LcdCls          CON     $51             ' clear LCD (use PAUSE 5 after)
    CursorPS        con     $45             'Cursor Position
    Backlight       con     $53             ' Backlighting 1-8
    
    TRISC = 0000010
    TRISB=000000
    TRISB=111111 
    Option_Reg.7=0    'Pull-Ups Enabled
    ANSELH=0          'PortB Digital
    WPUB.0=1
    WPUB.1=1
    WPUB.2=1
    WPUB.3=1
    WPUB.4=1
    INTCON.7=0
    Flasher = 0
    
    define OSC 4
    'OSCCON.6=1'0    '250khz
    'OSCCON.5=0    '250khz
    'OSCCON.4=0'0    '250khzHZ
    pause 10
    
    SEROUT2 LCD,84, [Prefix,Backlight,8] 'LCD used for debug purpose
    Serout2 LCD, 84, [Prefix,LcdCLS]
    pause 10
    SEROUT2 LCD,84, [Prefix,CursorPS,0, "RPM Simulator "]
    pause 1000
    Serout2 LCD, 84, [Prefix,LcdCLS] 
    
    high green
    high red
    high yellow
    
    Repeat
      GOSUB ON_Off     
      Flasher = Flasher + 1
    UNTIL Flasher = 5
    Flasher = 0
    
    '245 to 24000 for frequency out in the HPWM  23755 range
    '15.3HZ to 1.52kHZ at the 1/16 flip flop output
    
    Start: 
      gosub Get_Pulse
      Gosub Math
    Goto Start
    
    
    Get_Pulse:
      Pulsin Rate,0,Numerator
      pulsin Rate,1,Divisor
      Total=Numerator+Divisor
      
      If Total=0 then 'LED Green when PWM present, red when no PWM
         HIgh Yellow
         Low Red
         High Green
         else
         HIgh Yellow
         High Red
         Low Green
      endif
    return
    
    Math:
      SEROUT2 LCD,84, [Prefix,CursorPS,0, dec5 Numerator," ",dec5 Divisor, " ", dec5 Total]  
      Divisor=Divisor/10
      Speed = (240*Divisor)
      SEROUT2 LCD,84, [Prefix,CursorPS,64, dec5 Speed] 
      HPWM 1,127,Speed
    Return
    '___________________________________________
    '-----------Activate/Deactivate Outputs-----
    On_Off:
      high REd
      high Green
      high Yellow
      PAUSE 100
      high REd
      low Green
      high Yellow
      PAUSE 100
    RETURN

Members who have read this thread : 0

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