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

multiple adc +multiple pwm

Hi, i am using nrf52840 development kit , four servo motor . using 15.2 sdk version

here i am controlling the angle using adc values , i am done with one servo motor . pwm+adc. Now i need to control the angle for all the four motors , how to do that ? how can  i add the four adc channels . 

Parents
  • In order to add an ADC channel, you only need to change the pin, and change the channel number:

    void saadc_init(void)
    {
        ret_code_t err_code;
        nrf_saadc_channel_config_t channel_config =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
        err_code = nrf_drv_saadc_init(NULL, saadc_callback);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
        
        channel_config.pin_p = NRF_SAADC_INPUT_AIN1;
        err_code = nrf_drv_saadc_channel_init(1, &channel_config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
    }

    Then I recommend setting the "SAMPLES_IN_BUFFER" = n*number of channels, where n=1,2,3,4,...

    If you do this to the saadc example from the SDK, you will get 2 channels in the ADC. Just keep adding these until you have 4.

    It is quite similar to the PWM. I don't know if you used the pwm_library or the pwm_driver example/libraries, but it should be possible to add channels in either one of them. In the PWM_library, you may need to add a second instance of PWM in order to control 4 channels.

    BR,

    Edvin

  • what about the timer ? static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0); , i need to change?

  • ok. Try to debug. Do you get the BLE_GAP_EVT_CONNECTED event?

  • its appearing when i am trying to connect, it taking too much time to connect and disconnecting within ms .

  • yes i tried ,nothing i am getting error while debugging , running fine . It is advertising but when i am trying to connect , it showing connecting connecting ... and its appearing connected and disconnected .. refer the below

    connection state changed with status :0 and new state:2(connected)

    connected to D1:30:99:E6:C7:89

    connection state changed with status :34 and new state :0 (disconnected)

    Error:(0x22): GATT CONN LMP TIMEOUT 

    connection lost 

    connecting ...

    thats all again it is not appearing 

  • Is it possible for me to reproduce this on an nRF52840 DK? Can you zip the project folder and upload it here? you may need to delete the build folders if the zip file is too big to upload.

    Have you tried any of the other examples from the ble_peripheral folder? Are you able to connect to those?

  • Thank you so much Edvin, for your support. i got .. my mistake i didn't multiply the number of channels.. 

    can you explain me a bit , "SAMPLES_IN_BUFFER" = n*number of channels, where n=1,2,3,4,...by multiplying the number of channels what will happen ?

    and more what is the m_buffer_pool[0], m_buffer_pool[1]?  i am confused between these two

Reply Children
  • Hello,

    The reason I suggested SAMPLES_IN_BUFFER = n*num_of_channels is because the saadc buffer doesn't know what channel the different samples comes from.

    E.g. if you have SAMPLES_IN_BUFFER = 5, and use two channels, then in the first SAADC callback event, buffer[0], buffer[2] and buffer[4] will contain samples from channel 1, while buffer[1] and buffer[3] will contain the samples from channel 2.

    But in the next event, they will be shifter:

    Buffer[1] and buffer[3] will contain samples from channel 1, and buffer[0], [2] and [3] now contains samples from channel 2.

    If you set the SAMPLES_IN_BUFFER = 4, then buffer[0] and buffer[2] will always contain samples from channel 1, and buffer[1] and buffer[3] will always contain samples from channel 2.

    m_buffer_pool[0] and [1] is mostly in use by the ADC internally. The reason it is two of them is that once one sampling is done, it can pass one of the buffer, and start sampling on the other one immediately. Nothing that you need to worry about. The actual values from the samples you will find in the callback, where it is printed to the log in the example.

    Best regards,

    Edvin

Related