Arrays and pointers question

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
collumgary
Posts: 20
Joined: 07 Sep 2010 14:34

Arrays and pointers question

#1 Post by collumgary » 17 Oct 2010 10:12

Hi, the code has the variable text[0] through to text[7], this would sugest that these values are the componants of an array. However when I look to see where the array has been declaired, the only code I can see is

Code: Select all

char *text = "000.0000";
I can see that text is being pointed to. why is text not declaired with [ ]?, Is this pointer pointing to a variable which contains an array declairation?.

Thanks for any help

Code: Select all

/*
 * 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 = 9;

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

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: Arrays and pointers question

#2 Post by Sobrietytest » 17 Oct 2010 12:18

The pointer declaration simply points to a particular memory location where the first byte of 'text' can be found, subsequent memory bytes contain the rest of the array. There is no limitation to the size of the array at this stage, however, in the ME example the array size has been controlled by the code and can never excede 8 bytes.

Your question is important in that many people use pointers without managing the size of the variables that they contain, this isn't a problem in general computing but it's supremely important with microcontrollers where there is limited memory space. Personally I never use pointers and declare arrays specifically, in this way there is never any chance of causing a memory overflow.

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: Arrays and pointers question

#3 Post by p.erasmus » 17 Oct 2010 13:35

Sobrietytest wrote:Personally I never use pointers and declare arrays specifically, in this way there is never any chance of causing a memory overflow.
Sobrietytest

This is so true , micro's are no PC's with use memory !!!
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

collumgary
Posts: 20
Joined: 07 Sep 2010 14:34

Re: Arrays and pointers question

#4 Post by collumgary » 17 Oct 2010 15:54

Hi, I am relatively new to C and mikroC, can I just clarify a few points.

The text[0] to text[7] limmits the size of the array to 8 bytes?.

The pointer points to the memmory location which stores text[0], text[1] will be stored in the subsequent byte of memory,
text[2] through to text[7] will be stored in the same mannor to complete a continuous 64 bits of memory broken down into 8 containers of text bytes.

Is this a standard way of using arrays or is it MikroC specific?.

In the code the pointer text has been assigned a value of "000.0000", when the code is executed are the zeros replaced with the array values?, if so why are there 8 array values and only seven zeros?

when

Code: Select all

 Lcd_Out(2, 5, text);
is processed, will this read the full array from text[0] to text[7] to be sent to the LCD?

sorry if my questions lack understanding and thanks for any help.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: Arrays and pointers question

#5 Post by Mince-n-Tatties » 17 Oct 2010 21:08

000.0000 there are 7 zero's, yes... but what about the decimal point! that needs a memory location too, in this exact case its text[4].
Best Regards

Mince

collumgary
Posts: 20
Joined: 07 Sep 2010 14:34

Re: Arrays and pointers question

#6 Post by collumgary » 17 Oct 2010 21:55

Thanks for the input M&T, should the decimal point not be text[3] as the array starts at text[0]?.

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Arrays and pointers question

#7 Post by KaranSoin » 18 Oct 2010 02:12

ur right, its text[3]. And clarifying about arrays and pointers, char arrays (more commonly called strings), are treated in a slightly special way in C. They always end with a 0. Almost all the time, compiler takes care of this, eg char *SampleStr = "Oink";
SampleStr[0] = 'O';
SampleStr[1] = 'i';
SampleStr[2] = 'n';
SampleStr[3] = 'k';
SampleStr[4] = 0;

and keep in mind this is not ASCII 0 but actual binary 0 (ASCII '0' is binary 48). So if the SampleStr were to be addressed as an array, then it is an array of 5 bytes, 4 for the text and terminating 0.

Code: Select all

Lcd_Out(2, 5, text);
will this read the full array from text[0] to text[7] to be sent to the LCD?
In this case, the function Lcd_Out() receives an address of the first location of the array (and not the data itself). The Lcd_Out() keeps grabbing data from the address which has been passed to it (incrementing bytes), till it encounters a 0. So the Lcd_Out() is not aware of the size of the text[], simply keeps printing it till it encounters a 0. To understand this better, try putting the following line b4 calling the Lcd_Out() function and see what happens

Code: Select all

test[2] = 0;
Lcd_Out(2,5,text);

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: Arrays and pointers question

#8 Post by Sobrietytest » 18 Oct 2010 07:41

Is this a standard way of using arrays or is it MikroC specific?.
The ANSI C language uses pointers extensively for all types of variables, not just arrays. MikroC adheres to the ANSI C standard so pointers are available for us to use. However, as I mentioned earlier, care needs to be taken so that use of memory space is strictly controlled. The best way of doing this is to avoid using pointers altogether and choose a microcontroller that has adequate memory space for the task.

There is nothing wrong with using pointers - provided that you know exactly how much memory will be in use in the worst possible case. I suppose it comes down to personal preference, mine is to remove the guesswork and 'hardwire' the variable sizes.

collumgary
Posts: 20
Joined: 07 Sep 2010 14:34

Re: Arrays and pointers question

#9 Post by collumgary » 18 Oct 2010 13:04

Thanks for your help everyone, your time and comments are much appreciated.

Post Reply

Return to “mikroC PRO for PIC General”