C Language Advanced Concepts. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Similar documents
EL6483: Brief Overview of C Programming Language

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Basic Types, Variables, Literals, Constants

First of all, it is a variable, just like other variables you studied

Tokens, Expressions and Control Structures

High Performance Computing in C and C++

ME 4447/ME Microprocessor Control of Manufacturing Systems/ Introduction to Mechatronics. Instructor: Professor Charles Ume

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Variables. Data Types.

[0569] p 0318 garbage

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Character Stream : It provides a convenient means for handling input and output of characters.

C programming for embedded microcontroller systems.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Review of the C Programming Language for Principles of Operating Systems

C: How to Program. Week /Mar/05

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

A Fast Review of C Essentials Part I

ET156 Introduction to C Programming

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Chapter 2 - Introduction to C Programming

Introduction to C++ with content from

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

6.096 Introduction to C++ January (IAP) 2009

/* defines are mostly used to declare constants */ #define MAX_ITER 10 // max number of iterations

Data Type Fall 2014 Jinkyu Jeong

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

ANSI C Reserved Words

DEPARTMENT OF MATHS, MJ COLLEGE

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Fundamental of Programming (C)

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Introduction to Programming Using Java (98-388)

We do not teach programming

Review of the C Programming Language

Short Notes of CS201

ME 4447/6405 Introduction to Mechatronics Instructor: Professor Charles Ume

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Data Structures Unit 02

Advanced Systems Programming

CS201 - Introduction to Programming Glossary By

Data Representation and Storage. Some definitions (in C)

DECLARAING AND INITIALIZING POINTERS

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Structures, Operators

Computer System and programming in C

CS 61C: Great Ideas in Computer Architecture Introduction to C

CMPE-013/L. Introduction to C Programming

Lecture 2: C Programming Basic

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 1 & 2 Introduction to C Language

Data Types and Variables in C language

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Embedded Controller Programming 2

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Programming in C - Part 2

M.EC201 Programming language

Procedures, Parameters, Values and Variables. Steven R. Bagley

C Syntax Out: 15 September, 1995

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Lectures 5-6: Introduction to C

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Storage class and Scope:

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Computer Organization & Systems Exam I Example Questions

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C Language Summary (Continued)

Object-Oriented Programming

Fundamentals of Programming

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CMPE-013/L. Introduction to C Programming

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Type Conversion. and. Statements

Why embedded systems?

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

CS16 Exam #1 7/17/ Minutes 100 Points total

Presented By : Gaurav Juneja

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

ANSI C Programming Simple Programs

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Programming and Data Structures

Computer Systems Principles. C Pointers

ET156 Introduction to C Programming

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Transcription:

C Language Advanced Concepts 1

Switch Statement Syntax Example switch (expression) { } case const_expr1: statement1; case const_expr2: : statement2; default: statementn; The break statement causes control flow to break out of the currently executing statement Instead of using switch you could always use a series of if-then-else statements instead switch (state) { } case 1: nextstate = 2; case 2: nextstate = 3; case 3: nextstate = 1; default: nextstate = 1; 2

Pointers and Addresses A pointer is a variable that holds the address of a variable Syntax for pointer declaration: type_name *pointer_name; Examples int *ax; char *cp; // declare a variable ax, that is a pointer to an int // declare a variable cp, that is a pointer to a char Use the dereferencing operator * to access the value pointed by a pointer int a, *b; /* get the value pointed to by b */ a = *b; Pointers provide a way to return multiple data items from a function via function arguments b holds the address b the value we want to get 3

Pointers and Addresses (continued) Use the unary operator & to assign the address of a variable to a pointer Example: int x, y; int *ip; ip = &x; y = *ip; // y gets the value of x An array name is actually a pointer to the first element When accessing an array you are actually dereferencing a pointer Example: if an array is defined as int ax[10]; then i = ax[0]; is the same as i = ax[1]; is the same as i = ax[2]; is the same as i = *ax; i = *(ax+1); i = *(ax+2); 4

Example Write a function to square every element in an array We pass the array name into the function Remember that the array name is just a pointer to the first element Function can t modify the pointer, but it can modify what it points to Code void squareall(int a[], int n) { int i; for (i=0; i<n; i++) a[i] = a[i]*a[i]; } void main(void) { int m[] = {1,2,3,4}; squareall(m, 4); /* m now contains 2,4,9,16 */ } 5

Type casting Type casting forces a variable of some type into a variable of different type The format is (type) variable Example: Assume that x1,x2 are of type int (16 bits) Then the expression x1*x2 is also of type int However, the product may be too big to fit into an int It would however, fit into a long (32 bits) If we first convert x1,x2 into type long, then the product will be of type long: long result; int x1, x2; result = ((long) x1) * ((long) x2); // no overflow 6

Working with fixed addresses How to read from a certain memory address? Example: say you want to read the contents of memory at 0x1000 You can t simply do c = 0x1000; Instead you have to do char *address, n; : address = (char *) 0x1000; points to location 0x1000 n = *address; gets contents of memory at that location This is how I/O ports are defined #define PTT *(unsigned char volatile *) (0x0240) then you can do n = PTT; volatile : Tells compiler that this variable can change its value without specific instructions from the program 7

