Skip to content

Commit 116dadd

Browse files
committed
feat: get capacitor range
1 parent 94c3f1e commit 116dadd

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/commands.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
252252
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
253253
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
254254
// 16 GET_CAP_RANGE 17 SET_RGB2 18 READ_LOG 19 RESTORE_STANDALONE
255-
Unimplemented, Removed, Removed, DEVICE_Reset,
255+
MULTIMETER_GetCapRange, Removed, Removed, DEVICE_Reset,
256256
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 SET_RGB_COMMON 22 SET_RGB3 23 START_CTMU
257257
Unimplemented, LIGHT_RGBPin, Removed, CTMU_Start,
258258
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER

src/instruments/multimeter.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void ChargeCapacitor(uint8_t charge, uint16_t period);
1616
void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
1717
uint16_t charge_time);
1818
void GetCapacitance_ConfigADC_CTMU_TMR5();
19+
void GetCapacitor_Range(uint16_t charge_time);
1920

2021
void ChargeCapacitor(uint8_t charge, uint16_t period) {
2122
CAP_OUT_SetDigitalOutput();
@@ -95,6 +96,33 @@ uint16_t GetVoltage_Summed(uint8_t channel) {
9596
return voltage_sum;
9697
}
9798

99+
uint16_t GetCapacitor_Range(uint16_t charge_time) {
100+
101+
ChargeCapacitor(0, 50000);
102+
ChargeCapacitor(0, 50000); // TODO: Check if we need two of these
103+
104+
ADC1_SetOperationMode(ADC1_12BIT_AVERAGING_MODE, CH0_CHANNEL_CAP, 0);
105+
106+
TMR5_Initialize();
107+
TMR5_Period16BitSet(charge_time);
108+
TMR5_SetPrescaler(TMR_PRESCALER_64);
109+
TMR5_Start();
110+
111+
// Start charging the capacitor through 10K resistor
112+
CAP_OUT_SetDigitalOutput();
113+
CAP_OUT_SetHigh();
114+
115+
TMR5_WaitForInterruptEvent();
116+
117+
// Stop the charging process
118+
CAP_OUT_SetDigitalInput();
119+
CAP_OUT_SetLow();
120+
121+
uint16_t total = GetVoltage_Summed(CH0_CHANNEL_CAP);
122+
123+
return total;
124+
}
125+
98126
response_t MULTIMETER_GetVoltage(void) {
99127

100128
uint8_t channel = UART1_Read();
@@ -193,6 +221,16 @@ response_t MULTIMETER_GetCapacitance(void) {
193221
return SUCCESS;
194222
}
195223

224+
response_t MULTIMETER_GetCapRange(void) {
225+
226+
uint16_t charge_time = UART1_ReadInt(); // in microseconds
227+
228+
uint16_t range = GetCapacitor_Range(charge_time);
229+
UART1_WriteInt(range);
230+
231+
return SUCCESS;
232+
}
233+
196234
response_t MULTIMETER_GetCTMUVolts(void) {
197235

198236
uint8_t config = UART1_Read();

src/instruments/multimeter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ extern "C" {
2828
* @return SUCCESS
2929
*/
3030
response_t MULTIMETER_GetCapacitance(void);
31+
32+
/**
33+
* @brief Get an estimate of the capacitor range
34+
*
35+
* @description
36+
* This command takes only one argument over serial:
37+
* 1. (uint16) Charge time
38+
*
39+
* It returns the range value as uint16.
40+
*
41+
* @return SUCCESS
42+
*/
43+
response_t MULTIMETER_GetCapRange(void);
3144

3245
/**
3346
* @brief Measurements using Charge Time Measurement Unit

0 commit comments

Comments
 (0)