' File: TE_Controller_v1-6 ' Author: AHeintzelman ' Date: 2/22/09 ' Description: This program reads the temperature from a ' LM34 analog sensor and turns on a "peltier cooler" ' which is idealized as an LED on once the temperature ' gets too warm. It turns it off when it gets too cold ' as well. The actual temp is compared to the setpoint. ' ' This program also checks to see if the "set temperature" button ' is pressed and flashes an LED to let you know you are entering ' the "set temperature" function. The LED remains lit while in ' the function. Upon exiting the function, the LED flashes again ' and then turns off. Within the temperature set function, the program ' reads the potentiometer value and converts it into a temperature ' set point in Fahrenheit. ' ' This program displays the set temperature and TEC temperature ' on an 7-segment LED display. ' ' This program uses PWM to control a TEC idealized as an LED with ' a proportional control technique. ' ' Dual LED display added. '-----[ Definitions ]----- Symbol SetTempLED = 1 ' LED that flashes upon entering set temp function Symbol PowerLED = 2 ' LED that shows power is on to system Symbol SetTempButton = pin0 ' set temp button to enter set temp function Symbol TEC_OnOff_LED = 0 ' LED that shows when TEC is on or off Symbol AD0_Val = W0 ' temp read variables Symbol Sum = W1 ' sum of ADCread values Symbol TE_Temp_10 = W1 ' temp in voltage from LM34 Symbol N = B4 ' number of values to attain to create TEC temp average Symbol TE_Temp = B5 ' temp in whole numbers Symbol AD1_Val = W0 ' temp set variables Symbol SetPoint_10 = W1 ' set point temperature from pot x 10 Symbol TempSetPoint = B6 ' set point temperature ' ****** 7 segment display LED variables and set up Symbol DispEnable = 7 ' display driver enable pin Symbol DispClock = 5 ' display driver clock pin Symbol DispData = 6 ' display driver data pin Symbol DispDig10 = b7 ' tens place of the temperature to display Symbol DispDig1 = b8 ' ones place of the temperature to display ' ****** proportional control variables Symbol PropError = b10 ' proportional error Symbol PropGain = 25 ' proportional gain (should be ~1/8 of PWMMax) Symbol SignBit = b11 ' sign of proportional error (0 = pos, 1 = neg) Symbol DutyCycle = b12 ' duty cycle = PropGain * PropError MAX PWMMax Symbol PWMPeriod = 49 ' PWM wavelength or period Symbol PWMMax = b13 ' Maximum for the duty cycle, this is 100% duty Symbol TEC_PWM = 1 ' PWM1 (pin12), illuminates LED according to voltage supplied sends voltage to the opamp '-----[ Initializations ]----- let TempSetPoint = 80 ' initial startup value for setpoint temperature high PowerLED ' power-on LED, this will turn on once there is power to the micro gosub DispConfigure ' initialize and configure LED display PWMMax = PWMPeriod + 1 * 4 ' PWM Max calculation low SetTempLED ' make sure the set temp LED is off high DispEnable ' make sure the LED display driver is not enabled low DispClock ' make sure the clock pulses are off to the display driver '-----[ Main Routine ]----- Main: Sum = 0 For N = 1 to 32 ' sum 32 readings from the ADC ReadADC10 0, AD0_Val Sum = Sum + AD0_Val Next AD0_Val = Sum / 32 ' calculate the average for the 32 readings TE_Temp_10 = AD0_Val * 4 ' Voltage (TE_Temp_10) from the LM34 is calculated TE_Temp_10 = AD0_Val * 8 / 10 + TE_Temp_10 ' from the digital value (AD0-Val). In essence performing TE_Temp_10 = AD0_Val * 8 / 100 + TE_Temp_10 ' AD0_Val * 5000 (mV)/1024 (bits) since using a 10-bit ADC TE_Temp = TE_Temp_10 / 10 ' TE_Temp (whole value in degrees F, no decimal) DispDig10 = TE_Temp / 10 ' The tens place of the temp to display DispDig1 = TE_Temp % 10 ' The ones place of the temp to display gosub DispTemp ' display the current temp if SetTempButton = 0 then LED_Flash ' check the set button, if pressed goto LED_Flash, otherwise continue PropError = TE_Temp - TempSetPoint ' calculate the proprotional error of current temp from the set point SignBit = PropError / 128 ' sign bit shows if the error is negative or positive if SignBit = 0 then gosub PropControl ' if the current temp is greater than set point, goto PropControl if SignBit = 1 then gosub PropControlOff ' if the current temp is less than set point, goto PropControlOff if TE_Temp < TempSetPoint Then TurnOffPeltier if TE_Temp > TempSetPoint Then TurnOnPeltier goto Main '-----[ Subroutines ]----- PropControl: ' routine sets the pwmout as a function of the error DutyCycle = PropGain * PropError MAX PWMMax ' from the current temp and set point, limiting it to pwmout TEC_PWM, PWMPeriod, DutyCycle ' PWMMax return PropControlOff: ' turns the PWM off, no cooling is done pwmout TEC_PWM, 0, DutyCycle return TurnOffPeltier: ' LED shows if TEC is on Low TEC_OnOff_LED GoTo Main TurnOnPeltier: ' LED shows if TEC is off High TEC_OnOff_LED GoTo Main LED_Flash: ' flashing set temp LED upon entering set temp mode For N = 1 to 7 toggle SetTempLED pause 250 Next if SetTempButton = 0 then Set_Temp ' if button is pressed until end of flashing, goto Main ' then goto the set temp routine Set_Temp: pause 1000 do readADC10 1,AD1_Val ' read the potentiometer voltage if SetTempButton = 0 then LED_Flash ' if the button is pressed again, exit set temp mode SetPoint_10 = AD1_Val * 3 / 10 ' y = 0.0392x + 40 - this is the range and slope SetPoint_10 = AD1_Val * 9 / 100 + SetPoint_10 ' of the digital value of the pot SetPoint_10 = AD1_Val * 2 / 1000 + SetPoint_10 + 400 ' Temp set range from 40 to 80 deg F TempSetPoint = SetPoint_10 / 10 DispDig10 = TempSetPoint / 10 ' tens place of the set point DispDig1 = TempSetPoint % 10 ' ones place of the set point gosub DispTemp ' dislplay set point loop DispConfigure: ' routine to configure the display driver low DispEnable spiout DispClock, DispData, 1, (%00000001) high DispEnable return DispTemp: ' routine to display the current temp or set point low DispEnable spiout DispClock, DispData, 1, (%00000000,DispDig1,DispDig10) high DispEnable return