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

Similar documents
ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

ECE2049-E17 Lecture 6 1. ECE2049: Embedded Computing in Engineering Design E Term Lecture #6: Exam Review

ECE2049: Embedded Computing in Engineering Design A Term Fall Lecture #9: Exam Review w/ Solutions

ECE2049-E18 Lecture 6 Notes 1. ECE2049: Embedded Computing in Engineering Design E Term Lecture #6: Exam Review

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

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

The C Programming Language Guide for the Robot Course work Module

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

DEPARTMENT OF MATHS, MJ COLLEGE

UNIT- 3 Introduction to C++

Why embedded systems?

ISA 563 : Fundamentals of Systems Programming

Computers Programming Course 6. Iulian Năstac

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Programming in C++ 5. Integral data types

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Introduction to C Language

3. Java - Language Constructs I

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Fundamental of Programming (C)

ME 461 C review Session Fall 2009 S. Keres

C: How to Program. Week /Apr/23

Arithmetic and Bitwise Operations on Binary Data

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Fundamentals of Programming

6.096 Introduction to C++ January (IAP) 2009

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

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

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

CSC 1214: Object-Oriented Programming

C Language Programming

CS201 Some Important Definitions

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Expressions and Precedence. Last updated 12/10/18

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

EL6483: Brief Overview of C Programming Language

ECEN 449 Microprocessor System Design. Review of C Programming

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

CT 229. Java Syntax 26/09/2006 CT229

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

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Embedded Systems - FS 2018

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

Expression and Operator

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

Variables and Operators 2/20/01 Lecture #

ECE2049 Homework #2 The MSP430 Architecture & Basic Digital IO (DUE Friday 9/8/17 at 4 pm in class)

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

Variables and literals

Programming for Engineers Iteration

Technical Questions. Q 1) What are the key features in C programming language?

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Embedded Systems - FS 2018

Course Outline Introduction to C-Programming

Data Type Fall 2014 Jinkyu Jeong

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

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lecture 04 FUNCTIONS AND ARRAYS

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Computers Programming Course 5. Iulian Năstac

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Basics of Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Quick Reference Guide

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

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Operators. Java operators are classified into three categories:

Prof. Navrati Saxena TA: Rochak Sachan

(Refer Slide Time: 00:26)

Quick Reference Guide

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

Programming refresher and intro to C programming

Lecture Transcript While and Do While Statements in C++

printf( Please enter another number: ); scanf( %d, &num2);

Computer System and programming in C

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

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

OBJECT ORIENTED PROGRAMMING

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

The Arithmetic Operators

Lecture 03 Bits, Bytes and Data Types

Data Types and Variables in C language

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

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

Page 1. Agenda. Programming Languages. C Compilation Process

Work relative to other classes

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

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

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Computer Organization & Systems Exam I Example Questions

Arithmetic and Bitwise Operations on Binary Data

If you note any errors, typos, etc. with this manual or our software libraries, let us know at

Bitwise Data Manipulation. Bitwise operations More on integers

Transcription:

General Framework of a C program for MSP430 /* Compiler directives (includes & defines) come first */ // Code you write in this class will need this include #include msp430.h // CCS header for MSP430 family #include <stdlib.h> // C standard library /* defines are mostly used to declare constants */ #define MAX_ITER 10 // max number of iterations /* Then function prototypes */ /* Function prototypes should be used but are not strictly required if full function definition appears before main */ int somefunction(char opt, unsigned int scale); /* Global variables are visible everywhere in code */ /* Usually try to use local variables as much as possible */ /* but in embedded computing global variables are /* frequently used*/ int exitflag = 0; const float pi = 3.14156; /* Function implementations */ int somefunction(char opt, unsigned int scale) int a, b, ret_val; /* local integer variables */ scale = scale*opt + a; ret_val = scale*b return(ret_val); // returns an integer /* There can be only one main() function! */ /* Execution of program always starts in main () */ void main() char choice; int test_num, stuff; stuff = somefunction('a',test_num);

Basic Types of Programming Instructions 1) Variable declarations Defining what your data is int char float num1, sum, myarray[10]; initial, name[16]; myreal, flt_arr[5]; 2) Assignment statements Most are straight forward and calculator like int a, b, c; float d, e, f; char byte1, byte2; c = a + b; e = c * d; f = cos(e); byte2 = byte1 & 15; // would require including math.h // we'll use a lot of bitwise ops 3) Decision and Control tempok = 1; degf = get_degf(); while (tempok > 0) degf = degf - 10; degc = 5 * (degf-32)/9; degk = degc + 273.15; if (degk < 0) // IMPORTANT: printf() is a standard IO function in C but // our MSP430F5529 board doesn't support it!! printf( ERROR Temp < 0 K\n ); printf( Breaking Multiple Laws of Physics!\n ); tempok = 0; else printf( %10.4f F = %10.4f C = %10.4f K \n, degf, degc, degk);

