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
    I have manage to combine the code,compile it successfully and get the correct PWM frequency(5khz) thanks for all.
    (a) As you told me early the DutyVal of PR2*4=100% that means that my value of PR2=199 the DutyVal can vary within (0-796).One problem I have is in my lookup table the there is value greater than 796.What happen to this values greater than 796?

    (b) Also I have read PIC Midrange manual(http://ww1.microchip.com/downloads/e...doc/33023a.pdf) page 211 and 212 it says
    (1) " If the PWM duty cycle value is longer than the PWM period, the CCPx pin will not be cleared. This allows a duty cycle of 100%".
    (2) " Any value greater than 255 will result in a 100% duty cycle".
    is this two reason cause some part of my wave form not to have a PWM frequency of 5khz? having a long off time
    here is code and two phase PWM output

    Code:
    STEPCOUNT var byte
    STEPCOUNT1 var byte
    
    LED1  VAR  PORTB.1
    
    STEPCOUNT = 0   'pointer for phase one in sinearray
    STEPCOUNT1 = 12 'pointer for phase two  in sine array
    ADCON0 = %00000000
    ADCON1 = %00000000
    
    TRISB = %11111101
    TRISC = %11111001  'PMW output for CCP1 AND CCP2
    TRISA = %11111111
    TMR2 = 0
    PR2 = 199 'generate 5khz  'set for 18Khz HPWM
    CCP1CON = %00001100 'set CCP1 for PWM OPERATION
    CCP2CON = %00001100 'set CCP2 for PWM OPERATION
    T2CON = %00000100  'TIMER2ON and prescale of 1:1 
    
    sineval var byte[36]
    sineval[0] = 512    
    sineval[1] = 583   
    sineval[2] = 652
    sineval[3] = 717
    sineval[4] = 775
    sineval[5] = 825
    sineval[6] = 866
    sineval[7] = 896
    sineval[8] = 915
    sineval[9] = 921   
    sineval[10] =915
    sineval[11] =896
    sineval[12] =866   
    sineval[13] =825
    sineval[14] =775
    sineval[15] =716
    sineval[16] =652
    sineval[17] =583
    sineval[18] =512   
    sineval[19] = 441
    sineval[20] = 372
    sineval[21] = 308 
    sineval[22] = 249
    sineval[23] = 199
    sineval[24] = 158
    sineval[25] = 128
    sineval[26] = 109
    sineval[27] = 103
    sineval[28] = 109 
    sineval[29] = 128
    sineval[30] = 158
    sineval[31] = 199
    sineval[32] = 249
    sineval[33] = 308
    sineval[34] = 441
    sineval[35] = 512
    
    timerone var word 
    Temp var byte
    Temp1 var byte
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System          emp
    INCLUDE "ReEnterPBP.bas" 
     
    ASM
     
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _sine,   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 =  65080' gives1818hz interruptfrequency or about 50 htz sine 
    Main:
           PAUSE 5
     
    GOTO Main
     
    '---[TMR1_INT - interrupt handler]------------------------------------------
    
    sine:
         T1CON = 000000 'stop the timer
    
        TMR1L = timerone.byte0  'reload the timer
        TMR1H = timerone.byte1
        T1CON = 000001          ' start the timer
        
        
        TeMP = sineval[STEPCOUNT]
        CCP1CON.4 = Temp.0 ' bit 0
        CCP1CON.5 = Temp.1 ' bit 1       
        CCPR1L = Temp >>2  'Bit 2-7 
        
        TeMP1 = sineval[STEPCOUNT1]
        CCP2CON.4 = Temp1.0 ' bit 0
        CCP2CON.5 = Temp1.1 ' bit 1       
        CCPR2L = Temp1 >>2  'Bit 2-7
        TOGGLE LED1 
        stepcount =  stepcount +1
        stepcount1 =  stepcount1 +1
        
        if stepcount =36 then stepcount =0
        if stepcount1 =36 then stepcount1 =12
         
     
    @    INT_RETURN
    Help to help me to solve this problems
    thanks
    Attached Images Attached Images

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


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    One problem I have is in my lookup table the there is value greater than 796.What happen to this values greater than 796?
    You answered your own question in (1) from the Midrange manual:
    (1) " If the PWM duty cycle value is longer than the PWM period, the CCPx pin will not be cleared. This allows a duty cycle of 100%".
    Any dutycycle value above 796 will result in 100% dutycycle. In other words your SIN-output will be distorted/clipped.

    is this two reason cause some part of my wave form not to have a PWM frequency of 5khz? having a long off time
    Don't think so, I'd expect it to stay at 100% but I may be wrong. However this isn't correct:
    Code:
        if stepcount =36 then stepcount =0
        if stepcount1 =36 then stepcount1 =12
    You can't restart at the 12th byte, that will make the second output miss 1/3 of the cycle. You start at the 12th byte to get the correct phase angle offset but you must restart at 0 once it reaches 36.

    /Henrik.

  3. #3
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thanks HenrikOlsson for your information.But this look like is not clear to me. From what I now when stepcount1 =36 for 2nd phase then stepcount1=12 so as it start to the 12thbyte again because initially is declared as stepcount1=12.What confusing me is that initially in my declaration of variable stepcount1=12 why after one cylecle stepcount1 should declared as stepcount1=0.How can I archieve phase difference of 120 if stepcount=0 after one cylecle.

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


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    You have 36 values in you SIN table. To get a correct output you need each phase to cycle thru these 36 values. You get the phase difference by starting each phase 120° apart in the lookup table but from that point they will keep that phase difference if you reset the index pointer to 0 when they reach 36:
    Name:  SIN_PWM_2.JPG
Views: 68868
Size:  39.3 KB

    If you don't reset it to 0 but instead keep restarting the second phase at the 12th value (like you're currently doing) the output will look something like this:

    Name:  SIN_PWM_3.JPG
