C Programming Pointers Pt/1

This first tutorial will be a basic introduction to C programming Pointers.  They are an important part of the C language, providing a powerful and flexible method of manipulating data.  When pointers are used with functions, it allows the values of variables to be changed regardless of where they originated.

Declaring A Pointer

The code snippet below shows how to declare a basic pointer, there are 2 examples showing an int and a char variable.

So the first part of the code int and char in this case, is the type of variable the pointer points to.  The next part which starts with an asterisk (*) is known as the Indirection operator.  The indirection operator tells the compiler P_Name1 is a pointer to an integer and P_Name2 is a pointer to a char.  So the indirection operator tells the compiler which type of variable the pointer name points to, in this case either an integer or a char.  It is important to note that these are pointers and not variables, the compiler knows this due to the indirection operator, and the context in which it has been used.

Pointers can also be declared along with other non pointer variables, an example can be seen below.

Initializing Pointers

Initializing the pointer basically ensures the pointer actually points to something.  This is carried out by the pointer storing the memory address, of the variable it points to.  The ampersand (&) or Reference operator is used to do this, as it returns the address of the variable   An example below shows how the pointer is initialized.

The ampersand is placed before the variable we want the pointer to point to.  When placed before the name of a variable, the reference operator returns the address of the variable. The next example demonstrates a similar initialization, but the pointer and integer declarations have been added as well.

The image below shows a screen capture from Eclipse.  The variable Result has been initialized with a value of 10 (this has been done to avoid some random value being displayed), The value of P_Name is the memory address location of Result, and *P_Name has the same value as Result.

C Programming Pointer Tutorial Eclipse

Basic Example Of Pointer Usage

The next example builds on the previous code, by using similar named variables.  It also demonstrates how the indirection, and reference operators can be used in a basic form.

The result of this basic program is displayed below with a screen capture from Eclipse.

C Programming Pointer Tutorial EclipseSo what we can see here, well the variable Result was declared and initialized with a value of 10,  in line 4.  The first line from the image which reads “Direct Access, Result = 10” shows the value of Result printed, by directly accessing the variable value.  The second line which reads “InDirect Access, Result = 10” demonstrates indirect access to the value of Result, by using the pointer P_Name, which was initialized to point to Result in line 10.Line 15 of the program uses the indirection operator, to print the value stored in the address location pointed to by P_Name.

The third line which reads “The address of Result = 2686696” is produced by line 18 of the code.  This uses the reference operator to print the address of Result, this is the actual memory address of variable Result in the computer memory.  The forth line which reads the same as the third line, uses the pointer P_Name which points to the memory address of Result in line 19.

To summarize whats happening here:

*P_Name and Result both refer to the contents of Result

P_Name and &Result both refer to the memory address of Result

Computer Memory

At this stage it’s important to talk a little about computer memory as well as variable types.

When a variable is declared inside a C program, the compiler sets aside a unique memory location for that variable.  In the last example the pointer P_Name points to the address of the variable Result, and the program demonstrated how the memory location address could be displayed.  By having unique names for variables it ensures the correct memory location is accessed, the memory addresses are generally hidden from you and not usually used.

Different variable types consume different amounts of memory i.e. int, char etc, also the architecture of the system determines variable sizes i.e. 8 bit, 16 bit etc.  The simple program below can be run on your system.

The output from this program can be seen in the image below, this shows the differing amounts of memory each variable type consume.

C Programming Pointer Tutorial EclipseWith this knowledge of how different data types consume varying amounts of memory bytes,  how does the pointer handle the address of multi-byte variables?  Quite simply the pointer points to the first (lowest) address byte of the variable, and the variable size is known by the compiler.

Examples Code

The link below contains the zip file with the complete C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

Pointer examples code

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

C Programming Functions Pt/1

C programming functions Part 1, will be a basic introduction to functions in the C language.

A function is an independent part of the program, and can perform a specific tasks without interference from other parts of the program.  Functions can be passed parameters and can also return values.

There are 2 types of functions in C, the existing standard library functions i.e. printf, scanf, gets etc, and user defined functions, that are written for specific tasks by the user.

A user function when used in C programming, should first have a function prototype, then a function definition and then there is the function call inside of the main function.  The image below shows a basic program with a user defined function, illustrating a function prototype, function definition and function call.

C Programming Functions Tutorial Eclipse

The function prototype provides the compiler with information about the function, that will be defined later in the function definition.  The prototype contains the return type, the function name and any parameters with variable types the function is passed.  The function prototype ends with a semicolon (;).  The function prototype is usually included before the main function.

