You are on page 1of 14

EE306 Digital Electronic Dsign and Lab

Exp 3. Digital Design Using Microcontroller


(1st week)

1. Object
- Digital design experiment using 8051 microcontroller.
- In this assignment, we are going to design our own digital system to improve our design
ability.
- Understanding the interrupt operation of 8051 microcontroller.
- Getting familiar with COMPACT51 / KEIL uVIsionII development environment for 8051
microcontroller.

2. Problem statement
- Learn how to use 8051 microcontroller-based COMPACT51 board
- Learn how to generate 8051-based binary codes using KEIL uVisionII compiler.
- Design and implement stopwatch with this board.

3. Background
1) Experiment Environment Composition
- Connect COMPACT51 board with PC (using UART Cable)
- Setup KEIL uVisionII Compiler to generate 8051-based binary codes
※ If you set up the evaluation version, the maximum file size generated from the
compiler is limited to 2KB. The following explanation is about uVisionII V2.40a evaluation
version.

2) How to use KEIL uVisionII


(1) Create a new project (Project – New Project..)

Dept. of Electrical Engineering, KAIST 1


EE306 Digital Electronic Dsign and Lab

- Select the type of microcontroller. In this assignment, select the 8031AH of Intel which is
same as the experiment board.

- If you select “Yes” in the following question, STARTUP.A51 file is automatically added
to the current Source Group.

(2) Modify STARTUP.A51


- Open STARTUP.A51 file in the Project Workspace.

Dept. of Electrical Engineering, KAIST 2


EE306 Digital Electronic Dsign and Lab

- Modify following parts in STARTUP.A51 file.

(Before modification)
--------------------------------------------------------------------
CSEG AT 0
--------------------------------------------------------------------
(After modification)
--------------------------------------------------------------------
CSEG AT 5000H
--------------------------------------------------------------------

(3) Add C file


- Generate C file (main.c) which will be added to the current Source Group and compiled.
- Copy following 3 files and paste to the created project folder
CLCD.c, CMPT51.h, init.ini
- Click ‘Source Group 1’ in the Project Workspace using a right button of mouse Æ include
the appropriate files (CLCD.c, main.c) to the Source Group.

(4) Set up the ‘option’


- Open the ‘options’ in the Project Workspace by click ‘Target 1’ (Right button of the
mouse)

Dept. of Electrical Engineering, KAIST 3


EE306 Digital Electronic Dsign and Lab

- Open the ‘Target’ tap. Select ‘Memory Model’, ‘Code Rom Size’. Set up ‘off-chip code
memory’, ‘off-chip xdata memory’ as follows.

C000H
XDATA mem
B000H
CODE mem
5000H
ROM data
4000H

ROM
0000H
When you set up as above, the whole memory map becomes as above figure. ROM will
occupy 0000H ~ 4000H, and 4000H ~ 5000H (exactly, 4000H ~ 4100H) is used for data

Dept. of Electrical Engineering, KAIST 4


EE306 Digital Electronic Dsign and Lab

storage of monitor program in the ROM. 5000H ~ B000H is used for code memory, and
B000H ~ C000H is used for data memory. In this manner, since codes are placed beyond
5000H address, we modified STARTUP.A51 file previously.

- Open ‘C51’ tap. Set up 'Interrupt vectors at address' as follows.

8051 defines the interrupt vector as the jump-to address when each interrupt occurs as
shown in the following table: between 0000H ~ 0023H. But in case of our experiment board,
this address is defined as a part of ROM and you cannot write onto the ROM section.
Therefore, the interrupt service routine in these address region need to be moved to some
other place.
Our experiment boards use ‘double vectoring’ to solve this problem. There are already
‘LJMP’ instruction which changes the interrupt vector into ‘corresponding address + 5000H’
so that they use the address 5000H ~ 502BH when each interrupt occurs. So we have to
modify the interrupt vectors accordingly. By doing the above set-up, interrupt service
routines that we generate are placed at the location ‘original interrupt vector + 5000H’ (If
you want to know more detail, then refer to the references.)

Modified Interrupt Vector


