sinusoidal PWM


Closed Thread
Results 1 to 40 of 84

Thread: sinusoidal PWM

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    I have made a little house keeping on the thead and deleted all the un-necessary quotes and left only the necessary. The replies were following the answers so it it was really obvious.

    As Henrik stated quote only when absolute necessary. It makes reading the thread more comfortable and easy to follow. Saves resources too!

    Ioannis

  2. #2
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Smile Re: sinusoidal PWM

    hellow,

    thanx for the useful comments and suggestions,i learn a lot from them...

    Henrik and Scalerobotics,
    i've changed the code and include this Code:

    Code:
    Temp = SineVal[StepCount]    
    CCP1CON.4 = Temp.0   'Bit 0
    CCP1CON.5 = Temp.1   'Bit 1 
    CCPR1L = Temp  >> 2     'Bit 2-7
    and i'm getting a smooth signal for 20Mhz Osc but not for 4MHz,tried to scale the sine table but it didnt work...but for 20Mhz Osc things looks fine...Simulation results..pdf
    i'm not sure if it is possible to get a smooth spwm signal with a 4Mhz osc as i've tried to modified my code as suggested above but the waveform is still not smooth.....

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Not sure, but you might try the code on real hardware with a scope and see how it looks. I have noticed that simulations of this type don't work out too well. General wave form maybe, but I wouldn't trust it much more than that for this application. Looks like good improvement though!
    Last edited by ScaleRobotics; - 9th May 2011 at 17:14.

  4. #4
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    hi,
    i've noted that my code produce inverted sine waveform and it is not the one i intend to produce,here is my complete code:
    Code:
    define OSC 20
    
    ;*****************VARIABLE DECLARATION******************************************
    wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
    wsave1  VAR BYTE    $A0     SYSTEM      ' location for W if in bank1
    wsave2  VAR BYTE    $120    SYSTEM      ' location for W if in bank2
    wsave3  VAR BYTE    $1A0    SYSTEM      ' location for W if in bank3
    STEPCOUNT var byte                      'Define stepcount as byte type variable
    stepcount = 0
    
    ;**************SETTING THE REGISTERS WITH APPROPIATE BIT DEFINITION************* 
    ADCON0 = %00000000
    ADCON1 = %00000000  ;all anolog output
    trisb = %11111111   ;define porta as input
    trisc = %11111011   ;make ccp1/portc.2 an output pin
    trisa = %11111111   ;define porta as input
    TMR2 = 16
    PR2 = 78          ;set for 16Khz HPWM(=32 steps*10 times*50hz)        
    CCP1CON = %000001100       ;set to pwm mode
    T2CON=%00000100           ;enable timer2 and set timer2 prescaler value of 1:1
    
    ;****************A sine lookup table in an array********************************
    sineval var byte[32]
    sineval[0] = 148
    sineval[1] = 167
    sineval[2] = 185
    sineval[3] = 200
    sineval[4] = 210
    sineval[5] = 222
    sineval[6] = 228
    sineval[7] = 230
    sineval[8] = 228
    sineval[9] = 222
    sineval[10] = 212
    sineval[11] = 200
    sineval[12] = 185
    sineval[13] = 167
    sineval[14] = 148
    sineval[15] = 128
    sineval[16] = 108
    sineval[17] = 89
    sineval[18] = 71
    sineval[19] = 56
    sineval[20] = 43
    sineval[21] = 34
    sineval[22] = 28
    sineval[23] = 26
    sineval[24] = 28
    sineval[25] = 34
    sineval[26] = 43
    sineval[27] = 56
    sineval[28] = 71
    sineval[29] = 89
    sineval[30] = 108
    sineval[31] = 128
    
     
    timerone var word 
    Temp VAR byte 
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
    
    ;********Define INT_Handler as ISR********************************************** 
    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, TMR1ON          
    TMR1L = 255    
    TMR1H = 254
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    timerone = 62411           ;gives about 50 hz sine 
    Main:
            pause 5
            
    GOTO Main
     
    '---[TMR1_INT - interrupt handler]------------------------------------------
    
    
    sine:
        TMR1L = timerone.byte0
        TMR1H = timerone.byte1
        Temp = SineVal[StepCount]   
        CCP1CON.4 = Temp.0   'Bit 0
        CCP1CON.5 = Temp.1   'Bit 
        CCPR1L = Temp >> 2     'Bit 2-7
        stepcount = stepcount +1
        if stepcount = 32 then stepcount = 0    
    @    INT_RETURN
    any idea on how to invert the wave form...?thanx in advance

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Previously you started at the "end" of table and decremented the "pointer" but now you've apparently changed that so it starts at 0 and increments the pointer instead.

    If you want it phase shifted 180° ("inverted") then simply "reverse" your sin-table. 128, 108, 89, 71.... instead of 148, 167, 185....

  6. #6
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    hellow,

    i've managed to generate SPWM signal using 4MHz xtal ,thanx to Henrik for the tip of modifying the lookup table values,this is the screen shoot taken from the actual scope.....Name:  0000.JPG
