Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012.

Similar presentations


Presentation on theme: "CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012."— Presentation transcript:

1 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012

2 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 C: A High Level Programming Language Gives symbolic names to values –Don’t need to know which register or memory location Provides abstraction of underlying hardware –operations do not depend on instruction set –example: can write “a = b * c”, even though underlying hardware may not have a multiply instruction

3 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 C: A High Level Programming Language Provides expressiveness –use meaningful symbols that convey meaning –simple expressions for common control patterns (if-then-else) Enhances code readability Safeguards against bugs –can enforce rules or conditions at compile-time or run-time

4 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Compilation vs. Interpretation Different ways of translating high-level language Interpretation –interpreter = program that executes program statements –generally one line/command at a time –limited processing –easy to debug, make changes, view intermediate results –languages: BASIC, LISP, Perl, Java, Matlab, C-shell

5 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Compilation vs. Interpretation Compilation –translates statements into machine language –does not execute, but creates executable program –performs optimization over multiple statements –change requires recompilation can be harder to debug, since executed code may be different –languages: C, C++, Fortran, Pascal

6 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Compilation vs. Interpretation Consider the following algorithm: Get W from the keyboard. X = W + W Y = X + X Z = Y + Y Print Z to screen. If interpreting, how many arithmetic operations occur? If compiling, we can analyze the entire program and possibly reduce the number of operations. Can we simplify the above algorithm to use a single arithmetic operation?

7 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Compilation Source Code Analysis –“front end” –parses programs to identify its pieces –variables, expressions, statements, functions, etc. –depends on language (not on target machine) Code Generation –“back end” –generates machine code from analyzed source –may optimize machine code to make it run more efficiently –very dependent on target machine Symbol Table –map between symbolic names and items –like assembler, but more kinds of information

8 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 “Hello World” The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words hello, world This is a big hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy.

9 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 /******************************************************************************** * Program to print "Hello World" on the output screen * * set up for MPLAB environment, no specific processor * * Notes: use SIM Uart1 to see output * * * Gabriel Hugh Elkaim, 26-Mar-2011 * ********************************************************************************/ #include int main() { printf("hello, world\n"); // uses the I/O library to print return 0; } The Code

10 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Setting up the IDE Configuring the Simulator Set the Debug simulator to wait at the beginning of the main() function

11 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Resetting MPLAB ® X windows As you will see MPLABX has numerous adjustable windows. New MPLABX users can get a little confused about where and how the set the windows. If you get confused Windows -> Reset Window Restores MPLABX Windows back to their original locations

12 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Opening a Project Opening a Project

13 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Opening a Project Opening a Project 1) Navigate to the Project Directory 2) Select the Project 3) Select Open Project

14 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Opening a Project Opening a Project

15 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Building a Project Building a Project

16 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Building a Project Building a Project Successful Build Simulation ready to start Control Buttons Appear

17 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Running the Simulation Running the Simulation

18 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Pausing the Simulation Pausing the Simulation

19 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Windows used in Examples Variables Window Variable Window displays a particular set of program variables To Open the Variables window: Select: Windows->Debugging- >Variables

20 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Windows used in Examples Variables Window Variable Window displays several columns of data You may find it convenient to alter the columns displayed. “ right click” on the column heading

21 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Windows used in Examples UART1 Output UART1 Output Window prints out text from C programs To clear this window: Right click inside of the window then select Clear

22 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Windows used in Examples Watches Window Watches Window is similar to the Variables window but displays a different set of data To Open the Watches Window: Select: Windows->Debugging- >Watches

23 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Windows used in Examples Watches Window Watches Window needs to be ‘told’ what data to watch ** Column configuration is identical to Variables Window “ Right click” while in the Watches Window to add or delete watches

24 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Closing a Project Closing a Project

25 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Closing a Project Closing a Project

26 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Example Fundamentals of C A Simple C Program #include #define PI 3.14159 int main(void) { float radius, area; //Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("Area = %f", area); } Header File Function Variable Declarations Constant Declaration (Text Substitution Macro) Comment Preprocessor Directives

27 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Definition Comments Two kinds of comments may be used: –Block Comment /* This is a comment */ –Single Line Comment // This is also a comment Comments are used to document a program's functionality and to explain what a particular block or line of code does. Comments are ignored by the compiler, so you can type anything you want into them.

