This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Pwm and softdevice on nRF51822

Hello,

I know that this question was here before, but I've read all of the questions and answers and still I don't know what I do wrong.

I'm using 3 PWM channels to controll RGB led, and I'm using my RGB_update function to change color by writting letters on UART. Without Softdevice it work fine, but when I'm trying to put the same code to, for example, ble_app_proximity example, the RGB color is not changing. I's using SDK 6.1 and softdevice 7.1.

Any advices?

My main.c int main(void) { // LED & PWM initialization uint8_t R=150; uint8_t G=150; uint8_t B=150;

		nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;

  pwm_config.mode             = PWM_MODE_LED_255;
pwm_config.num_channels     = 4;
pwm_config.gpio_num[0]      = LED_1;
pwm_config.gpio_num[1]      = LED_2;
pwm_config.gpio_num[2]      = LED_4;
pwm_config.gpio_num[3]      = LED_3;  
	// Initialize the PWM library
	nrf_pwm_init(&pwm_config);

simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);




// Initialize.
app_trace_init();
leds_init();
timers_init();
gpiote_init();
buttons_init();
ble_stack_init();
device_manager_init();
gap_params_init();
advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
services_init();
conn_params_init();

// Start execution.
advertising_start();

// Enter main loop.
while(true)
{
			
			uint8_t cr = 'r';
			

	
			if (cr == 'R' || cr == 'r' || cr == 'G' || cr == 'g' || cr == 'B' || cr == 'b')
			{
		
				RGB_update(cr,&R,&G,&B);
				
			}
			if (cr == 's' || cr == 'S' || cr == 'H' || cr == 'h' )
			{
				HSV_update(cr,&R,&G,&B);
			}
			
		
			nrf_pwm_set_value(1, (uint32_t) R);   //R
			nrf_pwm_set_value(2, (uint32_t) G);   //G
    nrf_pwm_set_value(3, (uint32_t) B);	 //B	
		
		
   power_manage();
		
			
}

}

void RGB_update( uint8_t cr, uint8_t *R, uint8_t *G, uint8_t *B) {

if (cr == 'q' && cr == 'Q') { //uart_quit(); } if (cr == 'R' && *R<220) { (*R)+=25; } if (cr == 'G' && *G<220) { (*G)+=25; } if (cr == 'B' && *B<220 ) { (*B)+=25; } if (cr == 'r' && *R>30) { (*R)-=25; } if (cr== 'g' && *G>30) { (*G)-=25; }
if (cr == 'b' && *B>30) { (*B)-=25; }

}

  • Hmm,

    It seems that all init(config) of pwm is carried out after the routine call of ble_stack_init();(or not?) If you are using pwm lib so provided by nrf, which i think you are using, then please shift your pwm initialization steps into one method and call it BEFORE ble_stack_init();

    I have carried out the same with 3 led/channels and init before ble_stack_init() so proper timers get occupied first then let softdevice to occupy TIMER0.

    Secondly i pass parameters as nrf_pwm_set_value({0-3}, n); //n {0-255}(int) not int32_t. make sure you have set those LED pins as output in led_init(). Do let us know if you have fixed or this helped you or if problem still persist then do provide any other information regarding your code/arrangements please.

  • I am using the latest version of nrf51-pwm-library:

    github.com/.../main_sound.c

    After debugging for quite a while and finally got it working by placing pwm initialization AFTER ble_stack_init(); Actually after everything, related to softdevice and ble initialization, just before while loop.

Related