Code:' PIC12F675 ' LM35, temperature sensor ' @device mclr_off tempSensor con 3 'this is the analog input that the temperature sensor is connected to LED var GPIO.2 'LED output HEAT var GPIO.5 'Mirror heating output Define OSCCAL_1K 1 ' Calibrate internal oscillator ' Define ADCIN parameters Define ADC_BITS 10 ' Set number of bits in result Define ADC_CLOCK 3 ' Set clock source (3=rc) Define ADC_SAMPLEUS 50 ' Set sampling time in uS 'define some variable that are needed rawTempData Var Word ' Create rawTempData to store result calculatedTemp var Word ' Calculated temperature heatPWMValue var Word ' The mirrors'heat needed heatON var Bit ' Keeps track of the heat condition (ON or OFF) heating con 1 ' value of a heated mirror notHeating con 0 ' value of a nonheated mirror heatPWMCycleCounter var Word 'keeps track of the number of PWM loops we have done heatPWMCounter var Word 'keeps track of the required PWM count numberOfPWMCyclesNeeded con 50 'this will cause the temp to be checked around every 1/2 second PWMPauseTime con 1 'number of miliseconds to pause between PWM ticks ADCON0.7 = 1 ' Right justify result ANSEL = %00111000 ' Set AN3 analog, rest digital CMCON = 7 ' Analog comparators off 'show startup sequence GoSub startup 'Get the analog value of the sensor, convert it to celcius and display the results mainloop : ADCIN TempSensor, rawTempData ' Read channel 3 to rawTempData GoSub CalculateTemperature 'calculate the actual temp GoSub PWMheatControl 'control the heat based on the current temperature GoTo mainloop ' loop forever 'Calculate what the actual temperature is based on the value that was read in CalculateTemperature: 'The analog input has 10 bits or resolution wich means that 0 through 5 volts is 'converted to a number between 0 and 1023. Each step represents 5V / 1024 = 0.004883 'This is aproximately .0049 therefore if we multiply the analog result by 49 and divide by 100 'since we are doing integer math we will get a value cvery close to the actual temperature in celcius. calculatedTemp = rawTempData * 49 / 100 Return 'Control the HEAT based on the current temperature PWMheatControl: ' The heating procedure should follows below rules: ' - if calculatedTemp is >= 20*C, PWM value = 0% ' - if calculatedTemp is <20 but >=15, PWM value = 20% ' - If calculatedTemp is <15 but >=10, PWM value = 40% ' - If calculatedTemp is <10 but >=5, PWM value = 50% ' - If calculatedTemp is <5 but >0, PWM value = 70% ' - If calculatedTemp is <=0 but >=-10, PWM value = 90% ' - If calculatedTemp is bellow -10*C, PWM value = 100% 'set the PWM value based on what the temperature is If calculatedTemp >= 20 Then 'the temperature is 20 deg or above, turn the HEAT off and indicate that the MIRROR is not heated heatPWMValue = 0 Low led 'turn off LED Low HEAT 'turn off heat heatON = notHeating 'go into low power mode for a bit, when we wake up we will sample the temp again and see if we need to turn on the MIRROR nap 4 'nap for about a quarter second ' ' From that point, I tryed several codes but no one is working. ' So, please, fill up the "empty space" with necessary code. ' Thank you very much! ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' Bellow is what the old code contains ' ' ' 'loop through the PWM values a number of times ' For heatPWMCycleCounter = 1 To numberOfPWMCyclesNeeded ' 'turn on the fan and indicator LED ' High HEAT 'turn on HEAT ' High led 'turn on LED ' 'perform one cycle of PWM on the fan and LED ' For heatPWMCounter = 1 To 10 ' 'turn the HEAT and LED off if we have reached the end of its on time ' If heatPWMCounter > heatPWMValue Then ' Low led 'turn off LED ' Low HEAT 'turn off HEAT ' EndIf ' 'pause for a short time ' pause PWMPauseTime ' Next ' Next ' 'keep track of the fan state ' heatON = heating EndIf Return End 'Startup, flash the LED to show that the system is operational startup: 'turn on the LED for 2 seconds to indicate that the system is operational High LED pause 2000 Low led Return




Bookmarks