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: 15036
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