Prevent ADC rollover

This commit is contained in:
Ben V. Brown
2024-05-25 21:25:09 +10:00
parent c6dcd85c3a
commit 307fc2f404
4 changed files with 11 additions and 9 deletions

View File

@@ -33,7 +33,6 @@ void read_adc_fifo(void) {
if (pending_readings != 8) {
MSG((char *)"Discarding out of sync adc %d\r\n", pending_readings);
} else {
MSG((char *)"FIFO Count %d\r\n", pending_readings);
while (pending_readings) {
pending_readings--;
uint32_t raw_reading = ADC_Read_FIFO();
@@ -45,11 +44,6 @@ void read_adc_fifo(void) {
ADC_Temp.update(parsed.value << 2);
break;
case TIP_TEMP_ADC_CHANNEL: {
int32_t avg = ADC_Tip.average();
int32_t delta = (int32_t)parsed.value - (avg >> 2);
if (delta > 4000) {
MSG((char *)"ADC Chan %d -> %d -> %d\r\n", pending_readings, parsed.value, delta);
}
ADC_Tip.update(parsed.value << 2);
} break;
case VIN_ADC_CHANNEL:
@@ -60,7 +54,6 @@ void read_adc_fifo(void) {
}
}
}
ADC_FIFO_Clear();
// unblock the PID controller thread
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
@@ -88,6 +81,7 @@ void timer0_comp0_callback(void) {
// Timer 0 is used to co-ordinate the ADC and the output PWM
void timer0_comp1_callback(void) {
ADC_FIFO_Clear();
ADC_Start();
TIMER_ClearIntStatus(TIMER_CH0, TIMER_COMP_ID_1);
}

View File

@@ -271,7 +271,7 @@ typedef struct {
typedef struct {
int8_t posChan; /*!< Positive channel */
int8_t negChan; /*!< Negative channel */
uint16_t value; /*!< ADC value */
uint32_t value; /*!< ADC value */
// float volt; /*!< ADC voltage result */
} ADC_Result_Type;

View File

@@ -535,6 +535,10 @@ void ADC_Parse_Result(uint32_t *orgVal, uint32_t len, ADC_Result_Type *result) {
} else if ((dataType == ADC_DATA_WIDTH_16_WITH_128_AVERAGE) || (dataType == ADC_DATA_WIDTH_16_WITH_256_AVERAGE)) {
result[i].value = (unsigned int)((orgVal[i] & 0xffff) / coe);
}
// Saturate at 16 bits
if (result[i].value > 0xFFFF) {
result[i].value = 0xFFFF;
}
}
} else {
for (i = 0; i < len; i++) {
@@ -555,6 +559,10 @@ void ADC_Parse_Result(uint32_t *orgVal, uint32_t len, ADC_Result_Type *result) {
} else if ((dataType == ADC_DATA_WIDTH_16_WITH_128_AVERAGE) || (dataType == ADC_DATA_WIDTH_16_WITH_256_AVERAGE)) {
result[i].value = (unsigned int)((orgVal[i] & 0xffff) / coe);
}
// Saturate at 16 bits
if (result[i].value > 0xFFFF) {
result[i].value = 0xFFFF;
}
}
}
}

View File

@@ -114,7 +114,7 @@
#define ANIMATION_LOOP 1 // 0: off 1: on
#define ANIMATION_SPEED settingOffSpeed_t::MEDIUM
#define OP_AMP_Rf_Pinecil 680 * 1000 // 700 Kilo-ohms -> From schematic, R1
#define OP_AMP_Rf_Pinecil 680 * 1000 // 680 Kilo-ohms -> From schematic, R1
#define OP_AMP_Rin_Pinecil 2370 // 2.37 Kilo-ohms -> From schematic, R2
#define OP_AMP_GAIN_STAGE_PINECIL (1 + (OP_AMP_Rf_Pinecil / OP_AMP_Rin_Pinecil))