It is currently 23 Apr 2019 21:01

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: 17 Jan 2019 03:59 
Offline

Joined: 28 Oct 2007 03:16
Posts: 144
Location: West Australia
Below is some working code.
It's very basic but helped me to go through the basics of passing values to functions
Much help form Rus51 - Thank you.
////////////////////////////////////////////////////////////////////////////////
// Project:         WVL_POINTERS                                              //
// File:            WVL_Pointers.c                                            //
// Function:        Test pointers, functions, variables and arrays            //
// MCU:             PIC18F46K22                                               //
// Board:           EasyPICv7                                                 //
// Power            5V.                                                       //
// Compiler:        mikroC PRO for PIC version 7.2.0                          //
// Programmer:      On-board                                                  //
// Author:          WVL                                                       //
// Date:            17 January 2019                                           //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function - Add 1 to incoming number and do not return the result           //
unsigned int Number_Add(unsigned int incoming_number){                        //
    incoming_number++;                                                        //
    return;                                                                   //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////
// Function - Add 1 to incoming number and return the result                  //
unsigned int Number_Add_Return(unsigned int incoming_number){                 //
    incoming_number++;                                                        //
    return incoming_number;                                                   //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////
// Function - Add 1 to incoming number using a pointer                        //
unsigned int Number_Add_Ptr1(unsigned int *incoming_number){                  //
    *incoming_number = *incoming_number+1;                                    //
    return;                                                                   //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////
// Function  - Add 1 to incoming array using a pointer                        //
//             Input: pointer and array length                                //
void Array_Add_Ptr2(int *incoming_array,int array_length){                    //
   unsigned int i;                                                            //
   for(i=0;i<array_length;i++){                                               //
        incoming_array[i] = incoming_array[i]+1;                              //
   }                                                                          //
   return;                                                                    //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////

void main(){
    char                 x = 0;
    char                 *ptr1;
    unsigned int         *ptr2;
    unsigned int         a = 0;     // Used for Printout() that only displays ints
    unsigned int    result = 0;
    unsigned int    my_int = 0;
    char my_string[5] = "4567";     // 5 bytes including NULL terminator
    unsigned int  my_array[4] = {100,200,300,400};   // 4 records 8 bytes
    // Peripherals
    UART1_Init(9600);       // used RC6/Tx1 RCt/Rx1 not used RB6/Tx2 RB7/Rx2
    delay_ms(1);
    // Identify
    UART1_Write_Text("WVL_Pointers on EasyPICv7 PIC18F45K22 at 32MHz\r\n");

    while(1){
        // Function strlen() returns string length not including NULL termination
        result = strlen(my_string);   // result = 4, excludes the NULL termination
        Printout(UART1_Write,"\r\nMeasue length of my_string\r\n");
        Printout(UART1_Write,"Length:%u\r\n",result);

        // Measure array size - this will not work in a function - must send a count of elements in array
        Printout(UART1_Write,"Measue bytes, variable type and size of my_array\r\n");
        Printout(UART1_Write,"Total Bytes in my_array: %u\r\n",sizeof(my_array));
        Printout(UART1_Write,"Bytes per array record : %u\r\n",sizeof(my_array[0]));
        Printout(UART1_Write,"Records in my_arrays   : %u\r\n",sizeof(my_array)/sizeof(my_array[0]));
       
        // Use pointers to get addresses
        ptr1 = &PORTD;
        ptr2 = &my_array;
        Printout(UART1_Write,"Test using pointers to get addresses\r\n");
        Printout(UART1_Write,"Address of PORTD               : %u\r\n",ptr1); // 3971/F83h is the SFR address of PORTD
        Printout(UART1_Write,"Address of my_array            : %u\r\n",ptr2); // returns 109 as starting point of my_array[0]
        ptr2 = ptr2+1;  // now pointing to 2nd int of my_string
        Printout(UART1_Write,"Address of 2nd int of my_array: %u\r\n",ptr2);  // returns 111 because my_array is an int
        Printout(UART1_Write,"Value of 2nd int of my_array  : %u\r\n",*ptr2); // returns value stored at my_array[1]
       
        // Test function with no 'return'
        my_int = 123;
        a = 0;
        a = Number_Add(my_int);
        Printout(UART1_Write,"Function, pass-by-value, no return\r\n");
        Printout(UART1_Write,"my_int:%u\r\n",my_int);  // my_int unchanged as 'passed by value'
        Printout(UART1_Write,"a     :%u\r\n",a);       // a=0 because function had no 'return'
       
        // Test function with 'return'
        my_int = 123;
        a = 0;
        a = Number_Add_Return(my_int);
        Printout(UART1_Write,"Function, pass-by-value, return\r\n");
        Printout(UART1_Write,"my_int:%u\r\n",my_int);  // my_int unchanged as 'passed by value'
        Printout(UART1_Write,"a     :%u\r\n",a);       // a=124 because function had 'return'
       
        // Test function with pointer
        my_int = 123;
        a = 0;
        Number_Add_Ptr1(&my_int);                      // & needed to pass address of my_int
        Printout(UART1_Write,"Function, pass -by-pointer\r\n");
        Printout(UART1_Write,"my_int:%u\r\n",my_int);  // my_int =124 as 'passed by value'
        Printout(UART1_Write,"a     :%u\r\n",a);       // a=0 because it's unused
       
        // Test function with pointers and array length
        Array_Add_Ptr2(my_array,4);                    // & not needed as my_array is a pointer
        Printout(UART1_Write,"Function, pass-by-pointer, array-length\r\n");
        Printout(UART1_Write,"my_array[0]:%u\r\n",my_array[0]);
        Printout(UART1_Write,"my_array[1]:%u\r\n",my_array[1]);
        Printout(UART1_Write,"my_array[2]:%u\r\n",my_array[2]);
        Printout(UART1_Write,"my_array[3]:%u\r\n",my_array[3]);
       
        // Test function with pointers and sizeof() function - No you can't use sizeof() in a function!
        // Notes from Rus51 with some workrounds:
        // 1. If the array is a char array it will normally be terminated with 0
        // and it is a very easy task to find the end of the array.
        // 2. Make a global variable (accessible from everywhere) and write the array size to it.
        // 3. The first element of the array contains the array size.
        // 4. Write an end marker to the array so that a function can be written
        // which will find this end marker and so find the number of elements in the array
        // This function can be called from everywhere
       
        // housekeeping
        delay_ms(2000);
    }
}

Regards Bill Legge


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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: