Temperature Project C Code input?

Discussion in 'Programmer's Corner' started by Ben_C, Oct 19, 2010.

  1. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Hello :)
    Right I'm on my final college year and would appreciate your help and educational knowledge with my project. Im studying Electrical & Electronic Engineering and Starting University in 2011 so I shall be a long lasting member to this site ;).

    Im currently building a Digital Thermometer and have all the parts and Code for the PIC, which is a 16F688, but I want to apply a fan to the circuit which will operate only when the temperature gets to a certain degree. The fan of course will then switch off when its done its job and obtained the correct temperature and drops below this set point. Now the thing is I found the C code for my microchip online and dont have a clue how to write it, I have only been taught the basics of Assembly language haha.

    Does anybody have the answer to my question of what on earth to write and what ports do I use for the fan to work?

    You would help Massively!
    Ben C...
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    It would help massively if you posted the original code you are using.
     
  3. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Code ( (Unknown Language)):
    1.  
    2.  
    3. */
    4.  // LCD module connections
    5. sbit LCD_RS at RC4_bit;
    6. sbit LCD_EN at RC5_bit;
    7. sbit LCD_D4 at RC0_bit;
    8. sbit LCD_D5 at RC1_bit;
    9. sbit LCD_D6 at RC2_bit;
    10. sbit LCD_D7 at RC3_bit;
    11. sbit LCD_RS_Direction at TRISC4_bit;
    12. sbit LCD_EN_Direction at TRISC5_bit;
    13. sbit LCD_D4_Direction at TRISC0_bit;
    14. sbit LCD_D5_Direction at TRISC1_bit;
    15. sbit LCD_D6_Direction at TRISC2_bit;
    16. sbit LCD_D7_Direction at TRISC3_bit;
    17. // End LCD module connections
    18.  
    19. // Back Light Switch connected to RA1
    20. sbit BackLight at RA1_bit;
    21. // Define Messages
    22. char message0[] = "LCD Initialized";
    23. char message1[] = "Room Temperature";
    24.  
    25. // String array to store temperature value to display
    26. char *tempC = "000.0";
    27. char *tempF = "000.0";
    28.  
    29. // Variables to store temperature register values
    30. unsigned int temp_whole, temp_fraction, temp_value;
    31. signed int tempinF, tempinC;
    32. unsigned short C_Neg=0, F_Neg=0, TempH, TempL;
    33.  
    34. void Display_Temperature() {
    35.   // convert Temp to characters
    36.  if (!C_Neg) {
    37.      if (tempinC/1000)
    38.    // 48 is the decimal character code value for displaying 0 on LCD
    39.      tempC[0] = tempinC/1000  + 48;
    40.      else tempC[0] = ' ';
    41.   }
    42.   tempC[1] = (tempinC/100)%10 + 48;             // Extract tens digit
    43.   tempC[2] =  (tempinC/10)%10 + 48;             // Extract ones digit
    44.  
    45.   // convert temp_fraction to characters
    46.   tempC[4] =  tempinC%10  + 48;         // Extract tens digit
    47.  
    48.   // print temperature on LCD
    49.   Lcd_Out(2, 1, tempC);
    50.  
    51.   if (!F_Neg) {
    52.      if (tempinF/1000)
    53.       tempF[0] = tempinF/1000  + 48;
    54.      else tempF[0] = ' ';
    55.   }
    56.  
    57.   tempF[1] = (tempinF/100)%10 + 48;             // Extract tens digit
    58.   tempF[2] =  (tempinF/10)%10 + 48;
    59.   tempF[4] =  tempinF%10  + 48;
    60.   // print temperature on LCD
    61.   Lcd_Out(2, 10, tempF);
    62. }
    63.  
    64. // ISR for LCD Backlight
    65. void interrupt(void){
    66.   if (INTCON.INTF == 1)          // Check if INTF flag is set
    67.      {
    68.      BackLight =~BackLight;  // Toggle Backlight
    69.      Delay_ms(300) ;
    70.      INTCON.INTF = 0;       // Clear interrupt flag before exiting ISR
    71.      }
    72. }
    73.  
    74. void main() {
    75.   TRISC = 0x00 ;
    76.   TRISA = 0b00001100; //  RA2, RA3 Inputs, Rest O/P's
    77.   ANSEL =   0b00000000;
    78.   PORTA =   0b00000000;            //  Start with Everything Low
    79.   PORTC =   0b00000000;            //  Start with Everything Low
    80.   CMCON0 =  0b00000111;
    81.   Lcd_Init();                        // Initialize LCD
    82.   Lcd_Cmd(_LCD_CLEAR);               // CLEAR display
    83.   Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
    84.   BackLight = 1;
    85.   Lcd_Out(1,1,message0);
    86.   Delay_ms(1000);
    87.   Lcd_Out(1,1,message1);             // Write message1 in 1st row
    88.   // Print degree character
    89.   Lcd_Chr(2,6,223);
    90.   Lcd_Chr(2,15,223);
    91.  // different LCD displays have different char code for degree
    92.  // if you see greek alpha letter try typing 178 instead of 223
    93.  
    94.   Lcd_Chr(2,7,'C');
    95.   Lcd_Chr(2,16,'F');
    96.  
    97.   // Interrupt Setup
    98.     OPTION_REG = 0x00;   // Clear INTEDG, External Interrupt on falling edge
    99.     INTCON.INTF = 0;     // Clear interrupt flag prior to enable
    100.     INTCON.INTE = 1;     // enable INT interrupt
    101.     INTCON.GIE  = 1;     // enable Global interrupts
    102.  
    103.   do {
    104.   //--- perform temperature reading
    105.     Ow_Reset(&PORTA, 5);      // Onewire reset signal
    106.     Ow_Write(&PORTA, 5, 0xCC);   // Issue command SKIP_ROM
    107.     Ow_Write(&PORTA, 5, 0x44);   // Issue command CONVERT_T
    108.     INTCON.GIE  = 1;     // 1-wire library disables interrpts
    109.     Delay_ms(600);
    110.     Ow_Reset(&PORTA, 5);
    111.     Ow_Write(&PORTA, 5, 0xCC);    // Issue command SKIP_ROM
    112.     Ow_Write(&PORTA, 5, 0xBE);    // Issue command READ_SCRATCHPAD
    113.  
    114.     // Read Byte 0 from Scratchpad
    115.     TempL =  Ow_Read(&PORTA, 5);
    116.     // Then read Byte 1 from Scratchpad
    117.     TempH = Ow_Read(&PORTA, 5);
    118.     temp_value = (TempH << 8)+ TempL ;
    119.     // check if temperature is negative
    120.     if (temp_value & 0x8000) {
    121.       C_Neg = 1;
    122.       tempC[0] = '-';
    123.       // Negative temp values are stored in 2's complement form
    124.       temp_value = ~temp_value + 1;
    125.       }
    126.     else C_Neg = 0;
    127.     // Get temp_whole by dividing by 2
    128.     temp_whole = temp_value >> 1 ;
    129.     if (temp_value & 0x0001){  // LSB is 0.5C
    130.        temp_fraction = 5;
    131.        }
    132.     else temp_fraction = 0;
    133.     tempinC = temp_whole*10+temp_fraction;
    134.  
    135.     if(C_Neg)  {
    136.      tempinF = 320-9*tempinC/5;
    137.      if (tempinF < 0) {
    138.       F_Neg = 1;
    139.       tempF[0] = '-';
    140.       tempinF = abs(tempinF);
    141.       }
    142.      else F_Neg = 0;
    143.      }
    144.     else tempinF = 9*tempinC/5 + 320;
    145.     //--- Format and display result on Lcd
    146.     Display_Temperature();
    147.  
    148.   } while(1);
    149. }
    150.  
    151.  

    :D
     
    Last edited by a moderator: Oct 21, 2010
  4. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    HINT: Use the code tags to maintain your formatting

    Anyway, what is the problem? I am not sure what exactly you are trying to do, since you already have the code...
     
  5. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    The code is for simply getting a temperature from the DS1820 thermistor this is fine but I want to expand the project a bit and add a fan, so when the temperature gets to a certain level the fan will start. My tutor said we can incorporate code into this to work a motor or fan...

    It is possible :)

    And what do you mean by Code Tags? Sorry if I sound a bit of a novice lol...
     
  6. bertus

    Administrator

    Apr 5, 2008
    15,646
    2,344
    Hello,

    You can use code tags for having the monospaced formatted text in a block.

    Here is an example:

    [code=rich]
    place your code here
    [/code]


    The result will be:
    Code ( (Unknown Language)):
    1.  
    2. place your code here
    3.  
    Bertus
     
  7. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Atfter this line
    Code ( (Unknown Language)):
    1. tempinC = temp_whole*10+temp_fraction;
    you have the temperature in the tempC variable. Compare it to the treshold and activate the fan pin accordingly.
     
  8. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Thankyou, but as I said I dont have a clue what to write haha it seems really complex to me atm....

    I want the fan to operate when the temperature goes above average room temperature, say I wanted it to work at 27°C and to switch off when it gets below 20°C

    I dont want to sound cheeky but if its just a simple code to write would you be able to do it? :confused:
     
  9. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Well, I never programmed a pic, but assuming that RA2 is the output pin to the fan, it could be something like this:
    Code ( (Unknown Language)):
    1. //this goes near the "sbit BackLight at RA1_bit;" line
    2. sbit FAN at RA2_bit;//this should tie FAN to pin 2 in the A outputs (probably)
    3. FAN=0;//initialization, fan is off
    4.  
    5. //this goes after the "tempinC = temp_whole*10+temp_fraction;" line
    6. if( tempinC>27 && FAN==0)FAN=1;
    7. if( tempinC<20 && FAN==1)FAN=0;
    Remeber to use a transistor to drive the fan.
     
  10. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Ahh right, I can start to see how C language works, it looks pretty simple actually. Thankyou very much for your input, really helpful :)

    So implementing a fan is possible from RA2 but the thing is I already have a 10k resistor on this line would the fan still operate with this there? I mean would there be enough current for it to work?

    Of course I'll use a transistor with the Base connected to RA2 with the Collector connected to + and Emitter connected to -

    I've attached a file for you to see what I am building :D
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Any pin will work, in your case you have RA0 and RA4 free, so use one of them that suits your board better.

    Also download for example Eagle from cadsoft and make the schematic in there, because this one is a little messy.
     
  12. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Thankyou I'll have a look at this software, seems useful, also I need to get me a PCB designed and ordered to build my project on.

    Now back to the code. I've compiled it witout the FAN code applied and it works fine but when I added the code it comes up with an error code saying:

    Code ( (Unknown Language)):
    1.  
    2. "error - ; expected, but '=' found"
    3. "error - 'FAN' identifier redefined"
    4.  
    This is what it looks like with the FAN code added:

    Code ( (Unknown Language)):
    1.  
    2. // Back Light Switch connected to RA1
    3.  
    4. sbit BackLight at RA1_bit;
    5.  
    6. //this should tie FAN to pin RA0 in the A output
    7.  
    8. sbit FAN at RA0_bit;
    9.  
    10. FAN=0;
    11.  
    12. //initialization, fan is off
    13.  
    I'm using MikroC to compile seeing as I need the .hex file and I've checked the library in this software to see if I could enable some sort of "FAN" function but nothings there apart from LED's and such.

    Any ides on how to get rid of this error message? The other parts of the code are fine and it recognizes it :)
     
  13. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    try deleting the FAN=0;
     
  14. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Compilation succesful!
    But will this actually operate the fan then? I suppose if you dont know theres only one way to find out eh ? :D

    Thankyou very much for your input anyway, you've taken much stress off my mind :D:D:D
     
  15. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I don't know if you have noticed or not but the DS1820 device has a couple of registers that are provided to permit you to specify a T-high and T-low alarm limit. You can preload these two registers with the high and low temp alarm values and then you can let the device do the comparison for you. I think you can set the two registers to the same value if you only want to determine if the value is above or below a given temperature.

    hgmjr
     
  16. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    :) Thats very interesting, I didn't notice this no but I shall look into it. My tutors at college should know something about this but I very doubt it they'll probably just tell me to go and figure it out haha.

    I've been looking at the datasheet for this hardware but all im seeing is the datasheet is very biased toward the DS18S20 rather than the DS1820. I can see this alarm function but only on the DS18S20, are you sure this is available on the DS1820 ?? :confused:

    And how would I implement the code? Is it just a simple case of adding it to the code I already have?
     
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The function is named alarm function. You will find it both DS1820 and DS18s20
     
Loading...