Interrupt No. Interrupt Type Interrupt Vector (A)
(A+5000H)
- Reset 0x0000 -
0 External Interrupt 0 0x0003 0x5003
1 Timer/Counter 0 0x000B 0x500B
2 External Interrupt 1 0x0013 0x5013
3 Timer/Counter 1 0x001B 0x501B
4 UART 0x0023 0x5023

- Open the ‘Debug’ tap. Select ‘USE KEIL Monitor-51 Driver’ instead of ‘Use Simulator’
and set the initialization File as init.ini

Dept. of Electrical Engineering, KAIST 5


EE306 Digital Electronic Dsign and Lab

- Click the settings button and check the Serial interrupt as follows

(5) Compile
Now, Click ‘Project->Build All Target Files’ to generate binary codes.

(6) Load the program


- Click ‘Debug -> Start/Stop Debug Session’. Then binary codes are transmitted through
UART cable to the CODE segment section (5000H ~ B000H) of RAM in experiment boards.

Dept. of Electrical Engineering, KAIST 6


EE306 Digital Electronic Dsign and Lab

Register information and Assembly Code information which is placed at each memory
address is indicated as follows. Current yellow arrow indicates 0x0000, where the first
instruction of monitoring program in the ROM resides.

- Click ‘Debug->go’, then the monitoring program is started from the current address 0x0000,
and stopped at 0x5000, i.e. where the downloaded binary codes are placed.

Dept. of Electrical Engineering, KAIST 7


EE306 Digital Electronic Dsign and Lab

- And click ‘Debug Æ go’ again, then the downloaded binary codes is executed. At this time,
you can perform debugging by using several functions of uVisionII.

3) REG51.H
There are several SFRs (Special Function Registers) at 80H ~ FFH in the 8051
microcontroller, and we can control the operation of the processor by using these registers.
Since it is troublesome to find or memorize the addresses of registers every time, we try to
access the SFR, we use the header file (reg51.h) which includes the macro of register
addresses and available bit addresses.
You can find more detail information about SFRs in the references, and you can find reg51.h
in the directory of KEIL compiler (C:\Program Files\keil\C51\INC).

4) COMP51.H
This file is provided by manufacturers to define the addresses of peripheral devices,
contents of board status registers, and LCD-related function to help easily use the board.
Before using these macros, it is more important to understand the meaning of each macro by
using circuit diagrams of the board.

5) Timer / Counter
There are two timers named Timer0 and Timer1 in the 8051 microcontroller which will be
used. The registers that control timers are TMOD and TCON, and each bit of registers is

Dept. of Electrical Engineering, KAIST 8


EE306 Digital Electronic Dsign and Lab

assigned as follows. You can find the meaning of each bit and timer operation in the 8051
User's manual.

(MSB) (LSB)
TCON(Timer Control) TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TMOD(T/CModeControl) GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0

16-bit counter 1 TH1 (8 bits) TL1 (8 bits)


16-bit counter 0 TH0 (8 bits) TL0 (8 bits)
※ Upper 3 bits can be ignored according to the mode.

TCON register and each symbol of 16bit counter are defined in reg51.h file, so you can use
the name when you want to write or read a value to the bit of the register. (For example, TR0
= 0, TL0 = 0x66) This timer supports four modes. You can find the property of each mode in
the 8051 User's manual.
When you drive the timer using TCON register, it increases the counter value at every input
clock from the above 16 bit value. When the counter value becomes 65536 so that overflow
occurs, timer interrupt is generated.
Timer input clock can be obtained as (system clock/12) or from external input, according to
the C/T bit of TMOD register. If (system clock/12) is selected as input clock, since system
clock is 11.0592MHz, timer input clock will be 0.9216MHz. If you want to generate timer
interrupt every 1ms in this case for example, you can set up the starting value of the timer to
(65536 - 922) = 64614.

