|
|
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"
APP_PWM_INSTANCE(PWM1,1); // 创建一个使用定时器1产生PWM波的实例
#define BSP_PWM_0 12
#define BSP_PWM_1 13
static volatile bool ready_flag; // 使用一个标志位表示PWM状态
void pwm_ready_callback(uint32_t pwm_id) // PWM回调功能
{
ready_flag = true;
}
int main(void)
{
ret_code_t err_code;
/* 2-个频道的 PWM, 200Hz(5000us=5ms), 通过 开发板LED 管脚输出. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_PWM_0, BSP_PWM_1);
/* 切换两个频道的极性 */
pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;
/* 切换两个频道的极性 */
pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
/* 初始化和使能PWM. */
err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);//使能PWM
app_pwm_channel_duty_set(&PWM1, 0, 2500);
app_pwm_channel_duty_set(&PWM1, 1, 2500);
uint32_t value;
while(true)
{
// for (uint8_t i = 0; i < 40; ++i)
// {
// value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);
//
// ready_flag = false;
// /* 设置占空比 - 不停设置直到PWM准备好. */
// while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);
// /* 等待回调 */
// while(!ready_flag);
// APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
// nrf_delay_ms(25);
// }
}
}
|
|