ProblemwithPWMfrequency


Closed Thread
Results 1 to 38 of 38

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    Not that I think it's a problem but why TWO returns in the subroutine? The execution will never get to the second one.

    Are you sure that the output really IS "stationary", it's not just that you have the horisontal sweep set to fast?
    Wrap the ISR in a LED1 = 1 and LED1 = 0 and sample PortB.1 too with your "scope", that way you'll see IF and and what frequency the interrupt ACTUALLY runs as well as how long it takes to execute.
    With 4MHz crystal, PR2 set to 199 and prescaler set to 1:1 you should be getting 5kHz PWM but your "scope" (if I read it correctly) shows a period of 400us which means a PWM frequency is 2.5kHz, why is that?

  2. #2
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thankx HenrikOlsson for the comments I worked on on them

    For what I know the gosub coment within subroutine takes two return. The first return will bring you back to the subroutine(TEST) and the second return will bring you to the original gosub comand if I'm not wrong.

    I havebeen worked on the code for almost whole day I'm still getting the same output and I try know my interrupt frequency by Toggling PortB.1 the problem comes I cant see the square wave comes from that pin. here is code and output from ISIS.

    Code:
    define OSC 4
    Wsave var byte   $70system
    Wsave1 var byte  $A0system
    Wsave2 var byte  $120system
    Wsave3 var byte  $1A0system
    ADCON0 = %00000000
    ADCON1 = %00000000
    LED1   VAR  PORTB.1
    
    TRISB = %11111101
    TRISC = %11111001  'PMW output for CCP1 AND CCP2
    TRISA = %11111111
    PR2 =  199   'set for 5Khz HPWM
    CCP1CON = %00001100 'set CCP1 for PWM OPERATION
    CCP2CON = %00001100 'set CCP2 for PWM OPERATION
    T2CON = %00000100  'TIMER2ON and prescale of 1:1 
    index var byte
    temp var word
    timerone var word 
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System          
    INCLUDE "ReEnterPBP.bas"  
    ASM
     
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _TEST,  PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
     
    T1CON = 000001                 ; Prescaler = 1;1, TMR1 ON
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts 
     
    timerone = 64980 ;gives about 50 htz sine 
    Main:
           PAUSE 5
     
    GOTO Main
    
    TEST: 
        T1CON = 000000 'stop the timer
        TMR1L = timerone.byte0  'reload the timer
        TMR1H = timerone.byte1
        T1CON = 000001          ' start the timer
        gosub display
        CCP1CON.4 = Temp.0 ' bit 0
        CCP1CON.5 = Temp.1 ' bit 1       
        CCPR1L = Temp>>2  'Bit 2-7
        TOGGLE LED1  
        index=index+1
      if index =36 then index =0
       
         
     @    INT_RETURN
     
     display:
      lookup index,[115,102,90,79,70,62,57,53,52,53,57,62,70,79,90,102,115,128,141,154,166,177,186,194,199,203,204,203,199,194,186,177,166,154,141,128],temp
        
      return    'return to the subroutine 
      return    'return to the original GOSUB
    I don't understand why the output is 2.5Kh instead of 5khz

    help me on that problems
    thankx
    Attached Images Attached Images

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


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    No you need one RETURN at the end of the subroutine. That RETURN makes the code return to line after the GOSUB, the @INT_RETURN makes it return from the interrupt handler.

    Are you sure you have the scope connected to the correct output?


    The first thing you need to do i make sure that the PIC and that simulator of yours is running properly and that you're using it properly. Remove all the interrupt and PWM stuff and then make a simple program that just toggles that LED1 output. Connect your "scope" and make sure you can see the output toggle on the scope.

    When and only when you have that working put the interrupt stuff back in but move the TOGGLE command to the interrupt handler - EXACTLY as shown here. Don't try to add all your PWM stuff.

    When and only when you can see that LED1 output toggling on the scope try adjusting the interrupt frequency and see if that works the way you want.

    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.

    Now, when you have THAT working you are ready to start adding the PWM stuff.

    Don't try to skip a step until you have the previous one working and understand why and how.
    Last edited by HenrikOlsson; - 29th February 2012 at 18:31.

  4. #4
    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

  5. #5
    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.

  6. #6
    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.

  7. #7
    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.

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