diff --git a/src/ADC/ADC.c b/src/ADC/ADC.c index dc8eda0..86d993b 100644 --- a/src/ADC/ADC.c +++ b/src/ADC/ADC.c @@ -13,11 +13,13 @@ #include "RegEdit.h" #include "avr/io.h" +#define MAX_PIN_NUM 7 + static uint8_t porta_out; static uint8_t porta_dir; static bool IsInvalidPin(uint8_t pin_num){ - if(pin_num > 7){ + if(pin_num > MAX_PIN_NUM){ return true; } return false; @@ -97,6 +99,12 @@ void ADC_Disable() } +void ADC_SetPin(uint8_t pin_num) +{ + RegEdit_ClearRegister((void *) &ADC0.MUXPOS); + RegEdit_SetNum((void *) &ADC0.MUXPOS, pin_num); +} + uint16_t ADC_ReadValue_Impl(uint8_t pin_num) { RegEdit_SetNum((void *) &ADC0.COMMAND, ADC_STCONV_bm); diff --git a/src/ADC/ADC.h b/src/ADC/ADC.h index b3a7118..d5578d6 100644 --- a/src/ADC/ADC.h +++ b/src/ADC/ADC.h @@ -65,4 +65,11 @@ extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); void ADC_Setup(void); +/** + * @brief Sets the pin used in the MUX for ADC0. + * + * @param pin_num The number of the pin in Port A. + */ +void ADC_SetPin(uint8_t pin_num); + #endif //ADC_H diff --git a/tests/ADC/test_ADC.cpp b/tests/ADC/test_ADC.cpp index e6316bf..494c467 100644 --- a/tests/ADC/test_ADC.cpp +++ b/tests/ADC/test_ADC.cpp @@ -182,6 +182,19 @@ TEST(test_ADC, ADC_DisablePasses) ADC_Disable(); } +TEST(test_ADC, ADC_SetPinSetsRightRegisters) +{ + //It clears existing MUXPOS register values. + mock().expectOneCall("RegEdit_ClearRegister") + .withPointerParameter("reg", (void *) &ADC0.MUXPOS); + + //It Correctly sets the pin number. + mock().expectOneCall("RegEdit_SetNum") + .withPointerParameter("reg", (void *) &ADC0.MUXPOS) + .withUnsignedIntParameter("num", 4); + + ADC_SetPin(4); +} static uint16_t ADC_ReadValueFake(uint8_t pin_num)