Embedded Programming in C

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
C Language Programming. C has gradually replaced assembly language in many embedded applications. Data types –C has five basic data types: void, char,
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Development. Development Environment Editor Assembler or compiler Embedded emulator/debugger IAR Embedded Workbench Kickstart Code Composer Essentials.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
Lecture #5 Introduction to C++
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Introduction to Programming
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
By Mr. Muhammad Pervez Akhtar
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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.
Numbers in ‘C’ Two general categories: Integers Floats
Basic Data Types & Memory & Representation
C++ Lesson 1.
Test 2 Review Outline.
Chapter Topics The Basics of a C++ Program Data Types
Java Language Basics.
The Machine Model Memory
Introduction to C Programming
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
Basic Elements of C++.
C Short Overview Lembit Jürimägi.
C Basics.
Choice of Programming Language
Basic Elements of C++ Chapter 2.
Lecture 5 from (Chapter 4, pages 73 to 96)
Starting JavaProgramming
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
C Preprocessor(CPP).
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.
Embedded Programming in C
Programming in C Miscellaneous Topics.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Programming in C Miscellaneous Topics.
C Programming Getting started Variables Basic C operators Conditionals
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Operations and Arithmetic
Chapter 11 Programming in C
Module 10 Operations on Bits
An Overview of C.
C Language B. DHIVYA 17PCA140 II MCA.
Lecture 2: Bits, Bytes, Ints
Standard Version of Starting Out with C++, 4th Edition
Bit Manipulations CS212.
Chapter 12 Variables and Operators
Presentation transcript:

Embedded Programming in C Originated by: Jered Aasheim Tom Cornelius Modified by: Bobby Davis Presented January 19, 2006

Infinite Program Loop Embedded systems are usually meant to always execute the same program for the duration of the time the device is on. This can be done using a “forever loop”: void main(void) { … for(;;) // you can also use a while(1) loop // main program loop }

Why Use C? C allows the programming low-level access to the machine architecture. Compiled C programs are memory efficient and can execute very fast. Higher level language constructs allow designs to be more complicated, reduce the number of bugs, and help get more done per unit of time.

Data Types Type Size (bits) Dynamic Range unsigned char 8 0…255 -128…127 unsigned int 16 0…65,535 signed int -32,678…32,677 unsigned long 32 0…4,294,967,295 signed long -2,147,483,647… 2,147,483,647 Note: C does not support the “boolean” data type. Instead, use chars or ints and #define TRUE=1 and FALSE=0.

Number Representation In C, numbers can be expressed in decimal, hexadecimal, or octal: Decimal 2 63 83 Octal 00 02 077 0123 Hexadecimal 0x0 0x2 0x3F 0x53

Boolean Logical Operators C Notation AND && OR || NOT ! These should be familiar from Java “True” is any non-zero value “False” is zero Ex: 4&&-1 == 1, !(-67) == 0 Equality operators (“==”, “>=”, etc.) also work the same as in Java

Bit Operators Operator C Notation Example AND & 0x08 & 0xF0 // 0x00 OR | 0x08 | 0xF0 // 0xF8 Left shift << 0x08 << 1 // 0x10 Right shift >> 0x08 >> 3 // 0x01 XOR ^ 0xAA ^ 0xFF // 0x55 One’s complement ~ ~0x00 //0xFF

Reading/Writing Individual Bits The bitwise AND and OR operators can be used to read/write individual bits in a variable: Ex. unsigned char reg = 0xB5; if(reg & 0x08) // Is the 4th bit a “1”? … else // 4th bit is not a “1” reg = reg | 0x02; // Set the 2nd bit to a “1” reg = reg & 0xFE; // Set the 1st bit to a “0”

Scope There are 3 ways to declare a variable – global, local, static: ex. unsigned char inputStatus; // global ex. void f(void) { int sum; … } // local ex. void g(void) { static char ttlLine; … } // static The static keyword means that the variable will retain its value across function calls; there are situations where this is very useful for retaining state in a subroutine/function.

Preprocessor The preprocessor responds to preprocessor directives Output of preprocessor is then compiled These directives include all commands beginning with the “#” sign, as well as code comments

Preprocessor Commands #define This will create constants for your code The preprocessor will replace all instances of the constant with its numerical value before compiling Use #define’s LIBERALLY in your code (but define them at the top of your files!) Ex: #define TRUE 1 // true defined as 1 void function() { int gus = TRUE // gus is 1 }

Preprocessor Commands (2) #if and #ifdef These will conditionally compile your code Can be useful for debugging/testing Ex: #define DEBUG #define TEST 0 void function() { #ifdef DEBUG … // this code will be compiled #endif #if TEST … // this code will not be compiled }

Preprocessor Commands (3) #include This copies the contents of a header file wherever a #include resides in the code Conventionally, #include’s should be used only at the top of each source code file Ex: #include <dos.h> #include “myHeader.h” void whateverFcn() { … }

Header Files Header files will often take on the name of the source code file with which they are most readily associated (ex: lab1.h and lab1.c) Header files contain #define’s, type definitions, function prototypes, and external variable declarations…but NO executable code! Standard library headers are #include’d with encompassing <> brackets around the file name, while programmer-defined files use “” quotations (see last slide example)