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

Counting Time

I'm working with nrf52 dk to read the sensor dht22. i need to follow this steps to make sure the sensor is ready to send data.

  1. Wait until DHT responds and pulls the line to LOW after around 40 micro-seconds or else exit as timeout error.
  2. Next, check for 80us LOW followed by 80us HIGH from DHT. This condition denotes that DHT is ready to send data next.
  3. Now, check for 50us LOW followed by 26us to 28us HIGH denoting data bit ‘0’ or 50us LOW followed 70us HIGH denoting data bit ‘1’. Store the interpreted bit in an array. Repeat this for each of 40 bits.

I don't understand how i should do a simple counter to count the time and convert to micro seconds.

It's my first experience with nordic, can anyone give me some tips?

Thank you. 

Parents
  • Hi, there may be different ways to solve this, but it seems you have some tight timing requirements here. In that case it make sense to run this code when there is no BLE activity, and you for instance can temporary disable other interrupts that may interfere. In such case you may just use nrf_delay_us() for most part?

  • Hi, thank you for your answer.

    i was thinking of doing something like this to point 1:

    ----------------------------------------------------------

    start timer count

    while( line high) // just waiting to turn low

    Time = 'count time'

    if( time < 20 || time > 40){

    error

    }

    -------------------------------------------------------

    do you thing this is a good approach? can i get the time and compare the time in micro seconds?

Reply
  • Hi, thank you for your answer.

    i was thinking of doing something like this to point 1:

    ----------------------------------------------------------

    start timer count

    while( line high) // just waiting to turn low

    Time = 'count time'

    if( time < 20 || time > 40){

    error

    }

    -------------------------------------------------------

    do you thing this is a good approach? can i get the time and compare the time in micro seconds?

Children
  • You may take a look at the \nRF5_SDK_15.0.0_a53641a\examples\peripheral\timer in the nRF SDK. This should show how to setup a timer, and for instance setup compare events. For instance nrf_drv_timer_us_to_ticks() will help you to setup a timer with a specific us timeout.

  • i check the app_timer instead.

    this is my code so far:


    #include <stdint.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "nrf_drv_gpiote.h" // NordicSemiconductor::nRF_Drivers:nrf_drv_gpiote
    #include "nrf_delay.h" // NordicSemiconductor::nRF_Drivers:nrf_delay
    #include "app_error.h" // NordicSemiconductor::nRF_Libraries:app_error
    #include "startup_config.h" // NordicSemiconductor::Device:StartupConfig
    #include "nrf.h" // Device header
    #include "system_nrf52.h" // NordicSemiconductor::Device:Startup
    #include "RTE_Components.h" // Component selection
    #include "nrf_drv_timer.h" // NordicSemiconductor::nRF_Drivers:nrf_drv_timer
    #include "nrf_timer.h" // NordicSemiconductor::nRF_Drivers:nrf_timer
    #include "app_simple_timer.h" // NordicSemiconductor::nRF_Libraries:app_simple_timer
    #include "nrf_drv_rtc.h" // NordicSemiconductor::nRF_Drivers:nrf_drv_rtc
    #include "nrf_rtc.h" // NordicSemiconductor::nRF_Drivers:nrf_rtc
    #include "app_timer.h"
    #include "app_util_platform.h"
    #include "nrf_soc.h"
    #include "sdk_macros.h"

    #define level_0 0
    #define level_1 1


    //#define DHT22_PIN D1
    uint32_t DHT22_PIN = 12;
    uint32_t time = 0; // Set to 0 for no prescaling.
    //counter
    uint32_t ticks_now = 0;
    uint32_t ticks_old = 0;
    uint32_t ticks_final = 0;
    uint32_t time_us = 0;

    //configuring the given GPIO pin number as output
    void DHT22_OUTPUT(void)
    {
    nrf_gpio_cfg_output(DHT22_PIN);
    }

    //Function for configuring sense level for the given GPIO.
    void DHT22_ToLow(void)
    {
    nrf_gpio_cfg_sense_set(DHT22_PIN,NRF_GPIO_PIN_SENSE_LOW);
    }

    //Function for configuring the given GPIO pin number as input
    void DHT22_INPUT(void)
    {
    nrf_gpio_cfg_input(DHT22_PIN, NRF_GPIO_PIN_PULLUP);
    }

    uint32_t app_timer_ms(uint32_t ticks)
    {

    float numerator = ((float)time + 1.0f) * 1000.0f;
    float denominator = (float)APP_TIMER_CLOCK_FREQ;
    float ms_per_tick = numerator / denominator;

    uint32_t ms = ms_per_tick * ticks;

    return ms;
    }

    void DHT22_START(void)
    {
    DHT22_OUTPUT();
    DHT22_ToLow();
    nrf_delay_ms(18);
    DHT22_INPUT();

    //initialize counter
    rtc1_init(time);
    //start counter
    rtc1_start();
    //get 1st value
    ticks_old = rtc1_counter_get();

    //Function for reading the input level of a GPIO pin.
    while(nrf_gpio_pin_read(DHT22_PIN)!=level_0) //0 if the pin input level is low.

    //get 2nd value
    ticks_now = rtc1_counter_get();
    //stop counter
    rtc1_stop();
    //get diff
    ticks_final = ticks_diff_get(ticks_now,ticks_old);

    //ticks to micro seconds
    time_us = app_timer_ms(ticks_final) / 1000;

    if(time_us < (20 * 10^-6) || time_us > (40*10^-6))
    {
    printf("Failed to communicate with sensor");
    exit(0);
    }
    }

    int main(void)
    {

    }

  • is this a good approach?

    however it gives 5 message errors:

     Error: L6218E: Undefined symbol rtc1_init (referred from teste.o). 

Related