The function definition is the actual function.  The first line of the function definition is identical to the function prototype i.e. return type, the function name and any parameters. Additionally the function definition contains all the code that the function will execute, i.e. statements etc.  The function definition unlike the function prototype does not end with a semicolon, but uses curly brackets or braces ({ }), these surround the code that forms the function body.  The function definition can be placed before the main function, but it is often good practice to place it after.

The function call is used to execute the function, and is run from within the main function.

Defining A Function

A function definition in C programming consists of a function header and a function body.

return_Type – A function may return a value, the return type is the data type of the value the function returns i.e. int, char etc.  A function can also not return a value, in this case the return type would simply be the keyword void.

function_name – The function name is the unique name for the function chosen by the programmer.  There are certain rules that have to abide by for the C language.

parameters – When a function is called, a value is passed to the parameters.  A function needs to know what data type the arguments will be, any of C’s data types can be passed to a function.  If the function takes no parameters then the keyword void is used to indicate this.

C Programming Functions Tutorial Eclipse

Simple Function Examples

For all these examples Eclipse Kepler was used, there is a small downloadable zip file below with all the full examples including includes etc.

Example 1

First lets break down the entire code by line number:  Lines 1 and 2 contain global variables, variables a, b, d and e are all initialized with values.  Line 4 is the function prototype.  The main function starts on line 6 and finishes on line 15.  Line 8 and 9 are both function calls, for the same function but are passed different arguments.  Also line assigns it’s returned value to the variable c, and line assigns it’s return value to the variable f.  Lines 11 and 12 call a C library function called printf, this is used to firstly print the value of the variable c on the screen, return a line and then print the value of f on the screen.  Line 14 is required and in this case the main functions parameter is void, so nothing is returned, hence the zero. Line 17 is the start of the function definition, and also contains the function header.  Line 18 to 20 is the function body and the end of the function definition.

So what does this simple function do?  Well when the program is run line 8 is called first, this executes the product function using variables a and b as arguments.  In the function definition (and prototype) x and y can be looked at as place holders.  So when variables a and b are passed to the function, a is placed where x is and b is placed where y is.  Then the function simply multiplies the 2 arguments together and returns the result.  The result is then assigned to the variable c.

The same is true for the function call in line 9, variables with a different name and value are used, but otherwise it is the same sequence of events.  After this lines 11 and 12 are called and the values of c and f are printed on the screen.  So the result will be c = a*b therefore 50 = 10*5 and f = d*e therefore 25 = 5*5.

Example 2

First lets break down the entire code by line number, as with the previous example:  Line 1  contains the first function prototype, this function does not return a value, but accepts 1 parameter.  Line 2 contains the second function prototype, this function returns a value in the form of an integer, but does not accept any parameters.   The main function starts on line 4 and finishes on line 9.  Line 11 to 16 is the function definition for the first function. Line 18 to 24 is the function definition for the second function.

So whats going here?  Well when the main function is called, Line 6 is reached and the first function is called (function_1).  The function_1 is defined as not returning any value, but it accepts 1 parameter in the data type of an integer.  The function_2 is defined as returning a value in the data type of an integer, but not taking any parameters.  So when function_1 is called it then calls function_2, and the return value from function_2 becomes the parameter or argument passed to function_1.  The return value of function_2 is very simple in this case, and just equates to the variable a which has a value of 10.  So the variable a is passed to function_1, using the place holder analogy this value becomes the variable x.  There are 3 local variables inside function_1, these are b (10), c (5) and result.  Then x is multiplied with b and divided by c, the result of the calculation is then assigned to the variable result.  Finally the value of result is printed on the screen using the printf function.

So the final outcome of this program will be resultx*b/c therefore 20 = 10*10/5.

Examples Code

The link below contains the zip file with the complete C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

Function examples code

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

Stellaris DAC7571 Tutorial

The Stellaris DAC7571 tutorial will show how a LM3S6965 evaluation board is interfaced to the DAC7571 using the I2C bus.

DAC7571 Hardware

The DAC7571 is manufactured by Texas Instruments, and has the following key features which make it ideal for experimenting with:

  • 6 pins IC in a SOT23 package
  • 12 bit buffered voltage output, therefore the DAC has a range of 2 to the power of 12 = 4096
  • I2C interface
  • Supply voltage from +2.7V to +5.5V

