概述
本示例,只要介绍,如何使用PWM,又离成功进了一步 ^_^。
一、环境
1、硬件 (RTL8762DK + 128M Bits Falsh)
2、软件(keil IDE)
二、PWM的使用
1、创建examples_ble_pwm工程,基于《examples_ble_wdg工程》来创建,创建步骤请参考第二篇文章。
2、新建pwm文件夹,分别存放pwm.c与pwm.h这两个文件
1)、分别添加pwm.c与pwm.h到工程中
3、pwm.h
#ifndef _PWM_H_
#define _PWM_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "board.h"
#include "rtl876x_tim.h"
#include "rtl876x_rcc.h"
/* Defines --------------------------------------------------------------*/
/** Define pwm output pin.
* P0_1 is connected to LED0 on the EVB.
*/
#define PWM_OUT_PIN P0_1
/** Define pwm complementary output pin.
* P0_2 is connected to LED1 on the EVB.
*/
#define PWM_OUT_P_PIN P0_2
#define PWM_OUT_N_PIN P2_2
/** Define TIM num and pinmux of PWM output.
* Timer2 to Timer7 all have PWM output function.
* But only Timer2 and Timer3 support PWM complementary output function, other timers do not support it.
* When the complementary output function is turned on, when the dead time is not set,
* the complementary output PWM2_P of timer2 is in phase with PWM2, and PWM2_N and PWM2 are inverted.
* Timer3 is the same as timer2.
*/
#define PWM_TIMER_NUM TIM2
#define PWM_OUT_PIN_PINMUX timer_pwm2
#define PWM_OUT_P_PIN_PINMUX PWM2_P
#define PWM_OUT_N_PIN_PINMUX PWM2_N
/* Config PWM_PERIOD and PWM_DUTY_CYCLE */
#define PWM_PERIOD 1000000 //uint:us
#define PWM_DUTY_CYCLE 50 //uint:percent
/* PWM_HIGH_COUNT = */
#define PWM_HIGH_COUNT (((PWM_PERIOD)*((PWM_DUTY_CYCLE*40)/100))-1) //PWM CLOCK = 40000000
#define PWM_LOW_COUNT (((PWM_PERIOD)*(((100-PWM_DUTY_CYCLE)*40)/100))-1)
/** Config PWM_DEAD_ZONE_TIME. Max 8ms.
* When dead time is set, PWM2_P and PWM2_N will
* delay one dead time longer than PWM2 at high output level.
* When the dead_zone time is longer than the high-level time of PWM,
* PWM_P will always output low-level.
* When the dead_zone time is longer than the low-level time of PWM,
* PWM_N will always output low-level.
*/
#define PWM_DEAD_ZONE_TIME 1 //uint:ms
#define PWM_DEAD_ZONE_SIZE ((PWM_DEAD_ZONE_TIME)*32-1)
void pwm_demo(void);
#ifdef __cplusplus
}
#endif
#endif
4、pwm.c
#include "pwm.h"
/**
* @brief Initialization of pinmux settings and pad settings.
* @param No parameter.
* @return void
*/
void board_pwm_init(void)
{
Pad_Config(PWM_OUT_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH);
Pad_Config(PWM_OUT_P_PIN, PAD_P