nrf51822的pwm功能

添加pwm的功能,编译报错误

..\..\..\main.c(64): error:  #20: identifier "TIMER0_ENABLED" is undefined
PP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.

 

官方的SDK :nRF5_SDK_12.3.0_d7731ad\examples\peripheral\pwm_library\pca10028\

官方默认的nrf51422,根据自己的芯片型号改选项卡的设置:

nrf51822的pwm功能_第1张图片nrf51822的pwm功能_第2张图片

nrf51822的pwm功能_第3张图片

留意工程栏是否以下的文件:

nrf51822的pwm功能_第4张图片

main.c中功能实现代码:

#include "app_pwm.h"//添加头文件

APP_PWM_INSTANCE(PWM1,1);  Create the instance "PWM1" using TIMER1

static volatile bool ready_flag;            // A flag indicating PWM status.

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}

int main(void)
{

  app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(50L, 6);//配置50us周期,20KHz(1/50MHz)

    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

    /* Initialize and enable 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, 80);//设置pwm的占空比

}

编译的时候,错误提示没有定义定时器0,其原因是配置文件中定时器使能的宏没有打开,需要用到的哪个定时器也要打开。

nrf51822的pwm功能_第5张图片nrf51822的pwm功能_第6张图片

编译通过:

nrf51822的pwm功能_第7张图片

 0 Error(s), 0 Warning(s).完美!!!!!

你可能感兴趣的:(NRF51822)