The C Language - PowerPoint PPT Presentation


PPT – The C Language PowerPoint presentation | free to view - id: 7e592e-ODMzN


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

The C Language


The C Language – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 34
Provided by: PaulR211


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: The C Language

The C Language
Topics to Cover
  • ISRs
  • High Level Languages
  • Compilers vs. Interpreters
  • The C Language
  • 1st C Program
  • C Style
  • C Preprocessor
  • printf Function
  • eZ430X Header Files
  • 2nd C Program

Interrupt Service Routines
Interrupt Service Routine
  • Well-written ISRs
  • Should be short and fast
  • Should affect the rest of the system as little as
  • 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 Routines
Interrupt Service Routine
  • Who empties the trash?

Global Variable
Levels of Abstraction
High Level Languages
Assembly code
Machine (ISA) Architecture
Machine code
MSP430 Architecture
Logic gates, multiplexers, memory, etc.
Outline of Second Half of this Course
  • During the second half of the course, you will be
    introduced to fundamental high-level programming
  • Variables (Chapter 13)
  • Control structures (Chapter 14)
  • Functions (Chapter 15)
  • Arrays and Pointers (Chapter 16)
  • Simple data structures (Chapter 17)
  • Input and Output (Chapter 18)
  • Recursion (Chapter 19)

High Level Languages
High Level Languages
  • The closer a language is to your original
    specification, the easier the program is to
  • 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
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
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

if ((a gt '0') (a lt '9')) sum sum
10 sum sum (a - '0') else ...
High Level Languages
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

if(isCloudy) get(umbrella) else
High Level Languages
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
    initial programming costs
  • Easier to debug
  • Easier to maintain

main() readInput() checkForErrors()
doCalculation() writeOutput()
High Level Languages
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
  • something that should be true - if it isnt, then

assert(accountBalance gt 0)
Compilation vs. Interpretation
Compilers vs Interpreters
  • 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
Compilers vs Interpreters
Compilers vs Interpreters
  • Program code is interpreted at runtime
  • lines of code are read in
  • interpreter determines what they represent
  • requested function is performed

Interpretation is common LISP BASIC
Perl Java Matlab LC-2 simulator
UNIX shell MS-DOS command
line Interpretation can be slow...
interpret() while(1) // do forever
readInputLine() if(line0..3
"mult") doMultiply() else
if(line0..2 "add") doAdd() else
Compilers vs Interpreters
The assembly language stage is often
skipped Compiler often directly generates
machine code.
Compilers vs Interpreters
  • 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
  • Is an assembler considered a compiler?
  • assemblers do convert higher level code to
    machine code, but
  • they are usually in a class by themselves

The C Programming Language
The C 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 todays procedural
    languages such as C and Java.

Compiling a C Program
The C Language
Compiling a C Program
The C Language
C Source Code
C Compiler
A First Program
1st C Program
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
// //
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
All programs must have a main() routine. This
one takes no arguments (parameters).
Stop WD w/Password
Set P1.0 as output
Loop forever
Delay 65,536
Toggle P1.0
C Style
  • 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)

A Second Program
include ltstdio.hgt define STOP 0 int main(int
argc, char argv) int counter int
startPoint printf("\nEnter a positive number
") scanf("d", startPoint) for(counter
startPoint counter gt STOP counter--)
printf("\nCount is d", counter) return 0
Print a prompt
Indenting Style
C Style
  • Each new scope is indented 2 spaces from previous
  • Put on end of previous line, or start of next
  • 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...
More On Indenting Style
C Style
  • For very long clauses, you may want to add a
    comment to show what the brace is for

if(a lt b) / Lots of code here... /
// end if(a lt b) else / Lots of code
here... / // end else
The C Preprocessor
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
  • include ltstdio.hgt / a system header file
  • include "myheader.h" / a user header file /

Preprocessor commands are not terminated with
eZ430X System Functions
eZ430X Header Files
  • eZ430X.h and eZ430X.c
  • int eZ430X_init(int clock_speed) // init system
  • void ERROR2(int error) // fatal error
  • Setting system clock

include "msp430x22x4.h" include
"eZ430X.h" define myClock CALDCO_8MHZ define
CLOCK 8000000 // SMCLK 8 mhz void
main(void) eZ430X_init(myClock) //
init board ERROR2(5)
eZ430X Header Files
  • I/O facilities are not part of the C language
  • 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
  • The properties of the C I/O library functions are
    specified in header files
  • ltstdio.hgt (C standard library)
  • "eZ430X.h", "lcd.h" (eZ430X)

Output in C
printf Function
String literal
  • printf( format_string, parameters )
  • printf("\nHello World")
  • printf("\nd plus d is d", x, y, xy)
  • printf("\nIn hex it is x", xy)
  • printf("\nHello, I am s. ", myname)
  • printf("\nIn ascii, 65 is c. ", 65)
  • Output
  • Hello world
  • 5 plus 6 is 11
  • In hex it is b
  • Hello, I am Bambi.
  • In ascii, 65 is A.

Decimal Integer
Hex Integer
printf Function
  • 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
  • LCD - 100 x 160 x 4 pixels display

// 5 x 8 pixel Characters lcd_cursor(40,
5) lcd_printf("Hello World!")
Hello World!
Y (0-99) ?
X (0-159) ?
A Second Program
2nd C Program
include the lcd functions
// 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 lt HIGH fahrenheit STEP)
celsius (fahrenheit - 32) / 1.8
printf("\nfd, c.1f", fahrenheit, celsius)

Use defines for magic numbers
Use meaningful names for variables
1 digit to the right of the decimal point.
(No Transcript)