/*! Manages motor and heater based on measured temperature: o If temp is too low, don't start the motor o Adjust the heater power to keep the temperature at the target */ void Heater::manage_temperature() { int output, dt; unsigned long time; //make sure we know what our temp is. current_temperature = get_current_temperature(); // ignoring millis rollover for now time = millis(); dt = time - temp_prev_time; if (dt > TEMP_UPDATE_INTERVAL) { temp_prev_time = time; output = temp_update(dt); digitalWrite(DEBUG_PIN, (output > 0)?HIGH:LOW); analogWrite(outputPin, output); } }
/* ** ################################################################### ** ** This file was created by Processor Expert 10.5 [05.21] ** for the Freescale Kinetis series of microcontrollers. ** ** ################################################################### */ void calibrateParams(void) { adc16_chn_config_t adcChnConfig; #if FSL_FEATURE_ADC16_HAS_HW_AVERAGE adc16_hw_average_config_t userHwAverageConfig; #endif pmc_bandgap_buffer_config_t pmcBandgapConfig = { .enable = true, #if FSL_FEATURE_PMC_HAS_BGEN .enableInLowPower = false, #endif #if FSL_FEATURE_PMC_HAS_BGBDS .drive = kPmcBandgapBufferDriveLow, #endif }; uint32_t bandgapValue = 0; // ADC value of BANDGAP uint32_t vdd = 0; // VDD in mV #if FSL_FEATURE_ADC16_HAS_CALIBRATION // Auto calibration adc16_calibration_param_t adcCalibraitionParam; ADC16_DRV_GetAutoCalibrationParam(ADC16_INSTANCE, &adcCalibraitionParam); ADC16_DRV_SetCalibrationParam(ADC16_INSTANCE, &adcCalibraitionParam); #endif // FSL_FEATURE_ADC16_HAS_CALIBRATION. // Enable BANDGAP reference voltage PMC_HAL_BandgapBufferConfig(PMC_BASE_PTR, &pmcBandgapConfig); #if FSL_FEATURE_ADC16_HAS_HW_AVERAGE // Use hardware average to increase stability of the measurement. userHwAverageConfig.hwAverageEnable = true; userHwAverageConfig.hwAverageCountMode = kAdc16HwAverageCountOf32; ADC16_DRV_ConfigHwAverage(ADC16_INSTANCE, &userHwAverageConfig); #endif // FSL_FEATURE_ADC16_HAS_HW_AVERAGE // Configure the conversion channel // differential and interrupt mode disable. adcChnConfig.chnIdx = (adc16_chn_t)ADC16_BANDGAP_CHN; #if FSL_FEATURE_ADC16_HAS_DIFF_MODE adcChnConfig.diffConvEnable = false; #endif adcChnConfig.convCompletedIntEnable = false; ADC16_DRV_ConfigConvChn(ADC16_INSTANCE, ADC16_CHN_GROUP, &adcChnConfig); // Wait for the conversion to be done ADC16_DRV_WaitConvDone(ADC16_INSTANCE, ADC16_CHN_GROUP); // Get current ADC BANDGAP value and format it. bandgapValue = ADC16_DRV_GetConvValueSigned(ADC16_INSTANCE, ADC16_CHN_GROUP); // Calculates bandgapValue in 16bit resolution // from 12bit resolution to calibrate. #if (FSL_FEATURE_ADC16_MAX_RESOLUTION < 16) bandgapValue = bandgapValue << 4; #endif // ADC stop conversion ADC16_DRV_PauseConv(ADC16_INSTANCE, ADC16_CHN_GROUP); // Get VDD value measured in mV // VDD = (ADCR_VDD x V_BG) / ADCR_BG vdd = ADCR_VDD * V_BG / bandgapValue; // Calibrate ADCR_TEMP25 // ADCR_TEMP25 = ADCR_VDD x V_TEMP25 / VDD adcrTemp25 = ADCR_VDD * V_TEMP25 / vdd; // Calculate conversion value of 100mV. // ADCR_100M = ADCR_VDD x 100 / VDD adcr100m = ADCR_VDD*100/ vdd; // Disable BANDGAP reference voltage pmcBandgapConfig.enable = false; PMC_HAL_BandgapBufferConfig(PMC_BASE_PTR, &pmcBandgapConfig); } /*! * @brief Gets current temperature of chip. * * This function gets convertion value, converted temperature and print them to terminal. */ void ADC16_Measure(void) { adc16_chn_config_t chnConfig; // Configure the conversion channel // differential and interrupt mode disable. chnConfig.chnIdx = (adc16_chn_t)ADC16_TEMPERATURE_CHN; #if FSL_FEATURE_ADC16_HAS_DIFF_MODE chnConfig.diffConvEnable = false; #endif chnConfig.convCompletedIntEnable = false; // Software trigger the conversion. ADC16_DRV_ConfigConvChn(ADC16_INSTANCE, ADC16_CHN_GROUP, &chnConfig); //ADC16_DRV_ConfigConvChn(FSL_ADCONV1, 0U, &adConv1_ChnConfig0); // Wait for the conversion to be done. ADC16_DRV_WaitConvDone(ADC16_INSTANCE, ADC16_CHN_GROUP); // Fetch the conversion value. adcValue = ADC16_DRV_GetConvValueSigned(ADC16_INSTANCE, ADC16_CHN_GROUP); // Show the current temperature value. PRINTF("\r\n ADC converted value: %ld\t", adcValue ); // Calculates adcValue in 16bit resolution // from 12bit resolution in order to convert to temperature. #if (FSL_FEATURE_ADC16_MAX_RESOLUTION < 16) adcValue = adcValue << 4; #endif PRINTF("\r\n Temperature %ld\r\n", get_current_temperature(adcValue)); // Pause the conversion. ADC16_DRV_PauseConv(ADC16_INSTANCE, ADC16_CHN_GROUP); }