You are on page 1of 27

Microprocessors and

Microcontrollers
(EE-231)

Main Objectives
8051 Timers
Mode 1 Programming
Generating Large Delay
Mode 2 Programming
Counter Programming

8051 Programming in C

Generating a Large Time Delay


Example

Mode 2 Programming

It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded


into the timers register TH
After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL
Then the timer must be started by the instruction SETB TRX
After the timer is started, it starts to count up by incrementing the TL
register
When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded
automatically with the original value kept by the TH register
To repeat the process, we must simply clear TF and let it go without any
need by the programmer to reload the original value
This makes mode 2 an auto-reload, in contrast with mode 1 in which the
programmer has to reload TH and TL

Mode 2 Operation Block Diagram

Mode 2 Programming Steps


To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0 or timer 1)
is to be used, and the timer mode (mode 2) is selected
2. Load the TH registers with the initial count value
3. Start timer (SETB TRX)
4. Keep monitoring the timer flag (TF) with the JNB TFX , target instruction
to see whether it is raised
5. When TF goes high Clear the TF flag (CLR TFX)
6. Go back to Step4, since mode 2 is auto-reload

Mode 2 Programming Steps


Example

Mode 2 Programming Steps


Example

Mode 2 Programming Steps


Example

Counter Programming
Timers can also be used as counters counting events
happening outside the 8051
When it is used as a counter, it is a pulse outside of the
8051 that increments the TH, TL registers
TMOD and TH, TL registers are the same as for the timer
discussed previously
Programming the timer in the last section also applies to
programming it as a counter
Except the source of the frequency i.e. only the source that
lets us increment the count is different.

C/T Bit

The C/T bit in the TMOD registers decides the source of the clock for
the timer
When C/T = 1, the timer is used as a counter and gets its pulses from
outside the 8051
The counter counts up as pulses are fed from pins 14 and 15, these pins
are called T0 (timer 0 input) and T1 (timer 1 input)

Counter Programming
Example

Counter Modes

TCON Register

The timer control register is an 8-bit register.

TCON Register

Its a bit addressable register, and its bits can also be accessed by
following way.

Gate control illustration

If GATE = 1, the start and stop of the timer are done externally through
pins P3.2 and P3.3 for timers 0 and 1, respectively
This hardware way allows to start or stop the timer externally at any
time via a simple switch.

C Programming
The reasons for writing programs in C
It is easier and less time consuming
C is easier to modify and update
You can use code available in function libraries
C code is portable to other microcontroller with little or no
modification
A microcontroller can be programmed in C with just a little knowledge
of internal architecture.
A disadvantage is
It creates a larger hex file creating issues for a limited ROM space
controllers.

C Programming

You will be needing to master these in order to become a good


programmer.

Loops (While, for)


If else
Switches
Functions

Data Types

Following are the data types that we can use to program 8051
Unsigned char
Signed char
Unsigned int
Signed int
Sbit (single bit)
Bit and sfr
A good knowledge of their size and range can help a programmer make
a space efficient program.

Unsigned Char

The character data type is the most natural choice because


8051 is an 8-bit microcontroller and most of the data is managed as 8bit.
Unsigned char is an 8-bit data type in the range of 0 255 (00 FFH)
C compilers use the signed char as the default if we do not put the
keyword unsigned
Should be part of every program

Example

The Preprocessor directive

The commands to the compiler.


# include:
Many programs often repeat the same set of commands for several
times.
In order to speed up the process of writing a program, these commands
and declarations are usually grouped in particular files (header files)
that can easily be included in the program using this directive.

The Preprocessor directive


Example

# define:
Can be used like EQU directive to
give names, which can be used
inside the code.
E.g.
#define Input P0;
#define LEDS P1;

The Forever Loop


Example
Comment
Or we could have written
While(1)
{
p1=0x55;
p1=0xAA;
}

The Signed Char

The signed char is an 8-bit data type


It uses the MSB D7 to represent or +
Give us values from 128 to +127
We should stick with the unsigned char unless the data needs to be
represented as signed numbers
E.g. temperature or calculator
Example
Array:
A collection/array
of objects of the
same type

The Integer data type

Unsigned int:
The unsigned int is a 16-bit data type
Takes a value in the range of 0 to 65535 (0000 FFFFH)
Define 16-bit variables
Set counter values of more than 256
Since registers and memory accesses are in 8-bit chunks, the misuse of
int variables will result in a larger hex file
Signed int:
Signed int is a 16-bit data type
It uses the MSB D15 to represent or +
We have 15 bits for the magnitude of the number from 32768 to
+32767

The Integer datatype


Example

Bit and sfr data types

The bit data type allows access to single bits of bit-addressable memory
spaces 20 2FH
To access the byte-size SFR registers, we use the sfr data types

You might also like