28 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Comments Using Block Comments Block comments: /**/ –Begin with /* and end with */ –May span multiple lines /******************************************************** * Program: hello.c * Author: R. Ostapiuk ********************************************************/ #include #include /* Function: main() */ int main(void) { printf(“Hello, world!\n”); /* Display “Hello, world!” */ printf(“Hello, world!\n”); /* Display “Hello, world!” */}

29 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Comments Using Single Line Comments Single line comments: // –Begin with // and run to the end of the line –May not span multiple lines //======================================================= // Program: hello.c // Author: R. Ostapiuk //======================================================= #include #include // Function: main() int main(void) { printf(“Hello, world!\n”); // Display “Hello, world!” printf(“Hello, world!\n”); // Display “Hello, world!”}

30 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Comments Nesting Comments Block comments may not be nested within other delimited comments Single line comments may be nested /* code here // Comment within a comment code here // Comment within a comment*//* code here /* Comment within a comment */ code here /* Comment within a comment */ code here /* Comment within a… oops! */ code here /* Comment within a… oops! */*/ Example: Single line comment within a delimited comment. Example: Delimited comment within a delimited comment. Delimiters don’t match up as intended! Dangling delimiter causes compile error

31 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Comments Best Practices /******************************************************** * Program: hello.c * Author: R. Ostapiuk ********************************************************/ #include #include /******************************************************** * Function: main() ********************************************************/ int main(void) { /* /* int i; // Loop count variable int i; // Loop count variable char *p; // Pointer to text string char *p; // Pointer to text string */ */ printf(“Hello, world!\n”); // Display “Hello, world!” printf(“Hello, world!\n”); // Display “Hello, world!”}

32 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Variable Declarations

33 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Example Variables and Data Types A Simple C Program Variable Declarations Data Types Variables in use #include #define PI 3.14159 int main(void) { float radius, area; //Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("Area = %f", area); }

34 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Variables A variable may be thought of as a container that can hold data used in a program Definition A variable is a name that represents one or more memory locations used to hold program data. int myVariable; myVariable = 5; myVariable5

35 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 41 16 Variables 5.74532370373175 × 10 -44 015 Data Memory (RAM) int warp_factor; float length; char first_letter; ‘A’ Variables are names for storage locations in memory

36 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 float length; char first_letter; int warp_factor; 41 16 Variables 5.74532370373175 × 10 -44 015 Data Memory (RAM) ‘A’ Variable declarations consist of a unique identifier (name)…

37 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 float length; char first_letter; int warp_factor; 41 16 Variables 5.74532370373175 × 10 -44 015 Data Memory (RAM) ‘A’ …and a data type –Determines size –Determines how values are interpreted

38 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Example of Identifiers in a Program Identifiers Names given to program elements: –Variables, Functions, Arrays, Other elements #include #define PI 3.14159 int main(void) { float radius, area; //Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("Area = %f", area); }

39 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Identifiers Valid characters in identifiers: Case sensitive! Only first 31 characters significant* Identifier First Character ‘_’ (underscore) ‘A’ to ‘Z’ ‘a’ to ‘z’ Remaining Characters ‘_’ (underscore) ‘A’ to ‘Z’ ‘a’ to ‘z’ ‘0’ to ‘9’

40 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 ANSI C Keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Some compiler implementations may define additional keywords

41 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Data Types Fundamental Types The size of an int varies from compiler to compiler. MPLAB-C30 int is 16-bits If you need precise length variable types, use stdint.h uint8_t is unsigned 8 bits int16_t is signed 16bits, etc. TypeDescriptionBits char int float double single character integer single precision floating point number double precision floating point number 16 8 32 64

42 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Data Type Qualifiers Modified Integer Types Qualifiers : unsigned, signed, short and long Qualified TypeMinMaxBits unsigned char char, signed char unsigned short int short int, signed short int unsigned int int, signed int unsigned long int long int, signed long int unsigned long long int long long int, signed long long int 0 -128 0 -32768 0 0 -2 31 0 -2 63 255 127 65535 32767 65535 32767 2 31 -1 2 63 -1 2 32 -1 2 64 -1 8 8 16 32 64 32 64

43 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Data Type Qualifiers Modified Floating Point Types MPLAB-C30: (1) double is equivalent to long double if –fno-short-double is used MPLAB-C30 Uses the IEEE-754 Floating Point Format Qualified TypeAbsolute MinBits float double long double ± ~10 -44.85 ± ~10 -323.3 32 32 64 Absolute Max ± ~10 38.53 ± ~10 308.3 (1)

