Tags:
create new tag
view all tags

Exercises 2: C Programming

Goal:

We will program most of our sensor readout programs in the C programming language and programming a few simple C programs will help us with future exercises. As soon as programs become a bit more complex it becomes important to describe how the program must be built. This description is usually done in Makefiles in such a way that the simple command make will reconstruct the entire program compiling just the code that has been modified and linking the right files and libraries needed be the program.

Exercise -1:

I saw that during the last exercise session you had been cheating on me doing only those exercises for which you could find a cookbook recipe but the one where you had to think yourself you skipped!

I am talking about the calculations with octal and hex number systems. Since you will need this knowledge when we will work with chip registers, I come back to this exercise:

What is the octal value of 101 001 110 ?

How much is 7+5 when calculated in octal?

Convert the octal value of 345 to decimal

Write down the binary representation of the hex value 0x83ab

How much is 7+5 in hexadecimal?

What is the decimal value of 0xa3?

Do these calculations and note down the results. I will pass by and check!

Exercise 1: The code examples from the lectures

Create subdirectories exercises_2 in ~/exercises/problems, ~/exercises/solutions, ~/exercises/doc.
You will do this for each new exercise session, so I will not tell you any more!

Download the code examples from the lectures and put the file into ~/exercises/solutions/exercise_2 (create this directory!). They are attached as an archive file (file extension: .tar.gz) to this page (see on the bottom of the page). cd to ~/exercises/solutions/exercise_2 and extract the individual files with tar zxvf codeExamples.tar.gz. Check in the man page for tar what this does.

Have a look at these examples, compile them and test them. Make sure you understand every little detail. If you don't: ask!

Type at least a few of them yourself using the text editor.

The compile command is

gcc -o hello hello.c

if the source code is in a file called hello.c The binary output file will be called hello and you can execute it with ./hello.

Exercise 2: Hello World!

The first program to be written in C is traditionally the “Hello World!” program. We don’t want to change this tradition and you are therefore asked to write the program that prints “Hello World!”. Create a directory hello in ~/exercises/solutions/exercise_2 and create the source file hello.c in there.
Compile it with the gcc command and run the program to test it (./hello). What does the dot do?

Exercise 3: A calculator

We want to produce a calculator program capable of calculating the 4 basic calculations:

  • addition

  • subtraction

  • multiplication

  • division

The program takes 3 arguments: the first operand followed by the operator followed by the second operand. The user should type calculate 3.4 + 5.8 and the program should print:

3.4 + 5.8 = 9.2

Addition, subtraction, multiplication and division should be performed by functions implemented in separate files, compiled separately and linked to the main calculator program.

Write the 5 C files:

  • calculator.c

  • add.c

  • subtr.c

  • mult.c

  • divide.c

When trying the program you will experience a problem with multiplication. Can you figure out why? Instead of using “*” you must use “\*” and it will work.

Here we will compile the programs separately:

gcc -c calculator.c which will produce an object file calculator.o. Do the same thing with the other source files. Finally link everything to produce the executable:

gcc -o calculator calculator.o add.o subtr.o mult.o divide.o

Exercise 3b: Octal and Hex Calculations

Once your calculator works: Can you convert it to a calculator that calculates in decimal, octal and hex?
Have a look into the man page of sscanf and printf and check for input and output conversions.

Can this also work on negative numbers?

Now you can check if your hand calculations were correct!

Exercise 4: Calculate a sine function

You will probably not make it to this exercise in a single session. We can therefore postpone this one for the next lab session.

One of the devices we will use during the sensor exercises is a digital to analogue converter (DAC), allowing us to generate an arbitrary wave form. This is a 12 bit device and we want to generate a sine wave for it. In order to do this, calculate the values to be sent to the DAC in such a way, that the min/max values of the sine wave are adapted to the 12 bits of the DAC.

Generate 100 values for the sin(x) argument x running from 0 to 2π and write them to a file called sine.dat. You will have to add the math library when linking (add -lm argument when calling gcc).

Read sine.dat with libreoffice and generate a plot. Generate a plot with gnuplot.

-- Uli Raich - 2017-01-03

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatodt exercises_2.odt r1 manage 20.7 K 2017-09-18 - 19:28 UnknownUser  
Unknown file formatgz newCodeExamples.tar.gz r1 manage 13.3 K 2017-09-18 - 20:13 UnknownUser  
Edit | Attach | Watch | Print version | History: r8 < r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r8 - 2017-09-18 - uli
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback