Difference: AnAdditionalLectureOnCProgramming (2 vs. 3)

Revision 32017-11-03 - uli

Line: 1 to 1
 
META TOPICPARENT name="LectureSlides"

Start Presentation

Slide 1: An additional Lecture on C Programming

Creating a Signal Generator

Uli Raich

Course on Embedded Systems
UCC semester end 2017

Slide 2: C is too abstract?

I was told that you find the course on C too abstract!

  • What is is good for?
  • What are pointers good for?
  • Why use command line arguments?
  • What has all this to do with embedded systems?
  • Can I use it for my Physics experiments?

Slide 3: A pulse generator

A pulse generator is a device you use to test your electronics

(e.g. electronics to read out an experiment

which generates electronic signals


On a professional pulse generator you can:

  • change the wave form
    sine, rectangular, triangular, sawtooth,
    arbitrary user defined wave form
  • change the frequency
  • change the pulse height
  • change rise and fall times
  • and many parameters more

Slide 4: The oscilloscope

Before using the signal from the pulse generator

you observe the signal on an oscilloscope

How does an oscilloscope work?

What is

  • vertical gain
  • time base
  • auto versus normal, external, single trigger
  • trigger level and trigger position

Slide 5: A standard oscilloscope

A standard digital storage oscilloscope uses

  • a very fast (GHz) analogue to digital converter (ADC)
  • plenty of knobs and buttons to select
    gain, timebase, trigger parameters …
  • a screen to display the input signal
  • Price: ~ 5kUS$ - 50kUS$
I have a 60 US$ oscilloscope

(not useful for professional applications

but ok for the slow signals we produce

with our electronics) featuring:

  • 2 input channels
  • 40 MHz sampling rate (20 MHz band width)
  • No screen
  • USB connection to a computer

Slide 6: Oscilloscope Example

scope.png

Slide 7: Our pulse generator

Let us create our own pulse generator!
It should provide

  • sine wave
  • rectangular wave
What do we need?

  • A table of numerical values that describes the wave form
  • A digital to analogue converter (DAC) to convert the numbers into a signal level.

Slide 8: The DAC

We have a 12 bit DAC, which creates signal

levels from 0V to Vcc

What is the max. number this DAC can take in

decimal and in hex?

What is the signal resolution in ‰

This is Physics!

Slide 9: DAC access

The DAC has 3 registers with the following bit layout:

register 0 takes the lowest 4 bits of the DAC value

register 1 takes the middle 4 bits

register 2 takes the highest 4 bits

RS selects the register

The strobe line (STR) must pulse (go high and low again)

to read/ write the data (R/W line) from/to the DAC

What is the bit combination and sequence to write the

middle 4 bits with the data 0xa

Slide 10: Breaking down the problem

Even though this will be still a very small and simple

program, let’s break it down into smaller pieces:

  • Get the wave form from the user through command line arguments
  • Create the wave form within a table of
    100 short (16 bit) values. The upper 4 bits will always be zero
  • Function to write all 12 data bits to the DAC

Slide 11: Check the number of cmd line args

Write a piece of code that checks that the user

has entered 1 arguments

The program prints a “Usage” message and exits if the

no of arguments given by the user is not exactly 1

Slide 12: Solution: arg count

Changed:
<
<
cmdLineCount.png
>
>
cmdLineCountv2.png
 

Argument Check

If the no of args is correct we have to check

if the argument given is either

  • “sine”
  • or “rect”
To do this we need a string compare function:

int strcmp(const char *s1, const char *s2)

This function returns zero if the 2 strings match

You must include <string.h> to use this function

Check the argument

Improve your program to include a check

if the argument given is “sine” or “rect”.

Print an error message it it is not

and a success message if it is.

Solution: argument check

Changed:
<
<
checkArgs.png
>
>
genWavev2.png
 

Creating the wave form

As we said, the DAC takes 12 bit values which

can be stored in an array of short (16 bits)

The upper 4 bits of each element will be zero

The generation of the waveform goes into a

separate source file and its associated include file

I give you the example for the sine wave and

you write the code for the rectangular wave

Function test

First we create the framework

We will need:

  • the function itself (genWave.c)
  • an include file describing it (genWave.h) and containing
    definitions needed by it and useful to the calling program
  • a main routine (createWaveForm.c) to call it

Write the Test function

Write a test function (genWaveTest.c) taking one argument

(the wave type) and printing which wave type has been selected.

Which definitions should go into the include file?

How do you have to modify the main program

to call the genWave function?

Dummy function and test (main)

functionTest.png

The include file

includeFile.png

The test function

genWaveTest.png

Implementing the function

sine.png

Was it correct?

gnuplot.png

The rectangular wave form

Write the code for the generation of the rectangular wave form.

The signal should be zero for the first 50 values of the wave,

4095 for the following 50 values

Solution: Rectangular wave

rectWave.png

The rectangular wave form

gnuplotRect.png

Bit handling functions

Changed:
<
<
We need to learn a few bit handling functions before
>
>
We need to learn a few bit handling operators before
  being able to prepare the data for the DAC:
Changed:
<
<
| : bitwise or
>
>
| : bitwise or
 
Changed:
<
<
& : bitwise and
>
>
& : bitwise and
  data += 5 <=> data = data + 5

data |= 5 <=> data = data | 5

~data: invert all bits in data

data >> 4 all bits in data are shifted right by 4

data << 4 all bits in data are shifted left by 4

Changed:
<
<

>
>
example:

data = 16  00010000
data >> 4  00000001
 

DAC preparation

Write a function which prepares a byte for the DAC

The functions takes 3 arguments:

  • The data nibble (4 bits of data)
  • The register selection (0,1,2)
  • The read/write bit
  • Leave the strobe bit zero!
What will be the result if you want to write 5

to register to the middle nibble?

Solution: DAC preparation

sendToHWInclude.png

Now the test program

dacPrep.png

dacPrepResult.png

Strobe

Now create a strobe function which takes DAC data

and generates a strobe signal on the

STR line without touching the other bits

Solution: Strobe

strobe.png

Send one short value to the DAC

Changed:
<
<
Now that we know how to strobe it is easy to write
>
>
Now that we know how to strobe, it is easy to write
  a 12 bit value to the DAC

We must do it in 3 steps:

Changed:
<
<
  • and out all the bits in the data word
    except the last 4 (first data nibble)
>
>
  • "and" out all the bits in the data word
    except the last 4 (first data nibble)
 
  • “or” in the rw and register bits (must be reg 0! )
    and send the dataByte to strobe

Changed:
<
<
  • Shift the data word by for bits and do the same thing
    (now the register bits must be 1 of course)
>
>
  • Shift the data word by for bits and do the same thing
    (now the register bits must be 1 of course)

 
  • Shift again by 4 bits and repeat

Solution: send one data word

sendOneValue.png4

...and the test program for it

sendToHWTest.png

… with the result

sendToHWTestResult.png

Assembling the whole thing

Now we have all the bits and pieces and

finalizing the project becomes easy.

In the file accessing the hardware we add:

sendToDAC.png

and we call this in main.

And this is the final result

pulseGenResult.png

%SLIDESHOWEND%

-- Uli Raich - 2017-10-09

Comments

<--/commentPlugin-->

META FILEATTACHMENT attachment="scope.png" attr="" comment="" date="1507537923" name="scope.png" path="scope.png" size="82903" user="uli" version="1"
META FILEATTACHMENT attachment="waveGen.tar.gz" attr="" comment="" date="1507537882" name="waveGen.tar.gz" path="waveGen.tar.gz" size="833441" user="uli" version="1"
META FILEATTACHMENT attachment="checkArgs.png" attr="" comment="" date="1507537898" name="checkArgs.png" path="checkArgs.png" size="49767" user="uli" version="1"
META FILEATTACHMENT attachment="cmdLineCount.png" attr="" comment="" date="1507537898" name="cmdLineCount.png" path="cmdLineCount.png" size="21340" user="uli" version="1"
META FILEATTACHMENT attachment="createWaveForm.png" attr="" comment="" date="1507537898" name="createWaveForm.png" path="createWaveForm.png" size="64699" user="uli" version="1"
META FILEATTACHMENT attachment="dacPrep.png" attr="" comment="" date="1507537898" name="dacPrep.png" path="dacPrep.png" size="42958" user="uli" version="1"
META FILEATTACHMENT attachment="dacPrepResult.png" attr="" comment="" date="1507537898" name="dacPrepResult.png" path="dacPrepResult.png" size="8913" user="uli" version="1"
META FILEATTACHMENT attachment="functionTest.png" attr="" comment="" date="1507537898" name="functionTest.png" path="functionTest.png" size="41182" user="uli" version="1"
META FILEATTACHMENT attachment="genWaveTest.png" attr="" comment="" date="1507537899" name="genWaveTest.png" path="genWaveTest.png" size="24966" user="uli" version="1"
META FILEATTACHMENT attachment="gnuplot.png" attr="" comment="" date="1507537899" name="gnuplot.png" path="gnuplot.png" size="37025" user="uli" version="1"
META FILEATTACHMENT attachment="gnuplotRect.png" attr="" comment="" date="1507537922" name="gnuplotRect.png" path="gnuplotRect.png" size="29810" user="uli" version="1"
META FILEATTACHMENT attachment="includeFile.png" attr="" comment="" date="1507537922" name="includeFile.png" path="includeFile.png" size="36213" user="uli" version="1"
META FILEATTACHMENT attachment="pulseGenResult.png" attr="" comment="" date="1507537922" name="pulseGenResult.png" path="pulseGenResult.png" size="36834" user="uli" version="1"
META FILEATTACHMENT attachment="rectWave.png" attr="" comment="" date="1507537923" name="rectWave.png" path="rectWave.png" size="29665" user="uli" version="1"
META FILEATTACHMENT attachment="rectWaveGnuPlot.png" attr="" comment="" date="1507537923" name="rectWaveGnuPlot.png" path="rectWaveGnuPlot.png" size="29866" user="uli" version="1"
META FILEATTACHMENT attachment="sendOneValue.png" attr="" comment="" date="1507537923" name="sendOneValue.png" path="sendOneValue.png" size="33147" user="uli" version="1"
META FILEATTACHMENT attachment="sendToDAC.png" attr="" comment="" date="1507537959" name="sendToDAC.png" path="sendToDAC.png" size="30373" user="uli" version="1"
META FILEATTACHMENT attachment="sendToHWInclude.png" attr="" comment="" date="1507537959" name="sendToHWInclude.png" path="sendToHWInclude.png" size="29093" user="uli" version="1"
META FILEATTACHMENT attachment="sendToHWTest.png" attr="" comment="" date="1507537959" name="sendToHWTest.png" path="sendToHWTest.png" size="49464" user="uli" version="1"
META FILEATTACHMENT attachment="sendToHWTestResult.png" attr="" comment="" date="1507537959" name="sendToHWTestResult.png" path="sendToHWTestResult.png" size="30189" user="uli" version="1"
META FILEATTACHMENT attachment="sendWave.png" attr="" comment="" date="1507537959" name="sendWave.png" path="sendWave.png" size="53331" user="uli" version="1"
META FILEATTACHMENT attachment="sine.png" attr="" comment="" date="1507537960" name="sine.png" path="sine.png" size="72687" user="uli" version="1"
META FILEATTACHMENT attachment="sineFunc.png" attr="" comment="" date="1507537960" name="sineFunc.png" path="sineFunc.png" size="68602" user="uli" version="1"
META FILEATTACHMENT attachment="strobe.png" attr="" comment="" date="1507537960" name="strobe.png" path="strobe.png" size="46229" user="uli" version="1"
META FILEATTACHMENT attachment="waveGen.odp" attr="" comment="" date="1508157571" name="waveGen.odp" path="waveGen.odp" size="786000" user="uli" version="1"
Added:
>
>
META FILEATTACHMENT attachment="cmdLineCountv2.png" attr="" comment="" date="1509720249" name="cmdLineCountv2.png" path="cmdLineCountv2.png" size="21507" user="uli" version="1"
META FILEATTACHMENT attachment="argCheckv2.png" attr="" comment="" date="1509720390" name="argCheckv2.png" path="argCheckv2.png" size="49568" user="uli" version="1"
META FILEATTACHMENT attachment="genWavev2.png" attr="" comment="" date="1509720806" name="genWavev2.png" path="genWavev2.png" size="59492" user="uli" version="1"
 
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