Example Program continually reads Port T, bit 7 V DD Pushbutton Whenever that bit is set, it counts the number of on switches connected to Port T bits 6..3 and outputs the count to Port T bits 2..0 PT7 PT6 PT5 PT4 PT3 PT2 PT1 PT0 Toggle switches LED If PT7 is not set, it outputs zeros to Port T bits 2..0. 8

Approach Initialize Port T data direction register, then go into an infinite loop At each iteration, test PT7 If PT7 is on, count the 1 s on PT6:PT3 and output count to PT2:PT0; else output zeros to PT2:PT0 There are a number of ways to do this We ll show a couple of approaches 9

void main(void) { char count; Approach 1 EnableInterrupts; // Initialize bits 2:0 for output; others for input DDRT = 0x07; for(;;) { if (PTT & 0x80) { count = 0; // check bit PT7 if (PTT & 0x40) // check PT6 count++; if (PTT & 0x20) // check PT5 count++; if (PTT & 0x10) // check PT4 count++; if (PTT & 0x08) // check PT3 count++; // Write count to bits PT2:PT0. Doesn't affect other // bits since they are configured for input. PTT = count; } else PTT = 0; } _FEED_COP(); /* feeds the dog */ } /* loop forever */ 10

void main(void) { char s, count; int i; EnableInterrupts; DDRT = 0x07; // Initialize bits 2:0 for output; others for input for(;;) { if (PTT & 0x80) { // check bit PT7 count = 0; s = PTT >> 3; // shift PT6:PT3 to lower 4 bits } for (i=0; i<4; i++) { count += s & 0x01; // add next bit to count s >>= 1; // shift right again } // Show the use of a switch statement switch (count) { case 0: PTT = 0x0; case 1: PTT = 0x1; case 2: PTT = 0x2; case 3: PTT = 0x3; case 4: PTT = 0x4; } } else PTT = 0; _FEED_COP(); /* feeds the dog */ } /* loop forever */ Approach 2 11

Type modifiers These can be appended to the declaration of a variable volatile, static, extern, register, const Volatile Tells the compiler that the variable can be modified by something else other than the user s code Example: volatile unsigned char sw_input; Static Causes a local variable to retain its value when the function exits, and not disappear as what happens normally Example: static int count; 12

Extern Type modifiers (continued) Makes a global variable visible in other files Example: extern float ratio; Register A hint to the compiler that the variable will be heavily used and that you want it be kept in a processor register if possible Example: register char i; Const Creates a read-only variable (i.e., a constant) Example: const float pi = 3.14; 13

Structures A structure is a group of related variables that can be accessed through a common name Example: declare a structure type struct point { }; int x; int y; point x y Define an instance struct point pt; Access the elements of the structure sq_distance = pt.x * pt.x + pt.y * pt.y; 14

Bitfield You can specify that a structure is composed of bitfields Example struct mybitfields { } test; unsigned short a : 4; // a is 4 bits wide unsigned short b : 5; // b is 5 bits wide unsigned short c : 7; // c is 7 bits wide This structure has total size of 4+5+7 = 16 bits test.a = 2; test.b = 31; test.c = 0; The bits are arranged as follows: 00000001 11110010 cccccccb bbbbaaaa 15

Unions A union is an object that may hold (at different times) objects of different types and sizes They occupy the same physical memory Example: declare a union type union u_tag { }; int x; char c[2]; x u_tag c[0] c[1] Define an instance union u_tag u; first interpretation a two byte integer second interpretation a character array Access the elements of the union u.x = 0x1234; // now u.c[0] contains 0x12, u.c[1] contains 0x34 16

Example Declaration of port T (from include file mc9s12c32.h ) /*** PTT - Port T I/O Register; 0x00000240 ***/ typedef union { byte Byte; struct { byte PTT0 :1; /* Port T Bit 0 */ byte PTT1 :1; /* Port T Bit 1 */ byte PTT2 :1; /* Port T Bit 2 */ byte PTT3 :1; /* Port T Bit 3 */ byte PTT4 :1; /* Port T Bit 4 */ byte PTT5 :1; /* Port T Bit 5 */ byte PTT6 :1; /* Port T Bit 6 */ byte PTT7 :1; /* Port T Bit 7 */ } Bits; } PTTSTR; extern volatile PTTSTR _PTT @(REG_BASE + 0x00000240UL); #define PTT #define PTT_PTT0 #define PTT_PTT1 #define PTT_PTT2 #define PTT_PTT3 #define PTT_PTT4 #define PTT_PTT5 #define PTT_PTT6 #define PTT_PTT7 _PTT.Byte _PTT.Bits.PTT0 _PTT.Bits.PTT1 _PTT.Bits.PTT2 _PTT.Bits.PTT3 _PTT.Bits.PTT4 _PTT.Bits.PTT5 _PTT.Bits.PTT6 _PTT.Bits.PTT7 The @ is a CodeWarriorspecific extension to C, used for assigning variables to specific addresses. 17