E91 Embedded System. Intro

Similar documents
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

1. True or False? A voltage level in the range 0 to 2 volts is interpreted as a binary 1.

ELEG3924 Microprocessor Ch.7 Programming In C

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Keil C51 Cross Compiler

Analog to Digital Conversion of Sound with the MSP430F2013

ELEC EXPERIMENT 1 Basic Digital Logic Circuits

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Binary Adders: Half Adders and Full Adders

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

Dear Reader. The latest tutorial will be available from now on from the website of my consulting company Argenox Technologies:

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Lecture N -1- PHYS Microcontrollers

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Lecture 2 Notes: Flow of Control

Digital circuits make up all computers and computer systems. The operation of digital circuits is based on

Counters and Decoders

Lecture 22: C Programming 4 Embedded Systems

Understanding Logic Design

Top 10 Bug-Killing Coding Standard Rules

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

C++ Language Tutorial

PIC Programming in Assembly. (

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Object Oriented Software Design

Take-Home Exercise. z y x. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

Lecture 7: Programming for the Arduino

Lecture 5: Gate Logic Logic Optimization

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Object Oriented Software Design

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Selection Statements

Digital Electronics Detailed Outline

Chapter 3: Writing C# Expressions

Pemrograman Dasar. Basic Elements Of Java

Let s put together a Manual Processor

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Programming the Arduino

Lecture 2. Binary and Hexadecimal Numbers

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

The programming language C. sws1 1

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Sistemas Digitais I LESI - 2º ano

Chapter 7D The Java Virtual Machine

The string of digits in the binary number system represents the quantity

The C Programming Language course syllabus associate level

Introduction to Java

Example of a Java program

Timer A (0 and 1) and PWM EE3376

COMBINATIONAL and SEQUENTIAL LOGIC CIRCUITS Hardware implementation and software design

Statements and Control Flow

Life Cycle of a Memory Request. Ring Example: 2 requests for lock 17

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

DNA Data and Program Representation. Alexandre David

Logic in Computer Science: Logic Gates

Lab 1: Full Adder 0.0

Digital Logic Design. Basics Combinational Circuits Sequential Circuits. Pu-Jen Cheng

ECE 3401 Lecture 7. Concurrent Statements & Sequential Statements (Process)

Gates, Circuits, and Boolean Algebra

CHAPTER 3 Boolean Algebra and Digital Logic

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

6.087 Lecture 2 January 12, 2010

Sources: On the Web: Slides will be available on:

1. Introductory Overview

Programmable Logic Controllers Definition. Programmable Logic Controllers History

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

Numbering Systems. InThisAppendix...

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

5 Combinatorial Components. 5.0 Full adder. Full subtractor

Chapter 2 Logic Gates and Introduction to Computer Architecture

C++ Programming Language

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 OUTCOME 3 PART 1

ENGI 241 Experiment 5 Basic Logic Gates

Chapter 4: Computer Codes

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

Karnaugh Maps & Combinational Logic Design. ECE 152A Winter 2012

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Figure 8-1 Four Possible Results of Adding Two Bits

MACHINE ARCHITECTURE & LANGUAGE

CS 61C: Great Ideas in Computer Architecture Finite State Machines. Machine Interpreta4on

VB.NET Programming Fundamentals

CS201: Architecture and Assembly Language

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Objective-C Tutorial

RS-485 Protocol Manual

Informatica e Sistemi in Tempo Reale

Systems I: Computer Organization and Architecture

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

How to design and implement firmware for embedded systems

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Transcription:

E91 Embedded System Intro

Why embedded systems? Big bang-for-the-buck buck by adding some intelligence to systems. Embedded Systems are ubiquitous. Embedded Systems more common as prices drop, and power decreases.

Which Embedded System? We will use Texas Instruments MSP-430 + very popular (#2 next to microchip) + 16 bits (instead of 8) + low power + clean architecture + low cost (free) development tools - relatively low speed/capacity (i.e., no video or fancy audio) - low level tools (compared to Stamp, Arduino ) - 16 bits (instead of 32)

Course overview Mix of hardware and software Mostly C, some assembly Lots of information, less conceptual than many courses. Two hardware kits, EZ430 and the 430 Experimenter s Board About 2 hours of lecture per week. More hours in lab writeups will be short. In lab you will need to become familiar with a broad set of documentation. I will try to keep a complete list on website. Let me know if you find good documents I have not listed. You will need to be more self-reliant than in many courses (though I am always ready to help). Maybe some assignments.

Policy on working together : Lab groups should be 2 (preferably) or 3 students. We expect labs to be done as a group with your lab partners. You may discuss your lab with other groups, but you may not copy anything from their reports. Each group will submit a single report (with all members of the group listed).

Today Brief overview of o logic o numbers o C o MSP430 digital I/O Today s lecture will be very densely packed so you can start on the lab. We will come back and repeat some of the material in more depth as needed.

Brief Review of Logic In this class we will deal with logical circuits (i.e., inputs are either true (logical 1, for us this is 3.3V) or false (logical 0, for us this is 0V). In class you will learn why this is useful, for now, accept it. To deal with these signal levels we develop a special form of mathematics, Boolean algebra.

Boolean Operators Operators: o AND : C=A B (Read as C equals A and B). Input A Input B Output C 0 0 0 0 1 0 1 0 0 1 1 1 Truth Table Sometimes the is dropped: C=AB logic gate images adapted from: http://www.ee.surrey.ac.uk/projects/labview/gatesfunc/index.html

More logic A B D 0 0 0 0 1 1 1 0 1 1 1 1 A F 0 1 1 0 A B H 0 0 1 0 1 0 1 0 0 1 1 0

and even more. A B G 0 0 1 0 1 1 1 0 1 1 1 0 exclusive OR A B J 0 0 0 0 1 1 1 0 1 1 1 0 Note: XOR with ih1 inverts bit, XOR with 0 passes bit.

Number Systems 0 0000 0 Decimal Binary Hex Binary: 00001101 2 = 1 2 3 + 1 2 2 + 0 2 1 + 1 2 0 = 8 + 4 + 1 = 13 Hex: 00101010 2 = 2A 16 = 0x2A = 2 16 1 + 10 16 0 = 32 + 10 = 42 (check 1 2 5 + 1 2 3 + 2 2 1 = 32 + 8 + 2 = 42) 8 bits = 1 byte 0000 0000 2 1111 1111 2 0x00 0xff 0 2 8-1=255 (or -128 127 (-(2 7 ) 2 7-1))) 16 bits = 2 bytes = 1 word 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F 0000 0000 0000 0000 2 1111 1111 1111 1111 2 0x0000 0xffff 0 2 16-1=65535 (or -32768 32767 (-(2 15 ) 2 15-1))) 4 bits = 1 nybble (0 2 4-1=15)

A simple C program Constants associated with our chip Every program needs a main routine (between braces) #include <msp430x20x3.h> Declare i as volatile so compiler doesn t optimize it out of existence (or turn optimizations off). void main(void) i { All variables must be declare before they are used. volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR = 0x01; // Set P1.0 to output direction 1 is always true, so loop forever. while (1) { //Do this forever P1OUT = P1OUT 0x01; // Set P1.0 with or, for (i=0; i<0x5000; i++) {} // Delay P1OUT = P1OUT & ~0x01; 01 // Clear P1.0 for (i=0; i<0x5000; i++) {} // Delay } } Set bit 0 high (connected to LED) Don t worry about for now. Set bit 0 in P1DIR this makes it an output (next page). Comments start with // and go to end of line. Also note that every statement ends with ; or } Loop to waste time Set bit 0 low (LED turns off) Loop to waste time

A typical I/O pin Analog switch Inverted input P1REN=0, by default SD16AE=0, by default Electronically controlled digital switch 2 1 Mux (multiplexer) Tri State Output P1SEL=0, by default Schmitt trigger (hysteresis) Transparent latch

Effect of P1DIR 0 0 0, switch is open P1DIR=0 P1DIR=1 0 Vin P1OUT P1DIR 0 1 P1OUT P1OUT Hi Z 0 0 Hi Z 1 0 P1OUT Vin Latch is disabled

Variant 1 (more readable) #include <msp430x20x3.h> #define LED 0x01 Give constants meaningful names. void main(void) { volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR = LED; // Set P1.0 (LED bit) to output } while (1) { //Do this forever P1OUT = LED; // Turn on LED for (i=0; i<0x5000; i++) {} // Delay P1OUT &= ~0x01; // Turn off LED for (i=0; i<0x5000; i++) {} // Delay } Equivalent to: P1OUT = P1OUT LED;

Variant 2 (macros) #include <msp430x20x3.h> #define LED 0x01 #define SETBIT(p,b) (p = (b)) #define CLRBIT(p,b) (p &= ~(b)) Use Macros sparingly, but they can make code look much cleaner (see below) void main(void) { volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR = LED; // Set P1.0 to output direction while (1) { //Do this forever SETBIT(P1OUT,LED); // Set P1.0 for (i=0; i<0x5000; i++) {} // Delay CLRBIT(P1OUT,LED); // Clear P1.0 for (i=0; i<0x5000; i++) {} // Delay } } Expands to: (P1OUT = (0x01)) Note ; must be added.

Variant 3 (shorter) #include <msp430x20x3.h> #define LED 0x01 #define SETBIT(p,b) (p = (b)) #define CLRBIT(p,b) (p &= ~(b)) #define TGLBIT(p,b) (p ^= (b)) void main(void) id) { New macro to toggle (xor) bit volatile int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR = 0x01; // Set P1.0 to output direction } while (1) { //Do this forever TGLBIT(P1OUT,LED); // Toggle LED for (i=0; i<0x5000; i++) {} // Delay } Loop is half as long as before

C Data Types (that we will use) Type Size Representation Minimum Maximum (bits) char, signed char 8 ASCII 128 +127 unsigned char, bool 8 ASCII 0 255 int, signed int 16 2s complement 32 768 32 767 unsigned int 16 Binary 0 65 535 long, signed long 32 2s complement 22 147 483 648 2 147 483 647 unsigned long 32 Binary 0 4 294 967 295 enum 16 2s complement 32 768 32 767 float 32 IEEE 32 bit ±1.175 495e 38 ±3.40 282 35e+38

C Operators (Arithmetic) Arithmetic Operator name Basic assignment Addition Subtraction Syntax a = b a + b a b Unary plus +a Unary minus (additive inverse) a Multiplication Division Modulo (remainder) Increment Decrement Prefix Suffix Prefix Suffix a * b a / b a % b ++a a++ a a

More C Operators (Relational, Logical, Bitwise and Compound) Relational Operator name Syntax Equal to a ==b Not equal to a!=b Greater than a > b Less than a < b Greater than or equal to a >=b Less than or equal to a <=b Bitwise Operator name Syntax Bitwise NOT ~aa Bitwise AND a & b Bitwise OR a b Bitwise XOR a ^ b Bitwise left shift a <<b Bitwise right shift a >>b Logical Operator name Syntax Logical negation (NOT)!a Logical AND a &&b Logical OR a b Compound Operator name Syntax Addition assignment a += b Subtraction assignment a = b Multiplication assignment a *= b Division assignment a /= b Modulo assignment a %= b Bitwise AND assignment a &= b Bitwise OR assignment a = b Bitwise XOR assignment a ^= b Bitwise left shift assignment a <<=b Bitwise right shift assignment a >>=b

More C Statements a simple statement is a single statement that ends in a ; a compound statement is several statements inside braces: { simple statement; simple statement; } Indenting There are no rules about indenting code, but if you don t adopt a standard style, your code becomes unreadable.

Even more C Array definition int a [100]; //Array elements are a[0] to a[99]. Don t use a[100]! if then if (<expression>) <statement> <statement> may be a compound statement. if then else else if (<expression>) <statement1> else <statement2>

Iteration (do while while for ) is equivalent to do <statement> while ( <expression> ); while ( <expression> ) <statement> Yet more C for ( <expression> ; <expression> ; <expression> ) <statement> Recall: for (i=0; i<0x5000; i++) {} // Delay for (e1; e2; e3) s; e1; while (e2) { s; e3; } The break statement is used to end a for loop, while loop, do loop, or switch statement. Control passes to the statement following the terminated statement.

switch h( (one choice of many) switch (<expression>) { case <label1> : <statements 1> case <label2> : <statements 2> break; default : <statements 3> } Again with the C <expression> is compared against the label, and execution of the associated statements occur (i.e., if <expression> is equal to <label1>, <statements 1> are exectuted. No two of the case constants may have the same value. There may be at most one default label. If none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label, or if there is no default label, execution resumes just beyond the entire construct. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. This is usually not wanted, so be careful

Material taken from: http://en.wikipedia.org/wiki/c_syntax http://en.wikipedia.org/wiki/indent_style / iki/i d t t http://en.wikipedia.org/wiki/operators_in_c_and_c++ http://focus.ti.com/lit/ug/slau144e/slau144e.pdf http://focus.ti.com/lit/ds/slas491e/slas491e.pdf http://focus.ti.com/lit/ug/slau132c/slau132c.pdf