PDA

View Full Version : PWM for heating car's exterior mirrors



maciu1
- 25th December 2012, 17:53
Hello!
At my car, exteriors mirrors heating was done with help of an electronic module, based on a temperature sensor readings. Because that module is broken and too expensive to buy, after some searching on web, I found a schematic which contain a PIC12F675 and a LM35 temperature sensor, used for PWM a fan. Also, I found the necessary code.
Because I have no experience in PicBasicPro, after several attempts to modify the code, I had to give up and call for help. The code is written approx. 70%, only the main iteration is missing.
So, please, someone to complete blank spaces on attached file.

Thank you so much and Merry Christmas to all!

6781

maciu1
- 26th December 2012, 06:39
' 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

maciu1
- 27th December 2012, 06:23
Hy! No one here able to write 4-5 code lines? Come on!

Ioannis
- 27th December 2012, 08:17
Remember it is Christmas! Be patient and soon you will get your reply.

Ioannis

Acetronics2
- 27th December 2012, 10:08
Hy! No one here able to write 4-5 code lines? Come on!

looks you are included into " no one able " ones ... :D


So, please, fill up the "empty space" with necessary code.


Don't you think you're asking us too much ??? :rolleyes:

You should be able to use some "SELECT CASE" statement to drive your PWM value ... or a retrieve table ( "LOOKUP" ) to directly choose your on/off PWM values

Merry Christmas to you ...
read you next year ;)

Alain

maciu1
- 28th December 2012, 17:03
@Acetronics, as I sad, I have no experience with PicBasic Pro and other stuff. For an experimentated guy, 5 minutes are too much in order to write the missing "link" in the code. That is my feeling.
Happy new year!

Acetronics2
- 28th December 2012, 19:32
Just found these ones ...
http://www.qualityanswers.net/electronics/programming-pic16f870/?PHPSESSID=e32d2d2da823049fb0505e61b391a003
http://www.elforum.ro/viewtopic.php?f=11&t=37981

yess ... too bad :D

Alain

maciu1
- 29th December 2012, 12:24
Acetronics, if you have usefull ideas about my initial request, by my guest. Else, please, don't pollute my post with your amazing discoveries regarding old posts sent from others forums. You're so hard to follow...

ismellsmoke
- 29th December 2012, 22:10
Just a beginner myself, but seeing as you're still looking for help, I'll give it a stab.

I did a similar project to the one you referenced, except I used a 12F683. The main difference being that the 12F683 has one CCP pin, which means you can use the HPWM command and save a lot of headaches.

I also used an LM34 which does temp. in fahrenheit.

Here is the code which should be pretty easy to modify for your purpose:


'This program uses a 12F683 chip wired as follows:
'Pin 2 (GPIO 5) - Status LED Pin 7 (GPIO 0) - to LM34 Temp sensor
'Pin 3 (GPIO 4) - Low Speed LED Pin 6 (GPIO 1) - high speed LED
'Pin 4 (GPIO 3) - Not used (MCLR) Pin 5 (GPIO 2) - pwm to fan mosfet

' Defines:

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

' Registers:

TRISIO = 1
ANSEL = %00010001 ''Fosc/8 conversion speed for 4mHz processor, AN0 analog input
CMCON0 = 7
ADCON0.7 = 1 'Right Justified (10 bit conversion)


'Variables:

Raw_Temp var word
Temp Var word
Avg_Temp var word
LED1 var GPIO.5
LED2 var GPIO.4
LED3 var GPIO.1
X var byte

Raw_Temp = 0
Temp = 0
X = 0

'Program Start:

LED1 = 1 'Always on status light

START:
LED2 = 0
LED3 = 0
HPWM 1,0,1000
Gosub GETDATA
IF TEMP => 95 THEN GOTO MOTORSTART
PAUSE 5000
GOTO START

MOTORSTART: 'Starts motor at full speed
HPWM 1,255,1000
PAUSE 1000
GOTO THIRTY

THIRTY:
hpwm 1,77,1000
LED2 = 1
LED3 = 0
GOSUB GETDATA
IF TEMP < 92 THEN GOTO START
IF TEMP > 105 THEN GOTO SEVENTY
PAUSE 5000
GOTO THIRTY

SEVENTY:
HPWM 1,180,1000
LED2 = 1
LED3 = 1
GOSUB GETDATA
IF TEMP < 100 THEN GOTO THIRTY
IF TEMP > 115 THEN GOTO FULL
PAUSE 5000
GOTO SEVENTY

FULL:
HPWM 1,255,1000
LED2 = 0
LED3 = 1
GOSUB GETDATA
IF TEMP < 110 THEN GOTO SEVENTY
PAUSE 5000
GOTO FULL

GETDATA:
Avg_Temp = 0
for x = 1 to 5 'Hysteresis by averaging 5 samples
ADCIN 0,RAW_TEMP
pause 5
Avg_Temp = Avg_Temp + Raw_temp
pause 1000
next x
TEMP = ((Avg_Temp/5)*49) / 100 'Farenheit conversion
PAUSE 50
' SEROUT GPIO.4,4,[" Temp ",#Temp,13,10] 'For debugging
RETURN

END

Acetronics2
- 31st December 2012, 12:30
see next ...

Acetronics2
- 31st December 2012, 12:32
Acetronics, if you have usefull ideas about my initial request, by my guest. Else, please, don't pollute my post with your amazing discoveries regarding old posts sent from others forums. You're so hard to follow...

Hi, Maciu

I have ONE useful idea for you ...

Stop always begging and try some googling instead ...

BTW ... LM35 AND 12F675 remind me to have crossed such applications on the web ... may be not PBP ( ? - one sure in Mikro C - ) , but, as you are just begging for code ... Hex file will be more than enough !

Alain

PS: I hate lazy beggars ... but you already knew it :rolleyes:
re PS : lazy ???

I prove it : http://www.picbasic.co.uk/forum/showthread.php?t=12794

Acetronics2
- 31st December 2012, 12:54
the next part of the puzzle ...
http://jeanclaude.grimaldi.perso.sfr.fr/SitePtitrainJCG/electronique/microp/page53%28commandePWMdouble%29/

Alain