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

I use PWM only one channel is effective.this is my code

Hi.I use SDK12.2.I want to blink the RGB LED.so I use pwm.this is my code but it only bule color light on.does anyone know why?
#include <stdio.h>
#include <string.h>
#include "nrf_drv_pwm.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "boards.h"
#include "bsp.h"
#include "app_timer.h"
#include "nrf_drv_clock.h"
#define NRF_LOG_MODULE_NAME "APP"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_delay.h"
#define APP_TIMER_PRESCALER     0
#define APP_TIMER_OP_QUEUE_SIZE 2
int gLEDR = 10;
int gLEDG = 9;
int gLEDB = 8;
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
static uint16_t const m_top  = 10000;
static nrf_pwm_values_individual_t   seq_values;
static nrf_pwm_sequence_t const seq =
    {
        .values.p_individual = &seq_values,
        .length              = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats             = 0,
        .end_delay           = 0
    };

static void pwm_init(void)
{
    uint32_t err_code;
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            gLEDR | NRF_DRV_PWM_PIN_INVERTED,// channel 0
            gLEDG | NRF_DRV_PWM_PIN_INVERTED,// channel 1
            gLEDB | NRF_DRV_PWM_PIN_INVERTED, // channel 2
            NRF_DRV_PWM_PIN_NOT_USED  // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_4MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = m_top,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    err_code = nrf_drv_pwm_init(&m_pwm0, &config0, NULL);
    APP_ERROR_CHECK(err_code);
    // m_used |= USED_PWM(0);
}
void update_pwm(int16_t dutyR, int16_t dutyG, int16_t dutyB)
{
   
    seq_values.channel_0 = m_top-dutyR;
    seq_values.channel_1 = m_top-dutyG;
    seq_values.channel_2 = m_top-dutyB;
   
    nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 1,  NRF_DRV_PWM_FLAG_LOOP);
}
int main(void)
{
 
  pwm_init();
 
    while(1){
        for (int i=0; i<100; i++){
            update_pwm(i*100,0,0);
            nrf_delay_ms(1);
        }
        for (int i=0; i<100; i++){
            update_pwm(10000,i*100,0);
            nrf_delay_ms(1);
        }
        for (int i=0; i<100; i++){
            update_pwm(10000,10000,i*100);
            nrf_delay_ms(1);
        }
        for (int i=100; i>0; i--){
            update_pwm(i*100,10000,10000);
            nrf_delay_ms(1);
        }
        for (int i=100; i>0; i--){
            update_pwm(0,i*100,10000);
            nrf_delay_ms(1);
        }
        for (int i=100; i>0; i--){
            update_pwm(0,0,i*100);
            nrf_delay_ms(1);
        }
        update_pwm(0,0,0);
        nrf_delay_ms(200); 
    }
}
/**/
Parents
  • Hello,

    I tested your application by changing the pins to 17, 18 and 19, in order to see it on the LEDs on the DK. It worked fine there.

    The reason it doesn't work (only the blue LED works) is that pins 09 and 10 are reserved for the NFC antenna.

    If you want to use the NFC pins as GPIOs, you must define CONFIG_NFCT_PINS_AS_GPIOS in your preprocessor defines. Please note that you have to have this define, compile, flash, and then power cycle your nRF in order for it to take effect.

    In addition, if you use an nRF52 DK, the pins 09 and 10 are not connected to the pinout by default. In order to connect them, you must add a R0 on R28  and R27 (insert a 0 Ohm capacitor, or solder the pads on each of this resistor placeholders together). 

    If you use a DK, it is probably easier to use different pins, if you have any available.

    The setup description on how to prepare these pins as normal GPIOs is described here.

    Best regards,

    Edvin

Reply
  • Hello,

    I tested your application by changing the pins to 17, 18 and 19, in order to see it on the LEDs on the DK. It worked fine there.

    The reason it doesn't work (only the blue LED works) is that pins 09 and 10 are reserved for the NFC antenna.

    If you want to use the NFC pins as GPIOs, you must define CONFIG_NFCT_PINS_AS_GPIOS in your preprocessor defines. Please note that you have to have this define, compile, flash, and then power cycle your nRF in order for it to take effect.

    In addition, if you use an nRF52 DK, the pins 09 and 10 are not connected to the pinout by default. In order to connect them, you must add a R0 on R28  and R27 (insert a 0 Ohm capacitor, or solder the pads on each of this resistor placeholders together). 

    If you use a DK, it is probably easier to use different pins, if you have any available.

    The setup description on how to prepare these pins as normal GPIOs is described here.

    Best regards,

    Edvin

Children
Related