Embedded Controller Programming 2

Similar documents
CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to Programming Using Java (98-388)

Infix to Postfix Conversion

Introduction. C provides two styles of flow control:

Lecture 02 C FUNDAMENTALS

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

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

Fall, 2015 Prof. Jungkeun Park

Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

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

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

QUIZ. What are 3 differences between C and C++ const variables?

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Review of the C Programming Language for Principles of Operating Systems

C-LANGUAGE CURRICULAM

Programming for Engineers Iteration

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

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

Important Questions for Viva CPU

PESIT-BSC Department of Science & Humanities

CprE 288 Spring 2014 Homework 5 Due Fri. Feb. 21

CS 61c: Great Ideas in Computer Architecture


CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

DEPARTMENT OF MATHS, MJ COLLEGE

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Computer Organization & Systems Exam I Example Questions

from Appendix B: Some C Essentials

Model Viva Questions for Programming in C lab

MYcsvtu Notes LECTURE 34. POINTERS

EL6483: Brief Overview of C Programming Language

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Quick Reference Guide

Structures, Operators

CS61C : Machine Structures

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Review of the C Programming Language

Contents of Lecture 3

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

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

Computer Architecture I Midterm I

Lectures 5-6: Introduction to C

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Types, Operators and Expressions

Informatics Ingeniería en Electrónica y Automática Industrial

Introduction to C++ with content from

Programming in C - Part 2

DECLARAING AND INITIALIZING POINTERS

