ilacmano
Newbie level 6
My hobby is electronic and wish to work inverter DC12V/AC220V 50Hz project in Bascom AVR with microcontroller Atmega16/32 16MHZ and need help.
For 1st step of project is write code for PWM positiv half cycle sine wave on PWM1A and negative half cycle on PWM1B with 32 samples every cycle.
On PWM1A/PWM1B connect 1K resistor IRFZ44N and transformer 2x12V/220V. Simulated in Proteus Isis on output transformer haven't sinusoidal curve measured by oscilloscope, and on PWM1A/PWM1B not have 10ms (20ms) half cycle.
Sine_Dat calculated on PWM Period 255.
I dont' know where is may error in code.
For 1st step of project is write code for PWM positiv half cycle sine wave on PWM1A and negative half cycle on PWM1B with 32 samples every cycle.
On PWM1A/PWM1B connect 1K resistor IRFZ44N and transformer 2x12V/220V. Simulated in Proteus Isis on output transformer haven't sinusoidal curve measured by oscilloscope, and on PWM1A/PWM1B not have 10ms (20ms) half cycle.
Sine_Dat calculated on PWM Period 255.
I dont' know where is may error in code.
Code:
' 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