ProblemwithPWMfrequency


Closed Thread
Results 1 to 38 of 38

Hybrid View

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


    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.

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

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


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    Here's three cycles with the values from you last post.
    Name:  SIN_PWM_6.GIF
Views: 76738
Size:  7.0 KB

    What is that you think is wrong with it? I don't see it...

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    He checks for 36, shouldn't it be 35?

    Robert

  5. #5
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Quote Originally Posted by Demon View Post
    He checks for 36, shouldn't it be 35?
    No, after completing step 35, it increments, then it does a = 36 check. If it = 36, it goes back to step 0.
    http://www.scalerobotics.com

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Right, I forgot to check when the step was incremented, argh, and that's basic debugging procedure too (must be going senile).

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Could the length of PAUSE be an issue?

    http://www.picbasic.co.uk/forum/showthread.php?t=1853

    Would placing PAUSE 1 within a loop make a difference?

    What about using microseconds instead of milliseconds?

    There's a minimum delay in microseconds in the PBP manual, but I don't see oscillator setting in that last code.

    Robert

  8. #8
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    No change even if the pause 1, what I want to ask is the values in the look table greater than 255 may affect the sinwave and cause the signal to be distorted?. Can any one simulates the code and see the effect. Why the output look like this because I have try to eliminate the problem of flat spot for almost 4days but the problem still exit even although I try to edit my look table as HenrikOlsson told me about the value 512 ).

    Code:
    define OSC 4
    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 1
     
    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
    Attached Images Attached Images

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