Lecture_03_04_Embedded-C
Lecture_03_04_Embedded-C
Lecture_03_04_Embedded-C
Microprocessor
Lecture #3-4
Embedded C Language
2
Connecting an LCD with the uC
3
1
You MUST NOT choose the name of a VARIABLE as one of the above 5
Variables
6
Scope of the Variables
unsigned char globey; //a global void main()
{
void function_z (void) unsigned char main_loc; //local
{ globey = 34; //global
unsigned int tween; //local tween = 12; //local to function_z
tween = 12; //local while(1) // do forever..
globey = 47; //global {
main_loc = 12; // local to main() ……….
} }
} 7
Constants
• Numeric constants can be declared in many ways by indicating their
numeric base and making the program more readable.
• Integer or long integer constants may be written in
– Decimal form without a prefix (such as 1234)
– Binary form with 0b prefix (such as 0b101001)
– Hexadecimal form with 0x prefix (such as 0xff)
– Octal form with 0 prefix (such as 0777)
Identifying a variable as a constant will cause that variable to be stored
in the program code space rather than in the limited variable storage
space in RAM. This helps preserve the limited RAM space.
8
“Define”
• Suppose you have connected a buzzer in pin 2 of Port A.
• In order to make the buzzer ON you write PORTA.2=1;
• Would it not better if you could write the statement like buzzer=1 because it
is close to what you really meant?
• To do this you define buzzer as PORTA.2 by writing
#define buzzer PORTA.2
• The “#define buzzer PORTA.2” line causes the compiler to substitute the
label PORTA.2 wherever it encounters the word buzzer.
9
Arithmetic Operators
• Table 1–4 shows the typical arithmetic operators.
10
Bitwise Operators
11
Logical Operators
• Logical and relational operators are all binary operators but yield a result
that is either TRUE or FALSE. TRUE is represented by a nonzero value,
and FALSE by a zero value.
12
Relational Operators
• Relational operators use
comparison operations.
• As in the logical operators, the
operands are evaluated left to right
and a TRUE or FALSE result is
generated.
• They effectively “ask” about the
relationship of two expressions in
order to gain a TRUE or FALSE reply.
13
Increment, Decrement and Compound
Assignment
Increment or Decrement: a |= 3; // a = a OR 3
i = 1; b &= 2; // b = b AND 2
k = 2 * i++; // k = 2 and i = 2
c ^= 5; // c = c ex-ORed with 5
i = 1;
k = 2 * ++i; // k = 4 and i = 2 PORTC &= 3;
A compound assignment: // Write the current value on
a += 3; // a = a + 3 //PORTC ANDed with 3 and
b -= 2; // b = b – 2
// back to PORTC.
c *= 5; // c = c * 5
d /= a; // d = d / a
14
Operator Precedence
8/2 × (2 + 2)
15
Operator Precedence
16
Array
• An array is a data set of a declared type, arranged in order.
• An array is declared like any other variable or constant, except for the
number of required array elements:
int digits[10]; // this declares an array of 10 integers
char str[20]; // this declares an array of 20 characters
• The referencing of an array element is handled by an index or subscript.
• The index ranges from 0 to the length of the declared array less 1.
• str[0], str[1], str[2], . . . . . str[19]
17
if statement
An if statement has the following form:
if (expression) or if(expression)
{ statement;
statement1;
statement2;
...
}
18
if … else statement
if(expression) or if(expression)
{ statement1;
statement1; else
statement2; statement2;
...
}
else
{
statement3;
statement4;
...
}
19
if … else if … else statement
if(expr1)
statement1;
else if (expr2)
statement2;
else if(expr3)
statement3;
else
statement4;
20
void main(void)
An Example of {
“if … else” statement lcd_init(16);
if (k==0)
/* if_statement.c
j=10;
BRACU
else
*/
#include <mega32.h> j=20;
#include <alcd.h> sprintf(lcd,"j = %d",j);
#include <delay.h> lcd_gotoxy(0,0);
#include <stdio.h> lcd_puts(lcd);
int k=0, j; delay_ms(1000);
char lcd[16]; lcd_clear();
}
21 21
switch … case switch (expression)
{
do
do {
statement1
statement; statement2;
while (expression); ...
} while (expression);
27
break Statement
for (val =1; val<10; val++)
{
if (val==6)
break;
printf(“%d”,val);
}
❑ In the ‘for’ loop there are two statements, one is ‘if’
statement and other is ‘print’ statement.
❑ For val=1 to 5 print statement will be executed,
❑ But for val=6, the ‘for’ will be broken and the program will
come out of the for loop.
❑ That means for val=7 to 9 ‘for’ loop will not be executed.
28
continue Statement
• The continue statement will allow the program to start the next
iteration of a while, do … while, or for loop.
• The continue statement is like the break statement in that both stop
the execution of the loop statements at that point.
• The difference is that the continue statement starts the loop again,
from the top, where break exits the loop entirely.
for (val =1; val<10; val++)
{
if (val==6)
continue;
printf(“%d”,val);
}
29
Program with continue and break k=0;
delay_ms(1000);
Statement while (k<10)
#include <mega32.h> while (k<10) {
#include <alcd.h> { k++;
#include <delay.h> if (k==4)
k++;
break;
#include <stdio.h> if (k==4) sprintf(lcd,"k = %d", k);
continue; lcd_gotoxy(0,0);
int k=0; lcd_puts(lcd);
sprintf(lcd,"k = %d", k);
char lcd[16]; delay_ms(1000);
lcd_gotoxy(0,0);
lcd_clear();
lcd_puts(lcd);
}
delay_ms(1000); lcd_clear();
lcd_clear(); } 30
}
Functions
• A function is an encapsulation of a block of statements that can be
used more than once in a program.
• Some languages refer to functions as subroutines or procedures.
• A function may perform an isolated task requiring no parameters
whatsoever.
• A function may accept parameters in order to have guidance in
performing its designed task.
• A function may not only accept parameters but return a value as well.
• Even though a function may accept multiple parameters, it can only
return one.
31
Standard form of a function
type function_name (type param1, type param2, ... )
{
statement 1;
statement 2;
...
statement x;
}
32
Use of function
#include <alcd.h> void main(void)
#include <delay.h> {
#include <string.h> DDRC=0xFF;
while (1)
void led_glow(unsigned int value) {
{ if (PIND.2==0)
PORTC=value; led_glow(0x04);
delay_ms(1000); if (PIND.3==0)
PORTC=0; led_glow(0x08);
}
}
} 33
Thanks
34