int main(void) { //uses a 12MHz crystal because I messed the fuses ... so let's go slowly and run 1/256th of that speed to save electricity //EDIT: I managed to rechange the fuses to internal 8MHz oscillator, save 3 mA power CLKPR = (1 << CLKPCE) | (1 << CLKPS3); //PORTD is the 7segment control, everybody goes output DDRD = 0xFF; //PORTC bits [5-2] control which 7segment is selected //bits [1-0] are for input of temperature DDRC = 0b11111100; //start with all segments off PORTC = 0x00; //init the ADC InitADC(); //SimpleTestDigit(); //numbers99dot9(); //TestAllSegments(); /*********************************************************************/ cli(); // quiet for just a moment ShutOffADC(); // prepare ADC for sleep PRR = (1<<PRTWI) | (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRTIM2) | (1<<PRSPI) | (1<<PRADC) | (1<<PRUSART0); /*********************************************************************/ ReadTemp(); }
int main(void) { uint16_t tempSensor, battery; uint8_t capPushA, capPushB; uint8_t it=1; InitWDT(); InitCLOCK(); InitUART(); InitLED(); InitADC(); InitBuzzer(); InitCapPush(); EnableInterrupts(); while (1) { it--; if(it == 0) { it = 4; capPushA = senseCapPushA(); capPushB = senseCapPushB(); tempSensor = ReadTemp(); battery = ReadBattery(); MainLoop(capPushA, capPushB, tempSensor, battery); } SetupWDTToWakeUpCPU(2); // Wake up in 16 mS Sleep(); } return 0; }
void TestMgr::TestTempReading(void) { bool doExit = false; int pwmDutyCycle = 5; int testVal = 0; // Restart PID loop timer m_PidLoopRunTimer.ResetStartTimestamp(); while (!doExit) { if (m_AbortFlag) { doExit = true; break; } // Get the elapsed time since the PID loop was run last m_PidLoopElapsedTime = m_PidLoopRunTimer.GetElapsedMillisec(); // Run the PID loop at the specified interval if (m_PidLoopElapsedTime > (100-5)) { // Restart PID loop timer m_PidLoopRunTimer.ResetStartTimestamp(); // Read the plate temperature bool gotTemp = ReadTemp(); if (gotTemp) { uint64_t elapsedTime = m_RunTimer.GetElapsedMillisec(); std::cout << "[" << std::dec << std::setfill('0') << std::setw(8) << elapsedTime << "] "; std::cout << "Temp: "; std::cout << std::fixed << std::setw(10) << std::setprecision(5) << m_PlateTempDegreesC; std::cout << std::endl; } #if 1 m_IoHub.CoolerPwmUpdate(pwmDutyCycle); pwmDutyCycle += 5; if (pwmDutyCycle > 100) { pwmDutyCycle = 0; } if (testVal == 1) { m_IoHub.TurnOffCooler(); m_IoHub.TurnOffHeater(); } else { m_IoHub.TurnOnCooler(); m_IoHub.TurnOnHeater(); } testVal ^= 1; #endif } QThread::msleep(10); } }
void main() { Lcd_Init(); //TFT Screen Init LCD_Clear(BLACK); //fill screed with black color BACK_COLOR=BLACK; POINT_COLOR=GREEN; DispStr(0,0,"Screen Initialized!"); DispStr(0,1,"MCU:12C5A60S2,Speed:1MIPS"); //Display MCU Information if(ReadTemp()!=0) { DispStr(0,2,"DS18B20 Ready!"); } else { DispStr(0,2,"Ds18b20 Error."); } UART_Init(); //UART Init DispStr(0,3,"UART Ready!"); ITR_Init(); //Interrupt Init DispStr(0,4,"Interrupt Onset."); DispStr(0,5,"Connecting"); AT_CMD(); //ESP8266 Init DispStr(0,6,"Connectted!"); DispStr(0,12,"Real:"); while(1) { TempVal=ReadTemp(); //Read Celsius DispTemp(TempVal); //Display Celsius Value if(Cnt>30) { Send_TempVal(TempVal); //Send Temprature to UART Cnt=0; } } }
static void SortTemp(int low, int high) { TempStruct tmpMid, tmpSwap; int lowMid, highMid, nLen; nLen = high - low + 1; if (nLen > TEMP_BUFFER_SIZE) { lowMid = low; highMid = high; tmpMid = GetTemp((low + high) / 2); while (lowMid <= highMid) { while (lowMid < high && GetTemp(lowMid) < tmpMid) { lowMid ++; } while (low < highMid && tmpMid < GetTemp(highMid)) { highMid --; } if (lowMid <= highMid) { tmpSwap = GetTemp(lowMid); SetTemp(lowMid, GetTemp(highMid)); SetTemp(highMid, tmpSwap); lowMid ++; highMid --; } } if (low < highMid) { SortTemp(low, highMid); } if (lowMid < high) { SortTemp(lowMid, high); } } else { ReadTemp(low, MakeBook2.TempBuffer, nLen); if (nLen > 1) { SortMem(0, nLen - 1); } WriteTemp(low, MakeBook2.TempBuffer, nLen); } }
void TestMgr::RunWithoutPid(void) { bool doExit = false; while (!doExit) { if (m_AbortFlag) { doExit = true; break; } bool gotAcVoltage = m_FlukeSerial.ReadAcVoltage(m_AcVoltage); bool gotFreq = true; // m_FlukeSerial.ReadFreq(m_Freq); bool gotTemp = ReadTemp(); m_Freq = 0.0; if (gotAcVoltage && gotFreq && gotTemp) { uint64_t elapsedTime = m_RunTimer.GetElapsedMillisec(); std::cout << "[" << std::dec << std::setfill('0') << std::setw(8) << elapsedTime << "] "; std::cout << "Temp: "; std::cout << std::fixed << std::setw(10) << std::setprecision(5) << m_PlateTempDegreesC; std::cout << " , AC Voltage: "; std::cout << std::fixed << std::setw(11) << std::setprecision(6) << m_AcVoltage; std::cout << " , Freq: "; std::cout << std::fixed << std::setw(11) << std::setprecision(6) << m_Freq; std::cout << std::endl; VoltageLogWriteData(elapsedTime); } // Run the main loop at a 10ms rate QThread::msleep(10); } }
inline TempStruct GetTemp(int nPtr) { TempStruct tmp; ReadTemp(nPtr, &tmp, 1); return tmp; }
void TestMgr::RunWithPid(void) { bool doExit = false; float pwmDutyCycle = 0.0; // Initialize the state machine that runs the worker bees InitStateMachine(); // Initialize the PID Loop // PidConstants_t pidConstants; // m_PidRange.ChangeRangeId(0); // m_PidRange.GetRangeConstants(pidConstants); // m_PidLoop.InitLoopUsingConstants(pidConstants, m_PidRange.GetRangeEndTemp()); // p->m_PidLoop.SetLoopOnOff(PID_LOOP_ON); while (!doExit) { if (m_AbortFlag) { doExit = true; break; } // Get the elapsed time since the PID loop was run last m_PidLoopElapsedTime = m_PidLoopRunTimer.GetElapsedMillisec(); // Run the PID loop at the specified interval if (m_PidLoopElapsedTime >= (m_PidLoopUpdateTimeInMs - 10)) { // Restart PID loop timer m_PidLoopRunTimer.ResetStartTimestamp(); // Safety check if (m_PlateTempDegreesC > 100.0) { m_Sub20.HeaterPwmUpdate(0.0); m_Sub20.TurnOffHeater(); std::cout << "Temperature exceeds max allowed. Turning off heater and exiting." << std::endl; doExit = true; } // Read the plate temperature if (ReadTemp()) { // Run the state machine state_machine_run(&m_StateMachine, (uint32_t *)this, (uint32_t *)NULL); // Re-read the elapsed time since loop was run // uint64_t elapsedTimeInMs = m_PidLoopRunTimer.GetElapsedMillisec(); // Run the PID loop pwmDutyCycle = m_PidLoop.Compute(m_PlateTempDegreesC, (m_PidLoopElapsedTime / ((float)1000.0))); if (m_PidLoop.IsLoopOn()) { // Update either the heater or the cooler PWM PidDirection_t pidDirection = m_PidLoop.GetDirection(); assert (pidDirection == PID_DIR_DIRECT || pidDirection == PID_DIR_REVERSE); if (pidDirection == PID_DIR_DIRECT) { m_Sub20.HeaterPwmUpdate(pwmDutyCycle); } else { m_Sub20.CoolerPwmUpdate(pwmDutyCycle); } } } } // Get the elapsed time since the last voltage was logged m_LogVoltageElapsedTime = m_LogVoltageTimer.GetElapsedMillisec(); // Log voltage at the specified interval. if (m_LogVoltageElapsedTime >= (m_VoltageLogUpdateTimeInMs - 10)) { // Restart Log Voltage timer m_LogVoltageTimer.ResetStartTimestamp(); if (0 && m_FlukeSerial.ReadAcVoltage(m_AcVoltage)) { std::cout << "[" << std::dec << std::setfill('0') << std::setw(6) << m_LogVoltageElapsedTime << "] "; std::cout << "DC Voltage: "; std::cout << std::fixed << std::setw(11) << std::setprecision(6) << m_AcVoltage << std::endl; VoltageLogWriteData(m_RunTimer.GetElapsedMillisec()); } } // Run the main loop at a 10ms rate QThread::msleep(10); } }
void TestMgr::CalibrateTempToPwm(void) { // int responseBytes; // bool outOfOrderFlag = false; UtilTimer runTimer; UtilTimer startAveragingTimer; uint16_t startPwm = 10; uint16_t stopPwm = 100; uint16_t testPwm = startPwm; bool doExit = false; std::vector<float> tempVec; printf("Temp to PWM Calibrate Thread Start\n"); // Open file to store calibration data std::ofstream tempToPwmStream; std::stringstream fileNameSs; fileNameSs << "C:/ES/VocPhase1Test/PidData/TempToPwm.cpp"; std::string fileName = fileNameSs.str(); tempToPwmStream.open(fileName.c_str(), (std::ios::trunc | std::ios::out)); if (tempToPwmStream.is_open()) { tempToPwmStream << "struct TempToPwm_t {" << std::endl; tempToPwmStream << "\tfloat temp;" << std::endl; tempToPwmStream << "\tuint8_t pwm;" << std::endl; tempToPwmStream << "};" << std::endl; tempToPwmStream << std::endl; tempToPwmStream << "TempToPwm_t tempToPwm[] = {" << std::endl; } m_Sub20.TurnOffCooler(); runTimer.ResetCountdownTimer(20000); startAveragingTimer.ResetCountdownTimer(10000); while (!doExit) { if (m_AbortFlag) { doExit = true; break; } if (startAveragingTimer.IsCountdownTimerExpired()) { if (ReadTemp()) { printf("\n\nTempToPwm: temperature(%d): %f, mean: %f, stdev: %f\n", testPwm, m_PlateTempDegreesC, m_TempMean, m_TempStdev); tempVec.push_back(m_PlateTempDegreesC); } //if (m_PlateTempDegreesC >= 50.0) { // m_Sub20.TurnOffCooler(); //} } if (runTimer.IsCountdownTimerExpired() && (m_TempStdev < 0.15)) { float tempSum = std::accumulate(tempVec.begin(), tempVec.end(), 0.0); float tempMean = tempSum / tempVec.size(); unsigned int pwm = testPwm; if (tempToPwmStream.is_open()) { tempToPwmStream << "\t{ " << std::setprecision(3) << tempMean << ", " << pwm << " }," << std::endl; } printf("\n\nTempToPwm: Avg temperature for pwm %d is %f, mean: %f, stdev: %f\n", testPwm, tempMean, m_TempMean, m_TempStdev); printf("TempToPwm: Process new pwm: %d\n", testPwm); QThread::msleep(1000); // Set new pwm m_Sub20.HeaterPwmUpdate(testPwm); // Update for next time testPwm++; // Check if we are at the end of run if (testPwm >= stopPwm) { if (tempToPwmStream.is_open()) { tempToPwmStream << "};" << std::endl; tempToPwmStream.close(); } doExit = true; break; } runTimer.ResetCountdownTimer(20000); startAveragingTimer.ResetCountdownTimer(5000); tempVec.clear(); } QThread::msleep(1000); } printf("Calibrate Thread Exit\n"); QThread::msleep(100); // Signal to any slots listening DoneSignal(); }