diff --git a/src/load/load.c b/src/load/load.c index 6343875..66787ff 100644 --- a/src/load/load.c +++ b/src/load/load.c @@ -24,6 +24,20 @@ +//These two arrays allow us to track the A and B ports to know when +//one has been disabled before. +static bool porta_disabled[8] = {0}; +static bool portb_disabled[8] = {0}; + + +static bool valid_load(uint16_t val) +{ + if(val > 527 && val < 1000) { + return true; + } + return false; +} + void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) { ADC_Init(adc_pin); @@ -32,12 +46,13 @@ void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) uint16_t val = ADC_ReadValue(adc_pin); ADC_Disable(); - if(val > 527 && val < 1000){ + if(valid_load(val) && !porta_disabled[adc_pin]){ RegEdit_SetBit((void *) &PORTA.DIR, out_pin); RegEdit_SetBit((void *) &PORTA.OUT, out_pin); } else{ RegEdit_ClearBit((void *) &PORTA.OUT, out_pin); + porta_disabled[adc_pin] = true; } } @@ -49,11 +64,12 @@ void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) uint16_t val = ADC_ReadValue(adc_pin); ADC_Disable(); - if(val > 527 && val < 1000){ + if(valid_load(val) && !portb_disabled[adc_pin]){ RegEdit_SetBit((void *) &PORTB.DIR, out_pin); RegEdit_SetBit((void *) &PORTB.OUT, out_pin); } else{ RegEdit_ClearBit((void *) &PORTB.OUT, out_pin); + portb_disabled[adc_pin] = true; } }