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,