Example Bugs ...

Beta Testing discussion on mikroC.
Author
Message
Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

Example Bugs ...

#1 Post by Acetronics » 03 Mar 2009 18:48

Hi,

I noticed with great pleasure you had increased the Educationnal level : New Examples Bugs ( in the examples listings ) have been added ... :lol:

I also were pleased to see the PicFlash programmer ( V7.11 - from MkC installation ) worked much better : no programming failed. :D

Small sorry ... more ROM code lines used for MkC Pro ... :oops:

1079 vs 1039 ... but less RAM used ... :roll:

( tested on the debugged Onewire example - 16F877A listing exactly the same, but the new LCD Config. )

Alain

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Example Bugs ...

#2 Post by yo2lio » 03 Mar 2009 19:22

Acetronics wrote:Small sorry ... more ROM code lines used for MkC Pro ... :oops:

1079 vs 1039 ... but less RAM used ... :roll:
I can't agree with you ... Please repeat tests with big programs ... code is smaller with up to 20 %.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

Re: Example Bugs ...

#3 Post by Acetronics » 03 Mar 2009 20:47

yo2lio wrote:
1079 vs 1039 ... but less RAM used ... :roll:

I can't agree with you ... Please repeat tests with big programs ... code is smaller with up to 20 %.
Halas ... on this example, You'll have to agree ... :cry:

Programs have been checked WORD TO WORD to be sure both compilers treat exactly the SAME commands ... not to write " stupidities " :roll:

You know what ??? most of the R/C dedicated programs I Write are under 2 K Rom ... exceptionnally 3k !

Alain

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: Example Bugs ...

#4 Post by rajkovic » 04 Mar 2009 11:38

Acetronics wrote:
yo2lio wrote:
1079 vs 1039 ... but less RAM used ... :roll:

I can't agree with you ... Please repeat tests with big programs ... code is smaller with up to 20 %.
Halas ... on this example, You'll have to agree ... :cry:
Alain
Thanks for the report. We have investigated it and you are right. The code is bigger for this specific example because some pessimistic bank settings were repeated several times. We have corrected this and now the new size is 954.

yo2lio is also right, if you compare http_demo example (which is a big example for P16 family)
in MIkroC Pro code size is 6567 ( with this currently available version) and in old mikroC code size was 7490.

Thanks for helping us improve mikroC PRO.

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: Example Bugs ...

#5 Post by rajkovic » 04 Mar 2009 11:40

Acetronics wrote: I noticed with great pleasure you had increased the Educationnal level : New Examples Bugs ( in the examples listings ) have been added ... :lol:
Alain
Can you please be more specific about this. What is the problem?

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

#6 Post by Acetronics » 04 Mar 2009 13:50

No Problem ...

For the 16F887 Onewire example, translated to 16F877A ... I arrived to that:


Code: Select all

/*
 * Project name:
     OneWire (Interfacing the DS1820 temperature sensor - all versions)
 * Copyright:
     (c) Mikroelektronika, 2008.
 * Revision History:
     20080930:
       - initial release;
 * Description:
     This code demonstrates one-wire communication with temperature sensor
     DS18x20 connected to RA5 or RE2 pin.
     MCU reads temperature from the sensor and prints it on the LCD.
     The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
     results, the 18x20's temperature resolution has to be adjusted (constant
     TEMP_RESOLUTION).
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       EasyPIC5
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    DS18x20, LCD 2x16
     SW:              mikroC PRO for PIC
 * NOTES:
     - Place DS1280 jumper (J11) in lower position (board specific).
     - Pulling up PORTE and turning off PORTE LEDs may be required.

*/

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12
const unsigned short TEMP_RESOLUTION =12;
unsigned short CONF_WORD = 0b00011111 + (( TEMP_RESOLUTION - 9 ) << 5 );

char *text = "000.00";
unsigned temp;

