Posted: Tue Jan 06, 2009 4:54 am Post subject: ADC AND PWM
Hi
as posted by luciano the following code has solve my half of the problem which I faced in generating 50HZ out of Phase
signal on two pins on ATmega8L
' ======================================================
' Timer1 initialization
'
' Waveform Generation Mode:
' Phase Correct PWM mode / TOP= ICR1
'
' Timer clock source: I/O Clock / 8
'
' OC1A: Clear OC1A on Compare Match when up-counting.
' Set OC1A on Compare Match when down-counting.
' OC1B: Set OC1B on Compare Match when up-counting.
' Clear OC1B on Compare Match when down-counting.
'
TCCR1A = &B10110010 'See datasheet for details.
TCCR1B = &B00010010 'See datasheet for details.
' ======================================================
' This will control the PWM period.
' The Timer1 clock is 80000000 Hz / 8 = 10000000 Hz
' The Timer1 resolution is: 1/8000000 = 0.000001 (1 µs)
' The HEX value 270F = DEC 9999
' 10000 * 2 = 20000 ----- 1 µs * 20000 = 20000µs (20ms).
' The PWM period is: 20ms (50 Hz)
'
'
ICR1H=&H27 'Do not modify, used for 50 Hz PWM period.
ICR1L=&H0F 'Do not modify, used for 50 Hz PWM period.
'
' ======================================================
' Set OC1A and OC1B as outputs.
' ======================================================
But the least thing not last I want is to make the PWM dependent on
ADC reading.
Compare1a = 3500 'Set PWM duty cycle
Compare1b = 6500 'Set PWM duty cycle
what 3500 and 6500 means and how to relate it to the ADC so that
the decaying potential of battery be relate to it to change the
pwm accordingly (in an inverter).
regards
The period for Timer1 in your code is 9999 decimal (270F hexadecimal), to give 50Hz PWM frequency. This is the value that the timer's counter resets to 0 at, known as TOP or ICR1 register.
The Compare1a and Compare1b variables set the duty cycle of the PWM outputs, ie the length of time they stay ON in each PWM cycle. When the timer counter reaches this value, the PWM output pin changes state (eg ON to OFF). By adjusting these values, you change the ratio of ON to OFF. The minimum value is zero (always off, duty cycle=0%) and the max value is the period value (9999 in your case, for 100% duty cycle).
You can calculate the value for each PWM duty as a percentage of the max value (TOP), eg:
For 50%: Compare1a = 0.5 * 9999
For 80%: Compare1a = 0.8 * 9999 : Compare1b = (1-0.8 ) * 9999
Note that one of your PWM outputs is inverted, so the duty cycle is reversed, eg 80% duty cycle means the output is OFF for 80% of the cycle. (I think I've got this right...)
You will need to transform the ADC output values (0-1023) to a PWM duty cycle somehow... that depends on how the ADC values relate to required duty cycle. You will need to do some maths or have a lookup table (see help/samples on DATA and LOOKUP).
As this is a control loop system, you'll probably need to experiment a bit to get the relationship between ADC value and inverter output voltage right. It might be better to use the ADC to measure the output voltage (either DC or RMS AC depending on the type of inverter) and use that to control the PWM. That way you get a stable output voltage even if the load varies or the battery voltage drops. You can use another ADC input to measure battery voltage and shut off the inverter if the battery gets too low. _________________ If all else fails, read the manual. Even better: read the manual before something fails. If you can't find it in the manual, search the forum.
BascomAVR 2.0.8.5
Hi
Thanks for the valuable suggestion But right now I am n so confusing state of mind to do the maths. so far i know that the pwm will be inversely proportional to the ADC value.
I think I have to create the pwm variable then equate it to the pwm register with some maths as u suggested but there are some small hilltops to climb like disregarding the noise EMI etc while doing the conversion.
as for battery reading is concerned it should be pretty easy to do as it is a straight forward business.
the other hilltop is then to stop the pwm in case of mains presence but I have an idea to use an AND/NAND gate with three inputs to switchoff the outputs of the Ucontroller.
Any suggestion will be greatly appreciated.
regards
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum