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

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

Programming in C++ 5. Integral data types

Lecture 3. More About C

Expression and Operator

DEPARTMENT OF MATHS, MJ COLLEGE

Computer System and programming in C

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Engineering Computing I

Operators. Lecture 3 COP 3014 Spring January 16, 2018

A flow chart is a graphical or symbolic representation of a process.

LECTURE 3 C++ Basics Part 2

Operators. Java operators are classified into three categories:

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Operators in java Operator operands.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

JAVA Programming Fundamentals

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS102: Variables and Expressions

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS & IT Conversions. Magnitude 10,000 1,

Language Reference Manual

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

UNIT- 3 Introduction to C++

Differentiate Between Keywords and Identifiers

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Language Reference Manual

Variables and Operators 2/20/01 Lecture #

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Why embedded systems?

Programming for Engineers Iteration

Quick Reference Guide

Variables and literals

CHAPTER 3 Expressions, Functions, Output

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

1 Lexical Considerations

COMP2121: Microprocessors and Interfacing. Number Systems

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Objectives. In this chapter, you will:

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

ME 461 C review Session Fall 2009 S. Keres

The Arithmetic Operators

C/C++ Programming for Engineers: Working with Integer Variables

Operators & Expressions

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

CMPT 125: Lecture 3 Data and Expressions

Lexical Considerations

Introduction to C Language

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Types, Operators and Expressions

Java Programming Fundamentals. Visit for more.

COMP Primitive and Class Types. Yi Hong May 14, 2015

Computers Programming Course 6. Iulian Năstac

Types, Operators and Expressions

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Chapter 2: Introduction to C++

Chapter 3: Operators, Expressions and Type Conversion

Applied Computer Programming

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 7. Basic Types

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

C Programming Language

Chapter 2: Using Data

Basics of Java Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Java Notes. 10th ICSE. Saravanan Ganesh

More Programming Constructs -- Introduction

JAVA OPERATORS GENERAL

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Programming (1.0hour)

The C Programming Language Guide for the Robot Course work Module

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

Inf2C - Computer Systems Lecture 2 Data Representation

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators and Expressions:

Final Labs and Tutors

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

Add Subtract Multiply Divide

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

Operators And Expressions

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Information Science 1

Reserved Words and Identifiers

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Transcription:

C Programming Language 1

C C is better to use than assembly for embedded systems programming. You can program at a higher level of logic than in assembly, so programs are shorter and easier to understand. The C compiler will translate the C program into machine code for you. A possible disadvantage is that the resulting machine code may be longer and slower than if you wrote assembly to begin with. Brooks found that no matter what programming language is chosen, a professional developer will write in average 10 lines of code (LoC) day! 2

A C program consists of functions and variables. This is a function. It returns a single value. The main() function is required in every C program. Example C Program #include "derivative.h" /* Square the input value */ int square(int a) { int b; b = a*a; return b; } Causes the file derivative.h to be included here This is a comment You have to declare a variable before using it void main(void) { int x; x = 2; PTT = square(x); // output to Port T } void means that it doesn t return anything This is a comment 3

C Data Types for the HCS12 Type Description Memory Size Range char character 1 byte -128 to 127 unsigned char character 1 byte 0 to 255 int, short int integer 2 bytes -32768 to 32767 unsigned int integer 2 bytes 0 to 65535 long int extended precision integer 4 bytes -2 31 to 2 31-1 float, double floating point 4 bytes About ±10 38 Note: Data types are processor-specific In our MCU, an int is 16 bits In a 32-bit microprocessor, an int is 32 bits Examples of declarations int i; char cx, cy; You can optionally initialize the variable when it is declared unsigned int m = 0; /* declare integer variable m and initialize it to 0 */ char echo = y ; /* ASCII code of letter y is assigned to echo */ 4

Constants Numbers A number can be specified in different bases Examples 1234 01234 0x4F No prefix means decimal A 0 prefix means octal A 0x prefix means hex Characters A character in single quotes is represented by the ASCII code for the character A string is a sequence of characters enclosed in double quotes (automatically terminated by NUL, or 0) Examples A ABCD Has the value 0x41 Is stored as 0x41, 0x42, 0x43, 0x44, 0x00 5

