You are on page 1of 21

Design of Propeller clock

1. INTRODUCTION

1.1. Persistence of Vision

Persistence of vision (POV) refers to a phenomenon whereby the human retina retains an after-
image of anything it sees for a short time (approximately one twenty-fifth of a second). This
phenomenon renders a person unable to distinguish between different images viewed in quick
succession. By taking advantage of this phenomenon, it should be possible to produce an
electronic display that appears to have a significantly higher resolution than its hardware would
ordinarily dictate, by physically and rapidly moving the display hardware in space and modifying
the display output based on its current physical position. If the display hardware is moved fast
enough, the eye should perceive the visual output from the hardware at multiple physical
positions simultaneously, giving the perception of a two- or three-dimensional display even
though the physical hardware consists of just a one-dimensional “wand” of LEDs. The aim of
this project is to design and manufacture such a system.

1.2. Background Review and Design Considerations

There are many “persistence of vision displays” in existence, both commercial and non-
commercial. Appendix A shows a number of examples. These persistence of vision displays
differ vastly in size, shape and capabilities1, but all share the ability to physically move a number
of LEDs rapidly in physical space. The Figures show just three examples of possible POV
display shapes.A small number of LEDs are oscillated from side to side in order to produce a
two-dimensional arc display. This would commonly be used for displaying text, such as a digital
clock . In Figure 3, rotation is used rather than oscillation in order to produce the perception of a
circular display. This could be used to display text, images, video or any other visual media.
Figure 4 is an example of a three-dimensional POV display: a one-dimensional array of LEDs
are still used but these are rotated about an axis parallel to, rather than perpendicular to, the
LEDs in order to produce a cylindrical display (alternatively a sphere could be produced by
curving the line of LEDs). A three-dimensional display has the added benefit of being viewable
from any direction around the system.

1.3 Resolution and Colors

The resolution of a POV display is partially dictated by the number of LEDs physically present
in the system. However, due to the persistence of vision phenomenon caused by physical
movement of the LEDs, the perceived resolution in the direction(s) of motion is much greater
than the actual number of LEDs present. For example, if a column of 50 LEDs arranged
vertically were to be rapidly moved left and right in twodimensional space, and the output of
these LEDs were changed 100 times between the left-most and rightmost points of movement
(and the complete sweep from one side to the other were completed before the after-image had
disappeared from the retina), the effective (perceived) resolution of the display would be 100px
by 50px rather than the 1px by 50px resolution that the system hardware would suggest. In other
words, the resolution in any directions of physical movement is dictated by the number of times
the LED output is modified by software within one twenty-fifth of a second. Simple POV
displays can use single-colour LEDs in order to produce their text, images or other media. This
has the advantage of being significantly easier to wire and program, while limiting the
information/media that can be displayed. At the other end of the spectrum, full-colour (RGB)
LEDs are available with three input channels in order to produce eight colours initially.

2. PROJECT OBJECTIVES ˑ
ˑ Theˑ aimˑ ofˑ thisˑ projectˑ willˑ beˑ toˑ designˑ andˑ implementˑ aˑ “persistenceˑ ofˑ visionˑ
display”,ˑ asˑ definedˑ above.ˑ Theˑ displayˑ willˑ physicallyˑ consistˑ ofˑ aˑ one-ˑ orˑ two-
dimensionalˑ arrayˑ ofˑ LEDs,ˑ butˑ theseˑ LEDsˑ willˑ beˑ movedˑ rapidlyˑ inˑ physicalˑ spaceˑ
inˑ orderˑ toˑ produceˑ theˑ illusionˑ ofˑ aˑ three-dimensionalˑ displayˑ whenˑ viewedˑ fromˑ
anyˑ ofˑ aˑ numberˑ ofˑ angles.ˑ Thisˑ movementˑ willˑ beˑ inˑ aˑ rotating,ˑ ratherˑ thanˑ
oscillating,ˑ fashion,ˑ andˑ shouldˑ beˑ implementedˑ usingˑ aˑ motor.ˑ Aˑ microprocessorˑ
shouldˑ beˑ usedˑ toˑ receiveˑ and/orˑ computeˑ imagesˑ toˑ beˑ displayed.