6) Interrupt
(1) What is interrupt?
Generally, ‘interrupt’ is defined as the event which changes the operation sequence of the
processor. Assuming that, a process reads some sectors of the disk. The process transmits the
READ instruction to the disk, and the disk reads the corresponding sector and put them to
the sector buffer of the disk. And then the disk sends the interrupt signal to the host. The host
which receives the interrupt signals of the disk stops the current operation for a while, and
performs the read operation from the sector buffer of disk.
For this event, if there is no concept of interrupt, then the host has to poll the status of the
disk often enough since it cannot predict when the disk finishes the operation. However, due
to the interrupt signal, the host can perform other instructions after transmitting the READ
instruction to the disk.
Mode detailed explanation is available in the textbooks on operating systems.

(2) Interrupt control register


(MSB) (LSB)
IE(Interrupt Enable) EA - - ES ET1 EX1 ET0 EX0
IP(Interrupt Priority) - - - PS PT1 PX1 PT0 PX0

Dept. of Electrical Engineering, KAIST 9


EE306 Digital Electronic Dsign and Lab

There are interrupt control registers as above in 8051 microcontroller. You can find the
meaning of each bit in the 8051 user’s manual.

(3) Interrupt type, interrupt vector, and interrupt handler of 8051


Interrupt No. Interrupt type Interrupt vector Modified Interrupt vector
- RESET 0000H -
0 Ext. Interrupt 0 0003H 5003H
1 Timer/Counter 0 000BH 500BH
2 Ext. Interrupt 1 0013H 5013H
3 Timer/Counter 1 001BH 501BH
4 Serial Interrupt 0023H 5023H

8051 microcontroller supports 5 interrupts as shown in the above table. For each interrupt
vector address, there is an interrupt handler routine to handle the interrupt. When you write
interrupt handler using C language as below, KEIL C51 compiler will compile and place the
corresponding interrupt vector address. (Following exampling is about Timer0 interrupt
(Interrupt No. 1))

--------------------------------------------------------------------
void function_name() interrupt 1
{
...
}
--------------------------------------------------------------------

7) 82C55
82C55 is a device of 8-bit general input and output port of A, B, and C. It is often used for
extension of GPIO (General Purpose I/O) such as 8051 ports. You can see the 8255 User's
manual to know more detail.

Dept. of Electrical Engineering, KAIST 10


EE306 Digital Electronic Dsign and Lab

As shown in the above circuit diagram, three 7-segment LED modules are connected with
three ports of 82C55. To control them, set three ports to 'output' and write the value to each
port. For this programming, you can see the operation table below. (See the macro of
C_8255_A, C_8255_B, C_8255_C and C_8255_CR in Guru51.h)

A1 A0 /RD /WR Operation


0 0 0 1 Read from Port A
0 1 0 1 Read from Port B
1 0 0 1 Read from Port C
1 1 0 1 Read Control Register
0 0 1 0 Write to Port A
0 1 1 0 Write to Port B
1 0 1 0 Write to Port C
1 1 1 0 Write Control Register

8) 7-segment LED module

DP
The structure of the 7-segment LED module is shown as follows. When A~G and DP
become ‘HIGH’, the corresponding LED turns on. As shown in the circuit diagram of
82C55, ‘HIGH’ signal is required at the SEL_8255_IN_EX to drive the LED. By using
above circuit diagram and the 7-segment LED module figure, you can know the rules of
displaying numbers. For example, assume that you want to display ‘2’. To do this, A, B, D,
E, and G should become “HIGH” and the rests should become ‘LOW’. Therefore port A
should become ‘01011011(B)’.

9) LED control

As shown in the above figure, LED is connected with P1 ports of the 80C31. If you make
some of the bit in this port ‘LOW’, then current flows through LED and the corresponding
LED turns on.

Dept. of Electrical Engineering, KAIST 11


EE306 Digital Electronic Dsign and Lab

4. Pre-report
1) Pre-report
(1) Investigate the meaning of following registers by referring to the 8051 User's Manual.
- TCON, TMOD, TH1, TH0, TL1, TL0
- IE, IP
(2) Investigate the operation characteristics of 8255 by referring to the 8255 datasheet.
(3) To display the number 0~9 to the 7-segment LED module of the experiment board, what
kind of binary values should be written to those address?
(4) When you define the variables in KEIL uVisionII compiler, you can use the keywords
such as xdata, pdata, and data. Investigate what they are and what the differences are.

