Device = 18F4520
Clock = 8
Config OSC = INTIO67
// some LCD options...
//#option LCD_DATA = PORTD.4 // Assign the LCD connections
//#option LCD_EN = PORTD.3 //
//#option LCD_RS = PORTD.2 //
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// #option LCD_INIT_DELAY =100
// import LCD library...
// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
Include "InternalOscillator.bas"
Include "math.bas"
Dim ADVal As Word
Dim LED_g As PORTC.0 // GREEN LED tip desired temp
Dim LED_r As PORTC.1 // HEATER ON
Dim opti As PORTE.0
Dim Vref As Integer //want whole numbers
Dim Vsensor As Integer
Dim deg_S As Word //Sensor variable
Dim deg_R As Word // Vref variable
Dim gate As PORTC.7 // pin 25 controls mosfet gate
Dim beep As PORTC.5 //pin 24 beeper sound controlled by counter
Dim iron_sense As PORTC.4 // check iron sense switch pin 23
Dim counter As Word
Dim count_down As Integer // count down timer
// Read the AD ports...
Function REF_V() As Word
result = (ADC.Read(1)+1)*50/1024 //113 to 166 ADC.Read rsults
End Function
Function sensor() As Word
result = (ADC.Read(0)+2)*500/10 //14 (RT) to 37 (melt) *30+22
End Function
// COPY / PASTED from Tetris code. The ADC readings were jumping out of order
// I found the ADC.ReadMedian routine was buggy with the 18F4520. Instead of debugging it
// I've created my own routine to do the job. Also, the program below takes 3 samples instead
// of 64 (as performed by ADC.ReadMedian).
Function ReadMedian2(pChanne0 As Byte) As Word
// define local variables
Dim Results(3), tmpWord As Word, i, ReSort As Byte
// make a number of samples
For i = 0 To Bound(Results)
Results(i) = ADC.Read(pChanne0)
Next
Repeat
ReSort = 0
For i = 0 To Bound(Results)-1
If Results(i) > Results(i+1) Then
tmpWord = Results(i)
Results(i) = Results(i+1)
Results(i+1) = tmpWord
ReSort = 1
EndIf
Next
Until ReSort = 0
Result = Results(Bound(Results)/2)
End Function
//###################################################################
// Start Of Program...
//###################################################################
Input(PORTA.0) // temp ref voltage desired temp
Input(PORTA.1) // tip sensor
Low (LED_g) //pin 15 set lEDs OFF
Low (LED_r) //pin 16
High(opti)
Output(PORTC.7)
Output(PORTC.5)
Input (PORTC.4)
count_down=0
gate = 1
ADCON1 = %10000000 //set up ADC regesters
ADCON0.7 = 1
ADCON0.6 = 1
ADC.RightJustify = true
DelayMS (150)
LCD.Cls // clear LCD
//####################################################################
// main program loop.. .
//###################################################################
// call function REF_V() and assign the result to word variable 'Vref'
// Select Vsensor
// call function REF_V() and assign the result to word variable 'Vref'
While true
Vref= REF_V() //Reference voltage
Vsensor = sensor() // tip sensor
counter = count_down // count down timer
//#######################################
//voltage reference look up table
//#######################################
Select Vref //41-56 range for Vref
Case < 28 deg_R =182
Case < 29 deg_R =209
Case < 30 deg_R =235
Case < 31 deg_R =262
Case < 32 deg_R =289
Case < 33 deg_R =315
Case < 34 deg_R =342
Case < 35 deg_R =369
Case < 36 deg_R =375
Case < 37 deg_R =422
End Select
//#######################################
// sensor look up table
//#########################################
Select Vsensor
Case < 26 deg_S =182
Case < 27 deg_S =209
Case < 28 deg_S =235
Case < 29 deg_S =262
Case < 30 deg_S =289
Case < 31 deg_S =315
Case < 32 deg_S =342
Case < 33 deg_S =369
Case < 34 deg_S =375
Case < 35 deg_S =422
End Select
//DESIRED TEMP DESIRED TEMPDESIRED TEMP DESIRED TEMP
// call function REF_V() and assign the result to word variable 'Vref'
//################################
Vref= REF_V()
// LCD.Cls
LCD.MoveCursor (1,1)
LCD.Write("Degrees = ", DecToStr(Vref*10)," ") // DecToStr(deg_R)) DecToStr(Vref)," " 146 is solder time
DelayMS(200)
//TIP TEMP TIP TEMP TIP TEMP TIP TEMP
// call function sensor() and assign the result to word variable 'Vsensor'
Vsensor = sensor() // tip sensor
LCD.MoveCursor (2,1)
LCD.Write("sensor = ",DecToStr(Vsensor)," ") // DecToStr(deg_S))
DelayMS(250)
If Vsensor < Vref*10 Then // not hot enough If Vsensor < Vref
LED_r=1
LED_g=0
opti=0 // turn on opti couplier by going low
Else // Vsensor must be >= Vref, so desired temperature
LED_g=1
LED_r=0
opti=1 // turn off opti couplier by going high
End If
{
LCD.MoveCursor (2,1)
LCD.Write("count = ",DecToStr(counter)," ")
delayms(1000)
}
// If (iron_sense)=0 Then iron in holder HIGh if iron out of holder
//#################################
// count down timer
//###############################
If iron_sense=1 // iron out of holder
Then count_down = 0
ElseIf iron_sense=0 //reset count if iron used
Then count_down = count_down +1 // iron in holder start counting for 10 miniutes
// compare Vsensor and Vref values
DelayMS(250) //switch debounce
// If count_down =500 Then // aprox 9 minutes 550
// High (beep) //warning timing out
// EndIf
EndIf
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//TIME OUT so unit is shut off
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If(count_down) >= 550Then //10+ miniutes since iron used
Low(gate) // shut off iron
EndIf
//*************************************************************************
//When Vref is set to 215 then solder melts (160C aprox.) Vsensor jumps from 200 to 220
// readings at sensor = 28.4mv room temp 52-53mv solder melts
//*************************************************************************
Wend