示例#1
0
文件: main.c 项目: BlueSkyGjj/nRF52
int main(void)
{
    ret_code_t err_code;
    
    /* 2-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_LED_0, BSP_LED_1);
    
    /* 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);
    
    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;
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);
            
            /* ... or wait for callback. */
            while(!ready_flag);
            APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
            nrf_delay_ms(25);
        }
    }
    
}
示例#2
0
文件: pwmout_api.c 项目: pan-/mbed
static void pwm_reinit(pwm_t * pwm)
{
    app_pwm_uninit(pwm->instance);
    app_pwm_config_t pwm_cfg = APP_PWM_DEFAULT_CONFIG_2CH(pwm->period_us,
                                                          pwm->pins[0],
                                                          pwm->pins[1]);
    app_pwm_init(pwm->instance, &pwm_cfg, NULL);
    app_pwm_enable(pwm->instance);
    
    for (uint8_t channel = 0; channel < PWM_CHANNELS_PER_INSTANCE; ++channel) {
        if ((pwm->channels_allocated & (1 << channel)) && (pwm->pins[channel] != APP_PWM_NOPIN)) {
            app_pwm_channel_duty_ticks_set(pwm->instance, channel, pwm->duty_ticks[channel]);
        }
    }
}
示例#3
0
// Application main function.
int main(void)
{
    uint32_t err_code;

    // set up timers
    APP_TIMER_INIT(0, 4, 4, false);

    // initlialize BLE
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();

    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);

    // init GPIOTE
    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    // init PPI
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    // intialize UART
    uart_init();

    // prints to serial port
    printf("starting...\n");

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

    // RGB LED pins
    // (Common cathode)
    uint32_t pinR = 1;
    uint32_t pinG = 2;
    uint32_t pinB = 3;
   
    // 2-channel PWM, 200Hz
    app_pwm_config_t pwm1_cfg = 
      APP_PWM_DEFAULT_CONFIG_2CH(5000L, pinR, pinG);

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);

    // Create the instance "PWM2" using TIMER2.
    APP_PWM_INSTANCE(PWM2,2);                   
 
    // 1-channel PWM, 200Hz
    app_pwm_config_t pwm2_cfg = 
      APP_PWM_DEFAULT_CONFIG_1CH(5000L, pinB);

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM2);


    // Enter main loop.
    int dir = 1;
    int val = 0;

    // main loop:
    bool pwmEnabled = true;

    while(1) {

      // only if not paused 
      if (!pausePWM) {
        
        // enable disable as needed
        if(!enablePWM) {
          if(pwmEnabled) {
            app_pwm_disable(&PWM1);
            app_pwm_disable(&PWM2);

            // This is required becauase app_pwm_disable()
            // has a bug. 
            // See: 
            // https://devzone.nordicsemi.com/question/41179/how-to-stop-pwm-and-set-pin-to-clear/
            nrf_drv_gpiote_out_task_disable(pinR);
            nrf_gpio_cfg_output(pinR);
            nrf_gpio_pin_clear(pinR);
            nrf_drv_gpiote_out_task_disable(pinG);
            nrf_gpio_cfg_output(pinG);
            nrf_gpio_pin_clear(pinG);
            nrf_drv_gpiote_out_task_disable(pinB);
            nrf_gpio_cfg_output(pinB);
            nrf_gpio_pin_clear(pinB);

            pwmEnabled = false;
          }
        }
        else {
          if(!pwmEnabled) {

            // enable PWM 

            nrf_drv_gpiote_out_task_enable(pinR);
            nrf_drv_gpiote_out_task_enable(pinG);
            nrf_drv_gpiote_out_task_enable(pinB);

            app_pwm_enable(&PWM1);
            app_pwm_enable(&PWM2);
            pwmEnabled = true;
          }
        }

        if(pwmEnabled) {
          // Set the duty cycle - keep trying until PWM is ready
          while (app_pwm_channel_duty_set(&PWM1, 0, val) == NRF_ERROR_BUSY);
          while (app_pwm_channel_duty_set(&PWM1, 1, val) == NRF_ERROR_BUSY);
          while (app_pwm_channel_duty_set(&PWM2, 0, val) == NRF_ERROR_BUSY);
        }
        
        // change direction at edges
        if(val > 99) {
          dir = -1;
        }
        else if (val < 1){
          dir = 1;
        }
        // increment/decrement
        val += dir*5;
      }      
      // delay
      nrf_delay_ms(delay);
    }
}