ˑ Powerˑ willˑ needˑ toˑ beˑ transferredˑ intoˑ theˑ rotatingˑ bodyˑ inˑ orderˑ toˑ lightˑ theˑ
LEDs:ˑ aˑ meansˑ toˑ achieveˑ thisˑ willˑ needˑ toˑ beˑ considered.ˑ Ifˑ theˑ microprocessorˑ isˑ
notˑ mountedˑ withinˑ theˑ rotatingˑ portionˑ ofˑ theˑ system,ˑ aˑ meansˑ forˑ transferringˑ theˑ
LEDˑ dataˑ intoˑ theˑ rotatingˑ bodyˑ willˑ alsoˑ needˑ toˑ beˑ considered.ˑ Theˑ systemˑ shouldˑ
beˑ capableˑ ofˑ rotatingˑ safelyˑ atˑ 24ˑ revolutionsˑ perˑ secondˑ (1440ˑ RPM)ˑ inˑ orderˑ toˑ
achieveˑ aˑ smoothˑ virtualˑ image.ˑ Theˑ systemˑ displayˑ should,ˑ however,ˑ beˑ capableˑ ofˑ
runningˑ independentlyˑ ofˑ thisˑ revolutionˑ speed:ˑ inˑ otherˑ words,ˑ theˑ systemˑ mustˑ haveˑ
someˑ meansˑ ofˑ determiningˑ itsˑ currentˑ speedˑ inˑ orderˑ thatˑ fluctuationsˑ inˑ theˑ RPMˑ
doˑ notˑ ruinˑ theˑ perceivedˑ effect.ˑ

Theˑ displayˑ shouldˑ beˑ capableˑ ofˑ displayingˑ textˑ andˑ low-resolutionˑ imagesˑ suchˑ asˑ
shapesˑ andˑ cartoonˑ characters:ˑ inˑ orderˑ toˑ achieveˑ thisˑ theˑ perceivedˑ displayˑ
resolutionˑ shouldˑ beˑ aˑ minimumˑ ofˑ 32pxˑ byˑ 32px.ˑ Ideallyˑ theˑ resolutionˑ willˑ beˑ
greaterˑ thanˑ this,ˑ butˑ thisˑ willˑ beˑ dependentˑ onˑ aˑ numberˑ ofˑ hardwareˑ considerationsˑ
whichˑ willˑ beˑ investigatedˑ duringˑ theˑ courseˑ ofˑ theˑ project.ˑ Ifˑ timeˑ andˑ
software/hardwareˑ considerationsˑ permit,ˑ theˑ systemˑ couldˑ beˑ extendedˑ inˑ orderˑ toˑ
displayˑ photographs,ˑ animations,ˑ video,ˑ and/orˑ includeˑ interactivityˑ suchˑ asˑ basicˑ
gamesˑ thatˑ respondˑ toˑ userˑ input.

Theˑ systemˑ mustˑ beˑ ableˑ toˑ interfaceˑ withˑ aˑ PCˑ inˑ orderˑ toˑ receiveˑ imagesˑ forˑ
display:ˑ thisˑ wouldˑ ideallyˑ takeˑ placeˑ overˑ aˑ wirelessˑ linkˑ andˑ allowˑ theˑ systemˑ toˑ
receiveˑ imagesˑ on-the-flyˑ whilstˑ rotating,ˑ butˑ atˑ aˑ minimumˑ couldˑ justˑ beˑ achievedˑ
byˑ aˑ physicalˑ connectionˑ toˑ aˑ PCˑ whenˑ theˑ displayˑ isˑ notˑ moving.

Theˑ finishedˑ productˑ couldˑ beˑ usedˑ inˑ theˑ advertisingˑ industry,ˑ toˑ displayˑ companyˑ
logos,ˑ detailsˑ orˑ animationsˑ inˑ aˑ novelˑ way.ˑ Alternatively,ˑ theˑ productˑ couldˑ beˑ usedˑ
asˑ anˑ entertainmentˑ productˑ inˑ muchˑ theˑ sameˑ wayˑ asˑ largeˑ televisionˑ displaysˑ couldˑ
beˑ usedˑ atˑ variousˑ entertainmentˑ events.ˑ Again,ˑ theˑ noveltyˑ ofˑ theˑ deviceˑ wouldˑ beˑ
aˑ keyˑ factorˑ here,ˑ asˑ wellˑ asˑ theˑ potentiallyˑ reducedˑ costˑ ofˑ theˑ lowerˑ numberˑ ofˑ
LEDsˑ required.

3. DESCRIPTION:

The Main Part of the propeller display is Microcontroller AT89S52.We can also use the others
like ATMega32A, 8051, PIC microcontrollers but here I am using this because of its low cost.
The IR LED, which is stationary, is fixed on the base of rotating assembly. When the IR rays
sent by the IR LED falls on the photo diode, which is placed on the rotating PCB,it will generate
a low pulse on the interrupt pin of microcontroller which results in the generation of desired
pattern. We are making use of internal program execution EA(external access) pin has to be
made high with the help of VCC.A reset circuit is provided for the reset of microcontroller. The
anodes of LEDs are joined together and connected to 5v supply. Logic 0 level at the output of
port0 will result in illumination of LED.

3.1 DC Motor:

DC motors, apply a voltage to both terminals, and it will spins. DC motors are non- polarized
which means that it can reverse voltage so the motor will rotate in two directions, forward and
backward. Typical DC motors are rated from about 6V -12V. The larger ones are often 24V or
more but for the purpose of this project, it is necessary to use 6V-12V range motor. Voltage is
directly related to motor torque. The more voltage supplied, the higher the torque will be
produce.Specifications of most DC motors show high revolutions per minute (rpm) and low
torque. The DC motor is popular in a number of drive applications due to its simple operation
and control. In figure it has 2 main parts which is rotor and stator. Stator is the part where the
permanent magnet situated and used to generate the magnetic field and it is static. Rotor is the
rotary part in the motor and contains block of core and wire loops. It also called the armature.
3.2 Microcontroller:

In this project AT89S52 micro-controller is used which consists of 40pins that are dedicated to
various functions. Out of the 40 pins, a total of 32 pins are set aside for four ports P0, P1, P2, and
P3 where each port takes 8 pins. The rest of the pins are designated as GND, Vcc, XTAL1,
XTAL2, RST, EA, and PSEN. The micro-controller (AT89S52) has an on -chip oscillator but
requires and external clock to run it. Often a quartz oscillator is connected to inputs XTAL1 (pin
19) and XTAL2 (pin 18).We can also use the crystal oscillator The quartz oscillator connected to
XTAL1and XTAL2 also needs two capacitors 33pF.The RST pin (9th pin) is an input and is
active high. Upon applying a high pulse to this pin, the micro -controller will reset and terminate
all activities. This is often referred to as a power-on reset. Vcc is used to power up the entire
micro-controller, which is pin 40 and GND is pin 20. Here port P1 is used as output port to
which LEDs are connected.

3.3 Led Module:


LED module consisting of bright red color LEDs and it is fixed in corner of the arm of the
propeller. LED module is used to displaying the characters and symbols. TheseLEDs are
connected with each pins of the port A and two pins of the port B of microcontroller with a series
current limiting resistorof470 ohm.

3.4 Interrupter Module:


Interrupter module is our sensor module, consisting of the IR interrupt sensor from Motorola.
This sensor was selected from a variety of other alternatives, because of its small size, precise
interrupt sensing, and study casing. One great advantage of using this module is, interfacing it
with the microcontroller is just a matter of two resistors and a general purpose transistor.
MOC7811 is the sensing part of the interrupter module, while rest of the circuitry works as
signal conditioning circuit, wires emerge out from the module, respectively Vcc, Signal and
Ground. Output of the module is LOW, if interrupt occurs, otherwise it remains HIGH. It
consists of IR LED and Photodiode mounted facing each other enclosed in plastic body. When
light emitted by the IR LED is blocked because of some completely opaque object, logic level of
the photodiode changes. This change in the logic level can be sensed by the microcontroller or
by discrete hardware.

3.5 Mechanical Assembly:


Mechanical assembly plays a vital role in properfunctioning of this project. The display is
scannedeachtime, by rotating the whole assembly in acircular path.The basic idea developed
isown, byimplementing and modifying different ways.The display is displays the time by
rotating the whole assembly in a circular path. Following diagram shows the most reliableway.

