/** * @Function ADCIntHandler * @param None * @return None * @brief Interrupt Handler for A/D. Reads all used pins into buffer. * @note This function is not to be called by the user * @author Max Dunne, 2013.08.25 */ void __ISR(_ADC_VECTOR, ipl1) ADCIntHandler(void) { unsigned char CurPin = 0; INTClearFlag(INT_AD1); for (CurPin = 0; CurPin <= PinCount; CurPin++) { ADValues[CurPin] = ReadADC10(CurPin); //read in new set of values } //calculate new filtered battery voltage Filt_BatVoltage = (Filt_BatVoltage * KEEP_FILT + AD_ReadADPin(BAT_VOLTAGE_MONITOR) * ADD_FILT) >> SHIFT_FILT; SampleCount++; if (SampleCount > PointsPerBatSamples) {//if sample time has passed PrevFilt_BatVoltage = CurFilt_BatVoltage; CurFilt_BatVoltage = Filt_BatVoltage; SampleCount = 0; //check for battery undervoltage check if ((CurFilt_BatVoltage <= BAT_VOLTAGE_LOCKOUT) && (PrevFilt_BatVoltage <= BAT_VOLTAGE_LOCKOUT) && (AD_ReadADPin(BAT_VOLTAGE_MONITOR) > BAT_VOLTAGE_NO_BAT)) { BOARD_End(); while (1) { printf("Battery is undervoltage with reading %d, Stack is inoperable until charging\r\n", AD_ReadADPin(BAT_VOLTAGE_MONITOR)); while (!IsTransmitEmpty()); } } } //if pins are changed add pins if (PinsToAdd | PinsToRemove) { AD_SetPins(); } ADNewData = TRUE; }
/** * @Function AD_Init * @param None * @return SUCCESS or ERROR * @brief Initializes the A/D subsystem and enable battery voltage monitoring. * @author Max Dunne, 2013.08.10 */ char AD_Init(void) { if (ADActive) { return ERROR; } int pin = 0; //ensure that the battery monitor is active ActivePins = BAT_VOLTAGE_MONITOR; ADActive = TRUE; AD_SetPins(); for (pin = 0; pin < NUM_AD_PINS; pin++) { ADValues[pin] = -1; } INTEnable(INT_AD1, INT_DISABLED); INTClearFlag(INT_AD1); INTSetVectorPriority(INT_ADC_VECTOR, 1); INTSetVectorSubPriority(INT_ADC_VECTOR, 3); INTEnable(INT_AD1, INT_ENABLED); EnableADC10(); ADNewData = FALSE; //wait for first reading to ensure battery monitor starts in the right spot while (!AD_IsNewDataReady()) { #ifdef AD_DEBUG_VERBOSE PutChar('.'); #endif } //set the first values for the battery monitor filter Filt_BatVoltage = AD_ReadADPin(BAT_VOLTAGE_MONITOR); CurFilt_BatVoltage = Filt_BatVoltage; PrevFilt_BatVoltage = Filt_BatVoltage; return SUCCESS; }
static int ConvertDC(int speed) { int x; unsigned int batRead; float inVolt; float mult; batRead = AD_ReadADPin(BAT_VOLTAGE); inVolt = ((float) batRead) / 1023.0 * 33.0; mult = 9.90 / inVolt; // Cap it at 1000 return (int) (((speed * mult) > 1000.0) ? 1000.0 : (speed * mult)); }
int main(void) { unsigned int wait = 0; int readcount = 0; unsigned int CurPin = 0; unsigned int PinListing = 0; char FunctionResponse = 0; char TestFailed = FALSE; //SERIAL_Init(); //INTEnableSystemMultiVectoredInt(); BOARD_Init(); mJTAGPortEnable(0); printf("\r\nUno A/D Test Harness\r\nThis will initialize all A/D pins and read them %d times\r\n", TIMES_TO_READ); //printf("Value of pcfg before test: %X\r\n", AD1PCFG); // while(!IsTransmitEmpty()); //AD_Init(BAT_VOLTAGE); //AD_Init(); printf("Testing functionality before initialization\r\n"); /*adding pins individually */ printf("AD_AddPins on each pin indvidually which results in failure: "); for (CurPin = 1; CurPin < ALLADPINS; CurPin <<= 1) { FunctionResponse = AD_AddPins(CurPin); if (FunctionResponse != ERROR) { TestFailed = TRUE; break; } } if (TestFailed) { printf("FAIL\r\n"); } else { printf("PASSED\r\n"); } TestFailed = FALSE; /*removing pins individually*/ printf("AD_RemovePins on each pin indvidually which results in failure: "); for (CurPin = 1; CurPin < ALLADPINS; CurPin <<= 1) { FunctionResponse = AD_RemovePins(CurPin); if (FunctionResponse != ERROR) { TestFailed = TRUE; break; } } if (TestFailed) { printf("FAIL\r\n"); } else { printf("PASSED\r\n"); } TestFailed = FALSE; /*listing pins while inactive*/ printf("AD_ActivePins which should return 0: "); PinListing = AD_ActivePins(); if (PinListing != 0x0) { printf("FAILED\r\n"); } else { printf("PASSED\r\n"); } // /*calling ned when inactive*/ // printf("AD_End which should fail: "); // FunctionResponse = AD_End(); // if (FunctionResponse != ERROR) { // printf("FAILED\r\n"); // } else { // printf("PASSED\r\n"); // } /*activating module*/ printf("initializing using AD_Init: "); FunctionResponse = AD_Init(); if (FunctionResponse != SUCCESS) { printf("FAILED\r\n"); } else { printf("PASSED\r\n"); } /*attempting to reactivate*/ printf("initializing using AD_Init again returns error: "); FunctionResponse = AD_Init(); if (FunctionResponse != ERROR) { printf("FAILED\r\n"); } else { printf("PASSED\r\n"); } printf("Testing Functionality after initialization\r\n"); /*active pins after activation only has battery*/ printf("Ad_ActivePins should only return BAT_VOLTAGE: "); PinListing = AD_ActivePins(); if (PinListing == BAT_VOLTAGE) { printf("PASSED\r\n"); } else { printf("FAILED\r\n"); } /*each pin added should succeed*/ printf("Adding each pin using AD_AddPins indivdually: "); for (CurPin = 1; CurPin < ALLADMINUSBATT; CurPin <<= 1) { PinListing = AD_ActivePins(); FunctionResponse = AD_AddPins(CurPin); if (FunctionResponse != SUCCESS) { TestFailed = TRUE; break; } while (AD_ActivePins() != (PinListing | CurPin)); } if (TestFailed) { printf("FAIL\r\n"); } else { printf("PASSED\r\n"); } /*removing each pin should succeed */ printf("Removing each pin using AD_RemovePins indivdually: "); for (CurPin = 1; CurPin < ALLADMINUSBATT; CurPin <<= 1) { PinListing = AD_ActivePins(); FunctionResponse = AD_AddPins(CurPin); if (FunctionResponse != SUCCESS) { TestFailed = TRUE; break; } while (AD_ActivePins() != (PinListing | CurPin)); } if (TestFailed) { printf("FAIL: %X\r\n", 0xFEED); } else { printf("PASSED\r\n"); } while (1); printf("We will now add the odd pins and wait for them to be activated"); AD_AddPins(ODD_ACTIVE); while (!(AD_ActivePins() & ODD_ACTIVE)) { if (IsTransmitEmpty()) { printf("%X\r\n", AD_ActivePins()); } } printf("The Odd pins are now active as shown by Active pins: %X\r\n", AD_ActivePins()); printf("We will now enable the even pins and wait for them to be activated"); AD_AddPins(EVEN_ACTIVE); while (!(AD_ActivePins() & EVEN_ACTIVE)); printf("The Even pins are now active as shown by Active pins: %X\r\n", AD_ActivePins()); char numtoread = NUM_AD_PINS; unsigned char cur = 0; DELAY(400000) while (readcount <= TIMES_TO_READ) { DELAY(100000); printf("\r\n"); for (cur = 0; cur < numtoread; cur++) { printf("%d\t", AD_ReadADPin(1 << cur)); } printf("\r\n"); readcount++; } printf("Done Reading Them\r\n"); AD_End(); printf("Value of pcfg after test: %X", AD1PCFG); return 0; }
/** * @Function R2_BJT2_BatteryVoltage(void) * @param None. * @return a 10-bit value corresponding to the current voltage of the roach * @brief returns a 10:1 scaled value of the roach battery level * @author Max Dunne, 2013.07.12 */ unsigned int R2_BJT2_BatteryVoltage(void) { return AD_ReadADPin(BAT_VOLTAGE); }