44 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Variables How to Declare a Variable A variable must be declared before it can be used The compiler needs to know how much space to allocate and how the values should be handled Syntax type identifier 1, identifier 2,…,identifier n ; Example int x, y, z; float warpFactor; char text_buffer[10]; unsigned index;

45 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Syntax Variables How to Declare a Variable Variables may be declared in a few ways: type identifier; type identifier = InitialValue; type identifier 1, identifier 2, identifier 3 ; type identifier 1 = Value 1, identifier 2 = Value 2 ; One declaration on a line One declaration on a line with an initial value Multiple declarations of the same type on a line Multiple declarations of the same type on a line with initial values

46 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Examples Variables How to Declare a Variable unsigned int x; unsigned y = 12; int a, b, c; long int myVar = 0x12345678; long z; char first = 'a', second, third = 'c'; float big_number = 6.02e+23; It is customary for variable names to be spelled using "camel case", where the initial letter is lower case. If the name is made up of multiple words, all words after the first will start with an upper case letter (e.g. myLongVarName ).

47 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Variables How to Declare a Variable Sometimes, variables (and other program elements) are declared in a separate file called a header file Header file names customarily end in.h Header files are associated with a program through the #include directive MyProgram.h MyProgram.c

48 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 #include Directive Three ways to use the #include directive: Syntax #include Look for file in the compiler search path The compiler search path usually includes the compiler's directory and all of its subdirectories. For example: C:\Program Files\Microchip\MPLAB C30\*.* #include “file.h” Look for file in project directory only #include “c:\MyProject\file.h” Use specific path to find include file

49 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 main.h main.c #include Directive main.h Header File and main.c Source File unsigned int a; unsigned int b; unsigned int c; #include "main.h" int main(void) { a = 5; b = 2; c = a+b; } The contents of main.h are effectively pasted into main.c starting at the #include directive’s line

50 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 main.c #include Directive Equivalent main.c File Equivalent main.c file without #include unsigned int a; unsigned int b; unsigned int c; int main(void) { a = 5; b = 2; c = a+b; } After the preprocessor runs, this is how the compiler sees the main.c file The contents of the header file aren’t actually copied to your main source file, but it will behave as if they were copied

51 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 1 Variables and Data Types

52 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

53 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 01 Variables and Data Types Open the lab Project: On the class website Examples -> Lab1.zip 1 1 Open MPLAB ® X and select Open Project Icon (Ctrl + Shift + O) Open the project listed above. If you already have a project open in MPLAB X, close it by “right clicking” on the open project and selecting “Close”

54 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 01 Variables and Data Types Compile and run the code: 2 2 Click on the Debug Project button. Debug Project Continue 2 2 3 3 3 3 If no errors are reported, click on Continue button to start the program. Pause 4 4 4 4 Click on the Pause button.

55 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 01 Variables and Data Types Expected Results (1): 5 5 The UART 1 Output window should show the text that is output by the program, indicating the sizes of C’s data types in bytes.

56 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 01 Variables and Data Types Expected Results (2): 6 6 The Variables window ( alt + shift + 1) shows the values and addresses of the variables

57 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 0x09CC0x09CD Exercise 01 Variables and Data Types 00 42 00 42 00 32 00 32 48 00 48 00 0x09CE 0x09D0 0x09D2 0x09D4 0x09D6 0x09D8 0x09DA 0x09DC 0x09DE 0x09E0 0x09CF 0x09D1 0x09D3 0x09D5 0x09D7 0x09D9 0x09DB 0x09DD 0x09DF 0x09E1 short int char int long int float double Variables in Memory Multi-byte values stored in "Little Endian" format on PIC® microcontrollers 7 7 16-bit Data Memory

58 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 01 Variables and Data Types What does the code do? START Declare Constant Declare Variables Initialize Variables Print Variable Sizes Print Variable Sizes Loop Forever Loop Forever #define CONSTANT1 50 int intVariable; intVariable = CONSTANT1; printf("\nAn integer variable requires %d bytes.", sizeof(int)); while(1); Example lines of code from the demo program:

59 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Exercise 01 Conclusions Variables must be declared before used Variables must have a data type Data type determines memory use Most efficient data types: –int on 16-bit architectures –char on 8-bit architectures (if int is 16-bit) Don't use float/double unless you really need them

60 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 Questions?

61 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

62 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

63 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

64 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

65 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

66 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

67 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

68 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

69 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

70 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

71 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

72 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

73 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

74 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

75 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

76 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

77 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

78 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

79 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012

80 CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012


Download ppt "CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012."

Similar presentations


Ads by Google