Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

DS18B20 Program code Need Help

Status
Not open for further replies.

nirob220

Newbie
Joined
Jul 29, 2018
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
31
Hello
DS18B20 Program code worked but i want to add code fan control. if temperature 40.6 C over Fan on and if 40.6C under temperature Fan off..
Any one can help this program code. i already try but not work.... anyone help me..






Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const unsigned short TEMP_RESOLUTION = 9;
 
const int RES_FACTOR_1[4] = {5000, 2500, 1250, 625};
const unsigned int RES_FACTOR_2[4] = {0x0001, 0x0003, 0x0007, 0x000F};
const unsigned int RES_FACTOR_3[4] = {0x8000, 0xC000, 0xE000, 0xF000};
 
unsigned temp;
unsigned short  j, RES_SHIFT;
 
void Display_Temperature(unsigned int temp) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  unsigned int temp_whole, temp_fraction;
  unsigned short i;
  char text[8];
 
  // Isolate the fraction and make it a 4-digit decimal integer (for display)
  temp_fraction = temp & RES_FACTOR_2[RES_SHIFT - 1];
  temp_fraction = temp_fraction * RES_FACTOR_1[RES_SHIFT - 1];
  //portc = temp_fraction;
  // Handle the whole part of temperature value
  temp_whole = temp;
 
  // Is temperature negative?
  if ((temp_whole & 0x8000) != 0u) i = 1;  // Yes, i = 1
  else i = 0;                              // No,  i = 0
  PORTC = i;
  // Remove the fractional part
  temp_whole >>= RES_SHIFT;
 
  // Correct the sign if necessary
  if (i) temp_whole |= RES_FACTOR_3[RES_SHIFT - 1];
 
  //portd = temp_whole;
  IntToStr(temp_whole, text);  // Convert whole part to string
  LCD_Out(2, 6, text);         // Print whole part on LCD
  LCD_Chr_CP('.');             // Print dot to separate fractional part
 
 
  IntToStr(temp_fraction, text); // Convert fractional part to string
 
  // Add leading zeroes (we display 4 digits fractional part)
  if (temp_fraction < 1000u) LCD_Chr_CP('0');
  if (temp_fraction < 100u)  LCD_Chr_CP('0');
  if (temp_fraction < 10u)   LCD_Chr_CP('0');
 
  LCD_Out_CP(text);            // Print fractional part on LCD
 
  LCD_Chr_CP(223);             // Print degree character
  LCD_Chr_CP('C');             // Print 'C' for Centigrades
}//~
 
void main() {
  ADCON1 = 0xFF;               // Configure RA5 pin as digital I/O
  PORTE  = 0xFF;
  TRISE  = 0x0F;               // PORTE is input
  PORTB  = 0;
  TRISB  = 0;                  // PORTB is output
 
  // Initialize LCD on PORTB and prepare for output
  LCD_Init(&PORTB);
  LCD_Cmd(LCD_CURSOR_OFF);
  LCD_Out(1, 1, " Temperature:   ");
 
  do { // main loop
 
    OW_Reset(&PORTE,2);        // Onewire reset signal
    OW_Write(&PORTE,2,0xCC);   // Issue command SKIP_ROM
    OW_Write(&PORTE,2,0x44);   // Issue command CONVERT_T
    Delay_us(120);
 
    OW_Reset(&PORTE,2);
    OW_Write(&PORTE,2,0xCC);   // Issue command SKIP_ROM
    OW_Write(&PORTE,2,0xBE);   // Issue command READ_SCRATCHPAD
    Delay_ms(400);
 
    j = OW_Read(&PORTE,2);     // Get temperature LSB
    temp = OW_Read(&PORTE,2);  // Get temperature MSB
    temp <<= 8; temp += j;     // Form the result
    Display_Temperature(temp); // Format and display result on LCD
    Delay_ms(500);
 
  } while (1);
 
}//~!

 
Last edited by a moderator:

You need to be able to compare a floating point value which is difficult with that code.
This is an extract of some code I use (PIC16F628A) which you should be able to adapt. It calculates the temperature as integer values before and after the decimal point based on the registers from the DS18B20 being read into an array.
Code:
    Rx1WRaw = (unsigned int)Rx1WData[0] | ((unsigned int)Rx1WData[1] << 8);
    Rx1WSign = '+'; 
    if((Rx1WData[1] & 0xF0) == 0xF0)
    { 
        Rx1WSign='-'; 
        Rx1WRaw = (~Rx1WRaw) + 1 ; 
    } 

    Rx1WInt = ((Rx1WRaw >> 4) & 0x7F); 
    Rx1WFloat = ((Rx1WRaw & 0x000F) * 625);
'Raw' here is the unprocessed numbers read from the temperature registers.

Maybe useful to you.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top