void Display_Temperature(unsigned int temp2write) {

  char temp_whole;
  unsigned int temp_fraction;

  // check if temperature is negative
  if (temp2write & 0x8000) {
     text[0] = '-';
     temp2write = ~temp2write + 1;
     }

  // extract temp_whole
  temp_whole = ( temp2write & 0x7FF ) >> 4 ;

  // convert temp_whole to characters
  if (temp_whole/100)
     text[0] = temp_whole/100  + 48;
  else
     text[0] = ' ';
     
  if (temp_whole/10)
     text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  else {
  
       if (temp_whole/100)
          text[1] = '0';
       else
           text[1] = ' ';
       }
      
      
  text[2] =  temp_whole%10     + 48;             // Extract ones digit

  // extract temp_fraction and convert it to unsigned int
  
   temp_fraction = temp2write & 0x000F;
   temp_fraction = temp_fraction * 25/4;

  // convert temp_fraction to characters

  text[4] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  text[5] =  temp_fraction%10      + 48;         // Extract ones digit

  // print temperature on LCD
  Lcd_Out(2, 4, text);
}

void main() {
  ADCON1 = 7;                              // Configure AN pins as digital I/O
//  Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete

  
  Lcd_Init();                                    // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
  Lcd_Out(1, 1, "  Temperature:  ");
  // Print degree character, 'C' for Centigrades
  Lcd_Chr(2,11,178);  // different LCD displays have different char code for degree
                      // if you see greek alpha letter try typing 178 instead of 223

  Lcd_Chr(2,12,'C');
  
  // Configure DS Resolution
  
  Ow_Reset(&PORTE,2);
  Ow_Write(&PORTE,2,0xCC);
  Ow_Write(&PORTE,2,0x4E);
  Ow_Write(&PORTE,2,0);
  Ow_Write(&PORTE,2,0);
  Ow_Write(&PORTE,2,CONF_WORD);
  
  //--- main loop
  do {
    //--- perform temperature reading
    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

    temp =  Ow_Read(&PORTE, 2);
    temp = (Ow_Read(&PORTE, 2) << 8) + temp;

    //--- Format and display result on Lcd
    Display_Temperature(temp);

    Delay_ms(500);
  } while (1);
}

Please note

1) I Corrected the Config calculations that did not work properly.

2) had to add the DS18B20 Config sequence ( lost somewhere, in the new release )

3) Corrected the Temp_whole and Temp_fraction calculations that were false.

BTW, the result of :

Code: Select all

   temp_fraction = temp_fraction * 25/4;
is not the same as :

Code: Select all

temp_fraction *=  25/4;
in the second way 25/4 is treated as an Integer ... Ok ... but not logical !

4) I did not add the DS Waiting loop ... to be sure the reading is that of the last conversion launched ... ( for logging purposes ...)

Alain

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#7 Post by milan » 04 Mar 2009 14:33

Hi,
Acetronics wrote: 1) I Corrected the Config calculations that did not work properly.
Is this questions related to MCU configuration bits ?
If yes, please explain the problem.
When you change the MCU, configuration bits are set to default values automatically.
This example is made for P16F887 with default configuration bits and
if you change MCU to 16F877A there should be no problem.

--------------------------------------------------------------------
Acetronics wrote: 2) had to add the DS18B20 Config sequence ( lost somewhere, in the new release )

3) Corrected the Temp_whole and Temp_fraction calculations that were false.
Everything is there, TEMP_RESOLUTION and RES_SHIFT constants,
temp_whole and temp_fraction variables, it looks like you changed the example a lot.

When using B sensor (12bit resolution), just change the constant from 9 to 12, see this code and comments:

Code: Select all

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

Code: Select all

   temp_fraction = temp_fraction * 25/4;
is not the same as :

Code: Select all

temp_fraction *=  25/4;
in the second way 25/4 is treated as an Integer ... Ok ... but not logical !
Code 1 :
operators * and / have the same precedence so they are grouped from left to right:

Code: Select all

temp_fraction = temp_fraction * 25 / 4;
t1 <- temp_fraction * 25
temp_fraction <- t1 / 4
Code 2 :
operator *= has lower precedence than /:

Code: Select all

temp_fraction *= 25 / 4;
t1 <- 25/4
temp_fraction <- temp_fraction * t1
In C these are two different codes,
see Help-> Language Reference -> Operators -> Operators Precedence
SmartADAPT2 rules !

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

#8 Post by Acetronics » 04 Mar 2009 15:03

