Professional Documents
Culture Documents
Fundamentals of Microcontroller & Its Application: Unit-Iii
Fundamentals of Microcontroller & Its Application: Unit-Iii
Fundamentals of
CLASS NOTES
Microcontroller
& its Application
UNIT-III
Mr. RAJAN PATEL
Asst. Prof.
Electrical Engg. Dept., GCOERC
Course Outcome: CO-3
Write programs in C language for microcontroller 8051.
Teaching Hours: 6 Insem Marks: 10
Content: -
8051 Programming in C, Data types in C. Ports of 8051, their use, and programming in C (Byte
Level and Bit-level). Time delay programming in C. Timers and counters in 8051, Timer modes 0, 1,
2 and its programming in C and counter-programming.
Overview of C Language
C is a structured programming language developed by Dennis Ritchie in 1973 at Bell
Laboratories.
It is one of the most popular computer languages today because of its structure, high-level
abstraction, machine independent feature etc.
C language was developed to write the UNIX operating system,
Features of C language
It is a robust language with rich set of built-in functions and operators that can be used to
write any complex program.
The C compiler combines the capabilities of an assembly language with features of a high-
level language.
Programs Written in C are efficient and fast. This is due to its variety of data type and
powerful operators.
It is many time faster than BASIC.
C is highly portable this means that programs once written can be run on another machines
with little or no modification.
Another important feature of C program, is its ability to extend itself.
A C program is basically a collection of functions that are supported by C library. We can
also create our own function and add it to C library.
C language is the most widely used language in operating systems and embedded system
development today.
Character set
In C language characters are grouped into the following catagories,
Letters (all alphabets a to z & A to Z).
Digits (all digits 0 to 9).
Special characters, (such as colon:, semicolon ;, period ., underscore _, ampersand &etc).
White spaces.
What are Keywords in C?
Keywords are preserved words that have special meaning in C language.
The meaning of C language keywords has already been described to the C compiler.
These meaning cannot be changed.
Thus, keywords cannot be used as variable names.
There are total 32 keywords in C language.
Void Type
void type means no value.
This is usually used to specify the type of functions which returns nothing.
We will get acquainted to this data type as we start learning more advanced topics in C
language, like functions, pointers etc.
Derived data types
C supports three derived data types:
Data Types Description
Arrays Arrays are sequences of data items having homogeneous values. They have
adjacent memory locations to store values.
Operators in C Language
C language supports a rich set of built-in operators. An operator is a symbol that tells the
compiler to perform a certain mathematical or logical manipulation. Operators are used in
programs to manipulate data and variables.
C operators can be classified into following types:
A. Arithmetic operators
B. Relational operators
C. Logical operators
D. Bitwise operators
E. Assignment operators
F. Conditional operators
G. Special operators
Arithmetic operators
C supports all the basic arithmetic operators. The following table shows all the basic
arithmetic operators.
Operator Description
+ Adds Two Operands
- Subtract Second Operands From First
* Multiply Two Operand
/ Divide Numerator By Denominator
% Remainder Of Division
++ Increment Operator - Increases Integer Value By One
-- Decrement Operator - Decreases Integer Value By One
Relational Operators:
Relational operators are used for comparison of two values. Let’s see them one by one:
‘==’ operator checks whether the two given operands are equal or not. If so, it returns true.
Otherwise it returns false.
o For example, 5==5 will return true.
‘!=’ operator checks whether the two given operands are equal or not. If not, it returns true.
Otherwise it returns false. It is the exact boolean complement of the ‘==’operator.
o For example, 5!=5 will return false.
‘>’ operator checks whether the first operand is greater than the second operand. If so, it
returns true. Otherwise it returns false.
o For example, 6>5 will return true.
‘<‘operator checks whether the first operand is lesser than the second operand. If so, it
returns true. Otherwise it returns false.
o For example, 6<5 will return false.
Operator Description
== Check if two operand are equal
!= Check if two operand are not equal.
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand
Logical Operators:
They are used to combine two or more conditions/constraints or to complement the
evaluation of the original condition in consideration.
They are described below:
Logical AND: The ‘&&’ operator returns true when both the conditions in
consideration are satisfied. Otherwise it returns false.
o For example, a && b returns true when both a and b are true (i.e. non-zero).
Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in
consideration is satisfied. Otherwise it returns false.
o For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course,
it returns true when both a and b are true.
Logical NOT: The ‘!’ operator returns true the condition in consideration is not
satisfied. Otherwise it returns false.
o For example, !a returns true if a is false, i.e. when a=0.
Bitwise operators
Bitwise operators perform manipulations of data at bit level.
These operators also perform shifting of bits from right to left.
Bitwise operators are not applied to float or double (These are datatypes, we will learn
about them in the next tutorial).
Special operator
Operator Description Example
sizeof Returns the size of an variable size of(x) return size of the variable x
& Returns the address of an variable &x ; return address of the variable x
* Pointer to a variable *x ; will be pointer to a variable x
Precedence of operators
If more than one operators are involved in an expression, C language has a predefined rule
of priority for the operators.
This rule of priority of operators is called operator precedence.
In C, precedence of arithmetic operators ( *, %, /, +, -) is higher than relational operators(==,
!=, >, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, ||
and !).
o Example of precedence
(1 > 2 + 3 && 4)
void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y");
}
}
Out Put:
x is greater than y
Flowchart for simple-if:
Input pin
A logic one (1) is applied to a bit of the P register.
The output FE transistor is turned off and the appropriate pin remains connected to the
power supply voltage over a pull-up resistance.
Functions of Port 0
1. Simple I/O port:
When we use Port 0 as an input port, the internal latch should know that it’s being used for
input, and thus, a digital 1 (FFH) is written at the port address of 80H.
This turns off the transistors causing the pin to float in high impedance state connecting it to
the input buffer.
We can read data from ‘Read Pin Data’/’Read Latch Bit.’
When we use Port 0 as an output port, the latch programmed to 0 will turn on. Consequently,
the FET will connect to GND.
We will require an external pull up resistor (10k Ohm) here to give a logic ‘1’ for using Port
0 as an output port.
2. Access external memory:
When the 8051 wants to access external memory, the address for the memory generates due
to Port 0 and Port 2.
We get the lower half of the address from Port 0 and the upper half from Port 2.
This is done using ALE pulses, which help to latch the address to the external bus. Once done,
the Port 0 goes back to being an input port to read data from that memory.
Read/Write Operation of PORT-0
To write 0 to pin of the microcontroller:
Write a '0' to pin: The D latch output is one, the mosfet M1 is turned ON output is grounded
as shown in the figure.
Port 1
Features of Port 1:
Address is 90H
Construction: Port 1 has one D latch, two unidirectional buffers, 1 FET, and one internal pull-
up resistor at each pin.
It has only one function – to act as an Input-Output port.
The function of Port 1 – I/O port:
When Port 1 is functioning in the capacity of an input port, a digital ‘1’ (FFH) is written to
the latch. At 90H.
This turns off the transistor, and the pin floats in a high impedance state. Consequently, it
connects to the input buffer.
When Port 1 is functioning in the capacity of an output port, the latch is given a ‘LOW’ signal
(00H).
This turns the FER (Field Effect Transistor) o. The pull-up resistor is OFF, and the port is
used as an output port.
Port 2
Features of Port 2
Address is 10H
Construction: Port 2 has a D type latch, 1 FET, an internal pull-up resistor, two unidirectional
buffers, and a Control Logic block.
Its main functions are kind of similar to those of Port 0. It can be used as an input-output
port. And can access external memory in conjunction with Port 0.
Functions of Port 2
1. I/O port:
Quite similar to Port 0. The only difference here is that in Port 2, we use one FET with an
internal pull-up resistor instead of the two FETs we saw in Port 0.
2. Memory Access:
Port 2 is used in conjunction with Port 0 to generate the upper address of the external
memory location that needs to be accessed.
Example-2: - Write an 8051 C program to get a byte of data from PI, wait 1/2 second, and then send
it to P2.
Solution:
Example-4: - Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the
rest of the bits of P2.
Solution:
Timer
The 8051 has two counters/timers which can be used either as timer to generate a time
delay or as counter to count events happening outside the microcontroller.
Block Diagram
TCON Register
TCON is an 8-bit control register and contains a timer and interrupt flags.
Bit 7 - TF1: Timer1 Overflow Flag
1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back to zero).
Example
Let's generate a square wave of 2mSec period using an AT89C51 microcontroller with timer0
in mode0 on the P1.0 pin of port1. Assume xtal oscillator frequency of 11.0592 MHz.
As the Xtal oscillator frequency is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence,
the required count to generate a delay of 1mSec. is,
Count =(1×10^-3)/(1.085×10^-6) ≈ 921
The maximum count of Mode0 is 2^13 (0 - 8191) and the Timer0 count will increment from
0 – 8191. So we need to load value which is 921 less from its maximum count i.e. 8191. Also, here
in the below program, we need an additional 13 MC (machine cycles) from call to return of delay
function. Hence value needed to be loaded is,
Value=(8191-Count)+Function_MCycles+1 =7284= 0x1C74
So we need to load 0x1C74 value in Timer0.
1C74 = 0001 1100 0111 0100 b, now load lower 5-bit in TL0 and next 8-bit in TH0
so here we get,
TL0 = 0001 0100 = 0x14 and TH0 = 1110 0011 = 0xE3
Programming steps for delay function
o Load Tmod register value i.e. TMOD = 0x00 for Timer0/1 mode0 (13-bit timer mode).
o Load calculated THx value i.e. here TH0 = 0xE3.
o Load calculated TLx value i.e. here TL0 = 0x14.
o Start the timer by setting a TRx bit. i.e. here TR0 = 1.
o Poll TFx flag till it does not get set.
o Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
In this case every event for counter operations or machine cycles for timer operation, the
TH0– TL0 register-pair will be incremented by 1.
When the register pair overflows from FFFFH to 0000H, then the TF0 of TCON register will
be high, and it stops the timer/counter.
Mode 2 of Timer/Counter
The Mode 2 operation is the 8-bit auto reload timer or counter.
In the following diagram, we are using Mode 2 for Timer1.
When the Timer1 is working in Mode 3, it simply holds the count but does not run.
When Timer0 is in mode 3, the Timer1 is configured in one of the mode 0, 1 and 2.
In this case, the Timer1 cannot interrupt the microcontroller.
When the TF1 is used by TH0 timer, the Timer1 is used as Baud Rate Generator.