The DAC comes in an SMT package, TI supply SMT prototyping boards which allow the DAC to then give the footprint of a standard 8pin DIP package.  This method is useful as it allows the DAC to be used in various other projects and easily reused. The image below shows the completed prototyping board, fitted to small veroboard used for testing the I2C bus. Stellaris DAC7571 Tutorial using the LM3S6965 evaluation PCB

DAC7571 I2C

This is not a tutorial on I2C but it is necessary to give a brief decsription of the protocal, as these leads into explaining how to communicate with the DAC7571 correctly.  The datasheet for the DAC7571 can be downloaded from here I2C is a 2 wire protocol using a data wire (SDA) and a clock wire (SCL).  There is a Master device which initiates contact with up to 127 Slave devices on a single bus, each slave has an individual 7 bit address.  There can be more than one Master on a bus, but this is beyond the scope of this project, so will not be covered here.  For this project the master will be the LM3S6965 and the DAC is the slave (the DAC can only be a slave).  There is a certain sequence of events for any data bus, which is covered briefly in the next few lines.

  1. The SDA and SCL lines when in idle state naturally sit high or logic level 1.  The master initiates contact on the bus by pulling the SDA line low, when the SCL line is still high, this indicates to any slaves on the bus serial data transfer has started.
  2. After this the master generates the serial clock on the SCL line.
  3. The master then sends out an address byte of the slave it wants to communicate with.
  4. After sending out the address byte the master generates a ninth SCL pulse, then monitors the SDA line during the high period of this ninth clock cycle.
  5. The SDA line should now be pulled low by the receiving slave during the high period of the ninth clock cycle, this is an acknowledge signal and lets the master know the address sent successfully matches a slave on the bus. Further data bytes can be sent to or read from a slave by the master, using the a similar method to the steps explained.

The I2C protocol has fairly strict timing requirements, so it’s important to note while the bit stream is generated, the master ensures the timing for valid data.

The DAC7571 datasheet contains a great deal of information about interfacing, and all the necessary information regarding the I2C interface can be found on page 13 to 18. It is only possible to have 2 of the DAC7571 devices on one I2C bus, this is due to the way the slave address is set on the DAC. Pin 6 is called A0, pending how this pin is connected determines the address of the DAC, if the pin is connected to Gnd the slave address is 1001100 (0x4C), if A0 is connected to Vcc then the slave address will be 1001110 (0x4E).  A0 for this project was connected to Gnd. Now that the address is taken care of, it was necessary to understand how to send data to the DAC.  The image below is taken from page 17 of the datasheet. Stellaris DAC7571 Tutorial using the LM3S6965 evaluation PCB From the table it can be seen that 2 bytes of data need to be sent to the DAC, so this is effectively makes up 16 bits.  The first 4 bits are used for control data, the remaining 12 bits are used to set the DAC value, hence it’s a 12 bit DAC.  The first byte starts with two zeros then contains two control data bits, the last 4 bits of the first byte contain the first 4 bits of a 12 bit word. The second byte contains the last 8 bits of the 12 bit word.  The image below demonstrates how the data is broken up. Stellaris DAC7571 Tutorial using the LM3S6965 evaluation PCB

Software

The Stellaris has a library specifically written for the I2C protocol contained within the TI software bundle StellarisWare, this makes interfacing with devices which support I2C and many other protocals much easier, compared to lower end microcontrollers like the MSP430G series.  An essential read is the Stellaris Peripheral Driver Library in PDF format, which can be downloaded from here. There are sections dedicated to all the driver libraries, this document was referenced heavily for this project.  In order to use I2C on the Stellaris, the peripheral must be enabled (there are two I2C ports on the Stellaris I2C0 and I2C1). Then the port the peripheral is on must also be enabled, followed by the individual pins on that port. The following functions below carry out the port enabling.

Once the ports have been enabled the I2C needs to be initialised, via the following function which also sets the speed of the bus (false indicates 100kHZ).

After this the microcontroller can begin it’s duty as master, the code below can be used to test the DAC.  Further code was written using a function with all the code below placed inside, then the function was simply passed parameter values to set the DAC output voltage.

The DAC supply voltage is 3.3V, therefore 3.3V/4096 = 806μV per step, so a value of 1532 for example should be as follows 1532*806μV = 1.235V When the above code was written into a test program, 1.24V was observed on the multimeter.  The Hex values of 0x05 as the first byte and 0xFC on the second byte equate to a 12 bit binary value of 010111111100, which is equal to 1532 in decimal.

Example Code

The link below contains the zip file with the complete C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website Stellaris DAC7571 I2C C code

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.