36
Faculty of Electronic Engineering , Menofia University By / Eng : Ibrahim Reda Ibrahim Microcontrollers Programming

Microcontroller lec 3

Embed Size (px)

Citation preview

  1. 1. Why Programming language? Introduction to C programming. C programming for microcontrollers ( Micro C ). Flasher program
  2. 2. Why Programming language?
  3. 3. Programming Language Hardware is useless without SW. Programming language is the alternative of writing instruction in machine code. Programming languages are filling the gab between machine and human languages. Programming Language Low Level Language High Level Language
  4. 4. Sequence of Programming
  5. 5. 1.Preprocessing It is the first pass of any C compilation. It processes include-files. It defines the functions used in programming. Any Code contains # is foundeded in preprocessor stage. This tells the compiler what additional files are needed before processing the actual program. Its output is .I file. Examples : #include , #define pi 3.14 Stdio.h is the library or the header file that contains the functions which are used in the programming.
  6. 6. 2.Compiler It is the second pass. It takes the output of the preprocessor, and the source code, and generates assembler source code. It converts the high level Language into Assembly code. Its output is .S file
  7. 7. 3. Assembler It is the third stage of compilation. It takes the assembly source code and produces an assembly listing with offsets. The assembler output is stored in an object file. it converts basic computer instructions into a pattern of bits which can be easily understood by a computer and the processor can use it to perform its basic operations . This file contains Zeros and Ones. Its output is .O or .Obj file.
  8. 8. 4. Linker It is the final stage of compilation. It takes one or more object files or libraries as input and combines them to produce a single (usually executable) file. It links the Object files with the libraries. Its Output is .EXE file.
  9. 9. Introduction to C programming.
  10. 10. It is a part of every C program. The parenthesis after main indicate that main is program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. main()
  11. 11. This is used to display argument list on the monitor. This an output function. Examples: printf (argument); printf (Hello World !!); Note: n is an escape sequence for NEW LINE. t is an escape sequence for TAB. Printf()
  12. 12. This is used to input a single character or sequence of characters from the keyboard. It needs the control string codes to be recognized. Examples: scanf (format control string, &input list); scanf(%d,&x); Scanf()
  13. 13. Starts with /* and ends with */ It can be simple // Programmers insert comments to document programs and improve program readability. Can be placed anywhere in the program. Comments are ignored by the C compiler. Every line must has a comment Comments
  14. 14. There are 4 basic data types in C language: a) Character (char) : used to hold whole number values and ASCII characters. (8 bit) b) Integer (int) : used to hold whole number values. (16 bit) c) Floating Point (float) : used to hold real numbers. (32 bit) d) Double Point (double) : used to hold real numbers. (64 bit) Data Types
  15. 15. Signed By default all data types are declared as signed. Signed means that the data type is capable of storing negative values. Unsigned To modify a data types behavior so that it can only store positive values. Long This doubles the storage capacity of the data type being used. E.g. long int x will make the storage capacity of variable x to 4 bytes. Short If long data type modifier doubles the size, short on the other hand reduces the size of the data type to half. Data Modifiers
  16. 16. Constants A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. Examples : #define PI 3.14 float x; x = pi; // text replacement Declaring Variable as Constant The values of some variable may be required to remain constant through- out the program. Ex: const int size = 40
  17. 17. Variables are identifiers that can store changeable value. Variables are important since they contain the values needed for manipulation and evaluation of data. Variable names are stored in the computers memory. All variables must be declared before they may be used. Examples: char name; int x, y,z; float number; float n1, n2, sum; double counter; Variables
  18. 18. Global Variables Variables that are declared outside a function. They are known throughout the entire program. Local Variables Variables that are declared inside a function. They can only be referenced inside that function. They are also called automatic variables. Global VS local variable Global Variables #include int a,b,c, x,y,z; main() { } function() { } Local Variables #include main() { int a,b,c; } function() { int x,y,z;
  19. 19. if (condition1) { stmt 1; //Executes if Condition1 is true } else if (condition2) { stmt 2; //Executes if Condition2 is true } else { stmt 3; //Executes if both conditions are false } Conditional Statements
  20. 20. switch(condition) { case condition1 : statement 1 ; break; case condition2: statement 2; break; .. default : statement } Switch statement
  21. 21. Iteration Statements for ( initial condition;condition expression; increment expression) { statements; } Example: for(i=0;i> 2) = (00100110) 6.