4. SOFTWARE DESIGN:

4.1 From the C program to the machine language


The C source code is very high level language, meaning that it is far from being at the base level
of the machine language that can be executed by a processor. This machine language is basically
just zero’s and one’s and is written in Hexadecimal format, that why they are called HEX files.
There are several types of HEX files; we are going to produce machine code in the INTEL HEX-
80 format, since this is the output of the KEIL IDE that we are going to use shows that to convert
a C program to machine language, it takes several steps depending on the tool you are using,
however, the main idea is to produce a HEX file at the end. This HEX file will be then used by
the ‘burner’ to write every byte of data at the appropriate place in the EEPROM of the 89C51.

4.1.2 Organization of a C program


All C programs have this common organization scheme, sometimes it’s followed, sometimes it’s
not, however, it is imperative for this category of programming that this organization scheme be
followed in order to be able to develop your applications successfully. Any application can be
divided into the following parts, noting that is should be written in this order:
1. Headers Includes and constants definitions:
In this part, header files (.h) are included into your source code. Those headers files can
be system headers to declare the name of SFRs, to define new constants, or to include
mathematical functions like trigonometric functions, root square calculations or numbers
approximations. Header files can also contain your own functions that would be shared
by various programs.

2. Variables declarations:
More precisely, this part is dedicated to ‘Global Variables’ declarations. Variables
declared in this place can be used anywhere in the code. Usually in microcontroller
programs, variables are declared as global variables instead of local variables, unless your
are running short of RAM memory and want to save some space, so we use local
variables, whose values will be lost each time you switch from a function to another. To
summarize, global variables as easier to use and implement than local variables, but they
consume more memory space.
3. Functions’ body:
Here you group all your functions. Those functions can be simple ones that can be called
from another place in your program, as they can be called from an ‘interrupt vector’. In
other words, the sub-programs to be executed when an interrupt occurs is also written in
this place.
4. Initialization:
The particularity of this part is that it is executed only one time when the microcontroller
was just subjected to a ‘RESET’ or when power is just switched ON, then the processor
continue executing the rest of the program but never executes this part again. This
particularity makes it the perfect place in a program to initialize the values of some
constants, or to define the mode of operation of the timers, counters, interrupts, and other
features of the microcontroller.
5. Infinite loop
An infinite loop in a microcontroller program is what is going to keep it alive, because a
processor have to be allays running for the system to function, exactly like a heart have to
be always beating for a person to live. Usually this part is the core of any program, and
it’s from here that all the other functions are called and executed.

4.1.3 Simple C program for 89c51

Here is a very simple but complete example program to blink a LED. Actually it is the source
code of the example project that we are going to construct in the next part of the tutorial, but for
now it is important to concentrate on the programming to summarize the notions discussed
above.
After including basic headers for the SFR definitions of the 8952 microcontroller (REGX52.h)
and for mathematical functions (math.h), a function named ‘delay’ is created, which is simple a
function to create a delay controlled via the parameter ‘y’. Then comes the main function, with
an infinite loop (the condition for that loop to remain will always be satisfied as it is ’1′). Inside
that loop, the pin number 0 of port 1 is constantly turned ON and OFF with a delay of
approximately one second.

4.2 Using the KEIL environment


KEIL µVision is the name of a software dedicated to the development and testing of a family of
microcontrollers based on 8051 technology, like the P89V51RD2 which we are going to use
along this tutorial. You can download an evaluation version of KEIL at their website:
http://www.keil.com/c51/. Most versions share merely the same interface, this tutorial uses KEIL
C51 µvision 3 with the C51 compiler v8.05a.
To create a project, write and test the previous example source code, follow the following steps:
Open Keil and start a new project.

You will prompted to choose a name for your new project, Create a separate folder where all
the files of your project will be stored, chose a name and click save. The following window
will appear, where you will be asked to select a device for Target ‘Target 1′: From the list at
the left, seek for the brand name NXP, then under NXP, select 89c51. You will notice that a
brief description of the device appears on the right. Leave the two upper check boxes
unchecked and click OK. The P89V51RD2 will be called your ‘Target device’, which is the
final destination of your source code. You will be asked whether to ‘copy standard 8051
startup code‘ click No.

