int main(void) { initLEDs(); initButtons(); while (1) { if (isButtonPressed(SB2)) { turnOffLEDs(); } else { checkLEDsSettingButtons(); } _delay_ms(50); } return 0; }
void main(void) { DisableInterrupts; initializations(); EnableInterrupts; turnOffLEDs(); // reset game on startup resetGame(); TC7 = 15000; //set up TIM TC7 to generate 0.1 ms interrupt rate for(;;) { displayBoard(); if (interuptFlag) { tickCounter++; ticksSinceLastEvolution++; if (ticksSinceLastEvolution >= TICKS_BETWEEN_EVOLUTIONS) { ticksSinceLastEvolution = 0; evolveFlag = 1; } } if (evolveFlag) { evolveFlag = 0; evolve(); } if (boardsAreSame == 1) { resetGame(); } // check to see if the user would like to reset the game (presses right push button) if (rghtpb == 1) { rghtpb = 0; resetGame(); } _FEED_COP(); /* feeds the watchdog timer */ } /* loop forever */ /* make sure that you never leave main */ }
void manage_LED() { if(gVoltState != V_STANDBY) { turnOffLEDs(); return; } else LED2 = LED_ON; // LED control if(gFlags.spiTimeout || gFlags.canTxError || gFlags.canRxError || gFlags.ltcConversionFail || gFlags.openWire || !gFlags.master_online) LED4 = LED_ON; else LED4 = LED_OFF; if(gFlags.slaveDoubleID || gFlags.badSlaveId || (gFlags.balancingActive && !gFlags.disch_circuit_plugged)) LED3 = LED_ON; else LED3 = LED_OFF; }
void manage_voltages() { static uint32 startTime; static uint8 voltMeasureCount = 0; uint8 i; uint8 txError = 0; uint8 openWireCell = 0; int16 voltages[NB_CELL]; // Receive voltage values int32 val; // Used for manipulations // -------------- Mesure des tensions ----------------- // voltTimeout = 1 : le délai entre deux mesures est passé // On vérifie que le WDT n'est pas dépassé (LTC toujours en mode "measure") // // -> si gVoltState = MEASURE_START (début du cycle) // On vérifie la communication avec la LTC (chaque 10 mesures) // On lance la mesure des tensions // // -> si gVoltState = MEASURE_WAIT et les conversions sont terminées // On récupère les données // On fait la moyenne et on envoie tout ça au master // On vérifie si il y a une connexion ouverte // On met voltTimetout et gVoltState à V_STANDBY if (gVoltState != V_STANDBY) { #ifdef SPI_ENABLE if(ltcConfigurationOK(&g_ltcConfig)) // Verify the configurations { if(gVoltState == MEASURE_START) // Début du cycle de mesure de tensions { turnOffLEDs(); // Turn off LED to decrease input current and prevent measure errors voltMeasureCount++; // Nombre de mesures prises depuis le dernier envoi // Each 10 measures, clear the LTC measure register if(voltMeasureCount % 10 == 0) // Fast Self Check resets the ltc register to 0xFF so we make sure not to measure the registers are well updated ltcClearADC(); // ------------------ On lance la mesure des tensions (conversions) ------------------- ltcStartVoltageMeasure(); gVoltState = MEASURE_WAIT; // Maintenant, on doit attendre que les conversions soient terminées startTime = gElapsedTime; // Time reference } else if(gVoltState == SELF_TEST_START) { ltcSelfCheck(); // Start the LTC Self Test procedure } else if(gVoltState == DIAGNOSTIC_START) { ltcDiagnostic(); } else if(gVoltState == MEASURE_WAIT || gVoltState == SELF_TEST_WAIT || gVoltState == DIAGNOSTIC_WAIT) // Attente de la fin des conversions terminées { // TODO : vérifier si on est mieux d'attendre dans l'interruption de 16 kHz ce // qui nous éviterais de faire les checks sur les régistres à chaque fois if(ltcConversionFinished()) // Si les conversions sont terminées { if(gVoltState == SELF_TEST_WAIT) // Currently performing a self check { // Finish the self check by recalling the function and storing it's return value (1 = error) gFlags.ltcConversionFail = ltcSelfCheck(); } else if(gVoltState == DIAGNOSTIC_WAIT) { // Finish the diagnostic by recalling the function and storing it's return value (1 = error) gFlags.ltcConversionFail = ltcDiagnostic(); } else if(gVoltState == MEASURE_WAIT) { // -------------------- On récupère les données ----------------------- txError = ltcReadCellVoltage(voltages); // On récupère les données // Arithmetic average on the data acquired since the last transmission of the voltage to the master // V = (V1 + V2 + V3 + ... + Vn)/n n = number of measure taken since the last voltage message to the master for(i = 0; i < NB_CELL; i++) { val = (int32)voltages[i] + ((voltMeasureCount - 1) * (int32)gVoltages[i]); gVoltages[i] = val / voltMeasureCount; //gVoltages[i] = voltages[i]; } #ifdef CAN_ENABLE // ------------------- Envoie des tensions vers le maitre si recues sans erreur du LTC6803 --------------- if(!txError && voltMeasureCount >= NB_VOLT_SAMPLES) { gFlags.canTxError = CAN_SendVoltages(gSlaveID, gVoltages); //TODO: on ne fait rien avec ca... voltMeasureCount = 0; } #endif //CAN_ENABLE if(voltMeasureCount % 10 == 0) // Chaque 10 mesures, on démarre la vérification de connexions ouvertes { // Detection des connexions ouvertes openWireCell = ltcVerifyOpenWire(); if(openWireCell != 0){ gFlags.openWire = 1; CAN_SendDTC(11, 1, gSlaveID); // Sends a DTC message with possible additionnal data } } gVoltState = V_STANDBY; // On retourne à la première étape //gFlags.voltTimeout = 0; // On attent le prochain délai txError = 0; gFlags.ltcConversionFail = 0; } else gVoltState = V_STANDBY; } else { // If the conversions are not finished after 1 second, the LTC chip is probably not plugged to the cells if(gElapsedTime - startTime > 10) { gFlags.ltcConversionFail = 1; gVoltState = V_STANDBY; } } } } // if(ltcConfigurationOK(<cConfig)) #endif // SPI_ENABLE } }