C for Micro Controllers
-
Upload
tarun-bhalla -
Category
Documents
-
view
236 -
download
0
Transcript of C for Micro Controllers
-
8/3/2019 C for Micro Controllers
1/27
Corporate Excellence Program
In Electronics & Communication
C Language Module
Building Blocks
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
2/27
Corporate Excellence ProgramOutline: C Language
- Introduction to C Language
- Writing Programs in C
- Function Calls and Main Function
- A sample program on Functions
- Data Types in C
- A sample program on Data Types
- Arithmetic Operators in C
- A Sample Program on Arithmetic Operators
- Relational and Logical Operators in C
- A Sample Program on Relational and Logical Operators
- Bitwise Operators in C
-Sample Programs on Bit Wise Operators- Decision Control and Case Control Structure in C
- A Sample Program on Decision Control and Case Structures
- Loop Control Structures in C
- A Sample Program on Loop Control Structures in C
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
3/27
Corporate Excellence ProgramC Language?
Cis a simple programming language with few keywords and arelatively simple syntax.
Properties of C Language
- Itself has no input/output commands.
-Doesn't have support for strings as a fundamental data type.
-No useful math functions built in.- So C is useless by itself.
Because C is useless by itself, it requires Libraries
A LIBRARY is nothing but a collection offunctions
- Some of these libraries are :
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
4/27
Corporate Excellence ProgramWriting programs in C
Following steps should be followed to write any program
1. Initialize the Header Filea. Header File: contains prototypes and other compiler/pre- processor
directives. Or in other words DEFINITIONS
b. Syntax : #include
#include
#include
c. Different Header File are initialized based on the function that we want
:- initialization for the Input/Output functions
:-initialize for the getchar (void) and putchar(void)
function activation
:- initialize for the mathematical operation functions.
2. Define functions (if any)?
a. A logical set of statements written together to be reused in various contextsb. Can have some/none input parameters and onereturn value
c. Can be defined within the same file or externally in a .h file
d. It needs to be declared first in order to be used
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
5/27
3. Write the Main function
- The execution of any program begins from this function
- It is the most important function and must be present in every C program- Every program has exactly one main function
Syntax : main()
{ /* opening of the main function */
// set of execution statements could be any:
- Declaration statements;
- Arithmetic Operations;
- Function Calls;
return (instance of ) ;
} /* end of the main function */
4. Do Commenting
- used to make any program readable/understandable for other programmers
- is not an executable statement- should be given at the beginning or end of the line but not in middle of the line
5. Every statement in c should end with semicolon (;)
Note: C is a case sensitive Language
Corporate Excellence ProgramMain Function and Other Function Calls
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
6/27
Corporate Excellence ProgramSample program using Main
Sample #1 Write a program to print your name as VIJAY GUPTA?
Ans: /* to print name of VIJAY GUPTA */
# include // initialize the header file for input out function.
main () // initialize the main function
{
printf ( " VIJAY GUPTA" ); // printf is used to print t5he name or display the output
return (0); // Main is return type function so we must return some// integral value
}
Printf () command : This is the function which is used to print on the screen the value contain in variable
and is present in stdio.h .
Syntax: printf(, );
Note:
could be:
%f - to print real value
%d to print integral value
%c to print character value
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
7/27
Corporate Excellence ProgramData Types and Declaration
Type Declaration Instruction
- Is used to declare the type of variable- Any variable in the program is written at the beginning
- Should be declared before using it in any statement
- There are three common data types:
1. Int :- Used to declare the integral value of the variable
syntax: int a ;int c;
int b=2;
2. Float :- used to initialize the value in fraction or value in decimal number
syntax: float x;
float y=2.5;
3. Char :- is used to initialize the character type value in variable
syntax: char a ;
char b =ram ;
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
8/27
Corporate Excellence ProgramSample Program using Data Types
Sample #2: Enter two integers and print their sum
# include # include
Void main()
{
int f_num =0, s_num =0,sum;
Printf(Enter First Number: \n);
Scanf(%d, &f_num);
Printf(Enter Second Number: \n);
Scanf(%d, &s_num);
sum = f_num + s_num;
Printf ( sum of two number =%d,sum);
Getche();
}
Sample #3: Enter two float numbers and print their sum
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
9/27
Corporate Excellence ProgramArithmetic Operators in C
Operator: following are the operators used
1. Arithmetic operators:
Modulus Operator is used to get the remainder.
Eg. 5%3=2,
2. Precedence of Arithmetic Operators:
-Highest Precedence
/, *, %
-Lowest Precedence
+ , -
SN. Operation Operator
1 . Addition +
2. Subtraction -
3. Multiplication *
4. Division /
5. Increment ++6. Decrement --
7. Modulus %
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
10/27
Corporate Excellence ProgramSample Program using Arithmetic Operators
Sample #4: Convert a given number of days into months and remaining days?
# include # include
# include
Void main()
{
Int months, days;
Printf(Enter Days: \n);
Scanf(%d, &days);
months = days / 30;
days = days % 30;
Printf(Month : %d and Days : %d \n, months, days);
}
Sample #5: Write a program that tells the remainder when a NUMBER A is divided by NUMBER B
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
11/27
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
Relational refers to the relationships values can have with one another.
Logical the ways these relationships can be connected together using the rules
offormal logic.
Corporate Excellence ProgramRelational and Logical Operators in C
SN. Operation operator
1. Greater than >
2. Greater than equal to >=
3. Less than >
6. Left SHIFTs the operand by mentioned bits
-
8/3/2019 C for Micro Controllers
14/27
Corporate Excellence ProgramSample Program using Bitwise Operators
Sample #5: Write a program that ANDs, ORs, XORs any two number that are entered?
# include # include
Void main()
{
int num_1=0, num_2=0,and_num=0, or_num=0,xor_num=0;
Printf(Enter Number 1: \n);
Scanf(%d, &num_1);
Printf(Enter Number 2: \n);
Scanf(%d, &num_2);
and_num = num_1&num_2;
Printf(The Anded number is: %d, and_num);
or_num = num_1&num_2;
Printf(The ORed number is: %d, or_num);
xor_num = num_1&num_2;
Printf(The ORed number is: %d, xor_num);
}
Sample #6: Find whether a number is power of two? Also write it as a function.
Sample #7: Divide and multiply a number by 2 using bitwise operators?
Sample #7: Perform the sum of two numbers using bitwise operators
Sample #8: Write programs that set, clear, toggle and test a bit
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
15/27
Corporate Excellence ProgramSample Program using Bitwise Operators
Sample #5: Perform the sum of two numbers using bit-wise operators
#include
int main()
{
int a,b,sum, carry;
scanf("%d
%d",&a,&b);
sum = a ^ b;
carry = a & b;
while (carry != 0){
carry
-
8/3/2019 C for Micro Controllers
16/27
Corporate Excellence ProgramDecision Control Instructions
C Has Three major decision making instructions:-
1. If statement
2. If-else statement
3. Switch Case statement
If statement:- is used to implement the decision control instruction.
- Syntax: if (This condition is true)
{
execute this statement ;}
- Generally Relational operators are used to compare two values to get a TRUE or FALSE
- No semicolon at the end of the if statement
- No bracket is needed if single statement has to be executed
Sample 7: While purchasing of some items, a discount of the 10% is given if the quantities,purchased, are more than 100. If quantity , and price per item is input through the keyboard
then calculate the total expense.
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
17/27
Corporate Excellence ProgramIf statement: Example
#include
#include
Void main()
{
int qty, dis=0;
Float rate, tot;
Clrscr();
Printf ( Enter quantity and rate );
Scanf(%d%f, &qty, &rate);
if ( qty>100)
Dis=10;
Tot=(qty*rate)- (qty*rate*dis/100);
Printf( Total expense = %f tot);
Getch();}
Assignment 1Enter any number from the keyboard, if the number is more than 10 thenprint your name and address, else, do nothing.
Assignment 2On pressing Y on keyboard, your program should output Light GOES on,
else Light GOES off.
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
18/27
Corporate Excellence ProgramIf - else statement
If- else statement:- is also used to implement the decision control instruction.
- Syntax: if ( This condition is true)
{
execute this statement ;
}
Else
{
execute this statement ;
}
- if statement is used to execute when the condition is true but if-else statement
is used to execute two mutually exclusive set of statements.
- The syntax is similar to If Statement barring that there are no brackets
Example. If the input value of sensor is less than 500, then you print its DARK, else you
print itsLIGHT.
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
19/27
Corporate Excellence Programif - else statement
#include
#include
Void main()
{
Int sensor_value;
Clrscr();
Printf ( Enter the Sensor Value);
Scanf(%d, &sensor_value);if (sensor_value > 500) {
Printf( DARK);
}
Else
{
Printf( LIGHT);
}
Getch();
}
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
20/27
Corporate Excellence ProgramNested if - else statement
Nested If- else statement:- is the statement in which the entire if-else statement is written either in the body
ofifstatement or in the body of else statement.- Syntax: if ( condition 1)
{
if ( condition 2)
{
do this statement ;
and this statement ;
}
else
{
do this statement ;
and this statement ;
}
}
else
{
execute this statement ;
} www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
21/27
Logical Operators are used to connect together two independent statements.
Operator Action Meaning&& and The outcome will be true iff either conditions are TRUE
II Or The outcome will be true even if one condition isTRUE! Not Negativeof a condition
Sample 9: The marks obtained by a student in 5 different subjects are entered via the
key board . Write the program to calculate the division obtained by the student. If
percentage obtained is above or equal to 60
first division, if percent between 50 and 60
second division, if percent between 40 and 50 third division and below 40 fail.
Corporate Excellence ProgramNested if - else statement contd
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
22/27
Corporate Excellence Program
loop Control structure
Loops :- Is a programming control used to repeat a set of instructions for a certain number of times.
- The number of times can be defined ITERATIVELY or based on a CONDITION
- There are three methods by which we implement LOOPS in C
forstatement
while statement
do-while statement
1. While statement:Syntax: while(CONDITION)
{
do this ;
do this ;
}
- is used to execute a set of instructions till the condition is TRUE .
- when the condition is false, then the control will come out of the loop
- Examples:
- To implement an infinite LOOP, while (1)
- To implement a timed LOOP, while (i < 20) {
i++;
}
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
23/27
Corporate Excellence ProgramWhile loop Control structure
Example: Enter the number by the key-board and print the table of that number
#include
#include
Void main()
{
int num, counter=1, table ;clrscr();
Printf (enter the number whose table you want to print);
Scanf ( %d, &num);
While( counter
-
8/3/2019 C for Micro Controllers
24/27
Corporate Excellence ProgramFor loop Control structure
FOR statement .
- allows us to specify three things about the loop in the single line- three things are initialization, comparison and increment.
a. Setting the loop counter in the initial value.
b. Comparing the loop counter .
c. Increasing the value of the loop counter
Syntax: for ( initialize the counter; test counter; increment the counter)
{
do this;
and do this;
and do this
}
Example: Print the Fibonacci series: (1, 2,3, 5,8, ) with 20 terms
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
25/27
Corporate Excellence ProgramFor loop Control structure
#include
#include
Void main()
{
int i=0, num=1, prev_num=0;
clrscr();
printf(%d \n num);
for(i=0; i
-
8/3/2019 C for Micro Controllers
26/27
Corporate Excellence ProgramNesting of loop Control structure
Nested Loop:
- any loop inside the other loop is known as nested loop.
Syntax: for ( initialize the counter; test counter; increment the counter)
{
for(( initialize other counter; test counter; increment the counter)
{
for(( initialize other counter; test counter; increment the counter)
{
do this;
and do this ;
}
}
}
www.buildingblocks.in | www.buildingblocks.in/professional| 9818403093
http://www.buildingblocks.in/professionalhttp://www.buildingblocks.in/professional -
8/3/2019 C for Micro Controllers
27/27