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

how to resign pins in saadc example

Hi 

I'm not familiar with C language, I tested the saadc example in SDK, it worked well in P0.02

But I dont see how they set CH[X].PSELP or CH[X].PSELN (V(p) and V(n)) in main.c

I dont know how to implement this code to work with other ADC pins.  

Please help me with it and correct me if I was wrong. 

I'm using nrf52 dk and segger embedded studio. 

Cindy

Parents
  • Hi Cindy

    These parameters can be found in the channel_config struct, which is used to initialize the ADC channel inside the saadc_init() function. 

    By default this function will just use the default settings, defined by the NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P) macro, but you can override the various parameters after initializing the struct if you like. 

    As an example, to override the positive pin, you just run the following code: 

    channel_config.pin_p = NRF_SAADC_INPUT_AIN1;

    Then the full saadc_init() function would look like this:

    Best regards
    Torbjørn

  • Can you make an example of how to change it into a non-LED pin? what about the duty cycle?

  • Hi Cindy

    You can just replace "LED_1" with the GPIO number of your choice. 

    For instance, to use P0.08 set it like this:

    #define OUTPUT_PIN 8

    The duty cycle is set by calling the pwm_update_duty_cycle(uint8_t duty_cycle) function with a number between 0 and 100. 

    Best regards
    Torbjørn

  • Thanks for your help, it's all very helpful. 

    is there any way I can change the clock to a lower frequency, it seems 125kHz is the lower frequency this function can do?

    because I want to use a speaker here, I want the frequency is around 2kHz. 

    maybe I didn't do it right, please correct me.

    Thanks,

    Cindy

  • Hi Cindy

    Do you just need a 2kHz square wave, or do you want to use the PWM for more sophisticated sounds?

    Normally the base clock of the PWM is much higher than then output frequency of the signal, to allow you a better PWM resolution. 

    Best regards
    Torbjørn

  • the reason why I want to adjust the frequency is that my speaker isn't very loud with current code, I thought it's because the frequency is too high so the speaker can't have a high decibel level.

     

    Yesterday, I tried pwm_library, 

    /** @file
     * @defgroup pwm_example_main main.c
     * @{
     * @ingroup pwm_example
     *
     * @brief  PWM Example Application main file.
     *
     * This file contains the source code for a sample application using PWM.
     *
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    static volatile bool ready_flag;            // A flag indicating PWM status.
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
    {
        ready_flag = true;
    }
    
    int main(void)
    {
        ret_code_t err_code;
    
        /* 1-channel PWM, 100000 microseconds period = 0.1 second , output on pin 28. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1000L, 28);
    
        /* 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);
        
        //DUTY CYCLE SET TO 50%
        while (app_pwm_channel_duty_set(&PWM1, 0, 10) == NRF_ERROR_BUSY);
    
    
        while (true)
        {
    
        // Configure pin 5 as output
        nrf_gpio_cfg_output(4);
        // Set pin 5 high
        nrf_gpio_pin_set(4);
        nrf_delay_ms(1000);
        // Set pin 5 low
        nrf_gpio_pin_clear(4);
        nrf_delay_ms(1000);
    
        }
    
    }
    
    
    /** @} */

    "app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1000L, 28);" (1kHz)

    by changing the perid of time, I think I changed the frequncy?

    But it's still not very loud. Please let me know if I did it right?

    btw, I tested this speaker with Arduino, I used their tone function.

    Here's the description of their tone function

    Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.

    Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

    So I think I need a simple square wave. 

    Could it because the speaker doesn't get enough current? 

    https://media.digikey.com/pdf/Data%20Sheets/CUI%20Inc%20All%20Brands%20PDFs/CCV-084B16-SMT.pdf

    here's the datasheet of the speaker I'm using.

    Thanks for your help.

    Cindy

Reply
  • the reason why I want to adjust the frequency is that my speaker isn't very loud with current code, I thought it's because the frequency is too high so the speaker can't have a high decibel level.

     

    Yesterday, I tried pwm_library, 

    /** @file
     * @defgroup pwm_example_main main.c
     * @{
     * @ingroup pwm_example
     *
     * @brief  PWM Example Application main file.
     *
     * This file contains the source code for a sample application using PWM.
     *
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    static volatile bool ready_flag;            // A flag indicating PWM status.
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
    {
        ready_flag = true;
    }
    
    int main(void)
    {
        ret_code_t err_code;
    
        /* 1-channel PWM, 100000 microseconds period = 0.1 second , output on pin 28. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1000L, 28);
    
        /* 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);
        
        //DUTY CYCLE SET TO 50%
        while (app_pwm_channel_duty_set(&PWM1, 0, 10) == NRF_ERROR_BUSY);
    
    
        while (true)
        {
    
        // Configure pin 5 as output
        nrf_gpio_cfg_output(4);
        // Set pin 5 high
        nrf_gpio_pin_set(4);
        nrf_delay_ms(1000);
        // Set pin 5 low
        nrf_gpio_pin_clear(4);
        nrf_delay_ms(1000);
    
        }
    
    }
    
    
    /** @} */

    "app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1000L, 28);" (1kHz)

    by changing the perid of time, I think I changed the frequncy?

    But it's still not very loud. Please let me know if I did it right?

    btw, I tested this speaker with Arduino, I used their tone function.

    Here's the description of their tone function

    Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.

    Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

    So I think I need a simple square wave. 

    Could it because the speaker doesn't get enough current? 

    https://media.digikey.com/pdf/Data%20Sheets/CUI%20Inc%20All%20Brands%20PDFs/CCV-084B16-SMT.pdf

    here's the datasheet of the speaker I'm using.

    Thanks for your help.

    Cindy

Children
  • Hi Cindy

    If you need to generate a square wave of varying frequencies I don't think the PWM peripheral is the best choice, since the PWM is designed for applications where you use a fixed base frequency and modulate a signal on top of this. 

    Instead it should be enough to set up the TIMER, PPI and GPIOTE modules to toggle a pin when the timer overflows, and then you just change the compare value of the timer to change the frequency of the pin. 

    I made an example showing how to do this a while back and I will see if I can find it, hopefully by tomorrow. 

    Are you saying the speaker is louder when you use it with Arduino than when you use the Nordic kit?

    Some speakers need a particular hardware configuration to get optimal performance, but this is normally mentioned in the datasheet. 

    Best regards
    Torbjørn

Related