Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC soldering iron code

Status
Not open for further replies.

MrDEB

Well-Known Member
Been working on this for some time and pretty sure it now runs smoothly as desired.
Not real happy with the IF THEN statements in the counter section but it works as planed.
If after aprox 10 minutes the iron is not used then the whole unit shuts OFF including the bulky transformer that is sitting on the floor while the control unit is above the bench.
Need to edit the schematic a little but will post pictures etc. The Vsensor section I need to remove the //. I wanted to keep track of the counter progress.
Any suggestions for cleaning up code as I am not real sharp on proper indenting. I think I got it right?
Code:
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/6/2011                                                      *
*  Version : 1.0                                                            *
*  Notes   :compute voltage to temperature. Start with sensor at 50ohms.
*          : 180-200 ohms melts solder                                                               *
*          :                                                                *
*****************************************************************************
}
   
     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.6       // 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))*200/1024 //9 to 203 ADC.Read rsults
    End Function
    Function sensor() As Word
        result = (ADC.Read(0))*5  //14 (RT) to 37 (melt)
    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(pChannel 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(pChannel)
    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.6)
    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           
        //#######################################
        //voltage reference look up table
        //#######################################
               
        Select Vref                    //41-56 range for Vref
            Case < 105 deg_R =70  
            Case < 112 deg_R =150   
            Case < 114 deg_R = 200   
            Case < 119 deg_R =300
            Case < 121 deg_R =400
            Case < 123 deg_R =450
         End Select    
          
       
        
            //#######################################
           // sensor look up table
           //#########################################
        Select Vsensor
             
            Case < 24 deg_S =70  
            Case < 34 deg_S =150
            Case < 48 deg_S =200
            Case < 58 deg_S =300
            Case < 65 deg_S =400  
            Case < 88 deg_S =450
            Case < 88 deg_S =450
         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(deg_R),"    " )     // DecToStr(deg_R))  DecToStr(Vref),"    "
               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(deg_S),"    ")        //   DecToStr(deg_S)) 14 to 37
                DelayMS(250)
               
                // compare Vsensor and Vref values
                High(beep)
                If deg_S < deg_R 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
//*************************************************************************
//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                 
//*************************************************************************                 
                 
                  
      
       // If (iron_sense)=0 Then iron in holder HIGh if iron out of holder 
//#################################
 // count down timer
 //###############################
                  
       
   counter = (count_down)      
   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       
         DelayMS(250)                       //switch debounce
         if count_down =550 then            // aprox 9 minutes
         high (beep)                        //warning timing out   
         endif
   endif 
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
         //TIME OUT so unit is shut off
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                
    If(count_down >= 600)Then               //10+ miniutes since iron used
          Low(gate)                         // shut off iron
    EndIf         
    Wend
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top