chapter 11 - the c language. byu cs/ecen 124the c language2 topics to cover… isr’s high level...

32
Chapter 11 - The C Language

Upload: tyson-emans

Post on 01-Apr-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

Chapter 11 - The C Language

Page 2: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 2

Topics to Cover…

ISR’s High Level Languages Compilers vs. Interpreters The C Language 1st C Program C Style C Preprocessor printf Function eZ430X Header Files 2nd C Program

Page 3: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 3

Interrupt Service Routines

Well-written ISRs: Should be short and fast Should affect the rest of the system as little as

possible Require a balance between doing very little – thereby

leaving the background code with lots of processing – and doing a lot and leaving the background code with nothing to do

Applications that use interrupts should: Disable interrupts as little as possible Respond to interrupts as quickly as possible Communicate w/ISR only through global variables

(never through registers!!!)

Interrupt Service Routine

Page 4: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 4

MainRoutine

Interrupt Service Routines

Who empties the trash?Disable

Interrupts

GlobalVariable

Interrupt Service Routine

InterruptServiceRoutine

Page 5: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 5

Levels of Abstraction

Problems

Algorithms

Language

Machine (ISA) Architecture

Microarchitecture

Circuits

Devices Transistors

Logic gates, multiplexers, memory, etc.

MSP430 Architecture

Machine code

Assembly code

High Level Languages

Page 6: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 6

High Level Languages

The closer a language is to your original specification, the easier the program is to write.

Many, many programming languages LISP - LISt Processing PROLOG - logic programming MATLAB - matrix and vector manipulations BASIC – interpreter for small computers APL – matrix and vectors FORTRAN – formula translation COBOL – business and accounting PASCAL - procedural

….

High Level Languages

Page 7: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 7

High Level Languages

During the part of our course, you will be introduced to fundamental high-level programming constructs: Variables Control structures Functions Arrays and Pointers Simple data structures Recursion

High Level Languages

Page 8: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 8

High Level Languages

Allow us to use symbolic names for values Programmer simply assigns each value a

name Allow us to ignore many memory details, the

compiler takes care of … register usage variable allocation loads and stores from memory callee/caller protocol stack management for subroutine calls

High Level Languages

Page 9: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 9

High Level Languages

Provide abstraction of underlying hardware Hide low level details (ISA) from programmer Uniform interface (not tied to ISA) to program Portable software (works on different ISAs) The compiler generates the machine code

High Level Languages

if ((a >= '0') && (a <= '9')){ sum = sum * 10; sum = sum + (a - '0');}else ...

if ((a >= '0') && (a <= '9')){ sum = sum * 10; sum = sum + (a - '0');}else ...

Page 10: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 10

High Level Languages

Provide expressiveness Human-friendly orientation Express complex tasks with smaller amount

of code English-like and human readable

if-then-else… while… for... switch…

High Level Languages

if(isCloudy) get(umbrella);else get(sunglasses);

if(isCloudy) get(umbrella);else get(sunglasses);

Page 11: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 11

High Level Languages

Enhance code readability Can read like a novel…

if written with readability in mind Readability.. is very important

life cycle costs are more important than initialprogramming costs

Easier to debug Easier to maintain

High Level Languages

main(){ readInput(); checkForErrors(); doCalculation(); writeOutput();}

main(){ readInput(); checkForErrors(); doCalculation(); writeOutput();}

Page 12: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 12

High Level Languages

Provide safeguards against bugs Rules can lead to well-formed programs

structured programming (no GOTO statements) Compilers can generate checks

array bounds checking data type checking

Many languages provide explicit support for assertions

something that should be true - if it isn’t, then error

High Level Languages

assert(accountBalance >= 0);assert(accountBalance >= 0);

Page 13: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 13

Compilation vs. Interpretation

Interpretation: An interpreter reads the program and performs the operations in the program The program does not execute directly, but is

executed by the interpreter. Compilation: A compiler translates the program

into a machine language program called an executable image. The executable image of the program directly

executes on the hardware.

The interpreter and compiler are themselves programs

Compilers vs Interpreters

Page 14: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 14

Interpretation

Algorithm

