You are on page 1of 3

'****************************************************************

'* Title : THREE PHASE PWM GENERATION


'*
'* PIC
: PIC18F4431
'*
'* Date
: 4/25/2013
'*
'* Version : 1.0
'*
'****************************************************************
'Configuration bit of oscillator
@ __CONFIG
_CONFIG1H, _IESO_ON_1H&_OSC_HS_1H
'PIC initialization
DEFINE OSC 20 ' 20MHz oscillator
define LCD_RSREG PORTC
'LCD reset registration
DEFINE LCD_RSBIT 0
'LCD reset bit
DEFINE LCD_EREG PORTC
'LCD enable registration
DEFINE LCD_EBIT 1
'LCD enable bit
DEFINE LCD_DREG PORTA
'LCD D registration
DEFINE LCD_DBIT 0
'LCD D bit
'DT interrupt system include
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts
'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

'PWM registers configuration


PWMCON0=%1000000 'PWM 0,1,2,3,4,5 set in pair mode
PWMCON1=%1 'PWM timer sync configuration
' Inverter startup
pause 200
Lcdout $FE,1
pause 500
LCDOUT " PWM "
pause 500
LCDOUT "SPEED"
pause 500
LCDOUT $FE, $C0," CONTROLLER"
pause 2000
LCDOUT $FE,1
'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=%1
@ INT_ENABLE TMR1_INT
'PWM calculation variables
uduty VAR WORD 'U phase duty
vduty VAR WORD 'V phase duty
wduty VAR WORD 'W phase duty
timer var word
dutydiv var WORD 'Frequency diviser
dutymul VAR WORD 'Duty multiplier
t VAR byte 'Incremental value
carrier VAR word
'Variables definition
timer=63000 ' Frequency adjust (60950=80Hz)
carrier=1023' Carrier frequency adjust (1023=13kHz)
dutydiv=0 'Range : 0-65535
dutymul=65535 'Range : 0-65535 (for exemple, 32767 = 125 PWM)
t=%0 'T initial value
'PWM carrier frequency register configuration
PTPERL=carrier.lowbyte
PTPERH=carrier.highbyte
'Main program loop
mainlp:
'Debug display
LCDOUT $FE,2,"Timer var :"
LCDOUT $FE,$C0,#timer
if PORTC.2=0 then timer=timer-10
if PORTC.3=0 then timer=timer+10
'pause 10
goto mainlp
'PWM calculation and update interrupt
pwmint:
' Sinewaves frequency
TMR1L=timer.lowbyte
TMR1H=timer.highbyte
'Frequency diviser
pauseus dutydiv
'duty trial
uduty=(((sin(t)+170)*12)**dutymul)+4
vduty=(((sin(t+85)+170)*12)**dutymul)+4
wduty=(((sin(t+170)+170)*12)**dutymul)+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
'T variable verification and incrementation
if t<%11111111 then
t=t+%1
else
t=%0
endif

@ INT_RETURN

You might also like