Click File, New, and something similar to the following window should appear. The box
named ‘Text1′ is where your code should be written later.
Now you have to click ‘File, Save as’ and chose a file name for your source code ending
with the letter ‘.c’. You can name is ‘code.c’ for example, and click save. Then you have to
add this file to your project work space at the left as shown in the following screen shot:
After right-clicking on ‘source group 1‘, click on ‘Add files to group…‘, then you will be
prompted to browse the file to add to ‘source group 1′, chose the file that you just saved,
eventually ‘code.c’ and add it to the source group. You will notice that the file is added to
the project tree at the left.

In some versions of this software you have to turn ON manually the option to generate HEX
files. make sure it is turned ON, by right-clicking on target 1, Options for target ‘target 1′,
then under the ‘output‘ tab, by checking the box ‘generate HEX file‘. This step is very
important as the HEX file is the compiled output of your project that is going to be
transferred to the micro-controller.
You can then start to write the source code in the window titled ‘code.c’ then before testing
your source code, you have to compile your source code, and correct eventual syntax errors.
In KEIL IDE, this step is called ‘rebuild all targets’ and has this icon: .
You can use the output window to track eventual syntax errors, but also to check the FLASH
memory occupied by the program (code = 49) as well as the registers occupied in the RAM (data
= 9). If after rebuilding the targets, the ‘output window’ shows that there is 0 error, then you are
ready to test the performance of your code. In KEIL, like in most development environment, this
step is called Debugging, and has this icon: . After clicking on the debug icon, you will
notice that some part of the user interface will change, some new icons will appear, like the
run icon circled in the following figure:

You can click on the ‘Run’ icon and the execution of the program will start. In our example,
you can see the behavior of the pin 0 or port one, but clicking on ‘peripherals, I/O ports, Port
1′. You can always stop the execution of the program by clicking on the stop button ( )
and you can simulate a reset by clicking on the ‘reset’ button

4.3 Interrupt, timer and counter

Most microcontrollers come with a set of ‘ADD-ONs’ called peripherals, to enhance the
functioning of the microcontroller, to give the programmer more options, and to increase the
overall performance of the controller. Those features are principally the timers, counters,
interrupts, Analog to digital converters, PWM generators, and communication buses like
UART, SPI or I2C. The P89V51RD2 is not the most equipped micro-controller in terms of
peripherals, but never the less, the available features are adequate to a wide range of
applications, and it is one of the easiest to learn on the market.

4.3.1 Introduction to P89V51RD2 Peripherals

Figure below shows a simplified diagram of the main peripherals present in the
P89V51RD2 and their interaction with the CPU and with the external I/O pins. You can
notice that there are three timers/Counters. We use the expression “Timer/Counter” because
this unit can be a counter when it counts external pulses on its corresponding pin, and it can
be a timer when it counts the pulses provided by the main clock oscillator of the
microcontroller. Timer/Counter two is a special counter that does not behave like the two
others, because it have a couple of extra functionality.
The serial port, using a UART (Universal Asynchronous Receive Transmit) protocol can be used
in a wide range of communication applications. With the UART provided in the P89V51RD2
you can easily communicate with a serial port equipped computer, as well as communicate with
another microcontroller. This last application, called Multi-processor communication, is quite
interesting, and can be easily implemented with two P89V51RD2 microcontrollers to build a
very powerful multi-processor controllers.

If all the peripherals described above can generate interrupt signals in the CPU according to
some specific events, it can be useful to generate an interrupt signal from an external device
that may be a sensor or a Digital to Analog converter. For that purpose there are two
External Interrupt sources (INT0 and INT1).

This was a presentation of the available peripheral features in a P89V51RD2


microcontroller. Through this tutorial, we are going to study how to setup and use
external interrupts and the two standard timers (T0 and T1). For simplicity, and to keep
this tutorial a quick and straight forward one, The UART and the Timer/Counter 2 shall be
discussed in separate tutorials.
Propeller LED Display

4.4.2 External Interrupts


Let’s start with the simplest peripheral which is the external interrupt, which can be used to
cause interruptions on external events (a pin changing its state from 0 to 1 or vice-versa). In
case you don’t know, interruption is a mean of stopping the flow of a program, as a response
to a certain event, to execute a small program called ‘interrupt routine’.

