PWM control led gradient

Source: Internet
Author: User

PWM: pulse width modulation. It is a very effective technology that uses the digital output of a microprocessor to control analog circuits.

PWM is a digital coding method for analog signal levels. Through the use of high-resolution counters, the duty cycle of the square wave is modulated to encode the level of a specific analog signal.

In addition to tim6 and 7, the stm32 Timer. Other timers can be used to generate PWM output. Among them, the advanced timer tim1 and tim8 can generate up to 7 PWM outputs at the same time. The general timer can also generate up to four PWM outputs at the same time, so that stm32 can generate up to 30 PWM outputs at the same time! (Involved in timer re-image during programming)

The PWM of stm32 is the signal that the timx_arr Register determines the frequency (cycle) and the timx_ccrx Register determines the duty cycle.

PWM mode

The pulse width modulation mode generates a signal that is determined by the timerearr register for frequency and determined by the timereccrx register for duty cycle. Write "110" (PWM mode 1) or "111" (PWM mode 2) in the ocxm bit in the timereccmrx register, and set each channel to work in the PWM mode independently, each OCX outputs one PWM path. You must set the ocxpe bit of the timereccmrx register to enable the corresponding preload register, finally, you must set the Arpe bit of the timjavascr1 register to enable automatic re-loading of the pre-load register (in the up count or center symmetric mode ).

PWM edge alignment Mode

Configure up counting when the Dir bit in the timeconcr1 register is low. In PWM mode 1, when tim%cnt <tim%ccrx, the PWM reference signal, ocxref is high, otherwise it is low. If the comparison value in timereccrx is greater than the automatic reload value (timerearr), ocxref is maintained as "1 ". If the comparison value is 0, the ocxref value is "0 ". Figure 3-1 shows an instance of the PWM waveform with edge alignment when timerearr = 8.

Configuration of downward counting when the Dir bit of the timjavascr1 register is high, execute downward counting. In PWM mode 1, when tim%cnt> tim%ccrx, the reference signal ocxref is low; otherwise, the reference signal is high. If the comparison value in timereccrx is greater than the automatic reload value in timerearr, ocxref is maintained as "1 ". In this mode, a 0% PWM waveform cannot be generated.

PWM central alignment Mode

The central alignment mode when the CMS bit in the timeconcr1 register is not 00 (all other configurations have the same effect on the ocxref/OCX signal ). Depending on the settings of different CMS bits, the comparison flag may be set to 1 when the counter is counting up, 1 when the counter is counting down, or 1 when the counter is counting up or down. The Count direction bit (DIR) in the timeconcr1 register is updated by the hardware. Do not use the software to modify it. Figure 3-2 provides examples of PWM waveforms with central alignment-timerearr = 8; · PWM mode 1; · CMS = 01 In the timerecr1 register, in central alignment Mode 1, the flag is set when the counter is counting down.

Test Case: (PWM controls the gradient of LED brightness)

1 # include "pbdata. H "2 # include" LED. H "3 # include" Misc. H "4 # include" stm32f10x_tim.h "5 6 void rcc_configration (void) 7 {8 systeminit (); 9 rcc_apb2periphclockcmd (enabled, enable); 10 bytes (enabled, enable ); // timer clock enabling 11 rcc_apb2periphclockcmd (enabled, enable); // port multiplexing enabling, because pwm12} 13 14 void gpio_configration (void) 15 {16 gpio_led_configration (); 17} 18 19 void tim_configuration (void) 20 {21 tim_timebaseinittypedef tim_timebasestructure; 22 tim_ocinittypedef tim_ocinitstructure; 23 24 gpio_pinremapconfig (enabled, enable); // timer pin ing, partial re-imaging to pb5, pb5 is exactly the Pin connected to the LED (search for "re ") 25 26 tim_timebasestructure.tim_period = 2000; // set the initial value. The scheduled value is 1s27 tim_timebasestructure.tim_prescal; // set the pre-frequency to 28 tim_timebasestructure.tim_clockdivision = 0; // Time Division 29 tim_timebasestructure.tim_countermode = tim_countermode_up; // 30 tim_timebaseinit (tim3, & tim_timebasestructure); 31 32 // PWM initialization 33 leading = tim_ocmode_pwm1; // set to PWM mode 134 tim_ocinitstructure.tim_outputstate = tim_outputstate_enable; // output enable ????? 35 bytes = tim_ocpolarity_low; // set the output polarity to 36 37 tim_oc2init (tim3, & tim_ocinitstructure); // select the timer channel 238 tim_oc2preloadconfig (tim3, tim_ocpreload_enable ); // pre-load enabling 39 tim_cmd (tim3, enable); // timer peripherals open 40} 41 42 43 int main (void) 44 {45 u8 led_fx = 1; 46 2010led_dt = 0; 47 rcc_configration (); 48 gpio_configration (); 49 tim_configuration (); 50 while (1) 51 {52 // delay_ms (10 ); 53 If (led_fx = 1) 54 {55 led_dt ++; 56} 57 else58 {59 led_dt --; 60} 61 If (led_dt> 1000) led_fx = 0; 62 if (led_dt = 0) led_fx = 1; 63 64 tim_setcompare2 (tim3, led_dt); 65} 66}

 

 

01:17:28

 

PWM control led gradient

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.