High-Level Language Programc = a + b;

by hand

Interpreterread &

execute program text

Compilers vs Interpreters

Page 15: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 15

Interpretation

Program code is interpreted at runtime lines of code are read in interpreter determines what they represent requested function is performed

interpret(){ while(1) // do forever { readInputLine(); if(line[0..3] == "mult") doMultiply(); else if(line[0..2] == "add") doAdd(); else ... }}

interpret(){ while(1) // do forever { readInputLine(); if(line[0..3] == "mult") doMultiply(); else if(line[0..2] == "add") doAdd(); else ... }}

Interpretation is common: + LISP + BASIC + Perl + Java + Matlab + LC-2 simulator + UNIX shell + MS-DOS command line

Interpretation can be slow...

Compilers vs Interpreters

Page 16: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 16

Compilation

Algorithm

C-language programc = a + b;

by hand

Machine language programs0010110010010001111

assembler

Assembly language programADD r0,r1,r2

compiler

to machine for execution

The assembly language stageis often skipped…

Compiler often directly generates machine code.

Compilers vs Interpreters

Page 17: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 17

Compilation

Compilers convert high-level code to machine code compile once, execute many times resulting machine code is optimized may include intermediate step (assembly) slower translation, but higher performance when

executed Is an assembler considered a compiler?

assemblers do convert higher level code to machine code, but…

they are usually in a class by themselves

Compilers vs Interpreters

Page 18: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 18

The C Programming Language

Developed 1972 by Dennis Ritchie at Bell Labs C first developed for use in writing compilers

and operating systems (UNIX) A low-level high-level language Many variants of C 1989, the American National Standards Institute

standardized C (ANSI C, most commonly used C) “The C Programming Language” by Kernighan and

Ritchie is the C “Bible” C is predecessor to most of today’s procedural

languages such as C++ and Java.

The C Language

Page 19: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 19

Compiling a C ProgramThe C Language

Object Code

Assembler Code

C/C++ Code

Machine Code

Page 20: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 20

Compiling a C ProgramC Source Code

C Preprocessor

Library & ObjectFiles

ExecutableImage

C Compiler

The C Language

Preprocessedsource code

Source CodeAnalysis

1st Pass

SymbolTable

CodeGeneration

2nd Pass

Linker

Object module

Page 21: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 21

A First Program

//************************************// blinky.c: Software Toggle P1.0//************************************#include "msp430x22x4.h"

void main(void){ int i = 0; WDTCTL = WDTPW + WDTHOLD; // stop WD P1DIR |= 0x01; // P1.0 output for (;;) // loop { P1OUT ^= 0x01; // toggle P1.0 while (--i); // delay }}

Tells compiler to use all the definitions found in the msp430x22x4.h library. A .h file is called a header file and contains definitions and declarations.

All programs must have a main() routine. This one takes no arguments (parameters).

Set P1.0 as output

Loop forever

Toggle P1.0Delay 65,536

1st C Program

Stop WD w/Password

Page 22: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 22

Comments

Use lots of comments/* This is a comment */

// This is a single line comment Comment each procedure telling:/*----------------------------------* * ProcedureName – what it does * * Parameters: * * Param1 – what param1 is * * Param2 – what param2 is * * Returns: * * What is returned, if anything * *----------------------------------*/

Use lots of white space (blank lines)

C Style

Page 23: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 23

Indenting Style

Each new scope is indented 2 spaces from previous Put { on end of previous line, or start of next line Line matching } up below

Style is something of a personal matter.

Everyone has their own opinions…

What is presented here is similar to that in common use and a good place to start...

if(a < b) { b = a; a = 0; } else { a = b; b = 0; }

if(a < b) { b = a; a = 0; } else { a = b; b = 0; }

Style 1 if(a < b) { b = a; a = 0; } else { a = b; b = 0; }

if(a < b) { b = a; a = 0; } else { a = b; b = 0; }

Style 2

C Style

Page 24: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 24

More On Indenting Style

For very long clauses, you may want to add a comment to show what the brace is for:

if(a < b){

/* Lots of code here... */

} // end if(a < b)else{

/* Lots of code here... */

} // end else

if(a < b){

/* Lots of code here... */

} // end if(a < b)else{

/* Lots of code here... */

} // end else

C Style

Page 25: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 25

The C Preprocessor

#define symbol code The preprocessor replaces symbol with code

everywhere it appears in the program below#define NUMBER_OF_MONKEYS 259

#define MAX_LENGTH 80

#define PI 3.14159

#include filename.h The preprocessor replaces the #include directive itself

with the contents of header file filename.h#include <stdio.h> /* a system header file */

#include "myheader.h" /* a user header file */

Preprocessor command are not terminated with ‘;’

C Preprocessor

Page 26: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 26

eZ430X System Functions

eZ430X.h and eZ430X.c

int eZ430X_init(int clock_speed); // init system

void ERROR2(int error); // fatal error

Setting system clock

eZ430X Header Files

#include "msp430x22x4.h"#include "eZ430X.h"

#define myClock CALDCO_8MHZ#define CLOCK 8000000 // SMCLK = ~8 mhzvoid main(void){ eZ430X_init(myClock); // init board ERROR2(5);}

Page 27: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 27

C I/O

I/O facilities are not part of the C language itself Nonetheless, programs that do not interact with their

environment are useless The ANSI standard defines a precise set of I/O

library functions for portability Programs that confine their system interactions to

facilities provided by the standard library can be moved from one system to another without change.

The properties of the C I/O library functions are specified in header files <stdio.h> (C standard library) "eZ430X.h", "lcd.h" (eZ430X)

eZ430X Header Files

Page 28: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 28

Output in C

printf( format_string, parameters )printf("\nHello World");printf("\n%d plus %d is %d", x, y, x+y);printf("\nIn hex it is %x", x+y);printf("\nHello, I am %s. ", myname);printf("\nIn ascii, 65 is %c. ", 65);

Output:Hello world

5 plus 6 is 11In hex it is bHello, I am Bambi.In ascii, 65 is A.

printf Function

String literal

DecimalInteger

HexInteger

StringCharacterNewlin

e

Page 29: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 29

LCD

lcd.c Prototypes int lcd_init(void); void lcd_volume(int volume); void lcd_backlight(int backlight); int lcd_display(int mode); void lcd_clear(int value); void lcd_image(const unsigned char* image,

int column, int page); void lcd_blank(int column, int page,

int width, int height); void lcd_cursor(int column, int page); char lcd_putchar(char c); void lcd_printf(char* fmt, ...);

printf Function

Page 30: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 30

LCD

LCD - 100 x 160 x 4 pixels display

Y (

0-9

9)

Page 10

Page 9

Page 8

Page 7

Page 6

Page 5

Page 4

Page 3

Page 2

Page 1

Page 0

Page 12

Page 11

printf Function

Hello World!

// 5 x 8 pixel Characterslcd_cursor(40, 5);lcd_printf("Hello World!");

X (0-159)

Page 31: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 31

// File: ftoc.c// Date: 02/15/2010// Author: Joe Coder// Description: Output a table of Fahrenheit and Celsius temperatures.

#include "msp430x22x4.h"#include "eZ430X.h"#include "lcd.h"

#define LOW 0 // Starting temperature#define HIGH 100 // Ending temperature#define STEP 10 // increment

int main(void){ int fahrenheit; // Temperature in fahrenheit float celsius; // Temperature in celsius

WDTCTL = WDTPW + WDTHOLD; // Stop WDT eZ430X_init(CALDCO_1MHZ); // init board lcd_init();

// Loop through all the temperatures, printing the table for(fahrenheit = LOW; fahrenheit <= HIGH; fahrenheit += STEP) { celsius = (fahrenheit - 32) / 1.8; printf("\nf=%d, c=%.1f", fahrenheit, celsius); }}

Use #define’s for magic numbers

A Second Program

1 digit to the right of the decimal point.

#include the lcd functions

Use meaningful names for variables

2nd C Program

Page 32: Chapter 11 - The C Language. BYU CS/ECEn 124The C Language2 Topics to Cover… ISR’s High Level Languages Compilers vs. Interpreters The C Language 1 st

BYU CS/ECEn 124 The C Language 32