Hi, Milan

I Understand here you are a bit under pressure !!! :wink:

Point 1)

it's the DS 18B20 Config ... not the MCU config.

Point 2&3)

NO, there is NO config sequence for DS in your listing ...

Changing resolution from 9 to 12 DO NOT Work :

a) your config calculation is false ( in MkC examples , it was also false ! )
b) there's NO config sequence !!!

result given for temp. is > 150°C for room temp with a sensor in 9 Bits mode !!!

I just dug out the original program from yours ... programmed it as-is ...

temp is 158°C !!! for 9 bits asked for

temp is 19.5°C for 12 bits asked for ...

BUT temp steps are 0.5°C ... so, DS is 9 bits working !!!



I'm Ok for precedence ... but here it is not logical !!! should be the same ... :wink:

Here is YOUR Example, just adapted to 16F877A:

Code: Select all

/*
 * Project name:
     OneWire (Interfacing the DS1820 temperature sensor - all versions)
 * Copyright:
     (c) Mikroelektronika, 2008.
 * Revision History:
     20080930:
       - initial release;
 * Description:
     This code demonstrates one-wire communication with temperature sensor
     DS18x20 connected to RA5 or RE2 pin.
     MCU reads temperature from the sensor and prints it on the LCD.
     The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
     results, the 18x20's temperature resolution has to be adjusted (constant
     TEMP_RESOLUTION).
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       EasyPic5
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    DS18x20, LCD 2x16
     SW:              mikroC PRO for PIC
 * NOTES:
     - Place DS1820 jumper in left position to use RE2 as OW pin (board specific).
     - Turn on LCD backlight switch SW9.7 (board specific).
     - Pull up (place jumper  j5 in upper position)  (board specific)
       used for one wire bus may be required.
*/

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

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

char *text = "000.0000";
unsigned temp;

void Display_Temperature(unsigned int temp2write) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  char temp_whole;
  unsigned int temp_fraction;

  // check if temperature is negative
  if (temp2write & 0x8000) {
     text[0] = '-';
     temp2write = ~temp2write + 1;
     }

  // extract temp_whole
  temp_whole = temp2write >> RES_SHIFT ;

  // convert temp_whole to characters
  if (temp_whole/100)
     text[0] = temp_whole/100  + 48;
  else
     text[0] = '0';

  text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  text[2] =  temp_whole%10     + 48;             // Extract ones digit

  // extract temp_fraction and convert it to unsigned int
  temp_fraction  = temp2write << (4-RES_SHIFT);
  temp_fraction &= 0x000F;
  temp_fraction *= 625;

  // convert temp_fraction to characters
  text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  text[7] =  temp_fraction%10      + 48;         // Extract ones digit

  // print temperature on LCD
  Lcd_Out(2, 5, text);
}

void main() {
  ADCON1 = 7;                    // Configure AN pins as digital
  CMCON  = 7;                       // Disable comparators

  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Turn cursor off
  Lcd_Out(1, 1, " Temperature:   ");
  // Print degree character, 'C' for Centigrades
  Lcd_Chr(2,13,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,14,'C');

  //--- main loop
  do {
    //--- perform temperature reading
    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

    temp =  Ow_Read(&PORTE, 2);
    temp = (Ow_Read(&PORTE, 2) << 8) + temp;

    //--- Format and display result on Lcd
    Display_Temperature(temp);

    Delay_ms(500);
  } while (1);
}

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#9 Post by milan » 04 Mar 2009 16:18

Hi,

i tested the example with P16F877A (configuration bits = default), EasyPIC5, HS 8MHz
and 12bit Dallas 18B20 on RE2 pin.

It works correctly.

I used provided example in this folder:
...mikroC PRO for PIC\Examples\Development Systems\EasyPIC5\One Wire\

Code changes made :
1) Sensor specific

Code: Select all

const unsigned short TEMP_RESOLUTION = 9;
to

Code: Select all

const unsigned short TEMP_RESOLUTION = 12;
2) MCU specific

Code: Select all

  ANSEL  = 0;                                    // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 0;
to

Code: Select all

  ADCON1 |= 7;                                   // Configure AN pins as digital
  CMCON  |= 7;                                   // Disable comparators
