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

Reading current pulse train

I am working with the Nordic NRF52840. I need a way of reading current pulse counts over a two wire system coming in from an external sensor. This external sensor is reliable and works by having the output of the sensor reader (will be the nordic board) drop power on the sensor 3.3V, then counting current pulse values over the two wires. These current pulses represent the sensor value itself.

Therefore, I need the ability to reliably read the current pulses train from the sensor whilst powering the device.

I have some code for a reader device that can read this sensor successfully and want to effectively replicate this code onto the Nordic board. The way this code works is by having a comparator, two timers and a state machine to handle to pulse train. One timer is a 1ms IRQ timer. Another is a timer to count the current pulses. This second timer also utilises the comparator.

The pulse current is 4mA. The frequency of the current pulses is 88kHz.

Once I have obtained the current train number of pulses I can then handle this via a state machine to determine what state of processing on this to do. ( This is due to the sensor having different states) e.g. idle, powering on, taking reading etc.

My question is how do I create a timer to accurately read these 4mA current pulses. I want to store the last current pulses as a int. Then handle this in a state machine. Note the sensors are a chain and can contain more than one. But, this will be handled by the state machine.

Below is my current code layout. I need to setup 2 timers and a comparator. Note the comparator on my other reader code is setup to use one of the inputs connected to a DAC and using VREF thats modified to 117mV. This relates to 4mA. This is for the voltage reference. It is connected to the negative side of the comparator. The postive side is connected to the sensors (-) wire. As the positive wire is for power. The comparator setup is an output, and has high speed mode enabled.

//Reader functions for reading the sensor

#include "Reader.h"//Get access to the definitions in the headerfile
//Will need access to the hardware definitions here as well

int Test_reader()//Simple test function
{
    return  1;
}

//Global variables
_Bool pulses;//This is set to be true or false from the interrupt handler
int oldPulseCount;//This is used to detect if pulses are being received or not
int pulseGapms;//This is set from the interrupt handler

int SensorState;//The state of the statemachine
int SensorTimeout;//A time for the sensor timeout (Used for reset?)
int SensorType;//The definition of the sensor type
int ValuesToGet, valuesCount;//How many values need to be gotten and the number of values
int Values[MAX_VALUES_PER_SENSOR];//Raw values from the sensor the number or balues

//Initialisation functions
void Init_Reader(){//Setup the reader and run the initialisation routine
    
    Init_Comparator();
    Init_TB0();//Pulse Train counter
    Init_TB1();//1ms IRQ
    SensorState = SENSORSTATE_IDLE;//Start the state in idle mode
    SensorTimeout = 0;//Set the timeout time so start reading the chain of sensors straight away
}
#pragma  vector = TIMER1_B0_VECTOR
__interrupt void Timer1_B0_ISR(void)
{
    int pulseCount = 0;//Dont use 0 need to sample the counter value each 1ms from the other timer
    pulses = (oldPulseCount != pulseCount);//Check if there are a different number of pulses detected
    oldPulseCount = pulseCount;//Shift new data to old data
    pulseGapms = (!pulses) ? pulseGapms +1 : 0;//Count the size of the gap between the pulses
}
void Init_TB0(){//Timer 0 initialisation
    //Counts comparator input pulses
    //Use the comparator to convert current pulses to a voltage
}

void Init_TB1(){//Timer 1 initialisation
  //1ms timer
}

void Init_Pulses(){//Function to count the pulses found by the peripheral timer
    //Start being ready to count pulses
    oldPulseCount = 0;//Set no old current pulses
    pulses = false;//No current pulses yet detected
}

void Init_Comparator(){//Start the comparator that takes in current values and converts them to a voltage
    //Setup the comparator to convert the pulse train current to a voltage for the timer to use
}

void STATEMACHINE(){}//This will contain the handling of the diffrent states and processing of the pulse counts found

From the result of this i will then print out the sensor value via UART but that is not in the scope of this question,

Parents
  • Hi,

    I have some example code in this post. (The "gpiote_timer_ppi.zip" attachment in the post)

  • Also I cant find the pinout for the NRF52840-dk. I want to set the comparator output to the input of the counter timer. And give the comparator an external voltage reference to measure against. How do I setup this input? I have got the following code:

    //Configuring lpcomp
        NRF_LPCOMP -> PSEL = (LPCOMP_PSEL_PSEL_AnalogInput2 << LPCOMP_PSEL_PSEL_Pos);//Use analog input 2 as an input P04
        NRF_LPCOMP -> EXTREFSEL = (LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference0 << LPCOMP_EXTREFSEL_EXTREFSEL_Pos);//Set up external reference 0 P02
        NRF_LPCOMP -> REFSEL = (LPCOMP_REFSEL_REFSEL_ARef << LPCOMP_REFSEL_REFSEL_Pos);//Use external reference
        NRF_LPCOMP -> ANADETECT = (LPCOMP_ANADETECT_ANADETECT_Cross << LPCOMP_ANADETECT_ANADETECT_Pos);//Detect a change over the reference upon crossing the reference
        
        //Enable and start the low power comparator
        NRF_LPCOMP -> ENABLE = LPCOMP_ENABLE_ENABLE_Enabled;
        NRF_LPCOMP -> TASKS_START = 1;
    Are my comments of the pin connections for analog reference 0 and analog input 2 correct?

    I got them from this pinout for a similar nordic dk : https://os.mbed.com/platforms/Nordic-nRF52-DK/

Reply
  • Also I cant find the pinout for the NRF52840-dk. I want to set the comparator output to the input of the counter timer. And give the comparator an external voltage reference to measure against. How do I setup this input? I have got the following code:

    //Configuring lpcomp
        NRF_LPCOMP -> PSEL = (LPCOMP_PSEL_PSEL_AnalogInput2 << LPCOMP_PSEL_PSEL_Pos);//Use analog input 2 as an input P04
        NRF_LPCOMP -> EXTREFSEL = (LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference0 << LPCOMP_EXTREFSEL_EXTREFSEL_Pos);//Set up external reference 0 P02
        NRF_LPCOMP -> REFSEL = (LPCOMP_REFSEL_REFSEL_ARef << LPCOMP_REFSEL_REFSEL_Pos);//Use external reference
        NRF_LPCOMP -> ANADETECT = (LPCOMP_ANADETECT_ANADETECT_Cross << LPCOMP_ANADETECT_ANADETECT_Pos);//Detect a change over the reference upon crossing the reference
        
        //Enable and start the low power comparator
        NRF_LPCOMP -> ENABLE = LPCOMP_ENABLE_ENABLE_Enabled;
        NRF_LPCOMP -> TASKS_START = 1;
    Are my comments of the pin connections for analog reference 0 and analog input 2 correct?

    I got them from this pinout for a similar nordic dk : https://os.mbed.com/platforms/Nordic-nRF52-DK/

Children
No Data
Related