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 Reply Children
  • 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

  • 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