Views: 68578
Size:  46.6 KB

    Why? Because the second phase never "gets" the first 12 values of the lookup table so the output gets A) distorted and B) of another frequency since it doesn't contain the same number of "samples" as the the first phase.

    Thinking even further and adding the third phase, starting at the 24th value keep restarting at 24 the output would look something like:
    Name:  SIN_PWM_4.JPG
Views: 68896
Size:  43.9 KB

    I hope you see that the above is not correct and that you understand WHY it's not correct.



    Using your lookup table, starting the pointers at 0, 12 and 24 and restarting them all at 0 when they reach 36 the output looks like:
    Name:  SIN_PWM_5.JPG
Views: 69134
Size:  45.6 KB
    Which I think is more inline with what you're after, isn't it?

    Now, the waveform is distorted because you have the value 512 as the first AND as the last the value in the table which means that you'll get a dutycycle of 512 for two "samples" in a row. That is where th flat spot in the wave form is comming from.

    /Henrik.

  5. #5
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi HenrikOlsson

    Now Inderstand why stepcount1 should restart to zero.
    (1) But I have been working on the code and and modify my look up table as you told me about the first value and the last value, but the problem of flat spot still remaing I didn't understand why is this?
    (2)If I want to make acertain value of dutycylecle maximum (specific range of variation) let say(0- 80)% or (0-75%) should I do in the look up table or modify the formular?
    formular : ((sin(degree*pi/180)+1) /2)*1023 for 10 bit resolution.


    help me to solve this problems

    Thanks

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


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    1) Can't help without seeing the values.
    2) Not sure I understand what the problem is.... If you want a certain maximum dutycycle then simply rescale the values before storing them in the lookuptable (change the formula to produce a table with lower values). If you want to rescale at runtime then you need to perform the math in the PIC. Retrieve the "original" value, rescale, and output.

    /Henrik.

  7. #7
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi HenrikOlsson
    1) Can't help without seeing the values.
    Here is the values
    Code:
    Wsave var byte   $70system
    Wsave1 var byte  $A0system
    Wsave2 var byte  $120system
    Wsave3 var byte  $1A0system
    
    STEPCOUNT var byte
    STEPCOUNT1 var byte
    
    LED1   VAR  PORTB.1
    
    STEPCOUNT = 0   'pointer for phase one in sinearray
    STEPCOUNT1 = 12 'pointer for phase two  in sine array
    ADCON0 = %00000000
    ADCON1 = %00000000
    
    TRISB = %11111101
    TRISC = %11111001  'PMW output for CCP1 AND CCP2
    TRISA = %11111111
    TMR2 = 0
    PR2 = 199 'generate 5khz  'set for 18Khz HPWM
    CCP1CON = %00001100 'set CCP1 for PWM OPERATION
    CCP2CON = %00001100 'set CCP2 for PWM OPERATION
    T2CON = %00000100  'TIMER2ON and prescale of 1:1 
    
    sineval var byte[36]
    sineval[0] = 512    
    sineval[1] = 583   
    sineval[2] = 652
    sineval[3] = 717
    sineval[4] = 775
    sineval[5] = 825
    sineval[6] = 866
    sineval[7] = 896
    sineval[8] = 915
    sineval[9] = 921   
    sineval[10] =915
    sineval[11] =896
    sineval[12] =866   
    sineval[13] =825
    sineval[14] =775
    sineval[15] =716
    sineval[16] =652
    sineval[17] =583
    sineval[18] =512   
    sineval[19] = 441
    sineval[20] = 372
    sineval[21] = 308 
    sineval[22] = 249
    sineval[23] = 199
    sineval[24] = 158
    sineval[25] = 128
    sineval[26] = 109
    sineval[27] = 103
    sineval[28] = 109 
    sineval[29] = 128
    sineval[30] = 158
    sineval[31] = 199
    sineval[32] = 249
    sineval[33] = 308
    sineval[34] = 372
    sineval[35] = 441
    
    timerone var word 
    Temp var byte
    Temp1 var byte
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System          emp
    'INCLUDE "ReEnterPBP.bas" 
     
    ASM
     
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _sine,   ASM,  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 =  65080' gives1818hz nterruptfrequency or about 50 htz sine 
    Main:
           PAUSE 5
     
    GOTO Main
     
    '---[TMR1_INT - interrupt handler]------------------------------------------
    
    sine:
         T1CON = 000000 'stop the timer
    
        TMR1L = timerone.byte0  'reload the timer
        TMR1H = timerone.byte1
        T1CON = 000001          ' start the timer
        
        
        TeMP = sineval[STEPCOUNT]
        CCP1CON.4 = Temp.0 ' bit 0
        CCP1CON.5 = Temp.1 ' bit 1       
        CCPR1L = Temp >>2  'Bit 2-7 
        
        TeMP1 = sineval[STEPCOUNT1]
        CCP2CON.4 = Temp1.0 ' bit 0
        CCP2CON.5 = Temp1.1 ' bit 1       
        CCPR2L = Temp1 >>2  'Bit 2-7
        TOGGLE LED1 
        stepcount =  stepcount +1
        stepcount1 =  stepcount1 +1
        
        if stepcount =36 then stepcount =0
        if stepcount1 =36 then stepcount1 =0
         
     
    @    INT_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