Okay, the code is now capable to calculate 3 sine waves (one for each phase).
I can modify the frequency (by increasing or decreasing freqdiv), but I've difficulties to set a phase angle (0-120-240° for U, V and W respectively) and I've difficulties to set a sinus amplitude (for exemple, if I want a 0-50% dutycycle, I will write uduty=uduty*0.5, but PBP does not support float maths).
Thanks for your help. I use Darrel Taylor's interrupt system for sine-wave generation loop.
Code:
'PIC initialization
DEFINE LOADER_USED 1
DEFINE OSC 40
'DT interrupt system include
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts
'PWM calculation variables
uduty VAR WORD 'U phase duty
vduty VAR WORD 'V phase duty
wduty VAR WORD 'W phase duty
t VAR byte 'Incremental value
'Inverter U/F variables
freqdiv var WORD 'Frequency diviser
FREQdiv=0 'Range : 0-65535
'Port registers configuration
PORTB=%0 ' Clear ports
TRISB=%11000000 ' PWM 0,1,2,3,4,5 outputs
'PCPWM registers configuration
DTCON=%110 ' Deadtime (600ns)
PTCON0=%100 ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
PTCON1=%10000000 ' PWM time base is ON, counts up, 19.45kHz/4
PTPERL=%0 ' PWM low-side
PTPERH=%1 ' PWM high-side
'PWM registers configuration
PWMCON0=%1000000 'PWM 0,1,2,3,4,5 set in pair mode
PWMCON1=%1 'PWM timer sync configuration
'Interrupt processor
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag
INT_Handler TMR1_INT, _pwmint, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
'Timer configuration
T1CON=%10000001 ; Prescaler = 1:1, TMR1ON, 16-bit counter
@ INT_ENABLE TMR1_INT ; Enable Timer 1 interrupts
'Main loop
mainlp:
pause 1000
goto mainlp
'PWM calculation and update interrupt
pwmint:
t=0
for T=0 to 255 step 1
'Frequency diviser
pauseus freqdiv
'PWM U phase calculation
uduty=((sin(t)+128)*4)
'PWM V phase calculation
vduty=((sin(t)+128)*4)
'PWM W phase calculation
wduty=((sin(t)+128)*4)
'PWM U phase update
PDC0L=uduty.LOWbyte
PDC0H=Uduty.HIGHByte
'PWM V phase update
PDC1L=vduty.LOWbyte
PDC1H=vduty.HIGHByte
'PWM W phase update
PDC2L=wduty.LOWbyte
PDC2H=wduty.HIGHByte
next t
@ INT_RETURN
Bookmarks