/** * @brief Function for application main entry. */ int main(void) { uint8_t new_duty_cycle; uint32_t err_code; lfclk_init(); // Start APP_TIMER to generate timeouts. APP_TIMER_INIT(APP_TIMER_PRESCALER, OP_QUEUES_SIZE, NULL); /*Initialize low power PWM for all 3 channels of RGB or 3 channels of leds on pca10028*/ pwm_init(); while (true) { /* Duty cycle can also be changed from main context. */ new_duty_cycle = low_power_pwm_2.period - low_power_pwm_2.duty_cycle; err_code = low_power_pwm_duty_set(&low_power_pwm_2, new_duty_cycle); APP_ERROR_CHECK(err_code); nrf_delay_ms(500); new_duty_cycle = low_power_pwm_2.period - low_power_pwm_2.duty_cycle; err_code = low_power_pwm_duty_set(&low_power_pwm_2, new_duty_cycle); APP_ERROR_CHECK(err_code); nrf_delay_ms(500); } }
static void pwm_init(void) { uint32_t err_code; low_power_pwm_config_t low_power_pwm_config; APP_TIMER_DEF(lpp_timer_0); low_power_pwm_config.active_high = false; low_power_pwm_config.period = 220; low_power_pwm_config.bit_mask = BSP_LED_0_MASK; low_power_pwm_config.p_timer_id = &lpp_timer_0; err_code = low_power_pwm_init((&low_power_pwm_0), &low_power_pwm_config, pwm_handler); APP_ERROR_CHECK(err_code); err_code = low_power_pwm_duty_set(&low_power_pwm_0, 20); APP_ERROR_CHECK(err_code); APP_TIMER_DEF(lpp_timer_1); low_power_pwm_config.active_high = false; low_power_pwm_config.period = 200; low_power_pwm_config.bit_mask = BSP_LED_1_MASK; low_power_pwm_config.p_timer_id = &lpp_timer_1; err_code = low_power_pwm_init((&low_power_pwm_1), &low_power_pwm_config, pwm_handler); APP_ERROR_CHECK(err_code); err_code = low_power_pwm_duty_set(&low_power_pwm_1, 150); APP_ERROR_CHECK(err_code); APP_TIMER_DEF(lpp_timer_2); low_power_pwm_config.active_high = false; low_power_pwm_config.period = 100; low_power_pwm_config.bit_mask = BSP_LED_2_MASK; low_power_pwm_config.p_timer_id = &lpp_timer_2; err_code = low_power_pwm_init((&low_power_pwm_2), &low_power_pwm_config, pwm_handler); APP_ERROR_CHECK(err_code); err_code = low_power_pwm_duty_set(&low_power_pwm_2, 20); APP_ERROR_CHECK(err_code); err_code = low_power_pwm_start((&low_power_pwm_0), low_power_pwm_0.bit_mask); APP_ERROR_CHECK(err_code); err_code = low_power_pwm_start((&low_power_pwm_1), low_power_pwm_1.bit_mask); APP_ERROR_CHECK(err_code); err_code = low_power_pwm_start((&low_power_pwm_2), low_power_pwm_2.bit_mask); APP_ERROR_CHECK(err_code); }
/** * @brief Function to be called in timer interrupt. * * @param[in] p_context General purpose pointer (unused). */ static void pwm_handler(void * p_context) { uint8_t new_duty_cycle; static uint16_t led_0, led_1; uint32_t err_code; UNUSED_PARAMETER(p_context); low_power_pwm_t * pwm_instance = (low_power_pwm_t*)p_context; if (pwm_instance->bit_mask == BSP_LED_0_MASK) { led_0++; if (led_0 > TICKS_BEFORE_CHANGE_0) { new_duty_cycle = pwm_instance->period - pwm_instance->duty_cycle; err_code = low_power_pwm_duty_set(pwm_instance, new_duty_cycle); led_0 = 0; APP_ERROR_CHECK(err_code); } } else if (pwm_instance->bit_mask == BSP_LED_1_MASK) { led_1++; if (led_1 > TICKS_BEFORE_CHANGE_1) { new_duty_cycle = pwm_instance->period - pwm_instance->duty_cycle; err_code = low_power_pwm_duty_set(pwm_instance, new_duty_cycle); led_1 = 0; APP_ERROR_CHECK(err_code); } } else { /*empty else*/ } }
ret_code_t led_softblink_init(led_sb_init_params_t const * p_init_params) { ret_code_t err_code; ASSERT(m_led_sb.led_sb_state == NRF_DRV_STATE_UNINITIALIZED); ASSERT(p_init_params); if ( (p_init_params->duty_cycle_max == 0) || (p_init_params->duty_cycle_max <= p_init_params->duty_cycle_min) || (p_init_params->duty_cycle_step == 0) || (p_init_params->leds_pin_bm == 0)) { return NRF_ERROR_INVALID_PARAM; } memset(&m_led_sb, 0, sizeof(led_sb_context_t)); memcpy(&m_led_sb.params, p_init_params, sizeof(led_sb_init_params_t)); m_led_sb.is_counting_up = true; m_led_sb.duty_cycle = p_init_params->duty_cycle_min + p_init_params->duty_cycle_step; m_led_sb.leds_is_on = false; m_led_sb.bit_mask = p_init_params->leds_pin_bm; APP_TIMER_DEF(led_softblink_timer); m_led_sb.pwm_config.active_high = m_led_sb.params.active_high; m_led_sb.pwm_config.bit_mask = p_init_params->leds_pin_bm; m_led_sb.pwm_config.p_port = p_init_params->p_leds_port; m_led_sb.pwm_config.period = PWM_PERIOD; m_led_sb.pwm_config.p_timer_id = &led_softblink_timer; err_code = low_power_pwm_init( &m_led_sb.pwm_instance, &m_led_sb.pwm_config, led_softblink_on_timeout); if (err_code == NRF_SUCCESS) { m_led_sb.led_sb_state = NRF_DRV_STATE_INITIALIZED; } else { return err_code; } err_code = low_power_pwm_duty_set( &m_led_sb.pwm_instance, p_init_params->duty_cycle_min + p_init_params->duty_cycle_step); return err_code; }
/**@brief Timer event handler for softblink. * * @param[in] p_context General purpose pointer. Will be passed to the time-out handler * when the timer expires. * */ static void led_softblink_on_timeout(void * p_context) { static int32_t pause_ticks; ASSERT(m_led_sb.led_sb_state != NRF_DRV_STATE_UNINITIALIZED); ret_code_t err_code; if (pause_ticks <= 0) { if (m_led_sb.is_counting_up) { if (m_led_sb.duty_cycle >= (m_led_sb.params.duty_cycle_max - m_led_sb.params.duty_cycle_step)) { // Max PWM duty cycle is reached, start decrementing. m_led_sb.is_counting_up = false; m_led_sb.duty_cycle = m_led_sb.params.duty_cycle_max; pause_ticks = m_led_sb.params.on_time_ticks ? m_led_sb.params.on_time_ticks + APP_TIMER_MIN_TIMEOUT_TICKS : 0; } else { m_led_sb.duty_cycle += m_led_sb.params.duty_cycle_step; } } else { if (m_led_sb.duty_cycle <= (m_led_sb.params.duty_cycle_min + m_led_sb.params.duty_cycle_step)) { // Min PWM duty cycle is reached, start incrementing. m_led_sb.is_counting_up = true; m_led_sb.duty_cycle = m_led_sb.params.duty_cycle_min; pause_ticks = m_led_sb.params.off_time_ticks ? m_led_sb.params.off_time_ticks + APP_TIMER_MIN_TIMEOUT_TICKS : 0; } else { m_led_sb.duty_cycle -= m_led_sb.params.duty_cycle_step; } } } else { pause_ticks -= PWM_PERIOD; } err_code = low_power_pwm_duty_set(&m_led_sb.pwm_instance, m_led_sb.duty_cycle); APP_ERROR_CHECK(err_code); }