ProblemwithPWMfrequency


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi HenrikOlsson

    (1) Thankx. I worked on the steps you told me and I manage to control my interrupt frequency which is (50*36=1800) for this frequency the time1 will be loaded with 64980 this value gives me (1667hz) which is the lowest frequency but the value 65080 gives me the frequency 1818. I think as you told me early the PBP tpyes take longer time compared to ASM types ,thankx for it and I deside to use the value which gives (1818hz).

    (2)But sorry you are stastment below is not clear to me.
    EDIT: Now make sure that you can setup and control the dutycycle of the PWM module in the main loop (still no interrupts). Just toggle the dutycycle between two values and make sure you can see that on the scope.

    Thankx

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    2) I mean something simple like this:
    Code:
    Main:
      Temp = 40
      GOSUB UpdateDuty
      Pause 500
      Temp = 400
      GOSUB UpdateDuty
      Pause 500
    GOTO Main
    
    UpdateDuty:
      CCP1CON.4 = Temp.0 ' bit 0
      CCP1CON.5 = Temp.1 ' bit 1       
      CCPR1L = Temp>>2  'Bit 2-7
    RETURN
    This should switch the dutycycle between '40' and '400' att 500ms intervals which should be clearly visible on the "scope". Make sure you can do that before you try to move the PWM stuff to the ISR. Since you now know that is interrupt is firing properly and you can control the frequency all you need is to to move it back into the ISR.

    You are making progress but the key is to take it slow and try to understand each step. When something doesn't work, solve one problem at a time and take it slow.

    /Henrik.

  3. #3
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thankx HenrikOlsson for the comments, and explaination to me

    I have been worked on this code for the whole day but still I didi not get any output. The problem I faced is I don't get any output to the CCP1(TRISC<2>).I tryt to put led1 on main to measure the time I get it clear which is 1sec for each toggle which is true( 500+500)ms also I put led1 on UpdateDuty subroutine also I get clear a delay of 500ms to each toggle which is also true because UpdateDuty it is called aftter every 500ms. But I 'm confusing where is problem? And why is CCP1 does not output to (TRISC<2>)?.Here is the code.

    Code:
    PIC 16F877
    define OSC 4
    
    TRISC   = %11111011
    CCP1CON = %00001100
    T2CON   = %00000100
    PR2  = 199 for 5khz
    TRISB   = %11111101 
    TRISA   = %11111111
    LED1   VAR  PORTB.1
    temp var word
    Main:
      Temp = 40
      GOSUB UpdateDuty 
      Pause 500
      Temp = 400
      GOSUB UpdateDuty
      Pause 500
      TOGGLE LED1
    GOTO Main
    UpdateDuty:
      CCP1CON.4 = Temp.0 ' bit 0
      CCP1CON.5 = Temp.1 ' bit 1       
      CCPR1L = Temp>>2  'Bit 2-7
    RETURN
    end
    Help me to solve this problem thankx.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    I've tried the above code here and it works just fine. I don't have a 16F877 so I tried it on a 18F25K20, the dutycylce of PWM-signal changes just as expected - no problem.

    I looked at the datasheet for the 16F877 and the CCP1 pin isn't multiplexed with any other functions so it should work. Have you tried this with real hardware or are you still messing around with the simulator? My guess is that you're measuring on the wrong pin or that you have that simulator of yours set up wrong or that the PWM module isn't simulated properly.

  5. #5
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thankx HenrikOlsson

    True the problem was due to the simulator was not working properly .Now the code is working fine and I get the correct PWM frequency(5khz) after change the ISIS vasion Thankx for it. But I have the following problem of relating the dutycycle from the theory and that from picdatashetformural.

    From theory (dutycycle=(Time-ON/period) for my case
    case1: AS seen from the scope when I used the above fomural gives 1/2=0.5=50%

    case2: As seen from the scope when I used the above fomural gives 0.1/2=0.05=5%

    how these values(case1 and case2) can be related with 40 and 400 values

    from pic16f877 datasheet PWM_dutycycle= CCPR1L:CCP1CON<5:4>*TOSC*(TMR2 prscale value)

    I 'm confusing how can I relate the value from theory formular and that from datasheet to know that I get the correct results.

    Help me to solve this problems because always these two case confusing me.


    Thankx
    Attached Images Attached Images

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    I really hope you've learned something here....How much time have you (and I) wasted on that simulator by now? If you had put a PIC on a breadboard from the get go you'd be done by now.

    As you know by now the PWM period ends when TMR2=PR2. So if you set the CCPR1L value to whatever you have PR2 set to you'll get 100% dutycycle (this assumes 1:1 prescaler). So, if PR2 is 200 then setting CCPR1L to 200 will give you 100% dutycycle, setting it to 50 will give you 25% dutycycle and so on. The two lowest significant bits (CCP1CON<5:4>) adds 2 bits (or 4 times if you will ) worth of resolution. So if you prefer you could say that a DutyVal of PR2*4=100%.

    For example, DutyVal=800. Take the 2 low bits and stuff them CCP1CON5:4 and take the 8 high bits in stuff them in CCPR1L. If you take a close look at the 8 bits alone you'll see that it's the value 200.

    /Henrik.

  7. #7
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thankx HenrikOlsson
    I really appreciate your effort on this forum.

    (1) sorry I understand you explaination very well on how dutycycle relate with value of PR2. one problem is that you statements below is not clear to me and why PR2*4=100%:
    the two lowest significant bits (CCP1CON<5:4>) adds 2 bits (or 4 times if you will ) worth of resolution. So if you prefer you could say that a DutyVal of PR2*4=100%.
    For example, DutyVal=800. Take the 2 low bits and stuff them CCP1CON5:4 and take the 8 high bits in stuff them in CCPR1L. If you take a close look at the 8 bits alone you'll see that it's the value 200.
    a

    (2)From what I know for 8bit resolution
    CCP1CON.4 = duty.0
    CCP1CON.5 = duty.1
    CCPR1L = DUTY >> 2
    that is means that 2bit (CCPICON<5:4>) and 6bit obtain after shifting right(divide by 4) duty value are combine to form 8bit resolution

    What I want to know is that, if I used 10 bit resolution is that code here correct?

    CCP1CON.4 = duty.0
    CCP1CON.5 = duty.1
    CCPR1L = DUTY
    that is 2bit(CCPICON<5:4>) are combine with 8bit (CCPR1L)from duty value to form 10bit resolution.
    thankx

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