I tried on hardware and it works ok.

If you still have the problem, please give us more details,
"there is NO config sequence for DS in your listing" is not good enough :wink:
try to be more specific, try to run/adjust the provided examples first and do the code modifications after that.

If you do many changes in provided example code (like you did in posted code), it is harder to us to track possible code/logic mistakes.
So we always recommend to start from provided examples, I hope I gave a good explanation why we prefer this method.
SmartADAPT2 rules !

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

#10 Post by Acetronics » 04 Mar 2009 16:36

Hi, Milan

I JUST had Made the tests on my EAsy5 before writing to you ...

so,

Everything ALWAYS works well with MKE Tools ...

What do I do here :( ??? ...

... ciao à tutti !!! I do not want to disturb any more.

Alain

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#11 Post by milan » 04 Mar 2009 16:47

Hi,

one wire communication needs pull-up resistors (on RE2 in this case) [see help->OW library->schematics and notes in the example header]
and sometimes LEDs must be turned off (PORTA/E LEDs in this case) [see notes in the example header]

maybe it was a hardware problem.
SmartADAPT2 rules !

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

#12 Post by Acetronics » 04 Mar 2009 17:20

Stop being ridiculous, Milan ... :?

a PicBasicPro program of mine works more than fine with EXACTLY the same Board settings ...

Just with re-programming the MCU ...

Want the listing too ???

You did not do any real tests ... just write the DS config back to 9 bits and tell us what you read on the LCD ...

...

:roll:

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#13 Post by milan » 04 Mar 2009 18:26

Hi,

I tested the code on the hardware as I said before.
Acetronics wrote: You did not do any real tests ... just write the DS config back to 9 bits and tell us what you read on the LCD ...
I think you are missing the point.

Take a look at this part of code :

Code: Select all

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12
const unsigned short TEMP_RESOLUTION = 9;
Default value is 9 - for 9bit resolution sensor.

If someone has 18B20 - 12 bit sensor (this is your case) - then he should change this value from 9 to 12.
Acetronics wrote: You did not do any real tests ... just write the DS config back to 9 bits and tell us what you read on the LCD ...
So there is no point to set it back to 9 and test, as you said, because 12bit sensor is used.
You use 12bit sensor and i used it too to reproduce your HW.
For 12bit sensor TEMP_RESOLUTION should be 12.

Example is made in this way so if you have different sensors (9, 10, 11 or 12 bit resolution)
then you use the same example and make only one change in the entire code (change of the TEMP_RESOLUTION value).
This is very handy and our users are very happy with this solution, but somehow you stumbled on this constant :shock:

If you didn't understand the constant concept, feel free to ask, we will be happy to explain in more details.
If you somehow don't believe that I tested the code on hardware
I can send you the example folder (together with the hex file) and photos/movie of the hardware with hex loaded.
If this is the case you will have to wait until tomorrow (I am not in the office right now :D )
SmartADAPT2 rules !

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

#14 Post by Acetronics » 04 Mar 2009 20:36

Hi, Milan

Ok, finally got it !!! But you just gave me the key to it ... :lol:

Code: Select all


//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor: 

//  18S20: 9  (default setting; can be 9,10,11,or 12) 
//  18B20: 12 

IF i use a DS 18S20 I CAN use "9" or else for a number ??? ....

IF i use a DS18B20 I MUST use "12" and ONLY "12" ... and the DS will stay in the resolution it was formerly programmed.

But, the 18B20 has other resolutions available ( 9,10,11 and 12 bits ) ... That's where the misundertstanding came from.

I Modified it to allow all resolutions, because it was not so clear it MUST be 12 and no other number for DS18B20 ...


Soooo..... In a way I Might apologize ... :lol:

We just were talking of two different things ... no way to agree with each other, then !!!

Alain

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

#15 Post by Acetronics » 05 Mar 2009 20:59

Hi, Milan

I'm back ... :lol:

to wipe off any misunderstandings with Ow ... I propose you this one ( for free !!! )


Code: Select all


/*
 * Project name:
     OneWire (Interfacing the DS1820 temperature sensor - all versions)
 * Copyright:
     (c) Mikroelektronika, 2008.
 * Revision History:
     20080930:
       - initial release;
 * Description:
     This code demonstrates one-wire communication with temperature sensor
     DS18x20 connected to RA5 or RE2 pin.
     MCU reads temperature from the sensor and prints it on the LCD.
     The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
     results, the 18x20's temperature resolution has to be adjusted (constant
     TEMP_RESOLUTION).
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       EasyPIC5
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    DS18x20, LCD 2x16
     SW:              mikroC PRO for PIC
 * NOTES:
     - Place DS1280 jumper (J11) in lower position (board specific).
     - Pulling up PORTE and turning off PORTE LEDs may be required.

*/

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

//  Set TEMP_RESOLUTION to the resolution you want:
//   (can be 9,10,11,or 12)
// ( DS 1820 and DS 18S20 automatically set to default )

const unsigned short TEMP_RESOLUTION = 12;             //Resolution for DS 18B20
unsigned short CONF_WORD;

char *text = "000.00";
unsigned temp;
unsigned short Chip;

void Display_Temperature(unsigned int temp2write) {

  char temp_whole;
  unsigned int temp_fraction;
  
  switch (Chip)
   {
   case 0x10: temp2write = temp2write * 8; break;
   case 0x28: break;
   }

  // check if temperature is negative
  if (temp2write & 0x8000) {
     text[0] = '-';
     temp2write = ~temp2write + 1;
     }

  // extract temp_whole
  temp_whole = ( temp2write & 0x7FF ) >> 4 ;

  // convert temp_whole to characters
  if (temp_whole/100)
     text[0] = temp_whole/100  + 48;
  else
     text[0] = ' ';
     
  if (temp_whole/10)
     text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  else {
  
       if (temp_whole/100)
          text[1] = '0';
       else
           text[1] = ' ';
       }
      
      
  text[2] =  temp_whole%10     + 48;             // Extract ones digit

  // extract temp_fraction and convert it to unsigned int
  
   temp_fraction = temp2write & 0x000F;
   temp_fraction = temp_fraction * 25/4;

  // convert temp_fraction to characters

  text[4] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  text[5] =  temp_fraction%10      + 48;         // Extract ones digit

  // print temperature on LCD
  Lcd_Out(2, 4, text);
}

void main() {
  ADCON1 = 7;                              // Configure AN pins as digital I/O
//  Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete
//  PORTC = 0;
//  TRISC = 0;

  
  Lcd_Init();                                    // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
  Lcd_Out(1, 1, "  Temperature:  ");
  // Print degree character, 'C' for Centigrades
  Lcd_Chr(2,11,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,12,'C');
  
  // Identify sensor
  
  Ow_Reset(&PORTE,2);
  Ow_Write(&PORTE,2,0x33);                      // Read ROM Command
  Chip = Ow_Read(&PORTE,2);                   // Read first BYTE
  
  switch (Chip)
  {
   case 0x28: switch (TEMP_RESOLUTION)
              {
                case 9 : CONF_WORD = 0b00011111 ; break;
                case 10: CONF_WORD = 0b00111111 ; break;
                case 11: CONF_WORD = 0b01011111 ; break;
                case 12: CONF_WORD = 0b01111111 ; break;
              }
  break;
              
  case 0x10: goto NOCONF;
 }
  // Configure DS Resolution
  
  Ow_Reset(&PORTE,2);
  Ow_Write(&PORTE,2,0xCC);
  Ow_Write(&PORTE,2,0x4E);
  Ow_Write(&PORTE,2,0);
  Ow_Write(&PORTE,2,0);
  Ow_Write(&PORTE,2,CONF_WORD);
  
  NOCONF:
  
  //--- main loop
  do {
    //--- perform temperature reading
    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

    temp =  Ow_Read(&PORTE, 2);
    temp = (Ow_Read(&PORTE, 2) << 8) + temp;

    //--- Format and display result on Lcd
    Display_Temperature(temp);

    Delay_ms(500);
  } while (1);
}

just choose the resolution you want, compile, plug the sensor in ... program ... and it works by itself ... :wink:

Alain

Post Reply

Return to “mikroC Beta Testing”