Basic Instructions and Syntax in more detail After writing a C program you need to compile and link it to create an Executable file The COMPILER defines the size of the different data types and how they should be stored in the computer's memory Most general purpose compilers like Microsoft Dev Studio or linux-based gcc assume default data word size (usually of 32 bits or 64 bits depending on your computer). BUT, the Code Composer Studio Compiler is designed for the MSP430 MSP430 is a 16-bit processor! Default data sizes are based on 16 bits! >> Here are some data types and sizes (as they are defined in CCS!) int a; // 16 bit signed integer (2's comp) float b; // 32 bit IEEE floating point char c; // 8 bits (unsigned) unsigned int d; // 16 bit unsigned integer long int e; // 32 bit signed integer double f; // 64 bit Floating Point >> First task in programming is creating variables of suitable size and type for the data in your problem! For example, a is a 16 bit integer and can hold integer values between -32768 and 32767. If you need to represent a value outside of that range you cannot store it in variable a! >> Arrays In C arrays are indexed starting at 0 char initial, name[5]; // declare array of 5 char name = Susan ; initial = name[0]; // fill the name array // initial = 'S' // name[1] = 'u' and // name[4] = 'n'

>> Standard Operators -- These you can find in any C reference Math: + - * / = % (modulo) = Calculator-like a = 2*c + 1; b = (float)e/b; // cast e as float before division c = e % a; // c = remainder of e divided by a Unary and assignment : ++ -- += -= *= /= (really just short hand) a++; // a = a + 1; c--; // c = c - 1; c*=2; // c = 2*c; ** You do not need to use these operators but you need to be able to read code written by others who do use them!! Relational: > >= < <= ==!= if (x > y) // this code is executed when x > y z = z x; else // otherwise this code is executed z = z y; while (counter!= marker) // while counter not equal marker // do something counter--; // update counter if (A == test_val) // this code is executed when A equals test_val

Logical: && (AND) (OR) == (EQUIVALENCE) if ((A == 0) (g > 100)) // if A equals 0 OR if g greater than 1000 BE CAREFUL: Do not use single = to compare values!! Single = is an assignment operator!! while ((j < k) && (run_flag)) // while j less than k AND run_flag not 0) Bitwise: & (AND) (OR) ^ (XOR) ~ (NOT) >> (Right shift) << (Left shift) All variables are encoded in binary. What is stored in memory is just 1's and 0's Bitwise operators are just that. They do AND, OR, NOT and XOR operations on each bit in the word i = 0x0085; // i = 0085 hex = 0000000010000101 binary j = 0xF0F0; // j = F0F0 hex = 1111000011110000 binary k = i & j; // k = 0080 hex 0000000010000000 binary // each bit in i is AND'ed with corresponding bit in j // >> and << are right and left shifts // m = bit pattern in k shifted to right by 2 places // Equal to dividing k by 4 = 2^2 m = k >> 2; // m = 0020 hex = 0000000000100000 binary // n = bit pattern in k shifted to left by 4 places // Equal to multiplying k by 16 = 2^4 n = k << 4; // n = 0800 hex = 0000100000000000 binary Important: Bit-shifting often faster than multiplication! ================================================================ Brackets: Denotes multi-line block of code --> MUST match! ==============================================================

>> Statements for making decisions or looping IF ELSE statements: if ( kk > 100 ) kk = 0; else kk++; y = y+kk; // can also write as y =+ kk CASE statements: switch (choice) case 1: /* do something */ break; case 2: /* do something else */ break; case 4: /* do something else again*/ break; default: /* for all other values */ break WHILE loops i = strt; // initialize loop counter while (i < end_pt) /* Body of loop */ i++; // increment loop count

FOR loops Really just a different syntax for a simple loop for (i = strt; i < end_pt; i++) /* Body of loop */ // end for i >> This for loop accomplishes exactly the same thing as the while loop above The Forever Loop -- Never Ends >> Endless loops are not desirable (errors!) in most general purpose PC-style programs but are used all the time in the main( ) of embedded code int main() /* Declare and initialize variables */ while (1) /* update variable values, call functions and do everything my device has to do */. // end while (1)

C is a Structured or Modular Programming Language >> A C main() calls a series of smaller functions to complete a task --> These functions declared and implemented in the same file or in a different file #define MAX_BUTTONS 4 // only 4 buttons on the board int checkbutton(char buttonnum); void lighled(char lednum); void wait_awile(); /* Later in file implement checkbutton */ int checkbutton(char buttonnum) /* define local variables */ int buttonstate = 0; /* Body of function goes here */ /* Return */ return(buttonstate); /* Need to implement other functions too! */ /* Now we get to main() */ void main( ) /* define local variables */ int i, b_stat; char button; while (1) /* loop forever */ for (i = 0; i < MAX_BUTTONS ; i++) button = i + 1; b_stat = checkbutton(button); if (b_stat == 1) lightled(button); wait_awhile(); // Do other stuff