|
| |||||||
|
Опубликовано 2010-04-25 15:30:54 автором Ruslan Pulse Width Modulation (PWM, PWM). Lesson 8Probably , 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 ). 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 :
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.
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
here:
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 .
We will set the timer mode Fast PWM and will gradually change the frequency from 0 - 4 khz.
# 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) Добавить комментарийДля отправки комментария вы должны авторизоваться. |
|||||||