Опубликовано 2010-04-25 15:30:54 автором Ruslan

Pulse Width Modulation (PWM, PWM). Lesson 8


Probably , you do not just wondered how you can adjust the power of the consumer , for example to control the brightness of LED or adjust the engine speed . The easiest way - series with the load , such as LEDs, including the resistor , but he will bask and take precious energy and the high-power LEDs , the stronger will be our warm up resistor , this option is not for us. And what if the LED very quickly turn on and off , thus changing the length of inclusions at a constant rate ? For example, include LED 0.2 milliseconds every millisecond , the LED lights up , but not at full brightness . Similarly, with the engine - engine include 30 seconds every minute - then the engine spin up , but not at full speed - a relatively large weight of the rotor smooth out jerks from engaging the engine, and the resistance of friction it will slow down . Thus, the motor is spinning at half its capacity.

Pulse Width Modulation

- an approximation of the desired signal ( multi-level or continuous ) to the actual binary signals ( with two levels - on / off) , so that , on average, for a certain period of time , their values are equal.

main reason is the complexity of implementing the PWM ensure arbitrary voltage. There are some basic DC voltage (supply , battery , etc.). And based on it to get lower and have them feed the electric motors or other equipment . The easiest option - a voltage divider , but it has a lower efficiency , increased heat generation and energy consumption. Another option - the transistor circuit . It allows you to adjust the voltage without the use of mechanics. The main advantage of SHIP - its high efficiency power amplifiers , which is achieved by using only a key mode . This significantly reduces the power allocation for power converter (SP ). Dіagrami skvazhnostі

PWM a pulse signal of constant frequency and variable duty cycle , that is, the relationship to the pulse repetition period of its duration. With job duty cycle ( pulse width ) can change the average voltage at the output of the PWM . In digital technology , the outputs of which can take only one of two values , an approximation of the desired average output using PWM is completely natural . Let's try in practice to change the brightness of the LED. The scheme is very simple as in the first lesson :

skhema PWM dimmer svetodioda

The most important thing is the firmware :

void main (void) 
{
PORTB = 0x00; // Expose all outputs on port B 0 
DDRB = 0xFF; // Make port B as output, so we can change the log level on the legs ( or set to 0 or 1 ) 
while ( 1) // We organize an infinite loop 
      {
      PORTB.1 = 1 ; // Turn on LED 1 output port B 
      delay_us ( 5); // allow the LED will shine 5 microseconds 
      PORTB.1 = 0 ; // Turn off the LED on one output port B 
      delay_us ( 5); // Make the delay of 5 microseconds 
      }; 
} 

Compile and throws microns , the LED should light at half brightness. But you probably already noticed that our program only makes that sets the log levels on output u. To release the main loop of this routine work , we use a timer.

Aparatnye PWM
The timer can generate PWM in several modes :

  • Phase Correct PWM ( symmetric PWM )

    rezhim Phase Correct PWM
    In this mode, the timer counts from zero to a certain value depending on the mode , and then counts down to zero.

    Conclusion OCxx the first match with compare register is reset when the second set .

  • Fast PWM ( PWM fast )

    rezhym Phase Correct PWM In this mode, the timer operates as follows:

    • TCNTX variable increases from 0 to top , overflow TCNTX it is reset to 0 and the count starts again.
    • for each increase TCNTX checks whether it is insensitive comparison OCRXA / OCRXB, if equal , then the corresponding output OShh sbrasyvatsya to zero. When you reset the counter , this conclusion is set to 1

  • STS (reset at coincidence ) rezhim Phase Correct PWM Timer ticks from 0 to compare register for matching comparison register is reset to 0 . This mode is well suited for frequency adjustment , or when you want to count periods.

So, for our needs (adjusting brightness LED ) suitable mode Fast PWM . Run CodeWizard AVR, go to the tab timers-> timer1, exhibiting all , as shown in the screenshot

rezhim Fast PWM timer1

here:

  • Clock Source - choice of clock source timer , select System Clock - timer taktiruetsya frequency at which the microcontroller running
  • Clock Value - frequency selection of the timer , here we set 1000000 hz, it means that the variable will TCNT1 overflow with frequency 1000000/255 = 3921 hz, the PWM frequency is 3921 khz
  • out A ( terminal A ) defines the states legs OC1A at the coincidence counter register and compare register
  • Comp. A the value of compare register

We click File-> Generate and save.

# include <mega8.h> 

void main (void) 
{
// Port B initialization 
PORTB = 0x00; 
DDRB = 0x02; 

// Timer / Counter 1 initialization 
TCCR1A = 0x81; 
TCCR1B = 0x09; 
TCNT1H = 0x00; 
TCNT1L = 0x00; 
ICR1H = 0x00; 
ICR1L = 0x00; 
OCR1AH = 0x00; 
OCR1AL = 0x64; // number 100 in hexadecimal form
OCR1BH = 0x00; 
OCR1BL = 0x00; 

while (1) 
      {
      }; 
} 

Compile and Throwing in microns , now our LED should glow about half brightness .

Now let's try something to squeak by a timer :
Wiring speaker to u :

podklyucheniya speaker to mk

We will set the timer mode Fast PWM and will gradually change the frequency from 0 - 4 khz.
Code of such a program is as follows:

  # include <mega8.h> 
  # include <delay.h>    
void main (void) 
{
// Port B initialization 
PORTB = 0x00; 
DDRB = 0x02; 

// Timer / Counter 1 initialization 
TCCR1A = 0x40; 
TCCR1B = 0x09; 
TCNT1H = 0x00; 
TCNT1L = 0x00; 
ICR1H = 0x00; 
ICR1L = 0x00; 
OCR1AH = 0x00; 
OCR1AL = 0x64; 
OCR1BH = 0x00; 
OCR1BL = 0x00; 

while (1) 
      {
        OCR1AL + +; // Increment the compare register to 1 to change the frequency 
        delay_ms ( 100 ); 
      }; 
} 

Комментарии - (0)

Добавить комментарий

Для отправки комментария вы должны авторизоваться.