Skip to content

Commit

Permalink
fix: ctmu capacitor measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudyPadmal committed Oct 21, 2021
1 parent cc997d9 commit cca2197
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 170 deletions.
30 changes: 15 additions & 15 deletions pslab-core.X/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
// 16 SET_LO_CAPTURE 17 SET_HI_CAPTURE12 18 SET_LO_CAPTURE12 19 CAPTURE_DMASPEED12
Removed, Removed, Removed, Removed,
// 20 MULTIPOINT_CAPACITANCE 21 SET_CAP 22 PULSE_TRAIN 23 CAPTURE_THREE
Removed, Unimplemented, Removed, OSCILLOSCOPE_CaptureThree,
Removed, MULTIMETER_ChargeCapacitor, Removed, OSCILLOSCOPE_CaptureThree,
// 24 25 26 27
Undefined, Undefined, Undefined, Undefined,
},
Expand Down Expand Up @@ -232,19 +232,19 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
Undefined, Undefined, Undefined, Undefined,
},
{ // 11 COMMON
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, Unimplemented, Unimplemented, Unimplemented,
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
Unimplemented, VERSION_GetVersion, Undefined, Undefined,
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
Unimplemented, Undefined, Unimplemented, Unimplemented,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
Unimplemented, VERSION_GetVersion, Undefined, Undefined,
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SETRGB
BUFFER_Retrieve, Unimplemented, Unimplemented, Unimplemented,
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 16 GET_CAP_RANGE 17 SETRGB2 18 READ_LOG 19 RESTORE_STANDALONE
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 22 SETRGB3 23 START_CTMU
Unimplemented, Undefined, Unimplemented, Unimplemented,
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
Unimplemented, Unimplemented, Unimplemented, Unimplemented,
},
};
151 changes: 131 additions & 20 deletions pslab-core.X/instruments/multimeter.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,74 @@
#include "../commands.h"
#include "../bus/uart/uart1.h"
#include "../helpers/delay.h"
#include "../registers/converters/adc1.h"
#include "../registers/converters/ctmu.h"
#include "../registers/memory/dma.h"
#include "../bus/uart/uart1.h"
#include "logicanalyzer.h"
#include "../registers/system/pin_manager.h"
#include "../registers/timers/tmr5.h"

#define CHARGE 0
#define DISCHARGE 1

/* Function prototypes */
void ChargeCapacitor(uint8_t charge, uint16_t period);
void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
uint16_t charge_time);
void GetCapacitance_ConfigADC_CTMU_TMR5();

void ChargeCapacitor(uint8_t charge, uint16_t period) {
CAP_OUT_SetDigitalOutput();
CAP_OUT_SetDigital();

if (charge) {
CAP_OUT_SetHigh();
} else {
CAP_OUT_SetLow();
}

// Wait out until the capacitor is charged or discharged
DELAY_us(period);

CAP_OUT_SetLow();
// Switch to high impedance state
CAP_OUT_SetDigitalInput();
CAP_OUT_SetAnalog();
}

void GetCapacitance_InitCTMU_TMR5(uint8_t current_range, uint8_t trim,
uint16_t charge_time) {
CTMU_Initialize();
// Enables edge delay generation
CTMUCON1bits.TGEN = 1;
// Set current level for current source
CTMUICONbits.ITRIM = trim;
// Current range for current source (0.53uA for base current)
CTMUICONbits.IRNG = current_range;

TMR5_Initialize();
TMR5_PrescalerSet(TMR_PRESCALER_64);
TMR5_Period16BitSet(charge_time);
}

void GetCapacitance_ConfigADC_CTMU_TMR5() {
// Begin ADC sampling
ADC1_Enable();
DELAY_us(20);
ADC1_SoftwareTriggerEnable();

CTMU_Enable();
DELAY_us(1000);
// Analog current source output is grounded
CTMUCON1bits.IDISSEN = 1;
DELAY_us(1500);
// Disconnect current source from ground
CTMUCON1bits.IDISSEN = 0;
// Edge 1 has occurred and capacitor is now being charged
CTMUCON2bits.EDG1STAT = 1;

TMR5_Start();
TMR5_WaitForInterruptEvent();
}

