Professional Documents
Culture Documents
TOC
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1
2
3
4
5
6
7
Chapter 6 : Examples
6.3 Examples
Introduction
The purpose of this chapter is to provide basic information about microcontrollers that one needs to know
in order to be able to use them successfully in practice. This is why this chapter doesn't contain any super
interesting program or device schematic with amazing solutions. Instead, the following examples are
better proof that program writing is neither a privilege nor a talent issue, but the ability of simply putting
puzzle pieces together using directives. Rest assured that design and development of devices mainly
consists of the following method test-correct-repeat. Of course, the more you are in it, the more
complicated it becomes since the puzzle pieces are put together by both children and first-class
architects...
As seen in the figure above, in order to enable the microcontroller to operate properly it is necessary to
provide:
Power supply:
Reset signal: and
Clock signal.
Clearly, it is about very simple circuits, but it does not have to be always like that. If the target device is
used for controlling expensive machines or maintaining vital functions, everything gets increasingly
complicated. However, this solution is sufficient for the time being...
Power supply
Even though this microcontroller can operate at different power supply voltages, why to test Murphys
low?! A 5V DC is most commonly used. The circuit, shown in the figure, uses a cheap integrated threeterminal positive regulator LM7805, and provides high-quality voltage stability and quite enough current to
enable the microcontroller and peripheral electronics to operate normally (enough current in this case
means 1Amp).
Reset signal
In order that the mucrocontroller can operate properly, a logic 0 (0V) must be applied to the reset pin RS.
The push button connecting the reset pin RS to power supply VCC is not necessary. However, it is almost
always provided because it enables the microcontroller safe return to normal operating conditions if
something goes wrong. 5V is brought to this pin, the microcontroller is reset and program starts execution
from the beginning.
Clock signal
Even though the microcontroller has a built-in oscillator, it cannot operate without two external capacitors
and quartz crystal which stabilize its operation and determines its frequency (operating speed of the
microcontroller).
Of course, it is not always possible to apply this solution so that there are always alternative ones. One of
them is to provide clock signal from a special source through invertor. See the figure on the left.
Nevertheless, it is not so simple in practice... It is about contact bounce- a common problem with m e c h
a n i c a l switches. When the contacts strike together, their momentum and elasticity act together to
cause bounce. The result is a rapidly pulsed electrical current instead of a clean transition from zero to full
current. It mostly occurs due to vibrations, slight rough spots and dirt between contacts. This effect is
usually unnoticeable when using these components in everyday life because the bounce happens too
quickly. In other words, the whole this process does not last long (a few micro- or miliseconds), but it is
long enough to be registered by the microcontroller. When using only a push-button as a pulse counter,
errors occur in almost 100% of cases!
The simplest solution to this problem is to connect a simple RC circuit to suppress quick voltage changes.
Since the bounce period is not defined, the values of components are not precisely determined. In most
cases, it is recomended to use the values shown in figure below.
If complete stability is needed then radical measures should be taken. The output of the circuit, shown in
figure (RS flip-flop), will change its logic state only after detecting the first pulse triggered by contact
bounce. This solution is expensive (SPDT switch), but effecient, the problem is definitely solved. Since the
capacitor is not used, very short pulses can also be registered in this way.
In addition to these hardware solutions, there is also a simple software solution. When a program tests
the state of an input pin and detects a change, the check should be done one more time after a certain
delay. If the change is confirmed, it means that a switch or push button has changed its position. The
advantages of such solution are obvious: it is free of charge, effects of noises are eliminated and it can be
applied to the poorer quality contacts as well. Disadvantage is the same as when using RC filter, i.e.
pulses shorter than program delay cannot be registered.
Optocoupler
An optocoupler is a device commonly used to galvanically separate microcontrollers electronics from any
potentially dangerous current or voltage in its surroundings. Optocouplers usually have one, two or four
light sources (LED diodes) on their input while on their output, opposite to diodes, there is the same
number of elements sensitive to light (phototransistors, photo-thyristors or photo-triacs). The point is that
an optocoupler uses a short optical transmission path to transfer a signal between the elements of circuit,
while keeping them electrically isolated. This isolation makes sense only if diodes and photo-sensitive
elements are separately powered. In this way, the microcontroller and expensive additional electronics
are completely protected from high voltage and noises which are the most common cause of destroying,
damaging or unstable operation of electronic devices in practice. The most frequently used optocouplers
are those with phototransistors on their outputs. When using the optocoupler with internal base-to-pin 6
connection (there are also optocouplers without it), the base can be left unconnected. An optional
connection which lessens the effects of noises by eliminating very short pulses is presented by the broken
line in the figure.
Relay
A relays is an electrical switch that opens and closes under control of another electrical circuit. It is
therefore connected to ouput pins of the microcontroller and used to turn on/off high-power devices such
as motors, transformers, heaters, bulbs, antenna systems etc. These are almost always placed away
from the board sensitive components. There are various types of relays but all of them operate in the
same way. When a current flows through the coil, the relay is operated by an electromagnet to open or
close one or many sets of contacts. Similar to optocouplers, there is no galvanic connection (electrical
contact) between input and output circuits. Relays usually demand both higher voltage and current to start
operation, but there are also miniature ones which can be activated by a low current directly obtained
from a microcontroller pin.
The figure shows the solution specific to the 8051 microcontroller. A darlington transistor is used here to
activate relays because of its high current gain. This is not in accordance with rules, but is necessary in
the event that logic one activation is applied since the output current is then very low (pin acts as an
input).
In order to prevent the appearance of self-induction high voltage, caused by a sudden stop of current flow
through the coil, an inverted polarized diode is connected in parallel to the coil. The purpose of this diode
is to cut off the voltage peak.
It is important to limit their current, otherwise they will be permanently destroyed. For this reason, a
conductor must be connected in parallel to an LED. In order to determine value of this conductor, it is
necessary to know diodes voltage drop in forward direction, which depends on what material a diode is
made from and what colour it is. Typical values of the most frequently used diodes are shown in table
below. As seen, there are three main types of LEDs. Standard ones get ful brightness at current of
20mA. Low Current diodes get ful brightness at ten times lower current while Super Bright diodes produce
more intensive light than Standard ones.
COLOR
TYPE
V O LTAG E D R O P U D ( V )
Infrared
30
50
1.4
Red
Standard
20
30
1.7
Red
Super Bright
20
30
1.85
Red
Low Current
30
1.7
Orange
10
30
2.0
Green
Low Current
20
2.1
Yellow
20
30
2.1
Blue
20
30
4.5
White
25
35
4.4
Since the 8051 microcontroller can provide only low output current and since its pins are configured as
outputs when voltage provided on them is 0V, direct connecting to LEDs is performed as shown in figure
on the right (Low current LED, cathode is connected to the output pin).
LED displays
Basically, an LED display is nothing more than several LEDs moulded in the same plastic case. There are
many types of displays composed of several dozens of built in diodes which can display different
symbols.
Most commonly used is a so called 7-segment display. It is composed of 8 LEDs, 7 segments are
arranged as a rectangle for symbol displaying and there is an additional segment for decimal point
displaying. In order to simplify connecting, anodes and catodes of all diodes are connected to the
common pin so that there are common anode displays and common catode displays, respectively.
Segments are marked with the latters from A to G, plus dp, as shown in the figure on the left. On
connecting, each diode is treated separtely, which means that each must have its own current limiting
resistor.
Displays connected to the microcontroller usually occupy a large number of valuable I/O pins, which can
be a big problem especially if it is needed to display multy digit numbers. The problem is more than
obvious if, for example, it is needed to display two 6-digit numbers (a simple calculation shows that 96
output pins are needed in this case). The solution to this problem is called MULTIPLEXING. This is how
an optical illusion based on the same operating principle as a film camera is made. Only one digit is active
at a time, but they change their state so quickly making impression that all digits of a number are
simultaneously active.
Here is an explanation on the figure above. First a byte representing units is applied on a microcontroller
port and a transistor T1 is activated at the same time. After a while, the transistor T1 is turned off, a byte
representing tens is applied on a port and a transistor T2 is activated. This process is being cyclically
repeated at high speed for all digits and corresponding transistors.
The fact that the microcontroller is just a kind of miniature computer designed to understand only the
language of zeros and ones is fully expressed when displaying any digit. Namely, the microcontroller
doesn't know what units, tens or hundreds are, nor what ten digits we are used to look like. Therefore,
each number to be displayed must be prepared in the following way:
First of all, a multy digit number must be split into units, tens etc. in a particular subroutine. Then each of
these digits must be stored in special bytes. Digits get familiar format by performing masking. In other
words, a binary format of each digit is replaced by a different combination of bits in a simple subroutine.
For example, the digit 8 (0000 1000) is replaced by the binary number 0111 111 in order to activate all
LEDs displaying digit 8. The only diode remaining inactive in this case is reserved for the decimal point. If
a microcontroller port is connected to the display in such a way that bit 0 activates segment a, bit 1
activates segment b, bit 2 segment c etc., then the table below shows the mask for each digit.
D I G I T S T O D I S P L AY
D I S P L AY S E G M E N T S
dp
The model described here is for its low price and great capabilities most frequently used in practice. It is
based on the HD44780 microcontroller (Hitachi) and can display messages in two lines with 16 characters
each. It displays all the letters of alphabet, Greek letters, punctuation marks, mathematical symbols etc. In
addition, it is possible to display symbols made up by the user. Other useful features include automatic
message shift (left and right), cursor appearance, LED backlight etc.
LCD Pins
There are pins along one side of a small printed board. These are used for connecting to the
microcontroller. There are in total of 14 pins marked with numbers (16 if it has backlight). Their function is
described in the table bellow:
FUNCTION
PIN NUMBER
NAME
L O G I C S T AT E
DESCRIPTION
Ground
Vss
0V
Power supply
Vdd
+5V
Contrast
Vee
0 - Vdd
RS
0
1
R/W
0
1
0
1
From 1 to 0
D0
0/1
Bit 0 LSB
D1
0/1
Bit 1
D2
0/1
Bit 2
10
D3
0/1
Bit 3
11
D4
0/1
Bit 4
12
D5
0/1
Bit 5
13
D6
0/1
Bit 6
14
D7
0/1
Bit 7 MSB
Control of operating
Data / commands
LCD screen
An LCD screen consists of two lines each containing 16 characters. Each character consists of 5x8 or
5x11 dot matrix. This book covers the most commonly used display, i.e. the 5x8 character display.
Display contrast depends on the power supply voltage and whether messages are displayed in one or two
lines. For this reason, varying voltage 0-Vdd is applied on the pin marked as Vee. Trimmer potentiometer
is usually used for that purpose. Some LCD displays have built-in backlight (blue or green LEDs). When
used during operation, a current limiting resistor should be serially connected to one of the pins for
backlight power supply (similar to LEDs).
If there are no characters displayed or if all of them are dimmed when the display is on, the first thing that
should be done is to check the potentiometer for contrast regulation. Is it properly adjusted? The same
applies if the mode of operation has been changed (writing in one or two lines).
LCD Memory
The LCD display contains three memory blocks:
DDRAM Display Data RAM;
CGRAM Character Generator RAM; and
CGROM Character Generator ROM.
DDRAM Memory
DDRAM memory is used for storing characters to be displayed. The size of this memory is sufficient for
storing 80 characters. Some memory locations are directly connected to the characters on display.
It works quite simply: it is sufficient to configure the display so as to increment addresses automatically
(shift right) and set the starting address for the message that should be displayed (for example 00 hex).
After that, all characters sent through lines D0-D7 will be displayed in the message format we are used tofrom left to right. In this case, displaying starts from the first field of the first line since the address is 00
hex. If more than 16 characters are sent, then all of them will be memorized, but only the first sixteen
characters will be visible. In order to display the rest of them, a shift command should be used. Virtually,
everything looks as if the LCD display is a window which moves left-right over memory locations
containing different characters. This is how the effect of message moving on the screen is made.
If the cursor is on, it appears at the location which is currently addressed. In other words, when a
character appears at the cursor position, it will automatically move to the next addressed location.
Since this is a sort of RAM memory, data can be written to and read from it, but its contents is irretrievably
lost when the power goes off.
CGROM Memory
CGROM memory contains the default chracter map with all characters that can be displayed on the
screen. Each character is assigned to one memory location.
The addresses of CGROM memory locations match the characters of ASCII. If the program being
currently executed encounters a command send character P to port, then the binary value 0101 0000
appears on the port. This value is the ASCII equivalent to the character P. It is then written to LCD, which
results in displaying the symbol from 0101 0000 location of CGROM. In other words, the character P is
displayed. This applies to all letters of alphabet (capitals and small), but not to numbers.
As seen on the previous map, addresses of all digits are pushed forward by 48 relative to their values
(digit 0 address is 48, digit 1 address is 49, digit 2 address is 50 etc.). Accordingly, in order to display
digits correctly, each of them needs to be added a decimal number 48 prior to be sent to LCD.
From their inception till today, computers can recognize only numbers, but not letters. It means that all
data a computer swaps with a peripheral device has a binary format, even though the same is recognized
by the man as letters (keyboard is an excellent example). Every character matches the unique
combination of zeroes and ones. ASCII is character encoding based on the English alphabet. ASCII code
specifies correspondance between standard character symbols and their numerical equivalents.
CGRAM memory
Apart from standard characters, the LCD display can also display symbols defined by the user itself. It
can be any symbol in the size of 5x8 pixels. RAM memory called CGRAM in the size of 64 bytes enables
it.
Memory registers are 8 bits wide, but only 5 lower bits are used. Logic one (1) in every register represents
a dimmed dot, while 8 locations grouped together represent one character. It is best illustrated in figure
below:
Symbols are usually defined at the beginnig of the program by simply writing zeros and ones to registers
of CGRAM memory so that they form desired shapes. In order to display them it is sufficient to specify
their address. Pay attention to the first coloumn in the CGROM map of characters. It doesn't contain RAM
memory addresses, but symbols being discussed here. In this example, display 0 means - display ,
display 1 means - display etc.
RS
RW
D7
D6
D5
D4
D3
D2
D1
D0
EXECUTION TIME
Clear display
1.64mS
Cursor home
1.64mS
I/D
40uS
40uS
Cursor/Display Shift
D/C
R/L
40uS
Function set
DL
40uS
DDRAM address
40uS
BF
DDRAM address
D7
D6
D5
D4
D3
D2
D1
D0
40uS
D7
D6
D5
D4
D3
D2
D1
D0
40uS
CGRAM address
40uS
0 = Cursor off
B 1 = Cursor blink on
0 = Cursor shift
LCD Connection
Depending on how many lines are used for connecting the LCD to the microcontroller, there are 8-bit and
4-bit LCD modes. The appropriate mode is selected at the beginning of the operation. This process is
called initialization. 8-bit LCD mode uses outputs D0-D7 to transfer data in the way explained on the
previous page. The main purpose of 4-bit LED mode is to save valuable I/O pins of the microcontroller.
Only 4 higher bits (D4-D7) are used for communication, while other may be left unconnected. Each data
is sent to the LCD in two steps: four higher bits are sent first (normally through the lines D4-D7), then four
lower bits. Initialization enables the LCD to link and interpret received bits correctly. Data is rarely read
from the LCD (it is mainly transferred from the microcontroller to LCD) so that it is often possible to save
an extra I/O pin by simple connecting R/W pin to ground. Such saving has its price. Messages will be
normally displayed, but it will not be possible to read the busy flag since it is not possible to read the
display either.
Fortunately, there is a simple solution. After sending a character or a command it is important to give the
LCD enough time to do its job. Owing to the fact that execution of the slowest command lasts for
approximately 1.64mS, it will be sufficient to wait approximately 2mS for LCD.
LCD Initialization
The LCD is automatically cleared when powered up. It lasts for approximately 15mS. After that, the
display is ready for operation. The mode of operation is set by default. It means that:
1. Display is cleared
2. Mode
o DL = 1 Communication through 8-bit interface
o N = 0 Messages are displayed in one line
o F = 0 Character font 5 x 8 dots
3. Display/Cursor on/off
o D = 0 Display off
o U = 0 Cursor off
o B = 0 Cursor blink off
4. Character entry
o ID = 1 Displayed addresses are automatically incremented by 1
o S = 0 Display shift off
Automatic reset is in most cases performed without any problems. In most cases, but not always! If for
any reason the power supply voltage does not reach ful value within 10mS, the display will start to
perform completely unpredictably. If the voltage supply unit is not able to meet this condition or if it is
needed to provide completely safe operation, the process of initialization is applied. Initialization, among
other things, causes a new reset enabling display to operate normally.
Refer to the figure below for the procedure on 8-bit initialization:
It is not a mistake!
In this algorithm, the same value is transferred three times in a row.
In case of 4-bit initialization, the procedure is as follows:
6.3 Examples
The schematic below is used in the several following examples:
Apart from components necessary for the operation of the microcontroller such as oscillator with
capacitors and the simplest reset circuit, there are also several LEDs and one push button. These are
used to indicate the operation of the program.
All LEDs are polarized in such a way that they are activated by driving a microcontroller pin low (logic 0).
LED Blinking
The purpose of this example is not to demonstrate the operation of LEDs, but the operating speed of the
microcontroller. Simply put, in order to enable LED blinking to be visible, it is necessary to provide
sufficient amount of time to pass between on/off states of LEDs. In this example time delay is provided by
executing a subroutine called Delay. It is a triple loop in which the program remains for approximately 0.5
seconds and decrements values stored in registers R0, R1 or R2. After returning from the subroutine, the
pin state is inverted and the same procedure is repeated...
;************************************************************************
;* PROGRAM NAME : Delay.ASM
;* DESCRIPTION: Program turns on/off LED on the pin P1.0
;* Software delay is used (Delay).
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(DELAY.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
;RESET VECTORS
XRESET: MOV
;Reset vector
SP,#STACK_START
MOV
P1,#0FFh
CPL
P1.0
LCALL
Delay
;Time delay
SJMP
LOOP
MOV
R2,#20
LOOP:
Delay:
;500 ms time delay
F02:
MOV
R1,#50
F01:
MOV
R0,#230
DJNZ
R0,$
DJNZ
R1,F01
DJNZ
R2,F02
END
;25 ms
;End of program
;************************************************************************
;* PROGRAM NAME : WatchDog.ASM
;* DESCRIPTION : After watch-dog reset, program increments number in
;* register R3 and shows it on port P1 in binary format.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(WATCHDOG.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
WMCON
DATA
96H
WDTEN
EQU
00000001B
PERIOD
EQU
11000000B
1024ms
;RESET
VECTOR
CSEG
AT
JMP
XRESET
CSEG
0
; Reset vector
XRESET:
LAB:
ORG
100H
ORL
WMCON,#PERIOD
ORL
WMCON,#WDTEN
MOV
A,R3
; R3 is moved to port 1
MOV
P1,A
INC
R3
; Register R3 is incremented by 1
SJMP
LAB
END
; End of program
Timer T0 in mode 1
This program spends most of its time in an endless loop waiting for timer T0 to count up a full cycle.
When it happens, an interrupt is generated, routine TIM0_ISR is executed and logic zero (0) on port P1 is
shifted right by one bit. This is another way of demonstrating the operating speed of the microcontroller
since each shift means that counter T0 has counted up 2 16 pulses!
;************************************************************************
;* PROGRAM NAME : Tim0Mod1.ASM
;BASIC DIRECTIVES
$MOD53
$TITLE(TIM0MOD1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DECLARATION OF VARIABLES
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
;RESET VECTORS
CSEG
AT 0
JMP
XRESET
ORG
00BH
JMP
TIM0_ISR
ORG
100H
XRESET: MOV
; Reset vector
SP,#STACK_START
MOV
TMOD,#01H
; MOD1 is selected
MOV
A,#0FFH
MOV
P1,#0FFH
SETB
TR0
; Timer T0 is enabled
MOV
IE,#082H
; Interrupt enabled
CLR
LOOP1: SJMP
LOOP1
; Remain here
TIM0_ISR:
RRC
MOV
P1,A
flag
PORT1
RETI
END
; End of program
;************************************************************************
;* PROGRAM NAME : Split.ASM
;* DESCRIPTION: Timer TL0 rotates bit on port P1, while TL1 determines
;* the rotation direction. Both timers operate in mode
;* 3. Logic zero (0) on output P3.2 disables rotation on port P1.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(SPLIT.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DECLARATION OF VARIABLES
BSEG
AT
;DECLARATION OF BIT-VARIABLES
SEMAPHORE:
DBIT
DIRECTION
BIT
SEMAPHORE
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
ORG
00BH
JMP
TIM0_ISR
ORG
01BH
;RESET VECTORS
; Reset vector
JMP
TIM1_ISR
ORG
100H
XRESET: MOV
SP,#STACK_START
MOV
TMOD,#00001011B
; Define MOD3
MOV
A,#0FFH
MOV
P1,#0FFH
MOV
R0,#30D
SETB
TR0
; TL0 is turned on
SETB
TR1
; TL1 is turned on
MOV
IE,#08AH
; Interrupt enabled
CLR
CLR
DIRECTION
SJMP
LOOP1
; Remain here
R0,LAB3
LOOP1:
TIM0_ISR:
DJNZ
JB
DIRECTION,LAB1
RRC
LAB1:
SJMP
LAB2
RLC
MOV
P1,A
to port P1
LAB3:
RETI
TIM1_ISR:
DJNZ
R1,LAB4
R2,LAB4
by 256 times
DJNZ
rotation direction
CPL
SMER
MOV
R2,#30D
LAB4:
RETI
END
; End of program
;************************************************************************
;* PROGRAM NAME : Tim0Tim1.ASM
;* DESCRIPTION: Timer TO rotates bit on port P1 while Timer1
;* changes rotation direction. Both timers are configured to operate in mode
1.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(TIM0TIM1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DECLARATION OF VARIABLES
BSEG
AT
;DECLARATION OF BIT-VARIABLES
SEMAPHORE:
DBIT
DIRECTION
BIT
SEMAPHORE
;STACK
DSEG
STACK_START:
AT
03FH
DS
040H
;RESET VECTORS
CSEG
AT
JMP
XRESET
; Reset vector
ORG
00BH
JMP
TIM0_ISR
ORG
01BH
JMP
TIM1_ISR
ORG
100H
XRESET: MOV
SP,#STACK_START
MOV
TMOD,#11H
MOV
A,#0FFH
MOV
P1,#0FFH
MOV
R0,#30D
; R0 is initialized
SETB
TR0
; TIMER0 is turned on
SETB
TR1
; TIMER1 is turned on
MOV
IE,#08AH
CLR
CLR
DIRECTION
SJMP
LOOP1
; Remain here
enabled
LOOP1:
TIM0_ISR:
JB
DIRECTION,LAB1
RRC A
LAB1:
SJMP
LAB2
RLC
LAB2:
MOV
P1,A
to port P1
RETI
TIM1_ISR:
DJNZ
R0,LAB3
rotation direction
CPL
DIRECTION
MOV
R0,#30D
; Initialize R0
LAB3:
RETI
END
; End of program
Using Timer T2
This example describes the use of Timer T2 configured to operate in Auto-Reload mode. In this very
case, LEDs are connected to port P3 while the push button used for forced timer reset (T2EX) is
connected to the P1.1 pin.
Program execution is similar to the previous examples. When timer ends counting, an interrupt is enabled
and subroutine TIM2_ISR is executed, thus rotating a logic zero (0) in accumulator and moving the
contents of accumulator to the P3 pin. At last, flags which caused an interrupt are cleared and program
returns to the loop LOOP1 where it remains until a new interrupt request arrives...
If push button T2EX is pressed, timer is temporarily reset. This push button resets timer, while push
button RESET resets the microcontroller.
;************************************************************************
;* PROGRAM NAME : Timer2.ASM
;* DESCRIPTION: Program rotates log. "0" on port P3. Timer2 determines
;* the speed of rotation and operates in auto-reload mode
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(TIMER2.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DEFINITION OF VARIABLES
T2MOD
DATA
0C9H
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
; Reset vector
ORG
02BH
;STACK
;RESET VECTORS
JMP
TIM2_ISR
ORG
100H
XRESET: MOV
LOOP1:
SP,#STACK_START
MOV
A,#0FFH
MOV
P3,#0FFH
MOV
RCAP2L,#0FH
MOV
RCAP2L,#01H
CLR
CAP2
SETB
EXEN2
SETB
TR2
; Enable Timer T2
MOV
IE,#0A0H
; Interrupt is enabled
CLR
SJMP
LOOP1
; Remain here
TIM2_ISR:
RRC
P3,A
CLR
TF2
CLR
EXF2
A to PORT3
RETI
END
; End of program
;************************************************************************
;* PROGRAM NAME : Int.ASM
;* DESCRIPTION : Program counts interrupts INT0 generated by appearance of
high-to-low
;* transition signal on pin P3.2 Result appears on port P0. Interrupts INT1
are also
;* counted up at the same time. They are generated byappearing high-to-low
transition
;* signal on pin P3. The result appears on port P1.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(INT.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;RESET VECTORS
CSEG
AT
JMP
XRESET
; Reset vector
ORG
003H
JMP
Isr_Int0
ORG 013H
JMP
Isr_Int1
ORG
100H
MOV
TCON,#00000101B
XRESET:
; Interrupt INT0 is generated by
appearing
; high-to-low transition signal on pin
P3.2
; Interrupt INT0 is generated by
appearing
; high-to-low transition signal on pin
P3.3
LOOP:
MOV
IE,#10000101B
; Interrupt enabled
MOV
R0,#00H
MOV
R1,#00H
MOV
P0,#00H
; Reset port P0
MOV
P1,#00H
; Reset port P1
SJMP
LOOP
; Remain here
Isr_Int0:
INC R0
counter
MOV P0,R0
RETI
Isr_Int1:
INC R1
counter
MOV P1,R1
RETI
END
; End of program
;************************************************************************
;* PROGRAM NAME : 7Seg1.ASM
;* DESCRIPTION: Program displays number "3" on 7-segment LED display
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
STACK_START:
AT
03FH
DS
040H
;RESET VECTORS
AT
JMP
XRESET
ORG
100H
MOV
SP,#STACK_START
MOV
P1,#0
MOV
P3,#20h
; Activate display D4
MOV
A,#03
LCALL
Disp
XRESET:
CSEG
0
; Reset vector
LOOP:
the number
Disp:
MOV
P1,A
SJMP
LOOP
INC
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0 mask
DB
06H
; Digit 1 mask
DB
5BH
; Digit 2 mask
DB
4FH
; Digit 3 mask
DB
66H
; Digit 4 mask
DB
6DH
; Digit 5 mask
DB
7DH
; Digit 6 mask
DB
07H
; Digit 7 mask
DB
7FH
; Digit 8 mask
DB
6FH
; Digit 9 mask
END
; End of program
;************************************************************************
;* PROGRAM NAME: 7Seg2.ASM
;* DESCRIPTION: Program writes numbers 0-9 on 7-segment LED display
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG2.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
MOV
SP,#STACK_START
MOV
R3,#0
MOV
P1,#0
MOV
P3,#20h
; Activate display D4
MOV
A,R3
STACK_START:
;RESET VECTORS
XRESET:
LOOP:
; Reset vector
LCALL
Disp
number in
; Accumulator
MOV
P1,A
INC
R3
CJNE
R3,#10,L2
MOV
R3,#0
MOV
R2,#20
F02:
MOV
R1,#50
; 25 mS
F01:
MOV
R0,#230
DJNZ
R0,$
DJNZ
R1,F01
DJNZ
R2,F02
SJMP
LOOP
in R3
L2:
Disp:
INC
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0 mask
DB
06H
; Digit 1 mask
DB
5BH
; Digit 2 mask
DB
4FH
; Digit 3 mask
DB
66H
; Digit 4 mask
DB
6DH
; Digit 5 mask
DB
7DH
; Digit 6 mask
DB
07H
; Digit 7 mask
DB
7FH
; Digit 8 mask
DB
6FH
; Digit 9 mask
END
; End of program
transistor T4 is disabled and the whole process is repeated using transistor T3 and display D3 in order to
display digit 2. This procedure must be continuosly repeated in order to make impression that both
displays are active at the same time.
;************************************************************************
;* PROGRAM NAME: 7Seg3.ASM
;* DESCRIPTION: Program displays number "23" on 7-segment LED display
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG3.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
XRESET:
MOV
SP,#STACK_START
LOOP:
MOV
P1,#0
MOV
P3,#20h
; Activate display D4
MOV
A,#03
LCALL
Disp
MOV
P1,A
MOV
P1,#0
MOV
P3,#10h
; Activate display D3
MOV
A,#02
;RESET VECTORS
; Reset vector
that digit
LCALL
Disp
MOV
P1,A
SJMP
LOOP
Disp:
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0 mask
DB
06H
; Digit 1 mask
DB
5BH
; Digit 2 mask
DB
4FH
; Digit 3 mask
DB
66H
; Digit 4 mask
DB
6DH
; Digit 5 mask
DB
7DH
; Digit 6 mask
DB
07H
; Digit 7 mask
DB
7FH
; Digit 8 mask
DB
6FH
; Digit 9 mask
END
; End of program
;************************************************************************
;* PROGRAM NAME : 7Seg5.ASM
;* DESCRIPTION : Program displays number"1234" on 7-segment LED display
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG5.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
XRESET:
MOV
SP,#STACK_START
LOOP:
MOV
P1,#0
MOV
P3,#20h
; Activate display D4
MOV
A,#04
LCALL
Disp
STACK_START:
;RESET VECTORS
; Reset vector
MOV
P1,A
MOV
P1,#0
MOV
P3,#10h
; Activate display D3
MOV
A,#03
LCALL
Disp
MOV
P1,A
MOV
P1,#0
MOV
P3,#08h
; Activate display D2
MOV
A,#02
LCALL
Disp
MOV
P1,A
MOV
P1,#0
MOV
P3,#04h
; Activate display D1
MOV
A,#01
LCALL
Disp
MOV
P1,A
SJMP
LOOP
Disp:
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0 mask
DB
06H
; Digit 1 mask
DB
5BH
; Digit 2 mask
DB
4FH
; Digit 3 mask
DB
66H
; Digit 4 mask
DB
6DH
; Digit 5 mask
DB
7DH
; Digit 6 mask
DB
07H
; Digit 7 mask
DB
7FH
; Digit 8 mask
DB
6FH
; Digit 9 mask
Things are getting complicated... In addition to two digit multiplexing, the microcontroller also performs
other operations. In this example, contents of registers R2 and R3 are incremented in order to display
number counting (97, 98, 99, 00, 01, 02...).
This time, transistors which activate displays remain turned on for 25mS. The soubroutine Delay is in
charge of that. Even though digits shift much slower now, it is still not slow enough to make impression of
simultaneous operation. After both digits of a number blink for 20 times, the number on displays is
incremented by 1 and the whole procedure is repeated.
;************************************************************************
;* PROGRAM NAME : 7Seg4.ASM
;* DESCRIPTION: Program displays numbers 0-99 on 7-segment LED displays
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG4.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
MOV
SP,#STACK_START
MOV
R2,#0
MOV
R3,#0
MOV
R4,#0
INC
R4
STACK_START:
;RESET VECTORS
XRESET:
LOOP:
; Reset vector
R4,#20d,LAB1
MOV
R4,#0
MOV
P1,#0
INC
R2
units by 1
CJNE
R2,#10d,LAB1
MOV
R2,#0
; Reset units
INC
R3
CJNE
R3,#10d,LAB1
MOV
R3,#0
; Reset tens
MOV
P3,#20h
; Activate display D4
MOV
A,R2
LCALL
Disp
MOV
P1,A
LCALL
Delay
; 25ms delay
MOV
P1,#0
by 1
LAB1:
to A
MOV
P3,#10h
; Activate display D3
MOV
A,R3
LCALL
Disp
MOV
P1,A
LCALL
Delay
; 25ms delay
SJMP
LOOP
MOV
R1,#50
MOV
R0,#250
DJNZ
R0,$
DJNZ
R1,F01
to A
Delay:
F01:
; 5 ms delay
RET
Disp:
digits
INC
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0 mask
DB
06H
; Digit 1 mask
DB
5BH
; Digit 2 mask
DB
4FH
; Digit 3 mask
DB
66H
; Digit 4 mask
DB
6DH
; Digit 5 mask
DB
7DH
; Digit 6 mask
DB
07H
; Digit 7 mask
DB
7FH
; Digit 8 mask
DB
6FH
; Digit 9 mask
END
; End of program
Handling EEPROM
This program writes data to on-chip EEPROM memory. In this case, the data is a hexadecimal number 23
which is to be written to the location with address 00.
To make sure that this number is correctly written, the same location of EEPROM is read 10mS later in
order to compare these two numbers. If they match, F will be displayed. Otherwise, E will be displayed on
the LED display (Error).
;************************************************************************
;* PROGRAM NAME: EEProm1.ASM
;* DESCRIPTION: Programming EEPROM at address 0000hex and displaying message
;* on LED display.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(EEPROM1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
WMCON
DATA
96H
EEMEN
EQU
00001000B
enabled
EEMWE
EQU
00010000B
TEMP
DATA
030H
THE END
EQU
071H
; Display "F"
ERROR
EQU
033H
; Display "E"
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
MOV
IE,#00
MOV
SP,#STACK_START
;STACK
STACK_START:
;RESET VECTORS
XRESET:
; Reset vector
MOV
DPTR,#0000H
ORL
WMCON,#EEMEN
ORL
WMCON,#EEMWE
MOV
TEMP,#23H
MOV
A,TEMP
MOVX
@DPTR,A
CALL
DELAY
; 10ms delay
MOVX
A,@DPTR
A,TEMP,ERROR
MOV
A,#KRAJ
; Display F (correct)
MOV
P1,A
XRL
WMCON,#EEMWE
XRL
WMCON,#EEMEN
SJMP
LOOP1
; Remain here
EEPROM
moved to
compare to TEMP,
CJNE
label ERROR
LOOP1:
ERROR:
MOV
A,#ERROR
MOV
P1,A
LOOP2:
SJMP
LOOP2
DELAY:
MOV
A,#0AH
MOV
R3,A
LOOP3:
NOP
LOOP4:
DJNZ
B,LOOP4
LOOP5:
DJNZ
B,LOOP5
DJNZ
R3,LOOP3
; Display E (error)
; Delay
RET
END
; End of program
This example shows how to receive message sent by a PC. Timer T1 generates boud rate. Since the
11.0592 MHz quartz crystal is used here, it is easy to obtain standard baud rate which amouts to 9600
bauds. Each received data is immediately transferred to port P1 pins.
;************************************************************************
;* PROGRAM NAME : UartR.ASM
;* DESCRIPTION: Each data received from PC via UART appears on the port
;* P1.
;*
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(UARTR.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
STACK_START:
;RESET VECTORS
; Reset vector
ORG
023H
JMP
IR_SER
ORG
100H
MOV
IE,#00
MOV
SP,#STACK_START
MOV
TMOD,#20H
; Timer1 in mode2
MOV
TH1,#0FDH
routine
XRESET:
; 11.0592MHz
LOOP:
MOV
SCON,#50H
MOV
IE,#10010000B
CLR
TI
CLR
RI
SETB
TR1
; Start Timer1
SJMP
LOOP
; Remain here
IR_SER:
JNB
RI,OUTPUT
OUTPUT
MOV
A,SBUF
MOV
P1,A
CLR
RI
; P1
RETI
END
; End of program
;************************************************************************
;* PROGRAM NAME : UartS.ASM
;* DESCRIPTION: Sends values 0-255 to PC.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(UARTS.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
STACK_START:
;RESET VECTORS
; Reset vector
XRESET:
MOV
IE,#00
MOV
SP,#STACK_START
MOV
TMOD,#20H
; Timer1 in mode 2
MOV
TH1,#0FDH
MOV
SCON,#40H
; 8-bit UART
CLR
TI
CLR
RI
MOV
R3,#00H
; Reset caunter
SETB
TR1
; Start Timer 1
START:
MOV
SBUF,R3
LOOP1:
JNB
TI,LOOP1
CLR
TI
INC
R3
CJNE
R3,#00H,START
the
; label START
LOOP:
SJMP
LOOP
END
; Remain here
; End of program
*************************************************************************
;* PROGRAM NAME : Lcd.ASM
;* DESCRIPRTION : Program for testing LCD display. 4-bit communication
;* is used. Program does not check BUSY flag but uses program delay
;* between 2 commands. PORT1 is used for connection
;* to the microcontroller.
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(LCD.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;Stack
DSEG
AT
0E0h
Stack_Start:
DS
020h
Start_address
EQU
0000h
;Reset vectors
CSEG
AT
ORG
Start_address
JMP
Inic
Inic:
ORG
Start_address+100h
MOV
IE,#00
MOV
SP,#Stack_Start
CALL
LCD_inic
; Initialize LCD
;*************************************************
;* MAIN PROGRAM
;*************************************************
START:
MOV
A,#80h
CALL
LCD_status
MOV
A,#'M'
; Display character M.
CALL
LCD_putc
A,#'i'
; Display character i.
the first
LCD display.
transmission.
MOV
CALL
LCD_putc
MOV
A,#'k'
CALL
LCD_putc
MOV
A,#'r'
CALL
LCD_putc
MOV
A,#'o'
CALL
LCD_putc
MOV
A,#'e'
CALL
LCD_putc
MOV
A,#'l'
CALL
LCD_putc
MOV
A,#'e'
CALL
LCD_putc
MOV
A,#'k'
CALL
LCD_putc
MOV
A,#'t'
CALL
LCD_putc
MOV
A,#'r'
; Display character k.
; Display character r.
; Display character o.
; Display character e.
; Display character l.
; Display character e.
; Display character k.
; Display character t.
; Display character r.
CALL
LCD_putc
MOV
A,#'o'
CALL
LCD_putc
MOV
A,#'n'
CALL
LCD_putc
MOV
A,#'i'
CALL
LCD_putc
MOV
A,#'k'
CALL
LCD_putc
MOV
A,#'a'
CALL
LCD_putc
MOV
A,#0c0h
CALL
LCD_status
MOV
A,#'R'
; Display character R.
CALL
LCD_putc
; Display character o.
; Display character n.
; Display character i.
; Display character k.
; Display character a.
the first
of LCD display.
transmission.
MOV
A,#'a'
CALL
LCD_putc
MOV
A,#'z'
CALL
LCD_putc
MOV
A,#'v'
CALL
LCD_putc
MOV
A,#'o'
CALL
LCD_putc
MOV
A,#'j'
CALL
LCD_putc
MOV
A,#'n'
CALL
LCD_putc
MOV
A,#'i'
CALL
LCD_putc
MOV
A,#' '
CALL
LCD_putc
MOV
A,#'s'
CALL
LCD_putc
; Display character a.
; Display character z.
; Display character v.
; Display character o.
; Display character j.
; Display character n.
; Display character i.
; Display character .
; Display character s.
MOV
A,#'i'
; Display character i.
CALL
LCD_putc
MOV
A,#'s'
CALL
LCD_putc
MOV
A,#'t'
CALL
LCD_putc
MOV
A,#'e'
CALL
LCD_putc
MOV
A,#'m'
CALL
LCD_putc
MOV
A,#'i'
CALL
LCD_putc
MOV
R0,#20d
CALL
Delay_10ms
MOV
DPTR,#LCD_DB
; Clear display
MOV
A,#6d
CALL
LCD_inic_status
; Display character s.
; Display character t.
; Display character e.
; Display character m.
; Display character i.
MOV
R0,#10d
CALL
Delay_10ms
JMP
START
; Wait time(10x10ms)
;*********************************************
;* Subroutine for wait time (T= r0 x 10ms)
;*********************************************
Delay_10ms:
MOV
R5,00h
; 1+(1+(1+2*r7+2)*r6+2)*r5
MOV
R6,#100d
; (if r7>10)
MOV
R7,#100d
; 2*r5*r6*r7
DJNZ
R7,$
; $ indicates current
DJNZ
R6,$-4
DJNZ
R5,$-6
approximately
instruction.
RET
;
****************************************************************************
**********
;* SUBROUTINE: LCD_inic
;* DESCRIPTION: Subroutine for LCD initialization.
;*
;* (is used with 4-bit interface, under condition that pins DB4-7 on LCD
;* are connected to pins PX.4-7 on microcontrollers ports, i.e. four higher
;* bits on the port are used).
;*
;* NOTE: It is necessary to define port pins for controlling LCD operation:
;* LCD_enable, LCD_read_write, LCD_reg_select,similar to port for connection
to LCD.
;* It is also necessary to define addresses for the first character in each
;* line.
;
****************************************************************************
**********
LCD_enable
LCD.
BIT
P1.3
LCD_read_write BIT
P1.1
P1.2
LCD.
LCD_reg_select BIT
LCD.
LCD_port
SET
P1
Busy
BIT
P1.7
00h
appears.
LCD_Start_I_red
EQU
character
; in the first line of LCD
display.
LCD_Start_II_red EQU
40h
character
; in the second line of LCD
display.
LCD_DB:
DB
00111100b
DB
00101100b
DB
00011000b
; 2 -Display/cursor shift,
format
format
right/left
DB
00001100b
DB
00000110b
DB
00000010b
; 5 -Display/cursor home
DB
00000001b
; 6 -Clear display
DB
00001000b
shift off
LCD_inic:
;*****************************************
MOV
DPTR,#LCD_DB
MOV
A,#00d
; Triple initialization in 8-
bit
CALL
LCD_inic_status_8
MOV
A,#00d
CALL
LCD_inic_status_8
beginning
supply is on
MOV
A,#00d
lcall LCD_inic_status_8
MOV
A,#1d
CALL
LCD_inic_status_8
; 4-bit mode
MOV
A,#1d
CALL
LCD_inic_status
MOV
A,#3d
program executes in
;4-bit mode
CALL
LCD_inic_status
MOV
A,#6d
CALL
LCD_inic_status
MOV
A,#4d
CALL
LCD_inic_status
RET
LCD_inic_status_8:
;******************************************
PUSH
MOVC
A,@A+DPTR
CLR
LCD_reg_select
CLR
LCD_read_write
MOV
B,LCD_port
ORL
B,#11110000b
ORL
A,#00001111b
ANL
A,B
MOV
LCD_port,A
SETB
LCD_enable
are memorized
port
LCD_enable
MOV
B,#255d
DJNZ
B,$
; during initialization
improper reset
DJNZ B,$
DJNZ B,$
POP B
RET
LCD_inic_status:
;
****************************************************************************
MOVC
A,@A+DPTR
CALL
LCD_status
RET
;
****************************************************************************
;* SUBROUTINE: LCD_status
;* DESCRIPTION: Subroutine for defining LCD status.
;
****************************************************************************
LCD_status:
accumulator
PUSH
MOV
B,#255d
DJNZ
B,$
DJNZ
B,$
DJNZ
B,$
CLR
LCD_reg_select
CALL
LCD_port_out
SWAP
DJNZ
B,$
DJNZ
B,$
DJNZ
B,$
CLR
LCD_reg_select
CALL
LCD_port_out
POP
RET
;
****************************************************************************
;* SUBROUTINE: LCD_putc
;* DESCRIPTION: Sending character to be displayed on LCD.
;
****************************************************************************
LCD_putc:
PUSH
MOV
B,#255d
DJNZ
B,$
SETB
LCD_reg_select
CALL
LCD_port_out
SWAP
DJNZ
B,$
SETB
LCD_reg_select
CALL
LCD_port_out
POP
LCD
accumulator
LCD
RET
;
****************************************************************************
;* SUBROUTINE: LCD_port_out
;* DESCRIPTION: Sending commands or characters on LCD display
;
****************************************************************************
LCD_port_out:
PUSH
ACC
PUSH
MOV
B,LCD_port
ORL
B,#11110000b
ORL
A,#00001111b
ANL
A,B
MOV
LCD_port,A
SETB
LCD_enable
memorized
port
LCD_enable
POP
POP
ACC
RET
END
; End of program
;************************************************************************
;* SUBROUTINE NAME : BinDec.ASM
;* DESCRIPTION : Content of accumulator is converted into three decimal
digits
;************************************************************************
BINDEC:
MOV
B,#10d
DIV
AB
MOV
R3,B
MOV
B,#10d
DIV
AB
MOV
R2,B
MOV
B,#10d
DIV
AB
MOV
A,B
RET
The Easy8051 v6 is compatible with 14-, 16-, 20-, 28-, 40-pin PLCC44 and PLCC32 MCUs. It comes with an AT89S8253.
The board has a USB 2.0 programmer and many peripherals such as COG, port expander, MENU and 4x4 keypads etc.
[more info]
mikroProg for 8051 is supported with mikroC, mikroBasic and mikroPascal compilers for 8051. You may also use
mikroProg for 8051 as a standalone programming tool. [more info]
Featured Compilers
mikroBasic PRO for 8051
The 8051 core combined with modern modules is popular in the past. WithmikroBasic you can quickly develop
TOC
Chapter 1
Chapter 2
Chapter 3
Chapter
Chapter
Chapter
Chapter
4
5
6
7
;RESET
VECTOR
CSEG
AT
JMP
XRESET
0
; Reset vector
CSEG
XRESET:
ORG
100H
ORL
WMCON,#PERIOD
ORL
WMCON,#WDTEN
To enable the compiler to operate successfully, it is necessary that a document containing this program
has the extension, .asm in its name, for example: Program asm.
When a specialized program (mplab) is used, this extension will be automatically added. If any other
program for text processing (Notepad) is used then the document should be saved and renamed. For
example: Program.txt -> Program.asm. This procedure is not necessarily performed. The document may
be saved in original format while its text may be copied to the programmer for further use.
Compiling a program
The microcontroller does not undrestand assembly language as such. That is why it is necessary to
compile the program into machine language. It is more than simple when a specialized program (mplab)
is used because a compiler is a part of the software. Just one click on the appropriate icon solves the
problem and a new document with .hex extension appears. It is actually the same program, only compiled
into machine language which the microcontroller perfectly understands. Such documentation is commonly
named hex code and seemingly represents a meaningless sequence of numbers in hexadecimal
number system.
:03000000020100FA1001000075813F
7590FFB29012010D80F97A1479D40
90110003278589EAF3698E8EB25B
A585FEA2569AD96E6D8FED9FAD
AF6DD00000001FF255AFED589EA
F3698E8EB25BA585FEA2569AD96
DAC59700D00000278E6D8FED9FA
DAF6DD00000001FF255AFED8FED
9FADAF6DD000F7590FFB29013278
E6D8FED9FADAF6DD00000001FF2
55AFED589EAF3698E8EB25BA585
FEA2569AD96DAC59D9FADAF6D
D00000001FF255AFED8FED9FADA
F6DD000F7590FFB29013278E6D82
78E6D8FED9FA589EAF3698E8EB2
5BA585FEA2569AD96DAF6DD000
00001FF2DAF6DD00000001FF255A
ADAF6DD00000001FF255AFED8FE
D9FA
In the event that other software for program writing in assembly language is used, a special software for
compiling the program must be installed and used as follows - set up the compiler, open the document
with .asm extension and compile. The result is the same- a new document with extension .hex. The only
problem now is that it is stored in your PC.
Programming a microcontroller
In order to transfer a hex code to the microcontroller, it is necessary to provide a cable for serial
communication and a special device, called programmer, with software. There are several ways to do it.
A large number of programs and electronic circuits having this purpose can be found on the Internet. Do
as follows: open hex code document, set a few parameters and click the icon for compiling. After a while,
a sequence of zeros and ones will be programmed into the microcontroller through the serial connection
cable and programmer hardware. What's left is to place the programmed chip into the taget device. In the
event that it is necessary to make some changes in the program, the previous procedure may be
repeated an unlimited number of times.
The program is written and successfully compiled. All that's left is to dump the program to the
microcontroller. For this purpose it is necessary to have a software that takes the written and compiled
program and passes it to the microcontroller.
Start the program 8051 Flash_setup.exe...
Thats all! The microcontroller is programmed and everything is ready for operation. If you are not
satisfied, make some changes in the program and repeat the procedure. Until when? Until you feel
satisfied...
Development systems
A device which in the testing program phase can simulate any environment is called a development
system. Apart from the programmer, the power supply unit and the microcontrollers socket, the
development system contains elements for input pin activation and output pin monitoring. The simplest
version has every pin connected to one push button and one LED as well. A high quality version has LED
displays, LCD displays, temperature sensors and all other elements which can be supplied with the target
device. These peripherals can be connected to the MCU via miniature jumpers. In this way, the whole
program may be tested in practice during its development stage, because the microcontroller doesn't
know or care whether its input is activated by a push button or a sensor built in a real device.
Sockets
All microcontrollers manufactured by Atmel appear in a few standard DIP packages. In order to enable
their programming using one device, corresponding pins (having the same name) on sockets are
connected in parallel. As a result, by being placed in the appropriate socket, each microcontroller is
automatically properly connected. Figure on the right shows a microcontroller in 40-pin package and
connection of one of its I/O pins (P1.5). As seen, the pin can be connected to an external device
(connector PORT1), LED (microswitch SW2), push button or resistor through connectors. In the last two
cases, polarity of voltage is selected using on-board jumpers.
Programmer
The purpose of the programmer is to transfer HEX code from PC to appropriate pins and provide regular
voltage levels during chip programming as well. For this development system, the programmer is built in it
and should be connected to PC via USB cable. When the process of programming is completed, pins
used for it are automatically available for other application.
There is a connector on the development board enabling commection to external power supply source
(AC/DC, 8-16V). Besides, voltage necessary for device operation can also be obtained from PC via USB
cable. Jumper J5 is used for power supply selection.
8MHz Oscillator
The EASY8051A development system has built-in oscillator used as a clock signal generator. The
frequency of this oscillator is stabilized by 8Hz quartz crystal. Besides, it is also possible to select internal
RC oscillator during chip programming,.
Each I/O port pin is connected to one LED which enables visual indication of its logic state. In the event
that the presence of directly polarized LEDs and serial resistors is not acceptable in some applications,
DIP switch SW2 enables them to be disconnected from the port.
Similar to LEDs, each I/O port pin is connected to one push button on the development board. It enables
simple activation of input pins. Jumper J6 is used for selecting voltage polarity (+ or -) brought to pins by
pressing appropriate push button.
Being often applied in the industry, four high-performance LED displays set in multiplex mode belong to
the development system. Display segments are connected to the port P0 via resistors. Transistor drivers
used for activating individual digits are connected to the first four port P1 pins. It enables programs using
7-segment displays to be tested with minimum use of I/O ports. Similar to LEDs, DIP switch SW2 enables
transistor drivers to be disconnected from microcontroller pins.
LCD displays
The EASY8051A development system provides connection to eather graphic or alphanumeric LCD
display. Both types of displays are connected by being placed into appropriate connector and by switching
position of the jumper J8. If displays are not in use, all pins used for their operation are available for other
applications. Apart from connectors, there is also a potentiometer for contrast regulation on the board.
In order to enable programs using serial communication to be tested, the development system has built in
standard 9-pin SUB-D connector. The MAX232 is used as a voltage regulator.
Similar to other built-in circuits, electronics supporting serial communication can be enabled or disabled
by using jumpers J9 and J10.
Temperature measurement is one of the most common tasks of devices which operate in the industry. For
this reason, there is a circuit DS1820 on the EASY8051A development system which measures
temperature in the range of -55 to +125oC with accuracy greater than 0.50. Results of measurement are
transferred via serial communication to the pins P3.3 or P2.7. Jumper J7 is used for selecting pins for
data reception. In the event that no jumper is installed, port pins are available for other applications.
A built-in 12-bit AD Converter MCP3204 has four input channels connected to on-board connectors. Data
are interchanged with the microcontroller via SPI serial communication system using pins P1.5, P1.6,
P1.7 and P3.5. If A/D converter is not in use, these pins can be used for other applications (DIP switch
SW1). In order to check operation, there is a potentiometer on the development board used as a variable
voltage source. It can be brought to the converters input pins using one of four jumpers J12. As a special
convenience, a reference voltage source MCP1541 (4,096V) is built in. Jumper J11 is used to select
whether converter will use this voltage or 5V.
Digital to analog conversion (D/A) is another operation ofen performed by the microcontroller in practice.
For this reason, there is a special on-board chip which interchanges data with the microcontroller via SPI
communication system. It can also generate analog voltage in 12-bit resolution on its output pin. When it
is not in use, all microcontroller pins are available for other applications using DIP switch SW1. Similar to
A/D converter, jumper J11 is used for selecting reference voltage.
In order to enable microcontroller ports to be directly connected to additional components, each of them is
connected to one on-board connector. Besides, two pins of each connector are connected to power
supply voltage while each pin can be connected to + or - polarity of voltage via resistors (pull up or pull
down resistors). Presence and connection of these resistors are determined by jumpers. Jumper J3 which
controls port P3 is shown in figure on the right.
The Easy8051 v6 is compatible with 14-, 16-, 20-, 28-, 40-pin PLCC44 and PLCC32 MCUs. It comes with an AT89S8253.
The board has a USB 2.0 programmer and many peripherals such as COG, port expander, MENU and 4x4 keypads etc.
[more info]
mikroProg for 8051 is supported with mikroC, mikroBasic and mikroPascal compilers for 8051. You may also use
mikroProg for 8051 as a standalone programming tool. [more info]
Featured Compilers
mikroBasic PRO for 8051
The 8051 core combined with modern modules is popular in the past. WithmikroBasic you can quickly develop