As you noticed in figure, in the P89V51RD2, there are two external interrupt sources, one
connected to the pin P3.2 and the other to P3.3. They are configured using a number of SFRs
(Special Function Registers). Most of those SFRs are shared by other peripherals as you
shall see in the rest of the tutorial.

Figure 16: IE register

The first register you have to configure (by turning On or Off the right bits) is the IE
register, shown in figure 16. IE stands for ‘Interrupt Enable’, and it is used to allow
different peripherals to cause software interruption. To use any of the interrupts, the bit EA
(Enable ALL) must be set to 1, then, you have enable each one of the interrupts to be used
with its individual enable bit. For the external interrupts, the two bits EX0 and EX1 are used
for External Interrupt 0 and External Interrupt 1.

Using the C programming language under KEIL, it is extremely simple to set those bits,
simply by using their name as any global variables, using the following syntax:

EA = 1;

EX0 = 1;

EX1 = 1;

The rest of the bits of IE register are used for other interrupt sources like the 3 timers
overflow (ETx) and the serial interface (ES).

Figure 17: TCON register

Similarly, you have to set the bits IT0 and IT1 in the TCON register, shown in figure 4.2.B. The
bits IT0/IT1 are used to configure the type of signal on the corresponding pins (P3.2/P3.3) that
generated an interrupt according to the following table:
If IT0 or IT1 is set to 0, an interruption will keep reoccurring as long as P3.2 or P3.3 is set to
0. This mode isn’t easy to manage, and most programmers tends to use external interrupts
triggered by a falling edge (transition from 1 to 0).

Again, this register is ‘bit addressable’ meaning you can set or clear each bit individually
using their names, like in the following example:

IT0 = 1;

IT1 = 1;

4.4.2.1 The IE register


First, you have to Enable the corresponding interrupts, but writing 1′s to the corresponding
bits in the IE register. The following table shows the names and definitions of the concerned
bits of the IR register (you can always take a look at the complete IE register in figure 14):

EA Enable All interrupts

ET2 Enable Timer 2 interrupts (will not be treated in this tutorial)

ET1 Enable Timer 1 interrupts

ET0 Enable Timer 0 interrupts


4.3.3 Timer/Counter
The timer is a very interesting peripheral that is imperatively present in every
microcontroller. It can be used in two distinct modes:

1. Timer: Counting internal clock pulses, which are fixed with time, hence, we can say that it
is very precise timer, whose resolution depends on the frequency of the main CPU clock
(note that CPU clock equals the crystal frequency over 12).
2. Counter: Counting external pulses (on the corresponding I/O pin), which can be provided
by a rotational encoder, an IR-barrier sensor, or any device that provide pulses, whose
number would be of some interest.

Sure, the CPU of a microcontroller could provide the required timing or counting, but the
timer/counter peripheral relieves the CPU from that redundant and repetitive task, allowing it to
allocate maximum processing power for more complex calculations.

So, like any other peripheral, a Timer/Counter can ask for an interruption of the program,
which – if enabled – occurs when the counting registers of the Timer/Counter are full and
overflow. More precisely, the interruption will occur at the same time the counting register
will be reinitialized to its initial value.

So to control the behavior of the timers/counters, a set of SFR are used, most of them have
already been seen at the top of this tutorial.

4.3.3.1 The TCON register

The TCON register is also shared between more than one peripherals. It can be used to
configure timers or, as you saw before, external interrupts. The following table shows the
names and definitions of the concerned bits of the TCON register

TF1 Overflow interrupt flag, used by the processor.

TR1 Timer/counter 1 RUN bit, set it to 1 to enable the timer to count, 0 to stop counting.

TF0 Overflow interrupt flag, used by the processor.

TR0 Timer/counter 0 RUN bit, set it to 1 to enable the timer to count, 0 to stop counting.
As the IE register, TCON is also bit-addressable, so you can set its bit using its names, like
we did before. Example:

TR0 = 1;
Propeller LED Display

4.3.3.2The TMOD register