2) Experimental devices and components


- COMPACT51 board
- KEIL uVisionII program
- PC
- UART cable to connect the COMPACT51 board with PC

5. Demonstration
1) Problem

(A) 7-Segment
Implement the stopwatch. The timer starts when INT0 button is pushed, and the timer stops
when INT0 button is pushed again. The timer becomes “0.00” (cleared) when TIMER0
button is pushed.

Time is represented by the three 7 segment LED modules. During the first 10 seconds, the
leftmost module represents ‘second’, the middle one represents ‘1/10 second’, and the
rightmost one represents ‘1/100 second’. At this time, there is a decimal point between
leftmost one and right 2 logics, i.e., between 1 second and 1/10 second.

After 10 seconds, 2 left modules represent the ‘second’ level and the rightmost one
represents ‘1/10 second’. In other words, the position of a decimal point shifts right and the
number of 1/100 second disappears. (Resolution is decreased, and scale is increased.) Then,
the timer can represent up to 99.9 second.

(B) LED
During stopwatch operates, 8 LED modules present the number of 7 segment LED as binary
number. 4 LED modules present multiples of 10 second, and other 4 LED modules present
multiples of 1 second. For example, if number of 7 segment is 37.2, 8 LED modules should
be 0011 0111. Implement 0 as turned-off LED and 1 as turned-on LED. Don’t express
multiples of 0.1 second and 0.01 second, and express only multiples of 1 second and 10
second.
When reset button is pushed, 8 LED modules should be turned off.

Dept. of Electrical Engineering, KAIST 12


EE306 Digital Electronic Dsign and Lab

2) Useful tip for experiments


(1) Setup jumpers as follows.

INT1 SRC TO SRC

(2) Handle the button input

As shown in above figure, INT0 button is connected with INT0 (External interrupt of
80C31), so interrupt handler of interrupt 0 can treat this input.
Current jumper setting makes TIMER0 button input connect with T0 of 80C31. This pin is
multiplexed with P3.4 (4th port of 3rd pin), so you can read the value of P3.4 to check whether
this button is pushed or not. (HINT: Set “sbit TIMER0_BUTTON = P3^4;” and read the
value of TIMER0_BUTTON. If you get ‘0’, then the button is pushed.)

(3) Pseudo-code
(There is no need to follow this process. This is just hint!)
--------------------------------------------------------------------
#include <reg51.h>
#include "COMPT51.h"

sbit TIMER0_BUTTON = P3^4;

void button_int_handler() interrupt 0


{
...
}

void timer_int_handler() interrupt 1


{
...
}

void ResetTimer0(void)
{
/* Select mode, Use internal clock as a input clock of the timer */
/* Load the initial value of the timer */
/* Timer0 interrupt disable */
/* Stop timer0 */

Dept. of Electrical Engineering, KAIST 13


EE306 Digital Electronic Dsign and Lab

void main(void)
{
SRRW1 |= 0x02; /* 7 segment logic enable */
C_8255_CR = ...; /* 7 segment logic setup */
C_8255_A = ....; C_8255_B = ...; C_8255_C = ...; /* 7 segment logic initial value */

/* Set the external interrupt0 signal, enable */

while (1) {
...
if (there is a need of change the value of 7 segment logic) {
C_8255_A = ...;
C_8255_B = ...;
C_8255_C = ...;
}

if (if INT0 button is pushed) {


...
}

if (if TIMER0 button is pushed) {


...
}
}
}
--------------------------------------------------------------------
※ According to the design, interrupt handler includes many tasks or main routine includes
many tasks. This is a design choice, and you can select the right choice.

6. Consideration
1) How precise do you think the implemented stopwatch is? What are the factors that disturb
the accuracy of the stopwatch?
2) How do you think the other interrupt is occurred when the interrupt handler is already
busy?

7. Reference
1) 8051 User’s manual
2) 8255 User’s manual
3) 8051 & C Programming, Moon Wang-hwan, Sehwa

Dept. of Electrical Engineering, KAIST 14

You might also like