Program code for DS1820

General discussion on mikroC.
Post Reply
Author
Message
Andi
Posts: 3
Joined: 10 Feb 2005 13:56

Program code for DS1820

#1 Post by Andi » 10 Feb 2005 14:02

Hi.

Looked through the help file in mikroC and found som code on how to use the DS1820 temp sensor (look further down for the code. But I cant get it to work. I have put in the temp sensor the round side on the temp senor is on the round side marking on the board. All three legs are in. And the jumper is set to RA5. But all that displays on the LCD is:
Temperature:
0.0000 CCC

Maybe there is something worng with the code or me new sensor (got it with the board two days ago) is broken. Can someone suggest a solution?

//Andi

P.S. easyPic 2 is one of the greates test boards I have used!!! D.S

Code: Select all

// Set TEMP_RESOLUTION to the corresponding resolution of your DS18x20 sensor:
//  18S20: 9
//  18B20: 12 (default setting; can be 9,10,11,or 12)
const unsigned short TEMP_RESOLUTION = 12;

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);

}//~!

Steve
Posts: 233
Joined: 19 Aug 2004 02:33
Location: North Carolina

#2 Post by Steve » 10 Feb 2005 23:44

Just looking at it quickly, you have the jumper set to RA5 but the code is reading from RE2.

Try changing the jumper or change the code to reflect RA5.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#3 Post by LGR » 11 Feb 2005 00:02

If that doesn't work, you need to try the demo from Pascal or Basic, which are pretty well broken in. This will confirm that your hardware is working. If you can get one of those to work, and the C version doesn't work, then maybe there's a problem with C.

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#4 Post by pizon » 11 Feb 2005 10:51

Checked the example, it works. If you want to test it with RA5, set ADCON1 = 6 (plus all the OneWire() functions).
pizon

lorenz2828
Posts: 32
Joined: 29 Sep 2012 17:25

Re: Program code for DS1820

#5 Post by lorenz2828 » 30 Sep 2012 06:27

hi... im having a problem with this proj also.... it always displays 000.0000 C....
in protues simulation

ive used the code from the library and changed the temp_resolution to 12 because i will used DS18b20....

but when i simulate the program the display result on the LCS is 000.0000 C... pls guys help me with this... tnx......

this is my diagram
Attachments
my diagram.png
my diagram.png (21.58 KiB) Viewed 9827 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Program code for DS1820

#6 Post by filip » 01 Oct 2012 11:49

Hi,

I believe I have answered you here :
http://www.mikroe.com/forum/viewtopic.php?f=88&t=49931

Please, don't make multiple posts on the same subject.

Regards,
Filip.

nirob220V
Posts: 2
Joined: 29 Jul 2018 08:07

Re: Program code for DS1820

#7 Post by nirob220V » 29 Jul 2018 08:27

Hello my code is hear. And its worked. but i want to add fan controlling code, if temperature 40.7 is over Fan is on and temperature 40.7C is down fan off like this.
i already trying but not working. anyone help me fan controlling




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);

}//~!

Post Reply

Return to “mikroC General”