Arrays An array is a variable with multiple items When you declare an array you specify its size Example: int M[5]; /* array M has 5 integer elements */ You can refer to a particular element using an integer subscript in square brackets (note - the 1 st element has index 0) Example: M[0] = 1; // assign 1 to the first element of M You can initialize an array when it is defined Example: int M[] = {1,2,3,4,5}; // no need to specify size here 6

Arithmetic Operators + add and unary plus - subtract and unary minus * multiply / divide -- truncate quotient to integer when both operands are integer % modulus (or remainder) -- cannot be applied to float or double ++ increment (by 1) -- decrement (by 1) Examples What are the values of i,j,k,n after these statements? i = 23/12; j = 23 % 12; k = 5; n = k++; 7

Short hand for operators You can combine the assignment operator (=) with another operator Examples n += k; // same as n = n + k x *= 2; // same as x = x * 2 i -= 1; // same as i = i 1, also i-- 8

Relational and Logical Operators Relational operators compare the value of two operands If the result of the comparison is true, the value of the expression is 1 Otherwise the value of the expression is 0 Example (i == j) This expression has the value 1 if I equals j; 0 otherwise Relational and logical operators == equal to!= not equal to > greater than >= greater than or equal to < less than <= less than or equal to && and or! not (one s complement) 9

If-Else Statement if (expression) statement(s) else statement(s) Example if (a!= 0) r = b; else { } r = c; s = r + 0x0a; Control Flow -- The else part is optional -- Use { } to enclose multiple statements 10

While statement while (expression) statement(s) Control Flow Example sum all numbers in an array M int i, sum; int M[] = {12, 34, 56, 78, 90}; i = 0; sum = 0; while (i<5) { sum = sum + M[i]; i = i + 1; } This is an empty statement Example int_cnt = 5; while (int_cnt) ; // do nothing while int_cnt is not 0 11

Control Flow For-Loop Statement for (expr1; expr2; expr3) statement(s) where expr1, expr3 are assignments or function calls and expr2 is a relational expression expr1 is performed first, then statement(s) are executed as long as expr2 is true. expr3 is performed after each iteration. Example sum all numbers in an array M int i, sum; int M[] = {12, 34, 56, 78, 90}; sum = 0; for (i = 0; i < 5; i++) { sum += M[i]; } 12

Write C statements to do Find the largest value in an array. Array is defined as: int array[10]; Find the length of a character string, defined as char mystring[]; 13

Write C statements to do Find the largest value in an array. Array is defined as: int array[10]; int i, largest; largest = array[0]; for (i=1; i<10; i++) if (array[i] > largest) largest = array[i]; Find the length of a character string, defined as char mystring[]; int i=0, len=0; while (mystring[i]!= 0) len++; 14

Example Gray Code counter Problem statement: Continuously count up from 0 through 7, and then wrap around back to 0 Display the count as a 3 bit Gray code on Port T Recall a 3 bit Gray code is: 000,001,011,010,110,111,101,100 Approach: Make a table and initialize it with the Gray codes Have an infinite loop that increments a counter Get the value from the table corresponding to the count, and write it to PortT When the counter reaches 8, reset it back to zero 15

Pseudocode Initialize Table with Gray codes (note - this can be done at load time, and the table can be stored in ROM) Set up Port T, bits 0..2, for output Initialize the counter n=0 Repeat forever Get the nth value from the table; ie Table(n) Write that value to PortT increment n if n==8 n = 0 end (Optional: delay a short time) end C // Initialize Table with Gray codes char Table[] = {0x00,0x01,0x03,0x02,0x06,0x07,0x05,0x04}; int n = 0; DDRT = 0x07; //Set up Port T, bits 0..2, for output while (1) { PTT = Table[n]; // Write next value to PortT if (++n >= 8) n = 0; // Optionally delay a short time } 16

Summary / Questions Why is better to program in C, rather than assembly, if you can? When would you ever want to program in assembly? How do you declare a variable? 17