@@ -16,6 +16,7 @@ void ChargeCapacitor(uint8_t charge, uint16_t period);
1616void GetCapacitance_InitCTMU_TMR5 (uint8_t current_range , uint8_t trim ,
1717 uint16_t charge_time );
1818void GetCapacitance_ConfigADC_CTMU_TMR5 ();
19+ void GetCapacitor_Range (uint16_t charge_time );
1920
2021void 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+
98126response_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+
196234response_t MULTIMETER_GetCTMUVolts (void ) {
197235
198236 uint8_t config = UART1_Read ();
0 commit comments