response_t MULTIMETER_GetVoltage(void) {

Expand Down Expand Up @@ -38,39 +103,85 @@ response_t MULTIMETER_GetVoltage(void) {
DELAY_us(20);
ADC1_SoftwareTriggerEnable();

while (!ADC1_IsConversionComplete(channel_CTMU));
while (!ADC1_IsConversionComplete());

UART1_WriteInt(ADC1_ConversionResultGet(channel_CTMU));

return SUCCESS;
}

response_t MULTIMETER_GetVoltageSummed(void) {

uint8_t channel = UART1_Read();

ADC1_SetOperationMode(ADC1_12BIT_AVERAGING_MODE, channel, 0);

ADC1_Enable();
DELAY_us(20);
ADC1_AutomaticSamplingEnable();

ADC1_InterruptFlagClear();
while (!_AD1IF);
ADC1_InterruptFlagClear();

while (!ADC1_IsConversionComplete(channel_CTMU));


ADC1_WaitForInterruptEvent();

while (!ADC1_IsConversionComplete());

ADC1_AutomaticSamplingDisable();
ADC1_Disable();

uint16_t voltage_sum =
(ADC1BUF0) + (ADC1BUF1) + (ADC1BUF2) + (ADC1BUF3) +
(ADC1BUF4) + (ADC1BUF5) + (ADC1BUF6) + (ADC1BUF7) +
(ADC1BUF8) + (ADC1BUF9) + (ADC1BUFA) + (ADC1BUFB) +
uint16_t voltage_sum =
(ADC1BUF0) + (ADC1BUF1) + (ADC1BUF2) + (ADC1BUF3) +
(ADC1BUF4) + (ADC1BUF5) + (ADC1BUF6) + (ADC1BUF7) +
(ADC1BUF8) + (ADC1BUF9) + (ADC1BUFA) + (ADC1BUFB) +
(ADC1BUFC) + (ADC1BUFD) + (ADC1BUFE) + (ADC1BUFF);

UART1_WriteInt(voltage_sum);

return SUCCESS;
}
}

response_t MULTIMETER_ChargeCapacitor(void) {

uint8_t charge = UART1_Read();
uint16_t period = UART1_ReadInt();

ChargeCapacitor(charge, period);

return SUCCESS;
}

response_t MULTIMETER_GetCapacitance(void) {

uint8_t current_range = UART1_Read();
uint8_t trim = UART1_Read();
uint16_t charge_time = UART1_ReadInt();

LED_SetLow();

uint16_t reading = 0;

ADC1_SetOperationMode(ADC1_CTMU_MODE, CH0_CHANNEL_CAP, 0);
// Initiate CTMU and TMR5 registers to measure capacitance discharge rate
GetCapacitance_InitCTMU_TMR5(current_range, trim, charge_time);
// Fully discharge the measuring capacitor
ChargeCapacitor(DISCHARGE, 50000);
// Configure ADC, CTMU and TMR5 register bits to start measuring
GetCapacitance_ConfigADC_CTMU_TMR5();
// Stop sampling
ADC1_SoftwareTriggerDisable();
// Pause charging the capacitor
CTMUCON2bits.EDG1STAT = 0;

ADC1_WaitForInterruptEvent();
while (!ADC1_IsConversionComplete());

reading = (ADC1BUF0) & 0xFF;

// Reset modules
CTMU_Initialize();
ADC1_Disable();

UART1_WriteInt(reading);

LED_SetHigh();

return SUCCESS;
}
12 changes: 12 additions & 0 deletions pslab-core.X/instruments/multimeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ extern "C" {
* @return SUCCESS
*/
response_t MULTIMETER_GetVoltageSummed(void);

/**
* @brief Charge or discharge capacitor through a resistor
* @return SUCCESS
*/
response_t MULTIMETER_ChargeCapacitor(void);

/**
* @brief Use CTMU to measure capacitance value and read it
* @return SUCCESS
*/
response_t MULTIMETER_GetCapacitance(void);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit cca2197

Please sign in to comment.