Added code to pass the ADC timeout feature

This commit is contained in:
Jake Goodwin 2025-02-23 10:32:22 -08:00
parent 58ab3c12eb
commit c07fc415bf
2 changed files with 25 additions and 17 deletions

View File

@ -85,25 +85,29 @@ void ADC_Disable() {
RegEdit_ClearBit((void *)&ADCSRA, ADEN); RegEdit_ClearBit((void *)&ADCSRA, ADEN);
} }
uint16_t ADC_ReadValue_Impl(uint8_t adc_chan) { uint16_t ADC_ReadValue_Impl(uint8_t adc_chan) {
// RegEdit_SetNum((void *)&ADC0.COMMAND, ADC_STCONV_bm); if (IsInvalidChannel(adc_chan)) {
return UINT16_MAX;
/* Wait until ADC conversion done */
/*
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
;
} }
*/
/* Clear the interrupt flag by writing 1: */ // start conversion.
// ADC0.INTFLAGS = ADC_RESRDY_bm; RegEdit_SetBit((void *)&ADCSRA, ADSC);
// uint16_t adc_val = (uint16_t)ADC0.RES; /* Wait until ADC conversion done or it times out. */
uint16_t adc_val = 0; uint8_t cycles = 1;
adc_val = adc_val >> 5; while (1) {
return adc_val; if (!RegEdit_IsBitSet((void *)&ADCSRA, ADSC)) {
break;
}
// if It times out return an invalid value for a 12bit ADC.
else if (cycles >= ADC_WAIT_TIMEOUT) {
return UINT16_MAX;
}
cycles++;
}
uint8_t val = ADC;
return val;
} }
// Set the default for the function pointer. // Set the default for the function pointer.

View File

@ -13,6 +13,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
/**
* @brief The number of loop cycles it waits for ADC conversion before
* timing out.
*/
#define ADC_WAIT_TIMEOUT 16
/** /**
* @brief Initializes the AVR hardware in order to accept * @brief Initializes the AVR hardware in order to accept
* Input for ADC usage. * Input for ADC usage.
@ -27,7 +33,6 @@
*/ */
void ADC_Init(uint8_t adc_chan); void ADC_Init(uint8_t adc_chan);
/** /**
* @brief Returns the pin number for the adc channel or 255 on an error. * @brief Returns the pin number for the adc channel or 255 on an error.
* @param The ADC channel. * @param The ADC channel.
@ -65,5 +70,4 @@ extern uint16_t (*ADC_ReadValue)(uint8_t adc_chan);
*/ */
void ADC_Setup(void); void ADC_Setup(void);
#endif // ADC_H #endif // ADC_H