Introduction.In two previous articles we generated a clock signal of the frequency necessary to us by means of PWM, receiving on a LED equal intervals of a luminescence and its absence. This task takes place to be in practice (in one of the subsequent articles we will precisely face it). But most often PWM is used for other designated purpose. One of the most widespread — management of brightness of LEDs or the rotational speed of motors. Also by means of PWM it is possible to generate a sound (about what there will be following article). And in this article I would like to tell how on our controller it is possible to implement management of LED brightness.
Let's begin: change of settings of the main timer.We will take the project from this article as a basis. On the basis of the initTimerPWMled function we will create the initTimerPWMconstPeriod function. Not PWM_speed (PWM-and frequency), but timeEnabledState — time of action of an impulse will be function parameter any more. This short article perfectly tells about the theory of generation of tension on an output by PWM method. For a start we will decide on parameters:
- Let duration of the whole period of function will be 0xFFFF timer tics (it is possible to select any value, I selected this value for convenience and not to change a code for the following article about generation of a sound of PWM ohm)
- Then the function parameter timeEnabledState will show how many clock periods from 0xFFFF at an output there is "1". All rest of the time on 0 channel. For example, at timeEnabledState = 0x4000, the signal will have the following appearance.
At 0x8000 will be almost equal.
And at 0xF000 the signal "1" will be almost always.
Further it is necessary to change a method of forming of a signal. As we remember from the previous article, the group of bits of OCCM of the register CH1_CNTRL is responsible for it. Then we selected the mode of inverting of a signal of REF at CNT == CCR1. As CCR was = 0 by default, regulating ARR of the main counter — we gained the same effect. Now we in ARR write amount of clock periods of the entire period (0xFFFF), and we will write amount of clock periods of unit on an output (time of action of an impulse) to CRR. And we will select the mode 6 (0b110:1 if DIR = 0 (the account up), CNT
MDR_TIMER1->PSG = 0; // Делитель тактовой частоты отсутствует.
MDR_TIMER1->ARR = 0xFFFF; // Период постоянный. И дает возможнсоть выбирать период заполнения от 0 до 0xFFFF. MDR_TIMER1->CCR1 = timeEnabledState; // Канал будет держать 1 до этого значения и 0 - после. MDR_TIMER1->CH1_CNTRL = 6<<TIMER_CH_CNTRL_OCCM_Pos; // REF = 1, когда CNT < CCR1, 0 - CNT >= CCR1;
This article is a translation of the original post at habrahabr.ru/post/270887/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: firstname.lastname@example.org.
We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.