Practical example of C programming

Slides:



Advertisements
Similar presentations
Analog-to-Digital Converter (ADC) And
Advertisements

Bits and Bytes + Controlling 8 LED with 3 Pins Binary Counting and Shift Registers.
1 CSI 101 Elements of Computing Fall 2009 Lecture #4 Using Flowcharts Monday February 2nd, 2009.
Level ISA3: Information Representation
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
1 Lab Equipment. 2 TopicSlides DC Power Supply3-4 Digital Multimeter5-8 Function Generator9-12 Scope – basic controls13-20 Scope – cursors21-24 Scope.
Railway Foundation Electronic, Electrical and Processor Engineering.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Railway Foundation Electronic, Electrical and Processor Engineering.
Digital Fundamentals Floyd Chapter 1 Tenth Edition
8254 Counter/Timer Counter Each of the three counter has 3 pins associated CLK: input clock frequency- 8 MHz OUT GATE: Enable (high) or disable.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Circuits, Gigabyte, RAM, Silicon Chips, Storage Devices Made of melted sand and that is what circuits are embedded into to keep them together. The ability.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals with PLD Programming.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Floyd, Digital Fundamentals, 10 th ed Slide 1 Digital Fundamentals Tenth Edition Floyd © 2008 Pearson Education Chapter 1.
The Principle of Electronic Data Serial and Parallel Data Communication Transmission Rate Bandwidth Bit Rate Parity bits.
Floyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd © 2008 Pearson Education Chapter 1.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Encoding How is information represented?. Way of looking at techniques Data Medium Digital Analog Digital Analog NRZ Manchester Differential Manchester.
Analog and Digital Signals Binary Code 7th Grade Science.
Counters In digital logic and computing, a counter is a device which stores (and sometimes displays) the number of times a particular event or process.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Electronic instrumentation Digitization of Analog Signal in TD
A function generator is usually a piece of electronic test equipment or software used to generate different types of electrical waveforms over a wide.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
FUNCTION GENERATOR.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
DATA Unit 2 Topic 2. Different Types of Data ASCII code: ASCII - The American Standard Code for Information Interchange is a standard seven-bit code that.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Image and Sound Representation
Recursion Version 1.0.
EKT124 Digital Electronics 1 Introduction to Digital Electronics
Digital Control CSE 421.
Programming what is C++
Hun Wie (Theo) SJSU, 2011 Spring Prof: Dr. Sin-Min Lee
UNDERSTANDING YOUR COMPUTER
A bit of C programming Lecture 3 Uli Raich.
Timer and Interrupts.
Digital Fundamentals Floyd Chapter 1 Digital concepts Tenth Edition
Chapter 3 Data Representation
Introduction to electronic communication systems
Bit Operations Horton pp
Error Handling Summary of the next few pages: Error Handling Cursors.
Programmazione I a.a. 2017/2018.
Arrays in C.
File Input/Output.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Assembly Language Programming I: Introduction
Chapter One: Introduction
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Programmable Interval timer 8253 / 8254
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
asyn Driver Tutorial Measurement Computing 1608GX-2A0
Programmable Interval timer 8253 / 8254
Bits and Bytes Topics Representing information as bits
Digital Fundamentals Floyd Chapter 1 Tenth Edition
Standard Form.
Fundamentals of Python: First Programs
Homework Reading Programming Assignments Finish K&R Chapter 1
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
Hun Wie (Theo) SJSU, 2011 Spring Prof: Dr. Sin-Min Lee
Computer Organization and Assembly Language
Variables in C Topics Naming Variables Declaring Variables
ECE 352 Digital System Fundamentals
The Programmable Peripheral Interface (8255A)
Bit Operations Horton pp
Presentation transcript:

Practical example of C programming Additional Lecture on C Uli Raich Course on Embedded Systems UCC semester end 2017

C is too abstract? Dompreh tells me 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?

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

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

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

Oscilloscope Example

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.

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!

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 STR RS R/W d3 d2 d1 d0

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

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

Solution: arg count

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

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)

The include file

The test function

Implementing the function

Was it correct?

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

The rectangular wave form

Bit handling functions We need to learn a few bit handling functions before being able to prepare the data for the DAC: | : bitwise or & : 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

Preparing data for the DAC Remember the bit layout of the DAC register? We must write the lowest significant, medium significant and highest significant bits separately We must “or” in the register select bits and read/write bits (write is usually active low) We must strobe in the data (a pulse on the strobe bit) STR RS R/W d3 d2 d1 d0

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 First the include file:

Now the test program

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

Send one short value to the DAC 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: 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 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

...and the test program for it

… with the result

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: and we call this in main.

And this is the final result