Arduino software simulate PWM and improve PWM efficiency

By Kumantech | 22 June 2017 | 3 Comments
This article has been writing for long time, not good at drawing, just for your reference, if you have question, please leave message.
The source code was lost by the forum due to compose type, you can copy the code and paste to Arduino compiler, then press ctrl+T to reformat to be ok.
PWM means Pulse Wavelength Modulation, through adjusting output signal duty cycle to achieve changing output average voltage. You should know Arduino PWM well, in the Arduino Duemilanove 200, there are 6 PWM pin switch 8bit precision, which are pin 3, 5, 6, 9, 10, 11.
We can use analog Write () to control PWM pin to output PWM modulating wave which output frequency is about 500Hz. Resolution 8 bits that is 2 of the 8th power is equal to 256 levels of precision. But sometimes we feel 6 PWM are not enough, e.g. we make a 10 way Street lamp controller that 10 pins are needed. Arduino Duemilanove 2009 has 13 digital output pins, if they all can be PWM, condition is met.

Arduino PWM pressure regulating principle: PWM has several ways. However, because Arduinois limited by power supply, it normally use unipolarity PWM with stable periodand unfixed duty ratio.
Through adjusting timeratio of high/level one-period during one-period to gain different averagepower to a electric appliance.
Code:
prelang="arduino" line="1" file="PWM1.ino"]
const int PWMPin =13;
int bright = 0;
void setup()
{
   pinMode(PWMPin, OUTPUT);
}
void loop()
{
   if((bright++) == 255) bright = 0;
    for(inti = 0; i < 255; i++)
    {
       if(i < bright)
       {
           digitalWrite(PWMPin, HIGH);
           delayMicroseconds(30);
       }
       else
       {
           digitalWrite(PWMPin, LOW);
           delayMicroseconds(30);
       }
    }
}

This an example that Arduino D13 pin is controlled by PWM software. Which code can be tested by an Arduino board.
Program parsing: we can read from circulation, a full PWM period needs 255 times of circulation.
Suppose bright =100, in the 0 ~ 100 cycles, i is equal to 1 to 99 are less than bright, then the output PWM Pin high; And then 100 to 255 cycles inside, it is equal to 100 ~255 bigger than bright, so the output PWM Pin low. Regardless of the output high and low levels are maintained for 30us.
Then if bright =100, there are 100 cycles having high level, 155 cycles are having low.
If you ignore the instruction execution time, then the PWM waveform duty cycleis 100/255, if you adjust the bright value, you can change the brightness of the LED connected to D13.
After setting each for loop, add bright one, and return 0 when bright is added to 255. So, we see the final effect is the LED slowly brightened to the top and then suddenly dark back to re-brighten.
This is the most basic PWM method, it should be more thought you want
The we introduce a simple one with completely different style. But to drive a LED, it has the same effect above.
Code:
[prelang="arduino" line="1" file="PWM2.ino"]
const int PWMPin =13;
int bright = 0;
void setup()
{
   pinMode(PWMPin, OUTPUT);
}
void loop()
{
   digitalWrite(PWMPin, HIGH);
   delayMicroseconds(bright * 30);
   digitalWrite(PWMPin, LOW);
   delayMicroseconds((255 - bright) * 30);
   if((bright++) == 255) bright = 0;

It can be seenthat one FOR circle is missing in this code, it firstly output a high levelthen keep (bright*30)us, after that output a low level and keep ((255-bright)*30)us.Like this a full PWM period is made, resolution is 255.

 
MascarinenE
February 19,2018 16:23
Вы в ищите фирму, которая возведёт вам автомойку самообслуживания под ключ? Хотите разработать моющую станцию, которую отважно можно назвать молодой быстроразвивающейся компанией? Тогда вы на нужном пути! Сегодня время современных нанотехнологий. По
abadLoof
November 11,2017 12:35
Конечно. Это было и со мной. Можем пообщаться на эту тему. Здесь или в PM. --- ммм Точно. fifa 15 русская версия скачать торрент, fifa 15 украинская лига скачать торрент и <a href=http://15fifa.ru/skachat-fifa-15/>скачать fifa 15 с яндекс диска</a>
Wheerfigh
September 29,2017 13:47
buy dissertations online custom thesis writing service best dissertation topics thesis buy how to write a phd dissertation <a href="http://www.integraltechservice.by/index.php?option=com_k2&view=itemlist&task=user&id=740431">dissertation consultin
Leave a Reply
Your email address will not be published.Required fields are marked. *
Name *
Email *
Content
Popular blog
Categories