|  | Dual Active Bridge Development Board (Part-No. ) |
Content |  |  |  |  |  |

Loading...
Searching...
No Matches
dev_current_sensor.c
Go to the documentation of this file.
1
2
9#include <stdbool.h>
10#include "config/macros.h"
11#include "adc/adc1.h"
12
13#define SENSOR_OFFSET_NUM_MEASUREMENTS (1000)
14
15
22 struct SENSOR_OFFSET_CAL_s
23{
24 uint32_t Accumulator;
25 uint16_t MeasurementCounter;
26 uint16_t Offset;
27 bool CalibrationComplete;
28 uint16_t LimitHigh;
29 uint16_t LimitLow;
30};
31 typedef struct SENSOR_OFFSET_CAL_s SENSOR_OFFSET_CAL_t;
32
33
40 SENSOR_OFFSET_CAL_t isecAvgCurrentSensor = {.CalibrationComplete = false,
41 .Accumulator = 0,
42 .MeasurementCounter = 0,
43 .LimitHigh = ISEC_AVG_SENSOR_OFFSET_LIMIT_HIGH,
44 .LimitLow = ISEC_AVG_SENSOR_OFFSET_LIMIT_LOW};
45
46
57 static void __inline__ Dev_SensorOffsetCal(SENSOR_OFFSET_CAL_t* sensor, uint16_t adcReading)
58{
59 // current sensor offset is not measured
60 sensor->Accumulator += adcReading;
61 if (++sensor->MeasurementCounter >= SENSOR_OFFSET_NUM_MEASUREMENTS)
62 {
63 // average to determine the offset
64 uint16_t offset = (uint16_t) (__builtin_divud(sensor->Accumulator, sensor->MeasurementCounter));
65
66 sensor->Accumulator = 0;
67 sensor->MeasurementCounter = 0;
68
69 // check if result is within acceptable limits
70 if ((offset < sensor->LimitHigh) && (offset > sensor->LimitLow))
71 {
72 // measured offset is reasonable. Update stored value and exit
73 sensor->Offset = offset;
74 sensor->CalibrationComplete = true;
75 }
76 }
77}
78
79
87 void Dev_CurrentSensorOffsetCal(void)
88{
89 if (!isecAvgCurrentSensor.CalibrationComplete)
90 {
91 Dev_SensorOffsetCal(&isecAvgCurrentSensor, ADC1_ConversionResultGet(ISEC_AVG));
92 }
93}
94
95
101uint16_t Dev_CurrentSensor_Get_CalibrationStatus(void)
102{
103 return(isecAvgCurrentSensor.CalibrationComplete);
104}
105
106
111uint16_t Dev_CurrentSensor_Get_Offset(void)
112{
113 return(isecAvgCurrentSensor.Offset);
114}
115
116
121 void Dev_CurrentSensor_Clr_Offset(void)
122{
123 isecAvgCurrentSensor.CalibrationComplete=false;
124}
adc1.h
This is the generated driver header file for the ADC1 driver.
ISEC_AVG
@ ISEC_AVG
Definition adc_types.h:63
SENSOR_OFFSET_NUM_MEASUREMENTS
#define SENSOR_OFFSET_NUM_MEASUREMENTS
Number of measurement to calculate the average over.
Definition dev_current_sensor.c:13
ISEC_AVG_SENSOR_OFFSET_LIMIT_LOW
#define ISEC_AVG_SENSOR_OFFSET_LIMIT_LOW
Definition macros.h:84
ISEC_AVG_SENSOR_OFFSET_LIMIT_HIGH
#define ISEC_AVG_SENSOR_OFFSET_LIMIT_HIGH
Definition macros.h:83
Dev_SensorOffsetCal
static void __inline__ Dev_SensorOffsetCal(SENSOR_OFFSET_CAL_t *sensor, uint16_t adcReading)
Measure the sensor offset.
Definition dev_current_sensor.c:57
Dev_CurrentSensor_Get_CalibrationStatus
uint16_t Dev_CurrentSensor_Get_CalibrationStatus(void)
API function to get the calibration status.
Definition dev_current_sensor.c:101
Dev_CurrentSensor_Clr_Offset
void Dev_CurrentSensor_Clr_Offset(void)
API function to clear the sensor offset.
Definition dev_current_sensor.c:121
Dev_CurrentSensor_Get_Offset
uint16_t Dev_CurrentSensor_Get_Offset(void)
API function to get the sensor offset.
Definition dev_current_sensor.c:111
Dev_CurrentSensorOffsetCal
void Dev_CurrentSensorOffsetCal(void)
Measures the currents sensor offset.
Definition dev_current_sensor.c:87
isecAvgCurrentSensor
SENSOR_OFFSET_CAL_t isecAvgCurrentSensor
Data Object of sensor offset calibration.
Definition dev_current_sensor.c:40
ADC1_ConversionResultGet
static uint16_t ADC1_ConversionResultGet(enum ADC_CHANNEL channel)
Returns the conversion value for the channel selected.
Definition adc1.h:178
SENSOR_OFFSET_CAL_s
Stores data related to sensor calibration, including data related to sensor offset.
Definition dev_current_sensor.c:23
SENSOR_OFFSET_CAL_s::Accumulator
uint32_t Accumulator
used for sensor offset measurement at startup
Definition dev_current_sensor.c:24
SENSOR_OFFSET_CAL_s::LimitHigh
uint16_t LimitHigh
upper bound of what offset should be, offset needs to be < limitHigh and > limitLow for sensorCalibra...
Definition dev_current_sensor.c:28
SENSOR_OFFSET_CAL_s::MeasurementCounter
uint16_t MeasurementCounter
used for sensor offset measurement at startup
Definition dev_current_sensor.c:25
SENSOR_OFFSET_CAL_s::LimitLow
uint16_t LimitLow
low bound of what offset should be
Definition dev_current_sensor.c:29
SENSOR_OFFSET_CAL_s::CalibrationComplete
bool CalibrationComplete
set to true if the sensor is calibrated
Definition dev_current_sensor.c:27
SENSOR_OFFSET_CAL_s::Offset
uint16_t Offset
measured offset
Definition dev_current_sensor.c:26