Temperature Project C Code input?

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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...
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
Rich (BB code):
*/
 // LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
 
// Back Light Switch connected to RA1
sbit BackLight at RA1_bit;
// Define Messages
char message0[] = "LCD Initialized";
char message1[] = "Room Temperature";
 
// String array to store temperature value to display
char *tempC = "000.0";
char *tempF = "000.0";
 
// Variables to store temperature register values
unsigned int temp_whole, temp_fraction, temp_value;
signed int tempinF, tempinC;
unsigned short C_Neg=0, F_Neg=0, TempH, TempL;
 
void Display_Temperature() {
  // convert Temp to characters
 if (!C_Neg) {
     if (tempinC/1000)
   // 48 is the decimal character code value for displaying 0 on LCD
     tempC[0] = tempinC/1000  + 48;
     else tempC[0] = ' ';
  }
  tempC[1] = (tempinC/100)%10 + 48;             // Extract tens digit
  tempC[2] =  (tempinC/10)%10 + 48;             // Extract ones digit
 
  // convert temp_fraction to characters
  tempC[4] =  tempinC%10  + 48;         // Extract tens digit
 
  // print temperature on LCD
  Lcd_Out(2, 1, tempC);
 
  if (!F_Neg) {
     if (tempinF/1000)
      tempF[0] = tempinF/1000  + 48;
     else tempF[0] = ' ';
  }
 
  tempF[1] = (tempinF/100)%10 + 48;             // Extract tens digit
  tempF[2] =  (tempinF/10)%10 + 48;
  tempF[4] =  tempinF%10  + 48;
  // print temperature on LCD
  Lcd_Out(2, 10, tempF);
}
 
// ISR for LCD Backlight
void interrupt(void){
  if (INTCON.INTF == 1)          // Check if INTF flag is set
     {
     BackLight =~BackLight;  // Toggle Backlight
     Delay_ms(300) ;
     INTCON.INTF = 0;       // Clear interrupt flag before exiting ISR
     }
}
 
void main() {
  TRISC = 0x00 ;
  TRISA = 0b00001100; //  RA2, RA3 Inputs, Rest O/P's
  ANSEL =   0b00000000;
  PORTA =   0b00000000;            //  Start with Everything Low
  PORTC =   0b00000000;            //  Start with Everything Low
  CMCON0 =  0b00000111;
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  BackLight = 1;
  Lcd_Out(1,1,message0);
  Delay_ms(1000);
  Lcd_Out(1,1,message1);             // Write message1 in 1st row
  // Print degree character
  Lcd_Chr(2,6,223);
  Lcd_Chr(2,15,223);
 // different LCD displays have different char code for degree
 // if you see greek alpha letter try typing 178 instead of 223
 
  Lcd_Chr(2,7,'C');
  Lcd_Chr(2,16,'F');
 
  // Interrupt Setup
    OPTION_REG = 0x00;   // Clear INTEDG, External Interrupt on falling edge
    INTCON.INTF = 0;     // Clear interrupt flag prior to enable
    INTCON.INTE = 1;     // enable INT interrupt
    INTCON.GIE  = 1;     // enable Global interrupts
 
  do {
  //--- perform temperature reading
    Ow_Reset(&PORTA, 5);      // Onewire reset signal
    Ow_Write(&PORTA, 5, 0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTA, 5, 0x44);   // Issue command CONVERT_T
    INTCON.GIE  = 1;     // 1-wire library disables interrpts
    Delay_ms(600);
    Ow_Reset(&PORTA, 5);
    Ow_Write(&PORTA, 5, 0xCC);    // Issue command SKIP_ROM
    Ow_Write(&PORTA, 5, 0xBE);    // Issue command READ_SCRATCHPAD
 
    // Read Byte 0 from Scratchpad
    TempL =  Ow_Read(&PORTA, 5);
    // Then read Byte 1 from Scratchpad
    TempH = Ow_Read(&PORTA, 5);
    temp_value = (TempH << 8)+ TempL ;
    // check if temperature is negative
    if (temp_value & 0x8000) {
      C_Neg = 1;
      tempC[0] = '-';
      // Negative temp values are stored in 2's complement form
      temp_value = ~temp_value + 1;
      }
    else C_Neg = 0;
    // Get temp_whole by dividing by 2
    temp_whole = temp_value >> 1 ;
    if (temp_value & 0x0001){  // LSB is 0.5C
       temp_fraction = 5;
       }
    else temp_fraction = 0;
    tempinC = temp_whole*10+temp_fraction;
 
    if(C_Neg)  {
     tempinF = 320-9*tempinC/5;
     if (tempinF < 0) {
      F_Neg = 1;
      tempF[0] = '-';
      tempinF = abs(tempinF);
      }
     else F_Neg = 0;
     }
    else tempinF = 9*tempinC/5 + 320;
    //--- Format and display result on Lcd
    Display_Temperature();
 
  } while(1);
}

:D
 
Last edited by a moderator:
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...
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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...
 

bertus

Joined Apr 5, 2008
22,270
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:
Rich (BB code):
place your code here
Bertus
 

kubeek

Joined Sep 20, 2005
5,794
Atfter this line
Rich (BB code):
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.
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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:
 

kubeek

Joined Sep 20, 2005
5,794
Well, I never programmed a pic, but assuming that RA2 is the output pin to the fan, it could be something like this:
Rich (BB code):
//this goes near the "sbit BackLight at RA1_bit;" line 
sbit FAN at RA2_bit;//this should tie FAN to pin 2 in the A outputs (probably)
FAN=0;//initialization, fan is off

//this goes after the "tempinC = temp_whole*10+temp_fraction;" line
if( tempinC>27 && FAN==0)FAN=1;
if( tempinC<20 && FAN==1)FAN=0;
Remeber to use a transistor to drive the fan.
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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
 

Attachments

kubeek

Joined Sep 20, 2005
5,794
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.
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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:

Rich (BB code):
"error - ; expected, but '=' found"
"error - 'FAN' identifier redefined"
This is what it looks like with the FAN code added:

Rich (BB code):
// Back Light Switch connected to RA1
 
sbit BackLight at RA1_bit;
 
//this should tie FAN to pin RA0 in the A output
 
sbit FAN at RA0_bit;
 
FAN=0;
 
//initialization, fan is off
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 :)
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
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
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
:) 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?
 

t06afre

Joined May 11, 2009
5,934
:) 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?
The function is named alarm function. You will find it both DS1820 and DS18s20
 
Top