Vous êtes sur la page 1sur 33

LEC (1) part 2

Programming Language
(MikroC)

Programming
Language

Direct Flow Variables


Orders: Maths:
Control: :
>> >> +
Lcd_Chr(‘w’) >> for >> int
>> -
>> >> while >> float
Delay_ms(1) >> *
>> >> if >> char
>> /
ADC_Read(0) >> switch
>> %
1- direct orders :
 Do only one function .
EX :
 Delay_ms (1);
 Lcd_chr (“w”) ;
 ADC-Read(0) ;
2 – maths :
 Arithmetic Operators
Assignment Operators
Increment And Decrement Operators
 Relational Operators:
 Relational operators are used in comparisons for the
purpose of comparing two variables which can be integers
(int) or floating point numbers (float). If an expression
evaluates to true, a =1 is returned. Otherwise, a =0 is
returned. As I asked question and need answer with yes
or no .
Logical Operators

logic operations are commonly


used upon expressions, not upon
single variables (numbers) in the
program.
Therefore, logic operations refer to
the truth of the whole expression.
**If (temp>40&& press>50)
{alarm=1 ;}
Else
{}
the 2 conditions must be satisfied
to alarm be on .
** if (temp >=50 || press >60)
{alarm=1;}
Else { }
if any condition is satisfied the
alarm will be on .
Bitwise Operators
Unlike logic operations being performed upon variables, the bitwise operations are performed upon
single bits within operands.
Bitwise operators are used to modify the bits of a variable. They are listed in the table below:
(3)variables

.
 In mikro c , it must know the data type to reserve
appropriate size for it
 Ex :int temp ; >>>> type of variable is int
name of variable is temp
 Ex : x=13
I will choose int x ; or char x ; or float x; ????
All cases are right , but char x is the best to save size for
the ram .
 X= 1 ; >> I can use char x ; and int x ;
 X=“1” ; >> I can use only char x as x here is equal to
character 1 .
Declarations
 Every variable must be declared prior to being used for the first time in the
program. Since variables are stored in RAM memory, it is necessary to reserve
space for them (one, two or more bytes)

 Ex : int temp ; //Declare name and type of variable temp .


 Ex : int temp,press,case; //declare name and type of 3 variables .
 Ex : temp=20 ; // assign variable temp an initial value = 20 ;
 ** The process of assigning initial value and declaring type can be performed
in one step .

 Ex : int temp = 20 ; // Declare type, name and value of variable .


 ** If there are several variables being assigned the same initial value

Ex : int temp_1 ,temp_2 , temp_3= 25 ;


(4) Flow control
(A) Conditional Flow ( Selection Statements )
 IF Statement
 Switch Statement
(B)Iteration Statement ( program loop )
 For statement
 While statement
 Do statement
(c) Unconditional flow control
 Go to statement
 Break and continue statement
(A) Conditional Flow ( Selection Statements )

IF Statement
Case 1 :
if(condition ) {operation; }
Ex:
if (sw==1){led=1; }
Case 2 :
If (condition ) {operation 1 ;}
Else {operation 2 ;}
Ex:
if (sw==1){led=1; }
Else {led=0; }
Case 3 :
If(condition 1) {operation 1 ; }
Else if(condition 2) {operation 2 ; }
Else if(condition 3) {operation 3 ; }
.
.
Else {operation ; }
Ex :
If (temp>=20 && temp<25) {Lcd_Out(1, 3, “clod"); }
Else If (temp>=25 && temp<30) {Lcd_Out(1, 3, “normal"); }
Else If (temp>=30 && temp<=35) {Lcd_Out(1, 3, “hot "); }

Else {alarm=1 ; }
Switch case
Switch(condition){
Case condition 1 : operation 1 ; break ;
Case condition 2 : operation 2 ; break ;
Case condition 3: operation 3; break ;
Default : operation 4; }
Ex :
Switch(temp){
Case 37: lcd_out(1,1,”normal temperature “); break ;
Case 40: lcd_out(1,1,”hot temperature “); break ;
Default : lcd_out(1,1,” temperature is not critical “);
}
(B)Iteration Statement ( program loop )

For(initial ; condition ; increment )


{ operations; }

For statement
EX:
Ex: For (i=0; i<=10; i*=2)
For (i=0 ; i<5 ; i++ ) { alarm ; }
{ led=1;  used to repeat operation for
Delay_ms(1000); certain number of times
Led=0;
Delay_ms(1000);
}
 any for loop with out condition is endless loop
 EX:

For ( ; ; )
{
Alarm ;
}
While statement

while(condition ) EX:
{ operation ; } while (sw==1)
{ alarm=1; }
 repeating the operation
 While loop with out
till the condition is broke
condition is endless loop
 When the condition
change it will go out of the EX:
loop . While (1 )
 Check the condition first {alarm=1; }
before make the operation
 For loop  While loop
for(k=0 ; k<10 ;++k) K=o
{ alarm=1; While(k<10)
Dealy_ms(500); {alarm=1;
Alarm=0; Dealy_ms(500);
Delay_ms (500); Alarm=0;
} Delay_ms (500);
++k
}
Do statement

Do { EX:
Operation ; Do {
} Alarm;
While (condition) }
While {sw==1; }
 Do the operation before
check the condition
(c) Unconditional flow control

Go to statement
 Cause the program to jump to specified label
 A label can be any alphanumeric character set starting with letter
and terminating with the colon (: ) character

EX :
k=0;
nn: k=1 ;
++k;
.
.
.
If (k>=5 ){ goto nn ;}
Break and continue statement

Used to go out from loops


EX:
For (i=0 ; i<=10 ; ++i)
{
x+=1 ; // this mean x=x+1
If( x>5) break ;
}
some notes
Comments
 Used to clarify the operation of the program and a
programming statement .
 Ignored and not compiled by the compiler
Beginning and ending of the program
Terminating program statements
 In C language all program statement must be terminated
with the semicolon ( ; ) character , otherwise the compiler
error will be generated
Spaces
 The C compiler ignores all spaces .
Case sensitivity
 C language is case sensitive .
 Variable with lower case sensitive names are different from
variables with upper case names .
But
Mikro C identifiers are not case sensitive .
main and interrupt must be written in lower case
Variables names
 In C language , variables names can begin with an
alphabetical character or with the underscore character ( _
).
 Variable names can contain uppercases and lower cases
characters
 Variable names must not start with a digit.
 as temp_1 temp_2 sw_1 sw_2 led_red led_green
 Some of the names cannot be used as variable names as
already being used by the compiler itself. Such names are
called the key words. The mikroC compiler recognizes in
total of 33 such words:
INTEGER CONSTANTS
 Integer constants can be decimal, hexadecimal, octal or binary. The compiler
recognizes their format on the basis of the prefix added. If the number has no
prefix, it is considered decimal by default

Vous aimerez peut-être aussi