FORM 2 (Please put your name and form # on the scantron!!!!)

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

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

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

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Programming, numerics and optimization

.Net Technologies. Components of.net Framework

Provided by - Microsoft Placement Paper Technical 2012

CSE 30 Fall 2013 Final Exam

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Type Checking. Prof. James L. Frankel Harvard University

Variables and literals

Syntax and Variables

Ch. 11: References & the Copy-Constructor. - continued -

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

ECE 15B COMPUTER ORGANIZATION

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

CSE Lecture In Class Example Handout

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

ECE 206, Fall 2001: Lab 3

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

CS201 Some Important Definitions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Introduction to C: Pointers

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

C - Basics, Bitwise Operator. Zhaoguo Wang

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Lectures 5-6: Introduction to C

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

CSCI 171 Chapter Outlines

ECE 30 Introduction to Computer Engineering

Embedded Controller Programming 2

Heap Arrays. Steven R. Bagley

A QUICK INTRO TO PRACTICAL OPTIMIZATION TECHNIQUES

C/C++ Programming Lecture 7 Name:

Flow Control. CSC215 Lecture

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

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

A Fast Review of C Essentials Part I

C Praktikum. Advanced Pointers. Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar

C Programming Review CSC 4320/6320

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

Transcription:

Embedded Controller Programming 2 Section 3: C Language for Embedded Systems - Ken Arnold ecp2@hte.com Copyright 2006 Ken Arnold Overview Structures Unions Scope of Variables Pointers Operators and Precedence Program Control & Loops Copyright (c)ken Arnold 2 Copyright 1999-2001 Ken Arnold 1

C Data Types Structures An structure is a set of related variables sharing a common name: struct clocktime unsigned char cstring[15]; unsigned int hour; unsigned int minute; unsigned int second; ; struct clocktime mytime; struct clocktime catime = California, 10, 00, 00; Accessing Elements: catime.minute = 45; catime.hour = 10; Copyright (c)ken Arnold 3 C Data Types Structures Structures store data in contiguous areas, structures with bit access should be located in bit memory area. Structures can be expanded into structure arrays and nested within other structures. Be careful about size when working with uc s, since size is implementation dependant. Copyright (c)ken Arnold 4 Copyright 1999-2001 Ken Arnold 2

C Data Types Unions A union allows you to group variables of different data types together under a common name: union REGS struct WORD unsigned int reg1; unsigned int reg2; x; struct BYTE unsigned char reg1a; unsigned char reg1b; unsigned char reg2a; unsigned char reg2b; y; union REGS register; register.x.reg1 = 0x13FF; register.y.reg1a = 0x13; Copyright (c)ken Arnold 5 C Data Types Unions A structure keeps it s members in different places at the same time, whereas a union keeps it s members in the same place at different times Union data items do not have to be the same size The size of a union is always the same as it s largest member Copyright (c)ken Arnold 6 Copyright 1999-2001 Ken Arnold 3

C Data Types Type Definitions A type definition (typedef) is used to create a synonym for a given data type. Use typedefs to avoid entering more complex types or to standardize: typedef struct tagtimetype unsigned char hour, min, sec; unsigned int days; TIME_TYPE; TIME_TYPE time, *time_ptr, time_array[10]; typedef unsigned char UINT8; typedef signed char SINT8; UINT8 status; SINT8 counter; Copyright (c)ken Arnold 7 C Scope of Variables The compiler needs to know about all the variables in a program Modular programming supports separate files with functions, variables, etc. The SCOPE of a variable defines range of program over which the variable definition has some meaning Copyright (c)ken Arnold 8 Copyright 1999-2001 Ken Arnold 4

Scope of a Variable A piece of code outside the scope of a variable cannot refer to that variable by name C recognizes variables found at the top of a file (outside any function) anywhere within that file The compiler knows a variable defined outside a function only from the point of definition to the end of the file. You don t have to define a variable at the top of a file. Copyright (c)ken Arnold 9 Scope of a Variable You can use automatic variables (definition) only in the function where you define them. They are subject to overlaying unless you make them static Define any parameters passed into a function in the function; they are unknown outside it Copyright (c)ken Arnold 10 Copyright 1999-2001 Ken Arnold 5

C Scope of Variables (Examples) Variables in a function: void Foo( void ) int x; // May be overlaid. void Bar( void ) static int x; // Must stay around. Copyright (c)ken Arnold 11 C Scope of Variables (Examples) Variables in a file: static int x;// Only known in this file. int x; // Global variable. extern int x;// Global variable declared // elsewhere. Copyright (c)ken Arnold 12 Copyright 1999-2001 Ken Arnold 6

C Pointers Pointers are one of the most powerful C features We ve already seen examples with R0,R1,DPTR Pointers provide INDIRECT access to data in C C allows the programmer to create Pointer Variables which can be used to point to other variables or data items. Common usage includes indexing, accessing, and transporting data Copyright (c)ken Arnold 13 C Pointers Pointer declaration: char *px; /* px is a pointer to char variables */ char x; /* x is declared as a char variable */ The actual data type of pointers depends on the hardware being used Pointing to another variable is done as: px = &x; /* px gets the address of x */ You could also do this: char *px = &x; Copyright (c)ken Arnold 14 Copyright 1999-2001 Ken Arnold 7

C Pointer Operations Pointers can point to other pointers: char **ppx; // ppx is a pointer to a pointer to char Can be used when processing multidimensional arrays Pointers can also point to functions: void (*f)(int i); // Declaration of a function pointer // that takes one int argument and // returns nothing. NOTE: This may cause problems with overlaying. Use the OVERLAY directive Copyright (c)ken Arnold 15 C Pointer Operations Pointers can be generic or memory specific on the 8051 Examples: int *p1; // This takes 3 bytes: Universal Pointer int xdata *p2; // This takes 2 bytes of storage int idata *p3; // This takes 1 byte of storage p1 = p2 = p3; // These types can be converted Copyright (c)ken Arnold 16 Copyright 1999-2001 Ken Arnold 8

C Pointer Operations Continued &x char x = A ; char *pt; char array[10]; pt = &x pt = 0 pt == 0 *pt *pt = v pt = array *pt = B pt+5 produces a pointer to x. sets pt pointing to x. Assigns null pointer to pt. Checks to see if pt is NULL. references the value pointed to by pt, type is char. stores the value v in the location pointed to by pt, v has type char. points to first element in array. first element in array assigned value ASCII B. points to 6 th element of the array: array[5]. Copyright (c)ken Arnold 17 C Pointers to Arrays The Name of an Array is a pointer constant in C The name points to the first element of the array: char temp[10]; char *cptr; cptr = temp; Pointer constants can be used like any other constant except they follow rules imposed by the data it s pointing to. Temp+1 points to next element of the array, temp[1] temp[n] and *(temp+n) do the same thing Copyright (c)ken Arnold 18 Copyright 1999-2001 Ken Arnold 9

C Pointers to Arrays &temp[2] is the address of the third element of array temp[ ] These concepts also apply to arrays of higher dimensionality: char temp[30][40]; temp[0] is a pointer constant that is the address of the first row of the array temp[29] is a pointer constant to the last row of the array *(*(temp+n)+m) is temp[n][m] You can also combine pointers in an array of pointers Copyright (c)ken Arnold 19 Pointers to Structures and Unions Pointers to structures allow any member to be easily accessed: Pointers to unions can be defined and declared in the same fashion. Access is done by: typedef struct tagmeasure int time; int delay; char pattern; MEASURE; MEASURE x, *ptr; ptr = &x; ptr->delay = 30; ptr->pattern = 0x42; Copyright (c)ken Arnold 20 Copyright 1999-2001 Ken Arnold 10

C Precedence Operations Precedence rules tell compiler how to interpret lines with multiple operations, for example: A + B * C becomes: A + (B*C) Not always obvious: I && J == true Use parenthesis if there is any question about precedence! i.e. for (A+B)*C Copyright (c)ken Arnold 21 C Precedence Operations Be careful in I/O port masking ( > has precedence over the & ): while (porta & 0x20 > 0) ; while ((porta & 0x20) > 0) ; Copyright (c)ken Arnold 22 Copyright 1999-2001 Ken Arnold 11

C Operators and Precedence Left to Right: () Function call [] Array element reference -> Pointer to struct/union member. Structure member reference Right to Left: - Unary minus + Unary plus ++ Increment -- Decrement! Logical negation ~ Ones complement * Pointer de-reference & Address sizeof Size of an object (type) Type cast (conversion) Refer to the K&R C book for the complete list. Copyright (c)ken Arnold 23 C Program Control Looping and Branching Decision making can be done through several structures in C If/then/else Switch/do case Conditional operator Do/while loops Iteration (for loop) Copyright (c)ken Arnold 24 Copyright 1999-2001 Ken Arnold 12

C Program Control C can be a structured programming language This means: Avoid spaghetti code! Use goto sparingly Program never jumps into or out of a routine without: Saving and restoring stack and other pertinent registers Interrupts are a special case of this Stack corruption is not easy with acceptable programming Structured programming is aided by modularity Structured programming is aided by encapsulation MAKE FLOW CHARTS for your program before coding! The basic element of a structured program is the block. Copyright (c)ken Arnold 25 C Program Control Branching Constructs If/Then/Else can be used for program branching. In assembly we use jz, jnz, jb, jnb, jc, etc. The C compiler does this for us. C = 0 No P1!= 0 Yes C = 20 if (P1!= 0) C = 20; else C = 0; Copyright (c)ken Arnold 26 Copyright 1999-2001 Ken Arnold 13

C Program Control Branching (if/else-if) Example 1: if (a>b) if (a>d) c=10; else c=0; Example 2: if (a>b) if (c>d) e=20; else if (a>c) if (d>f) e=30; else e=10; else e=0; Copyright (c)ken Arnold 27 Branching (if/else-if) Example 3: if (a>b) if (a>d) c=10; else c=0; Example 4: if (a>b) if (c>d) e=20; else if (a>c) if (d>f) e=30; else e=10; else e=0; Copyright (c)ken Arnold 28 Copyright 1999-2001 Ken Arnold 14

C Program Control Branching (switch) Switch/case can also be used for branching: switch ( k ) case 0: x = 1; break; case a : c = x ; break; default: x = 0; c = 0; break; Copyright (c)ken Arnold 29 C- Program Control Branching (conditional) C has a conditional operator which is shorthand for if/else kind of decision: C = ( P1!= 0 )? 20 : 0; No P1!= 0 Yes C = 0 C = 20 Copyright (c)ken Arnold 30 Copyright 1999-2001 Ken Arnold 15

Looping (while) C provides several looping mechanisms: while (cond==true) do_some_operation; Copyright (c)ken Arnold 31 Looping (while) Use while ( 1 ) for an infinite loop. Exit a loop with the break instruction: while ( 1 ) do_some_operation; if ( need_to_exit ) break; Copyright (c)ken Arnold 32 Copyright 1999-2001 Ken Arnold 16

Looping (do-while) The do-while ALWAYS executes the loop at least once! do do_some_operation; while (cond == TRUE); Copyright (c)ken Arnold 33 C Program Control Looping (for-loop) Looping a fixed number of iterations can be done with the for-loop: for (I=0; I<100; ++I) do_some_operation; Shorthand for a while loop, including an initialization step, test step, and modification step. Copyright (c)ken Arnold 34 Copyright 1999-2001 Ken Arnold 17

Summary Data Structures Operators and Precedence Program Control & Loops Tool-Specific Issues Copyright (c)ken Arnold 35 Reading and Homework Read chapters 4 and 6 in the text, about switch inputs and real time constraints to prepare for the next lecture (Chapter 5 should be skimmed) Submit one or more class project ideas NOTE: The sample keypad scanning code on the web site has a bug that causes the keys on the top row to do nothing! For extra credit, fix the bug and send the corrected source file to ecp2@hte.com Copyright (c)ken Arnold 36 Copyright 1999-2001 Ken Arnold 18