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); } } }
void pwmout_write(pwmout_t *obj, float value) { pwm_t * pwm = (pwm_t *) obj->pwm_struct; if (value > 1.0f) { value = 1.0f; } app_pwm_channel_duty_set(pwm->instance, obj->pwm_channel, (app_pwm_duty_t)(value * 100.0f) ); }
/**@brief Function for initializing the PWM output */ static void pwm_init(void) { ret_code_t err_code; /* 1-channel PWM, 200Hz, output to pin 15 */ app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, BSP_LED_1); err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback); APP_ERROR_CHECK(err_code); /* Keep trying to set duty cycle until PWM ready */ while(app_pwm_channel_duty_set(&PWM1, 0, 100) == NRF_ERROR_BUSY); app_pwm_enable(&PWM1); }
/**@brief Battery level Collector Handler. **************************************** */ static void bas_c_evt_handler(ble_bas_c_t * p_bas_c, ble_bas_c_evt_t * p_bas_c_evt) { uint32_t err_code; switch (p_bas_c_evt->evt_type) { case BLE_BAS_C_EVT_DISCOVERY_COMPLETE: // Battery service discovered. Enable notification of Battery Level. APPL_LOG("[APPL]: Battery Service discovered. \r\n"); APPL_LOG("[APPL]: Reading battery level. \r\n"); err_code = ble_bas_c_bl_read(p_bas_c); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]: Enabling Battery Level Notification. \r\n"); err_code = ble_bas_c_bl_notif_enable(p_bas_c); APP_ERROR_CHECK(err_code); printf("Battery service discovered \r\n"); break; case BLE_BAS_C_EVT_BATT_NOTIFICATION: { APPL_LOG("[APPL]: Battery Level received %d %%\r\n", p_bas_c_evt->params.battery_level); uint8_t batteryLevel = p_bas_c_evt->params.battery_level; // Normalize the battery level //if(batteryLevel > 100) batteryLevel = 100; batteryLevel = batteryLevel < 100 ? batteryLevel : 100; printf("batteryLevel = %d %%\r\n", batteryLevel); while(app_pwm_channel_duty_set(&PWM1, 0, batteryLevel) == NRF_ERROR_BUSY) printf("Battery = %d %%\r\n", batteryLevel); break; } case BLE_BAS_C_EVT_BATT_READ_RESP: { APPL_LOG("[APPL]: Battery Level Read as %d %%\r\n", p_bas_c_evt->params.battery_level); //printf("Battery Read As = %d %%\r\n", p_bas_c_evt->params.battery_level); break; } default: break; } }
static void ir_led_set_pwm(uint32_t physical_pin, uint32_t duty_cycle, uint32_t us_period) { //Uniniatialize PWM if there is any initialized. app_pwm_uninit(&pwm0); //Configuring PWM single channel. app_pwm_config_t pwm0_cfg = APP_PWM_DEFAULT_CONFIG_1CH(us_period, physical_pin); //Initializing PWM. APP_ERROR_CHECK(app_pwm_init(&pwm0, &pwm0_cfg, NULL)); //Enable PWM. app_pwm_enable(&pwm0); //Set duty cycle. while(app_pwm_channel_duty_set(&pwm0, 0, duty_cycle) == NRF_ERROR_BUSY); }
// 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); } }