Views: 14162
Size:  143.3 KB

    however,there are things i'm not happy with this signal...
    1)i expected to see at least one complete cycle on the screen for a single screen shoot,but this is not the case,it is either positive or negative cycle!what should i do?

    2)in theory,SPWM signal is obtained by comparing the triangular wave having a certain switching frequency with a sine wave of certain modulating frequency (50Hz for my case),now the HPWM frequency is 18KHz,is this the switching frequency?if not how do i know the switching frequency for this kind of generation?i want a switching frequency around 2KHz. i want to use this signal to control a single phase inverter.

    here is my code:
    Code:
    define OSC 4
    
    ;*****************VARIABLE DECLARATION******************************************
    wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
    wsave1  VAR BYTE    $A0     SYSTEM      ' location for W if in bank1
    wsave2  VAR BYTE    $120    SYSTEM      ' location for W if in bank2
    wsave3  VAR BYTE    $1A0    SYSTEM      ' location for W if in bank3
    STEPCOUNT var byte                      'Define stepcount as byte type variable
    stepcount = 36
    
    ;**************SETTING THE REGISTERS WITH APPROPIATE BIT DEFINITION************* 
    ADCON0 = %00000000
    ADCON1 = %00000000  ;all anolog output
    trisb = %11111111   ;define porta as input
    trisc = %11111011   ;make ccp1/portc.2 an output pin
    trisa = %11111111   ;define porta as input
    TMR2 = 117
    PR2 = 55          ;set for 18Khz HPWM(=36 steps*10 times*50hz)        
    CCP1CON = %000001100       ;set to pwm mode
    T2CON=%00000100           ;enable timer2 and set timer2 prescaler value of 1:1
    
    ;****************A sine lookup table in an array********************************
    sineval var byte[36]
    sineval[0] = 115
    sineval[1] = 102
    sineval[2] = 90
    sineval[3] = 79
    sineval[4] = 70
    sineval[5] = 62
    sineval[6] = 57
    sineval[7] = 53
    sineval[8] = 52
    sineval[9] = 53
    sineval[10] = 57
    sineval[11] = 62
    sineval[12] = 70
    sineval[13] = 74
    sineval[14] = 90
    sineval[15] = 102
    sineval[16] = 115
    sineval[17] = 128
    sineval[18] = 141
    sineval[19] = 154
    sineval[20] = 166
    sineval[21] = 177
    sineval[22] = 186
    sineval[23] = 194
    sineval[24] = 199
    sineval[25] = 203
    sineval[26] = 204
    sineval[27] = 203
    sineval[28] = 199
    sineval[29] = 194
    sineval[30] = 186
    sineval[31] = 177
    sineval[32] = 166
    sineval[33] = 154
    sineval[34] = 141
    sineval[35] = 128
     
    timerone var word 
    Temp VAR byte 
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
    
    ;********Define INT_Handler as ISR********************************************** 
    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, TMR1ON          
    TMR1L = 255    
    TMR1H = 254
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    timerone = 64980          ;gives about 50 hz sine 
    Main:
            pause 5
            
    GOTO Main
     
    '---[TMR1_INT - interrupt handler]------------------------------------------
    
    
    sine:
        TMR1L = timerone.byte0
        TMR1H = timerone.byte1     
        Temp = SineVal[StepCount]   
        CCP1CON.4 = Temp.0   'Bit 0
        CCP1CON.5 = Temp.1   'Bit 1
        CCPR1L = Temp >> 2     'Bit 2-7
        if stepcount = 0 then stepcount = 36 
        stepcount = stepcount -1
       
    
    @    INT_RETURN

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hi,
    1)i expected to see at least one complete cycle on the screen for a single screen shoot,but this is not the case,it is either positive or negative cycle!what should i do?
    I haven't checked your code but going by the comments you expect a 50Hz "sinewave", a full period at 50Hz is 20ms but your scope is set to 200us/div, there's 10 divisions so you're only displaying 2ms on the screen. I see a 10:1 note there but does that apply to the horizontal sweep?

    I don't understand the part with the triangular wave being compared with a sine etc but the switching frewuency IS the PWM frequency, or 18kHz in this case. If you want 2kHz you need to reconfigure the PWM module to output 2kHz which might also require adjusting the sine-table depending on how the numbers play out.

Members who have read this thread : 1

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