Lecture-7 (Microprocessors and Microcontrollers)
Lecture-7 (Microprocessors and Microcontrollers)
Microcontrollers
(EE-231)
Main Objectives
8051 Timers
Mode 1 Programming
Generating Large Delay
Mode 2 Programming
Counter Programming
8051 Programming in C
Mode 2 Programming
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
TCON Register
Its a bit addressable register, and its bits can also be accessed by
following way.
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
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
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;
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 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