Before explaining the TMOD register, let us agree and make it clear that the register IS
NOT BIT-ADDRESSABLE, meaning you have to write the 8 bits of the register in a single
instruction, by coding those bits into a decimal or hexadecimal number, as you shall see
later.

So, as you can see in figure 16, the TMOD register can be divided into two similar set of
bits, each group being used to configure the mode of operation of one of the two timers.

Figure 18: TMOD register

For the given Timer/Counter, the corresponding bits of TMOD can be defined as in the
following table:

Gate signal. For normal operation clear this bit to 0.

G If you want to use the timers to capture external events’s length, set it to 1, and the timer 1/0 will stop counting when
External Interrupt 1/0 pin is low (set to 0 V). Note that this feature involves both a timer and an external interrupt, It
you’re responsibility to write the code to manage the operation of those two peripherals.

C/T’ Set to 1 to use the timer/counter 1/0 as a Counter, counting external events on P3_4/P3_5, cleared to 0 to use it as
timer, counting the main oscillator frequency divided by 12.

M1
Timer MODE: Those two last bits combine as 2 bit word that defines the mode of operation, defined as the table below.
M0

4.4.3.3 Timer/counter modes of operation


Each timer/counter has two SFR called TL0 and TH0 (for timer/counter0) and TL1 and
TH1 (for timer/counter 1). TL stands for timer LOW, and is used to store the lower bits of
the number being counted by the timer/counter. TH stands for TH, and is used to store the
higher bits of the number being counted by the timer/counter.
M1 M0 Mode Description

Only TH0/1 is used, forming an 8bit timer/counter.

Timer/counter will count up from the value initially stored in TH0/1 to 255, and then overflow
back to 0.

0 0 0 If an interrupt is enabled, an interrupt will occur upon overflow.

If used as timer, pulses from the processor are divided by 32 (after being divided by 12). The result
is the main oscillator frequency divided by 384.

If used as counter, external pulses are only divided by 32.

Both TH0/1 and TL0/1 are used, forming a 16 bit timer/counter.

Timer/counter will count up from the 16 bit value initially stored in TH0/1 and TL0/1 to 65535,
and then overflow back to 0.

0 1 1 If an interrupt is enabled, an interrupt will occur upon overflow.

If used as timer, pulses from the processor are only divided by 12.

If used as counter, external pulses are not divided, but the maximum frequency that can be
accurately counted equals the oscillator frequency divided by 24.

TL0/1 is used for counting, forming an 8 bit timer/counter. TH0/1 is used to hold the value to
be restored in TL upon overflow.

Timer/counter will count up from the 8 bit value initially stored in TL0/1 and to 255, and
then overflow, setting the value of TH0/1 in TL0/1. This is called the auto-reload function.

1 0 2
If an interrupt is enabled, an interrupt will occur upon overflow.

If used as timer, pulses from the processor are only divided by 12.

If used as counter, external pulses are not divided, but the maximum frequency that can be accurately
counted equals the oscillator frequency divided by 24.

1 1 3 This mode is beyond the scope of this tutorial.

Timer modes 1 and 2 are the most used in 8051 microcontroller projects, since
they offer a wide range of possible customization.
Main routine:
1. Load proper value in IE register, so that the interrupts INT0 and T0 are enabled. (IE = 83H)
2. Offer higher priority to the INT0 (External) interrupt. (IP = 01H)
3. Configure timer 1 as 16-bit timer, and timer 0 as 8-bit auto reload mode timer. ( TMOD =
12H)
4. INT0 should be configured as edge interrupt. (IT0 = 1)
5. Configure port 3 as input port. (P3 = 0FFH)

6. Move input string to the video RAM area. (call ‘ramc’ function)
7. Start the timers.
8. Initiate an infinite loop.

Interrupt Routines:
(a)External Interrupt:
1.Stop the timers.
2.Move th1 and tl1 into convenient registers.
3.Divide this 16 bit value by our total number of segments.
4.Subtract the answer from 256, and load the result in th0.
5.Now, reset the video RAM pointer and character segment pointers to their initial respective
positions.
6. Start the timers.
7. Return from interrupt.

(b) Timer 0 Interrupt:


1. Call the display routine.
2. Clear timer overflow flag.
3. Return from interrupt.
4. Clear timer overflow flag.
5. Return from interrupt.

You might also like