I Want to know that how to generate the 50 -60 hz frequency by using pwm . If any body have any idea about then please tell me .
I Want to know that how to generate the 50 -60 hz frequency by using pwm . If any body have any idea about then please tell me .
Hi Rahil,
PWM can be converted to voltage using a lowpass filter - a simple R-C can be enough. To generate a sinusoidal voltage at such a filter the pulse width must be modulated sinusoidally.
pulse width = sine (time) * MaxPulseWidth
Time can be a simple count that increments with time. The increments must occur at a rate that forces the sine of the count to roll over to the same value 120 times per second. For example the sine of 360 degrees is the same as 0 degrees or 180 degrees or 720 degrees. So you could make the counter increment at a rate of 360 steps in 1/60th of a second. You can also reset the count when it reaches 360. The sine of the count will vary from 0 to 1, so it must be multiplied by the maximum pulse width before being sent to your PWM generator.
Note that the frequency of the PWM signal should be much higher than the 60 Hz you are trying to create and the lowpass filter should have a cutoff frequency above 60 Hz.
Doug
hi sir,
I Want to know that how to generate the 60 hz frequency by using pwm using ardunio uno.please help me to generate it.and can you please send the code also
Can't just give you the full code but I will give you hints.
Configure TimerCounter1 (TCCR1A and TCCR1B) to give a PWM of >6khz.
Make a array of sine wave values with the resolution you want (100 in this case)
// In main.c
uint8_t SinTable[100];
for (i=0;i<100;i++)
{
SinTable[i]= sin(6.28/100)* 127+127 ; // 127 is the middle value, 127 is the amplitude, 6.28 for full "circle"
}
Feed them with a second timer to the pwm configured for 50hz * 100 values (5000hz) interrupt or 60hz*100 for 60hz.
Like so.
ISR(timer0_ovf_vect)
{
static uint8_t actualPos=0;
OCR1A=SinTable[actualPos++];
if (actualPos==100) actualPos=0;
}
Want to change the frequency from 50 to 60 dinamically? Timer0 should be configured as CTC mode, change the ICR0 value to set another TOP or OCR0 if you configured CTC with OCR0 as top.
And calculate it.
ICR0 = (F_CPU / ( (desiredFrequency*100) * configured_prescaler)) / 2 ; // IT IS DIVIDED BY TWO BECAUSE IT IS IN CTC MODE.
Hope it helps probably not,
Have a nice day!
Can't just give you the full code but I will give you hints.
Configure TimerCounter1 (TCCR1A and TCCR1B) to give a PWM of >6khz.
Make a array of sine wave values with the resolution you want (100 in this case)
// In main.c
uint8_t SinTable[100];
for (i=0;i<100;i++)
{
SinTable[i]= sin(6.28/100)* 127+127 ; // 127 is the middle value, 127 is the amplitude, 6.28 for full "circle"
}
Feed them with a second timer to the pwm configured for 50hz * 100 values (5000hz) interrupt or 60hz*100 for 60hz.
Like so.
ISR(timer0_ovf_vect)
{
static uint8_t actualPos=0;
OCR1A=SinTable[actualPos++];
if (actualPos==100) actualPos=0;
}
Want to change the frequency from 50 to 60 dinamically? Timer0 should be configured as CTC mode, change the ICR0 value to set another TOP or OCR0 if you configured CTC with OCR0 as top.
And calculate it.
ICR0 = (F_CPU / ( (desiredFrequency*100) * configured_prescaler)) / 2 ; // IT IS DIVIDED BY TWO BECAUSE IT IS IN CTC MODE.
Hope it helps probably not,
Have a nice day!