It is currently 15 Oct 2018 20:44

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: bytetohex
PostPosted: 20 Mar 2018 22:06 
Offline

Joined: 11 May 2006 22:00
Posts: 41
Still on the quest to move from basic to C. I found this code on the web, it's purpose is to read ds18X20 chip, determine the family group, serial number and temperature, then display the results on an LCD. When I first ran it the serial number was displayed but no information on the family group was displayed and the temperature was 000.0000°C
After a little playing around and reading I found that the line:
bytetohex(family_code, family_code_hex[2]);
in void Resolution()
didn't appear to be doing anything in the way of any conversion. If I removed that line and then replaced the 3 following instances of:
if (family_code_hex[2] == 0xXX) with if (family_code == 0xXX)
Then the code works as expected.

So, the questions are, why did the bytetohex not work and what was the purpose of including it when my modified method worked?

A final, off topic question. Is C fussy as to where functions are placed. My preference would to be to place them after the end of void(main) but the compiler gives a "Undeclared identifier 'Resolution' in expression OneWire.c" error message.

Thanks for any help

// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_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_RES_B = 12;
const unsigned short TEMP_RES_S = 9;
unsigned short TEMP_RESOLUTION;

char family_code;
char family_code_hex[2];

char *text = "000.0000";
unsigned temp;
char sernum;
char sernum_hex[8];
int i;
int colona;


void Resolution()
     {
      Ow_Reset(&PORTE, 2);                         // Onewire reset signal
      Ow_Write(&PORTE, 2, 0x33);                   // Issue command Read_ROM
      Delay_us(120);

      family_code = Ow_Read(&PORTE, 2);
      bytetohex(family_code, family_code_hex[2]);
      if (family_code_hex[2] == 0x28)
         {
         Lcd_Out(2, 1, "     ");
         Lcd_Out(2, 1, "18B20");
         TEMP_RESOLUTION = Temp_Res_B;
         }
      if (family_code_hex[2] == 0x10)
         {
         Lcd_Out(2, 1, "     ");
         Lcd_Out(2, 1, "18S20");
         TEMP_RESOLUTION = Temp_Res_S;
         }
      if (family_code_hex[2] == 0xFF)
         {
         Lcd_Out(2, 1, "NoID ");
         TEMP_RESOLUTION = 0;
         }
     }


void Display_Temperature(unsigned int temp2write)
     {
     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, 7, text);
     }

void main() {
  ANSELE = 0;                                    // Configure PORTE pins as digital
  TRISE.B2 = 1;                                  // Configure RE2 pin as input

  Lcd_Init();                                    // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off

  // Print degree character, 'C' for Centigrades
  Lcd_Chr(2,15,223);                             // 176 ASCII for degree symbol
  Lcd_Chr(2,16,'C');


  // Main loop
  do {

     Resolution();


     //Unique Part Serial Number
     Ow_Reset(&PORTE, 2);                         // Onewire reset signal
     Ow_Write(&PORTE, 2, 0x33);                   // Issue command Read_ROM
     Delay_us(120);

       colona = 17;
       for(i = 0; i <= 7; i++)
          {
            colona = colona - 2;
            sernum = Ow_Read(&PORTE, 2);
            bytetohex(sernum, sernum_hex);
            Lcd_Out(1, colona, sernum_hex);
          }


    // 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(100);
  } while (1);
}


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 20 Mar 2018 22:32 
Offline

Joined: 27 Jun 2010 12:07
Posts: 983
Location: England
Do not convert family_code to hex. Just use it as it is.

ByteToHex function converts a byte to an ASCII string, which is not what you want.

Before you delete the line of code with ByteToHex in it, place the cursor anywhere within the text of ByteToHex and press F1. The help text will show that you were using the function wrongly in any case.


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 20 Mar 2018 23:18 
Offline

Joined: 11 May 2006 22:00
Posts: 41
Thanks for the response hexreader. What you suggest is what I did. I was more interested as to why a piece of code that seems to have been used by a number of people apparently worked for them but not for me. Any way I managed to get it working, and for some perverse reason I seem to learn more by correcting incorrect code than starting from scratch and reading the instructions!!


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 21 Mar 2018 01:38 
Offline

Joined: 18 Feb 2006 13:17
Posts: 4978
ex-egll wrote:
Any way I managed to get it working, and for some perverse reason I seem to learn more by correcting incorrect code than starting from scratch and reading the instructions!!
Well, you may get some results this way, but when you start from a seedy variation on faulty example in Help, don't get your hopes up :mrgreen: . All you'll get is a seemingly working code. Just like 'a number of people' that probably just wanted to see something on LCD and were happy when they did. Wouldn't it be better to start with reading on One Wire - at least enough to recognize good code?

Apart from the bytetohex nonsense, what you now have is code that starts measurement and then disturbs power supply when the sensor needs it stable by reading previous measurement result. Not to mention that the loop timing doesn't conform to measurement speed. Don't you want to have good code instead of something that only looks like it works?

_________________
Replacement libraries for mP PRO and PIC18 processors, mP PRO tips & trics


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 21 Mar 2018 05:34 
Offline

Joined: 11 May 2006 22:00
Posts: 41
Well janni, you have probably hit the nail on the head there! I can plead guilty to some of the comments you have made unfortunately. I must admit to using code that "works" rather than sitting down and hitting the books as half the time the books leave me totally befuddled and frustrated. I could never have been an engineer!

Having played around with DS18x20's before and having seen issues I will go off and hit the spec sheets wrt the timing loop.

Thanks for the input


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 21 Mar 2018 17:03 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3764
Location: Nieuwpoort, Belgium
Perhaps this article can also be of some help: https://libstock.mikroe.com/projects/view/994/article-the-ds18-s-20-ds18b20-ds1820-lib-and-ow-utilities-lib-usage.

_________________
Kind regards, Dany.
There is a crack in everything, that's how the light gets in... (L. Cohen)


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 21 Mar 2018 18:30 
Offline

Joined: 11 May 2006 22:00
Posts: 41
Interesting, many thanks Danny


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 23 Mar 2018 13:25 
Offline

Joined: 24 Aug 2014 17:55
Posts: 148
Location: Sheffield
Quote:
A final, off topic question. Is C fussy as to where functions are placed. My preference would to be to place them after the end of void(main) but the compiler gives a "Undeclared identifier 'Resolution' in expression OneWire.c" error message.


yes you can like this


unsigned int maths_abs(unsigned int val);  //declare your subroutines before main

void main() {
volatile unsigned int trans,abss;
trans=2000;

  while(1){
          abss=maths_abs(trans);
           }


}

//subroutines located below function main you could decalre your subroutines in a header file and then just include that at the top of your code if you wanted to

unsigned int maths_abs(unsigned int val)
{
unsigned int abbs;
 abbs=abs(2-log10(val));
 return abbs;
 }



Regards
Dave

_________________
"When the bugs strike it's no good bashing your head against a brick wall, you might damage the wall"


Top
 Profile  
 
 Post subject: Re: bytetohex
PostPosted: 23 Mar 2018 23:36 
Offline

Joined: 11 May 2006 22:00
Posts: 41
Many thanks for the response on the other part of my post. After looking at many different MikroC programmes I put two and two together and came up with the dame solution. I am waaaaaay to old to be learning a new language, but at least it keeps the grey matter active!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: