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.

pic 16f887 c program in mikro c compiler ..cant understand a word of it!

Status
Not open for further replies.

libyantiger

Member level 5
Joined
Aug 10, 2016
Messages
90
Helped
0
Reputation
0
Reaction score
3
Trophy points
1,288
Activity points
2,269
hi
after spend year studing assembly and 3 month studying c langauge "general c not c for mcu"
i thought i have the magic wand of going to "embeded systems design"

i thought it is time to go for c language that easy to learn and quick to wirite....time for making some serious project and no more led blinking

however it is not as easy as i thought i find program that reallly challanges my skills and it brought me down to ground

lets first write what the program inventor said about it

"Temperature measurement is one of the most common tasks performed by the
microcontroller. A DS1820 sensor is used for measurement here. It is capable of measuring
temperature in the range of -55 °C to 125 °C with 0.5 °C accuracy. For the purpose of
transferring data to the microcontroller, a special type of serial communication called 1-wire is used.
Due to a simple and wide use of these sensors, commands used to run and control them are in
the form of functions stored in the One_Wire library. There are three functions in total:
 Ow_Reset is used for reseting sensor;
 Ow_Read is used for receiving data from sensor; and
 Ow_Write is used for sending commands to sensor.

This example implies the advantage in using libraries with ready-to-use functions. Concretely,
you donÂ’t have to study documentation provided by the manufacturer in order to use this sensor. It is sufficient to copy some of these functions in the program. If you want to know how any of them is declared, just right click on it and select the Help option.

Code:
[COLOR=#808080][I]/*Header******************************************************/[/I][/COLOR]
here is the  code
 
sbit LCD_EN at RB5_bit[COLOR=#339933];[/COLOR]
sbit LCD_D4 at RB0_bit[COLOR=#339933];[/COLOR]
sbit LCD_D5 at RB1_bit[COLOR=#339933];[/COLOR]
sbit LCD_D6 at RB2_bit[COLOR=#339933];[/COLOR]
sbit LCD_D7 at RB3_bit[COLOR=#339933];[/COLOR]
sbit LCD_RS_Direction at TRISB4_bit[COLOR=#339933];[/COLOR]
sbit LCD_EN_Direction at TRISB5_bit[COLOR=#339933];[/COLOR]
sbit LCD_D4_Direction at TRISB0_bit[COLOR=#339933];[/COLOR]
sbit LCD_D5_Direction at TRISB1_bit[COLOR=#339933];[/COLOR]
sbit LCD_D6_Direction at TRISB2_bit[COLOR=#339933];[/COLOR]
sbit LCD_D7_Direction at TRISB3_bit[COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// End LCD module connections[/I][/COLOR]
[COLOR=#993333]const[/COLOR] [COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]short[/COLOR] TEMP_RESOLUTION [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]9[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#993333]char[/COLOR] [COLOR=#339933]*[/COLOR]text [COLOR=#339933]=[/COLOR] [COLOR=#ff0000]"000.0000"[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#993333]unsigned[/COLOR] temp[COLOR=#339933];[/COLOR]
[COLOR=#993333]void[/COLOR] Display_Temperature[COLOR=#009900]([/COLOR][COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]int[/COLOR] temp2write[COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
[COLOR=#993333]const[/COLOR] [COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]short[/COLOR] RES_SHIFT [COLOR=#339933]=[/COLOR] TEMP_RESOLUTION [COLOR=#339933]-[/COLOR] [COLOR=#0000dd]8[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#993333]char[/COLOR] temp_whole[COLOR=#339933];[/COLOR]
[COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]int[/COLOR] temp_fraction[COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// check if temperature is negative[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR]temp2write [COLOR=#339933]&[/COLOR] [COLOR=#208080]0x8000[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]0[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#ff0000]'-'[/COLOR][COLOR=#339933];[/COLOR]
temp2write [COLOR=#339933]=[/COLOR] ~temp2write [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]1[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#009900]}[/COLOR]
[COLOR=#666666][I]// extract temp_whole[/I][/COLOR]
temp_whole [COLOR=#339933]=[/COLOR] temp2write [COLOR=#339933]>>[/COLOR] RES_SHIFT [COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// convert temp_whole to characters[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR]temp_whole[COLOR=#339933]/[/COLOR][COLOR=#0000dd]100[/COLOR][COLOR=#009900])[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]0[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_whole[COLOR=#339933]/[/COLOR][COLOR=#0000dd]100[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#b1b100]else[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]0[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#ff0000]'0'[/COLOR][COLOR=#339933];[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]1[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]temp_whole[COLOR=#339933]/[/COLOR][COLOR=#0000dd]10[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract tens digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_whole[COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract ones digit[/I][/COLOR]
[COLOR=#666666][I]// extract temp_fraction and convert it to unsigned int[/I][/COLOR]
temp_fraction [COLOR=#339933]=[/COLOR] temp2write [COLOR=#339933]<<[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#0000dd]4[/COLOR][COLOR=#339933]-[/COLOR]RES_SHIFT[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
temp_fraction [COLOR=#339933]&=[/COLOR] [COLOR=#208080]0x000F[/COLOR][COLOR=#339933];[/COLOR]
temp_fraction [COLOR=#339933]*=[/COLOR] [COLOR=#0000dd]625[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// convert temp_fraction to characters[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]4[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_fraction[COLOR=#339933]/[/COLOR][COLOR=#0000dd]1000[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract thousands digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]5[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]temp_fraction[COLOR=#339933]/[/COLOR][COLOR=#0000dd]100[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract hundreds digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]6[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]temp_fraction[COLOR=#339933]/[/COLOR][COLOR=#0000dd]10[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract tens digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]7[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_fraction[COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract ones digit[/I][/COLOR]
[COLOR=#666666][I]// Display temperature on LCD[/I][/COLOR]
Lcd_Out[COLOR=#009900]([/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000dd]5[/COLOR][COLOR=#339933],[/COLOR] text[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#009900]}[/COLOR]
[COLOR=#993333]void[/COLOR] main[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
ANSEL [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Configure AN pins as digital I/O[/I][/COLOR]
ANSELH [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR]
C1ON_bit [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Disable comparators[/I][/COLOR]
C2ON_bit [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR]
Lcd_Init[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Initialize LCD[/I][/COLOR]
Lcd_Cmd[COLOR=#009900]([/COLOR]_LCD_CLEAR[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Clear LCD[/I][/COLOR]
Lcd_Cmd[COLOR=#009900]([/COLOR]_LCD_CURSOR_OFF[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Turn the cursor off[/I][/COLOR]
Lcd_Out[COLOR=#009900]([/COLOR][COLOR=#0000dd]1[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000dd]1[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#ff0000]" Temperature: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// Print degree character, 'C' for Centigrades[/I][/COLOR]
Lcd_Chr[COLOR=#009900]([/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR][COLOR=#0000dd]13[/COLOR][COLOR=#339933],[/COLOR][COLOR=#0000dd]223[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// different LCD displays have different[/I][/COLOR]
[COLOR=#993333]char[/COLOR] code [COLOR=#b1b100]for[/COLOR] degree
[COLOR=#666666][I]// if you see greek alpha letter try typing 178 instead of 223[/I][/COLOR]
Lcd_Chr[COLOR=#009900]([/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR][COLOR=#0000dd]14[/COLOR][COLOR=#339933],[/COLOR][COLOR=#ff0000]'C'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#666666][I]//--- main loop[/I][/COLOR]
[COLOR=#b1b100]do[/COLOR] [COLOR=#009900]{[/COLOR]
[COLOR=#666666][I]//--- perform temperature reading[/I][/COLOR]
Ow_Reset[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Onewire reset signal[/I][/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0xCC[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command SKIP_ROM[/I][/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0x44[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command CONVERT_T[/I][/COLOR]
Delay_us[COLOR=#009900]([/COLOR][COLOR=#0000dd]120[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
Ow_Reset[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0xCC[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command SKIP_ROM[/I][/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0xBE[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command READ_SCRATCHPAD[/I][/COLOR]
temp [COLOR=#339933]=[/COLOR] Ow_Read[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
temp [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]Ow_Read[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]<<[/COLOR] [COLOR=#0000dd]8[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]+[/COLOR] temp[COLOR=#339933];[/COLOR]
[COLOR=#666666][I]//--- Format and display result on Lcd[/I][/COLOR]
Display_Temperature[COLOR=#009900]([/COLOR]temp[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
Delay_ms[COLOR=#009900]([/COLOR][COLOR=#0000dd]500[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#009900]}[/COLOR] [COLOR=#b1b100]while[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#0000dd]1[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
----------------------------------------

here are my question

to me function Display_Temperature(temp); is basless ......from where it comes? what it does ? i cant see those curley at the end of the program

that really does show the contents of this function or what it does ?

*text is pointer ....pointer is supposed to point to a memory location not to just normal float value ?

char *text = "000.0000"; ???????????????????

the interger varible int temp2write is being manipulated with some number to detect if it zero or not ( assembly is best...go to status register if u want so)

i cant get this ?

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

temp_whole = temp2write >> RES_SHIFT ; we can shift a byte right ...to shift a byte to another byte right is beyond my kin


>> is the shift sign .....res_shift is constant how a varible can be shifted right to a constsant i just mind torture experience??

also it is silly to defind a = b-1 where b =1 ....is not it straight forward just to say that a =0?

this madness happen in this line

const unsigned short TEMP_RESOLUTION = 9;
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;


help me my mind is blown and i feel i have to burn all books that i have read ....no matter how you kill your slef in reading it is always
frustrating
 
Last edited by a moderator:

here is the code
No.... here is the code - with formatting preserved:

Code:
/*
 * Project name:
     OneWire (Interfacing the DS1820 temperature sensor - all versions)
 * Copyright:
     (c) Mikroelektronika, 2009.
 * Revision History:
     20080930:
       - initial release;
       - 20090720 - modified by Slavisa Zlatanovic;
 * 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:             PIC16F887
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
     Dev.Board:       EasyPIC6
                      http://www.mikroe.com/en/tools/easypic6/
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    DS18x20, LCD 2x16
                      http://www.mikroe.com/en/tools/components/
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/en/compilers/mikroc/pro/pic/
 * NOTES:
     - Place DS1280 jumper (J11) in the right position(RE2).
     - 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;

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() {
  ANSEL  = 0;                                    // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 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,13,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,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);
}
to me function Display_Temperature(temp); is basless
A prototype definition is optional if the function is written before it's first use.
what it does ?
There is a comment in code to tell you:
Code:
    //--- Format and display result on Lcd
    Display_Temperature(temp);
i cant see those curley at the end of the program
There is a closing brace at the end of the function, just before main
*text is pointer ....pointer is supposed to point to a memory location
Yes it does - it points to the memory address of string 'text'
not to just normal float value ?
A pointer can point to anything
char *text = "000.0000"; ???????????????????
Code:
char *text = "000.0000"; reserve 9 bytes of (RAM) memory and initialise 8 bytes ASCII and null (zero)
the interger varible int temp2write is being manipulated with some number to detect if it zero or not
bit 15 of a signed integer is set if the number is negative. The programmer chose to put a '-' character for display and then convert to a positive number.
 
I tried to reformat the original message but it has embedded special characters. In future Libyantiger, just copy the lines of source code as text and past them into the message window then type '[ C O D E ]' just before it and '[ / C O D E ]' just after it, remove the spaces, I had to put them in so it didn't treat this message as code!

Brian.
 
temp_whole = temp2write >> RES_SHIFT ; we can shift a byte right ...to shift a byte to another byte right is beyond my kin
shift temp2write to the right by (RES_SHIFT) bits and store the result into temp_whole.

it is silly to defind a = b-1 where b =1 ....is not it straight forward just to say that a =0?
This is only silly if you KNOW that b will only ever be 1.

RES_SHIFT can have value 1,2,3 or 4 depending on what definition was given for TEMP_RESOLUTION as explained in code comments:
Code:
//  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;
 

shift temp2write to the right by (RES_SHIFT) bits and store the result into temp_whole.

This is only silly if you KNOW that b will only ever be 1.

RES_SHIFT can have value 1,2,3 or 4 depending on what definition was given for TEMP_RESOLUTION as explained in code comments:
Code:
//  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;


RES_SHIFT is defined as constant sir? so it may not change its value?
TEMP_RESOLUTION is also constant?
 

TEMP_RESOLUTION is a constant that can be defined as 9, 10, 11, or 12 depending upon which version of DS18x20 you have connected.

RES_SHIFT could be defined with value 1,2,3 or 4 - but the author chose to make the definition automatic by defining it as TEMP_RESOLUTION - 8.
 

No.... here is the code - with formatting preserved:

Code:
/*
 * Project name:
     OneWire (Interfacing the DS1820 temperature sensor - all versions)
 * Copyright:
     (c) Mikroelektronika, 2009.
 * Revision History:
     20080930:
       - initial release;
       - 20090720 - modified by Slavisa Zlatanovic;
 * 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:             PIC16F887
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
     Dev.Board:       EasyPIC6
                      http://www.mikroe.com/en/tools/easypic6/
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    DS18x20, LCD 2x16
                      http://www.mikroe.com/en/tools/components/
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/en/compilers/mikroc/pro/pic/
 * NOTES:
     - Place DS1280 jumper (J11) in the right position(RE2).
     - 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;

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() {
  ANSEL  = 0;                                    // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 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,13,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,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);
}
A prototype definition is optional if the function is written before it's first use.
There is a comment in code to tell you:
Code:
    //--- Format and display result on Lcd
    Display_Temperature(temp);
There is a closing brace at the end of the function, just before main
Yes it does - it points to the memory address of string 'text'
A pointer can point to anything
Code:
char *text = "000.0000"; reserve 9 bytes of (RAM) memory and initialise 8 bytes ASCII and null (zero)
bit 15 of a signed integer is set if the number is negative. The programmer chose to put a '-' character for display and then convert to a positive number.





A prototype definition is optional if the function is written before it's first use

cant get you sorry please tell me more about this







about the function .....you could write for example lcd(init


then later in the program you will tell that

lcd(initi))

(
set enable pin

unset write pin

etc )


which mean we that the compiler knows what this function is and what it does

or you mean that the temperature function is a library build in function?

could not see any temperature function in library...?




about the pointer that is pointing to specifc address that has float number in it "0000.000"

what does this mean
if (temp2write & 0x8000) {
text[0] = '-';

first bit in the addressed byte is be - character if condition is true?
or the entire byte of that address being pointed by the pointer be filled with the character code of -

so so confused
 
Last edited:

A prototype definition is optional if the function is written before it's first use

cant get you sorry please tell me more about this
Forget I mentioned this - I misunderstood your question.

about the function .....
Your first explanation is correct - this is not a library function.

float number in it "0000.000"
No... This is NOT a float number, it is a string array. There is no float number anywhere in this code.
 

Forget I mentioned this - I misunderstood your question.

Your first explanation is correct - this is not a library function.

No... This is NOT a float number, it is a string array. There is no float number anywhere in this code.




if it is not library function and if it not defined withing the code how it is supposed to compile normally



i mean i cant just write void(supernove)void
and then not write the function whithin the code and supose it to work...

i just uploaded picture try to tell you what my mind see as sound declaration and exceution of function in a legal way

this been violated by tempreature function


just see picture is it funny to see this 24331480_1973929652857012_1195396856_n.jpg
 

mikroC compiler allows you to miss out the function definition if the function appears before first use. In your example the function appears after use, so function definition (prototype) is essential.

Add a function definition for your DS18x20 program if you want to, but mikroC will not care either way.

Function definition might be compulsory on other compilers, or maybe not, I do not know.

If it makes you happy then add a definition something like this:
Code:
void Display_Temperature(unsigned int temp2write);   // function prototype --- Format and display result on LCD

char *text = "000.0000";
unsigned temp;

// function --- convert unsigned int to ASCII string and display result on LCD
void Display_Temperature(unsigned int temp2write) {
 

mikroC compiler allows you to miss out the function definition if the function appears before first use. In your example the function appears after use, so function definition (prototype) is essential.

Add a function definition for your DS18x20 program if you want to, but mikroC will not care either way.

Function definition might be compulsory on other compilers, or maybe not, I do not know.

If it makes you happy then add a definition something like this:
Code:
void Display_Temperature(unsigned int temp2write);   // function prototype --- Format and display result on LCD

char *text = "000.0000";
unsigned temp;

// function --- convert unsigned int to ASCII string and display result on LCD
void Display_Temperature(unsigned int temp2write) {




string of arrays? but why i can see groupe of zeros (00000000)?
 

string of arrays?
No it is not a string of arrays, the area of memory referenced as "text" and pointed to by pointer *text can be thought of as a string, or as an array of ASCII characters.

char *text = "000.0000"; --- will reserve 9 bytes of RAM and initialise those 9 bytes to hexadecimal 30 30 30 2E 30 30 30 30 00

Notice how the 9th byte is zero, which is the null terminator that signifies end of string.

why i can see groupe of zeros (00000000)?
I do not know, as I do not know what you are looking at.

If you mean the zeros in "000.0000", then those are not zeros, they are ASCII characters.
 

No it is not a string of arrays, the area of memory referenced as "text" and pointed to by pointer *text can be thought of as a string, or as an array of ASCII characters.

char *text = "000.0000"; --- will reserve 9 bytes of RAM and initialise those 9 bytes to hexadecimal 30 30 30 2E 30 30 30 30 00

Notice how the 9th byte is zero, which is the null terminator that signifies end of string.

I do not know, as I do not know what you are looking at.

If you mean the zeros in "000.0000", then those are not zeros, they are ASCII characters.




thanks all is well one last thing


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


i didnt get the the details of this operation
 

Code:
// extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);       // shift the DS18x20 temperature register word so that the lowest 4 bits contain the fractional value 1/2 1/4 1/8 1/16
temp_fraction &= 0x000F;                                       // mask off all but lower 4 bits
temp_fraction *= 625;                                            // not sure why we multiply by 625 .... have to think about this one ... give me some time to work it out

Ah... the clue is in this datasheet text:
The core functionality of the DS18B20 is its direct-to-digital temperature sensor. The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively.
This means that at the end of these 3 lines of code, temp_fraction will contain a number in thousandths of a degree
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top