' Program : PWM Sine Wave 50Hz inverter.bas
' Date : 05-06-12
' Processor : ATMEGA 16/32 16 MHz
' Input: Internal sine wave value table
' Process : Pulse width modulation
$regfile = "m16def.dat" ' specify the used micro
$crystal = 16000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 128 ' default use 32 for the hardware stack
$swstack = 64 ' default use 10 for the SW stack
$framesize = 64
' variable
Dim Half_cycle As Byte
Dim Sine_table(32) As Byte ' sine_table
Dim Index As Byte ' Positive or Negativ Half_cycle
' Config Pin
Config Pind.5 = Output 'Pin OC1A - PWM1a
Config Pind.4 = Output 'Pin OC1B - PWM1b
'*************************************************
'* Halfperiod = 10 ms.
'* There are 32 pwm values in a half period 10ms/32
'* We need to change the OCR value every 10000/32 =312.5 microsec
'* Let Timer0 overflow after 312.5 us.
'* Set prescaler to 64 *
'* 1 timer step = 64/16MHz = 4 us ; 312.5/4 = 78.12
'* Preload value = 256 - 78.
'*************************************************
Config Timer0 = Timer , Prescale = 64 '64/16MHz = 4us
Enable Ovf0
On Ovf0 Timer0_ovf
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1
' PWM Freq 31.25 kHz for PWM = 8 bit or PWM Freg 15.625 kHz for PWM = 9 bit
'restore Sine_dat
For Index = 0 To 31
Sine_table(index + 1) = Lookup(index , Sine_dat) ' read sine_dat
Next Index
Index = 0
Half_cycle = 0 ' positiv sine cycle
'Pwm_count = 0
Enable Timer0 ' PWM Counter
Enable Interrupts
Do
Loop ' Loop forever
End ' end program
'************************ Interrupt0 ****************************************
Timer0_ovf:
Timer0 = 255 - 78 ' preload
If Half_cycle = 0 Then ' select half cycle positiv
Pwm1a = Sine_table(index + 1) ' positive half cycle
Pwm1b = 0
Elseif Half_cycle = 1 Then
Pwm1b = Sine_table(index + 1) ' negativ sine cycle
Pwm1a = 0
End If
Incr Index ' increments Index for 1
If Index => 32 Then
Index = 0 ' reset Index to 0
If Half_cycle = 0 Then
Half_cycle = 1 ' select negativ Half Cycle
Elseif Half_cycle = 1 Then
Half_cycle = 0 ' select positiv Half Cycle
End If
End If
Return
'**************** SINE WAVE DATA baase on PWM PERIOD 255 **********************
Sine_dat:
Data 25 , 50 , 74 , 98 , 120 , 142 , 162 , 180 , 197 , 212 , 225 , 235 , 244 , 250 , 254 , 255 , 254 , 250 , 244 , 235 , 225 , 212 , 197 , 180 , 162 , 142 , 120 , 98 , 74 , 50 , 25 , 0