First, PWM Overview:
PWM (pulse width modulation): Pulsed-wide modulation technique that is equivalent to obtaining the desired waveform by modulating the width of a series of pulses.
Several basic concepts of PWM:
- 1) Duty cycle: The duty ratio is defined as the percentage of the signal that is high at the time of the period, and the duty cycle of the square wave is 50%.
- 2) Modulation frequency: The reciprocal of the cycle.
- 3) Pulse width: The time at which the signal is high.
Second, the nRF51822 PWM generation:
NRF51822 by the timer, PPI and gpiote to achieve the function of PWM, by the timer to generate an event, the PPI capture this event and convert the event to a task to pass to Gpiote, Gpiote module According to the delivery of the mission to perform operations. The schematic diagram of the NRF51822 implementation of PWM is shown in the following:
As can be seen, the implementation of PWM function, the need to use 2 cc channels, by setting the comparison values of cc[0] and cc[1] channels, so that they produce comparison value matching events at different times, thereby triggering the Gpiote module control pin at set point in time output high and low level, so that Output signals of different pulse widths can be produced, i.e. PWM output signals.
Third, Code Analysis:
Main
1 intMainvoid)2 {3 ret_code_t Err_code;4 5 /*2-channel PWM, 200Hz, output on DK LED pins.*/6 app_pwm_config_t pwm1_cfg = app_pwm_default_config_2ch (5000L, Bsp_led_0, bsp_led_1);//PWM parameter Configuration7 8 /*Switch The polarity of the second channel.*/9pwm1_cfg.pin_polarity[1] =App_pwm_polarity_active_high;Ten One /*Initialize and enable PWM.*/ A Err_code = App_pwm_init (&pwm1, &pwm1_cfg, pwm_ready_callback);//PWM initialization (the core is in this, this time does not speak) - App_error_check (err_code); - app_pwm_enable (&PWM1);//Enable PWM the - uint32_t value; - while(true) - { + for(Uint8_t i =0; I < +; ++i) - { +Value = (I < -) ? I5) : ( --(I- -) *5); A atReady_flag =false; - /*Set the duty cycle-keep trying until PWM is ready ...*/ - while(App_pwm_channel_duty_set(&PWM1,0, value) = =nrf_error_busy);//Set the value of the PWM1 0 channel - - /*... or wait for callback.*/ - while(!ready_flag); in App_error_check (App_pwm_channel_duty_set (&PWM1, 1, value));//Set Channel 1 -Nrf_delay_ms ( -); to } + } - the}
Note: The PWM parameter configuration macro is defined as follows
1 /** @brief PWM Instance default configuration (2 channels).*/2 #defineApp_pwm_default_config_2ch (Period_in_us, Pin0, Pin1)3 { 4. Pins ={pin0, pin1},5. pin_polarity ={app_pwm_polarity_active_low, app_pwm_polarity_active_low},6. Num_of_channels =2, 7. Period_us =Period_in_us8}
Note: The PWM callback function is as follows
1 Static volatile bool Ready_flag; // A flag indicating PWM status. 2 3 void pwm_ready_callback (uint32_t pwm_id) // PWM callback function4 {5 true ; 6 }
@nRF51822 Basic Experiment Series:
[nRF51822] 7, basic experimental Code Analysis Daquan (top ten)
[nRF51822] 8, basic experimental code Analysis Daquan · Experimental 11-ppi
[nRF51822] 9, basic experimental code Analysis Daquan · Experimental 12-ADC
[nRF51822] 10, basic experimental code Analysis Daquan · Experimental 15-RTC
@beautifulzzzz -The Internet of Things & ubiquitous computing practitioners
e-mail: [Email protected]
I-blog:blog.beautifulzzzz.com
[nRF51822] 12, basic experimental code Analysis Daquan · Experimental 19-PWM