Вы находитесь на странице: 1из 92

SIMPLE Micro

Controller Unit
PROGRAMMING

8051

myhobby Educational Trainer


Heart
EAF2 Electronics Series
Systems

- Training Tools -

Hello. I am MCU
your friendly IC

PART 1

For the Intel 8051


Micro Controller Unit

ONE

A DIY Training Course


Absolutely for Beginners

Software Programming versus Firmware Programming

Sotware :
BASCOM 8051

PC Computer Versus Micro controller Unit


(MCU)
III

www.mcselec.com

The BASCOM 8051 Integrated Development


Environment
Developed Firmware to Control LEDs
Software Simulation

III

Run Firmware on the Real Hardware

IV

Flow Chart - Program Structure

Controlling LEDs and 7-segment LED


Display

Shareware & Courseware

Hello. I am MCU
your friendly IC

LCD

LED Light

CPU RAM

ROM

I/O

Micro Controller Unit


Switch

F. Dandy Menor

Sensor

SIMPLE Micro Controller Unit


PROGRAMMING

PART 1

For the Intel 8051


Micro Controller Unit

ONE

Think before you PRINT.


Help to Save the Environment
This documents is free to view
on Adobe PDF. Thanks.

Dedicated to :
my loving mother, my KIDDY - Christopher Christian and John Markus and to all MCU
DIY Learner and Hobbyist

F. Dandy Menor
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

SIMPLE Micro Controller Unit Programming


c Heart Systems, 2009

Cover Design &


Photography :

F. Dandy Menor

Copyright
The SIMPLE Micro Controller Unit Programming is copyright
protected. In your study of Micro Controller Programming, you may
be reproduced, store in any media and photocopied in part not whole
and used that to the best it can do for you to learn 8051 MCU programming with the BASCOM 8051 IDE. You are not however, allowed to print and sell this document for profit.

CD ROM
The physical CD ROM enclosed herein (if directly obtained from
authorised vendor), at the time of released has been checked and
found to be functionally working. If found to be defective at the time
of purchase, you may notify Heart Systems and if found to be correct, Heart Systems will replace the defective CD ROM. The CD
ROM is not free.

Program Exercises
Program exercises supplied on the CD ROM maybe copied, distribute, printed, transmitted and store in any media in its original form
and provided that, user will follow the terms and conditions supplied
in each program.

Video Files
Video files supplied on the CD ROM are more than welcome to
copy, distribute and share without editing any part of it.

W ar
ning !!!
arning

Important Notice

All brand or product names mentioned are trademarks or registered


trademarks of their respective holders.

Starter MCU-EB80512 & Night Eyes Security


Systems version 2 described

Disclaimer

in this Document involved the use of


electricity. The user is advised to observe all safety precautions when
doing the experiments described.

Trademarks

The information furnished in this Document is believe to be reliable


and accurate. It is provided as it is without warranty of any kind,
express or implied. Heart Systems and/or the author is not liable
for any damages of whatsoever including direct, incidental, consequential, loss of business profit or any special damages in the use
and misuse of any of the information contained.
The author or Heart Systems does not guarantee or warrant the
Microcontroller, software, IC and components, Programmers and
equipment described here.

The

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems has the right to modify this Document for


improvement at any time as required without prior notice.
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

WHO Should read this


The SIMPLE Micro Controller Unit Programming is redesigned and enhanced to be use by anyone who is just
starting to learn to program the Intel 8051 Micro Controller Unit and its derivative MCUs specially the
Atmel AT89S51 and AT89S52 - - specially those with Zero background in Micro Controller Programming.
It is also for 8051 Lovers, for electronic hobbyists and DIYer's, Students and Technicians who want to avail the
Easy, Affordable, Fast and Fun [EAF2 mark] Tools - - - Software and Hardware.
{Well, I have a confession to make, it was originally intended for my 3 kiddy, just to make them leave a bit of the
PC games, Network games, PSP and mobile phone SMS - - something for the Author to give them a DIY lesson
to do and Learn.}

What You Should Know


The SIMPLE Micro Controller Unit Programming is a Shareware and Courseware.
It is FREE, means - - freedom to use by anyone who would like to start Learning the Intel 8051 Micro Controller unit
particularly the derivative 8051 compatible MCU - the popular ATMEL AT89S51 and the AT89S52. It is free to use
but not free of cost.
The Target boards which described here are not free of cost. If you need these Target Boards to run your firmware
in the real world - to execute your firmware in real time, you may avail it at minimum cost. Please contact the
webmaster who own the board at odcenter@gmail.com - visit http://www/1diycentre.com . Again, it is not free
and zero cost. Thank you for your support.

STARTER MCU-EB8051-2
Trainer Kit

NE2ss Trainer Kit

Inspirations
Thats Right! I am standing here, living proof that if you work hard enough...... and you want it bad enough.....
dreams do come true. So follow your dreams man, follow your dreams because we all die young
-from the movie ROCKSTARmyhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Acknowledgement
SIMPLE Micro Controller Unit Programming will never finished without the support, help, guidance of individuals and companies and sources of inspirations and power. I would like to thank them.
Thanks to Intel Corp. for the legendary 8051 core architecture of MCU, and Atmel for the nice derivative 8051 core
MCU with Flash ROM - the AT89S51 and AT89S52.
Mr. Mark Albert of MCS Electronic - The Nederland (www.mcselec.com), thank you very much for having the
BASCOM 8051 Integrated Development Environment (IDE) software - demo version which I used to write the
program, your permission and authorization to used and discussed in this Trainer Kit and share in the CD ROM.
Mr. Rand Liu - of Shenzhen China, thank you for your help on the HS AT89 Flash MCU Programmer, the official
programmer to burn the firmware into the Flash ROM of the AT89S51 and AT89S52 MCU from Atmel (http://
www.atmel.com). I am glad for your support.
Mr. Jerry V. Verzosa - R&D Manager of CCT Tech, who open up his place for me to stay during a long vacation in
Shenzhen China (Nov. 2009) while writing the Chapter 6 and the rest of the chapters. Thanks for those noodles and
beers which I consumed while burning the oil, and those nice DVD movies.
My friend and partner in many ways - Arnold Ponard , thanks for your unparallel support specially in designing the
PCB. My loving mother, my brother and sisters, thanks for the nice place and food in Baguio City - its been a long
time.
My friends in the Broker Forum in HK and China who helped me to have some electronics parts for free, many
thanks.
Companies who developed solution and tools which I used to finished this Trainer Kits : HP Pavillon DV1000 and
Mio for the notebook PC, Pinnacle for the Studio Video editing, Adobe for the Photoshop and Pagemaker and
SONY Cybershot for the nice microcapture camera.
To 1diyTEAM, maraming salamat.
F. Dandy Menor
Baguio City, 2600
The Philippines
March 2009
Shenzhen City
Guangdong, Republic of China
November 2009

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Preface
When I was working with my AV1000G - an integrated Audio-Video Tone Control with analog Surround and Build-in
Power Amplifier sometimes in 1999, the kiddy are keep watching me and asking me of what I am doing, what keep
me busy day and night working with my toy - the messy wiring, dirty PCB, speakers and small TV, asking me why
not to play with them.
AV1000G - an integrated AudioVideo Tone Control with analog SurJohnny the most spoiled kid took a picture as shown
round and Build-in Power Amplifier
below. He was 2 years old at that time. I could not tell if
he have an interest in what I am doing or simply a bystander.
I remember, he accidentally touch a 220Vac live wire in
the transformer section of my Tone Control. From then
on, he stay away from my Toys.
In 2003, when I first published the STARTER MCUEB8051 Trainer Kit - a learning platform for Programming the Intel 8051 MCU with Assembly Programming
Language, I see him once again so close to what I am
doing - perhaps attracted to those running LEDs. He
said he wanted to learn what I am doing. Well, the other
kiddy are busy playing on the Play Station.
From time to time, I shown to them my Micro Controller STARTER Trainer Kit with Assembly Programming
and ask them to read the Users Manual, I invest a
Samsung notebook PC and provide them one complete
Trainer Kit. They said, reading my book is hard to understand.

1992 University Project

1999 lets make something smaller


2003 let my try the
soldering iron

I let it go!!!!
I just find out that this book is just a demonstration to
their friends and classmates that I wrote the book - it
just stays somewhere in the house and get dirty.
Now is close to the summer of 2009, the kiddy are still
busy with Network Games, PSP, mobile phone, etc. Its
just the lucky time that I have ample time to write a dedicated Trainer Kit for them to learn about the Wonders
of programming the Intel 8051 MicroController Unit. This
time, it is Easier, Faster and I am hoping they have a
wonderful 2009 Summer programming Course.
I wish they are good also in DIY Learning (as good and
addicted as playing network games and PSP) and be a
good influence of Frank Sinatra...............
...................I did it my Way.

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

2003 - STARTER MCU-EB8051-2 Trainer Kit


for the Intel 8051 MCU using the Acebus
IDE - Assembly language

Heart Systems

Preface
Here is the hard core! As I conduct several Seminar in different school offering Electronics/Computer course in
programming, I notice the demand to have a STARTER KIT, like English 101, for those new comer in the world of
programming embedded controller - the Micro Controller Unit or MCU.
More and more student is now in-need of a learning platform in Programming Micro Controller Unit - where their
project is now driven by an intelligent controller (the Micro Controller as the heart and brain). Somewhere, they
need to modify the firmware and/or programmed the Micro Controller according to their likes.
I often see several ads about Seminar - Introduction to Programming Micro Controller - unfortunately, it is still
expensive to my point of view to students, specially to the less fortunate one. I really hope, the MCU community
can provide it for free or at minimum cost which fits the budget of students and Hobbyists, including to provide
software tools and low cost Target boards.
As an alternative of Learning to Program a Micro Controller - the Simple Micro Controller Unit Programming
has been revised, streamline and made Shareware and Courseware to be ----- a Do-It-Yourself Learning platform for
the Intel 8051 Micro Controller and its derivative like the AT89S51 and AT89S52 from Atmel.
The programming language used in this Training Course is BASIC Language - a programming language find by
users to be EASY to understand, implement and use. The BASIC Language use a simple instructions, easy to
understand human programming and commands, not like the Assembly Language - - - and it is Free. You dont
need to buy it, since this is a DEMO software. Thanks to Mark Albert - The Nederlands, for his permission to use
in package here. His website is http://www.mscelec.com. If you like BASCOM 8051 IDE, please avail the registered version. Later, if the 8051 core MCU is too boring for you, you can avail his other IDE called BASCOM AVR
for the AVR core MCU, still from Atmel. The good news is - the Atmel 8051 core AT89S51 or AT89S52 can be
replaced by AVR core MCU like the ATMEGA8515 or ATMEGA162.
The hardware which acts like a motherboard - the Target Board, which run the firmware in real time, is a standard
Trainer Kit being used in Electronics Engineering course in college / Universities in the Philippines and Hobbyist as
a STARTER from zero programing, plus, the newly cooked Target Board - Night Eyes Security Systems 2
(NE2ss) with more stuffs like LCD, Sensor interface and RF Modules.
These hardware were used in the testing, evaluation and debugging of all firmware shared on the CD ROM. Without
the Target Board, it is impossible for me to conclude a real and working firmware. However, it is possible for me to
write the code and run on the Software Simulation on the computer but not in real time execution.
This DIY course - Simple Micro Controller Unit Programming is claimed to be Easy, Affordable (actually free), Fast
and Fun. If you make it right, it brings you Self fulfilling DIY Learning and a lot of adventure.
HAPPY PROGRAMMING !!!!

Warning!!!
SIMPLE Micro Controller Unit Programming is definitely a boring documents to read for professional firmware
developer.
It used both Filipino and English Language.

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

CONTENTS

Who Should Read This


What You Should Know
Inspiration
Acknowledgement
Preface
Table of Content
Chapter 1
Personal Computer Versus Micro Controller Unit
Software Programming ..2
Software and Firmware Programming
..3
PC Versus MCU..4
Why We Need MCU
..6
The Language of MCU ..6
Basic Language for Programming MCU ..7
Hardware Needed for Learning MCU Programming..7
MCU Programmer / Burner
..7
Training Setup ..10
This Chapter
..11
With or Without Hardware
..11
Chapter 2
Introduction To Use the BASCOM 8051 IDE
BASCOM 8051 Basic Operations
..13
BASCOM 8051 Editor ..14
First Program ......15
Simulator Window
..16
Modify the First Program
..23
Modify the First Program Agian ..25
Light 2 LEDs
..27
Improved our Program ..29
Lets Blink the Green LED
..30
Lets Blink All LEDs
..33
End of Program Software Simulation
..34
Chapter 3
Run Your Firmware in the Real Hardware
Lets Connect the Hardware
..36
Hardware Setting
..39
The Real Hardware Response of PROG01.Hex ..43
Run All the created Firmware in Chapter 2
..44

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

CONTENTS

Cha[ter 4
Flow Chart - PDL - Simulation - Program Structure
Flow Chart To Design Program Flow
..47
Software Simulation of PROG02_Blinker_Securiry
..51
Software Simulation - Step into Code
..52
Lack of Instructions
..53Program Description Language
Program Comments
..56

..55

Chapter 5
The World of LEDs & 7-Segments LEd Dislay
Welcome to the World of LEDs ..59
Lets Hit the Road of LEDs
..60
Unlimited Combinations to Light the LEDs
..64
The World of 7-segmetn LEDs ..65
7-Segment Software Simulation ..68
Byte Command to Control the 7-segment LED
..69
Byte Data and Table for the 7-segment Display ..70
Loop Control Instructions
..73
The 2nd Digit of the 7-segment Display ..76
The End of the 7-segment Display
..80
The New Target Board
Welcome to LCD Displaying with NE2ss ..80
Chapter 6
Displaying on the LCD, Input on Tact Switch, Buzzer
Target Board - Our Learning Platform
..81
NE2ss - The New Target Board and Learnng Board
..83
The World of LCD
.. 84
LCD Dislaying PDL
..85
New Setup with NE2ss Target Board
..87
Simulate Exercise 04
..91
Location Control for the LCD Display
..95
Moving Display ..97
Lets Play with the Buzzer

..99

Input - the User Command


..103
Exercise No. 05 - the explanation
ARM and DISarm Keys .. 112
LCD, Buzzer and Switch
..112

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

..110

Heart Systems

CONTENTS

Chapter 7
Interfacing with Rx Module and Motion Sensor
Interfacing Modular Board
..114
Secured Reception from the RF Module
..116
MCU Definition of Signal from the 315 MHz Rx Module
using BITWAIT
...117
using DEBOUNCE
...118
using While..Wend
...119
using If Then ... Else
...120
Which is the Best Detection Method?
..121
using the If Then .. Else
.. 123
Test Method for using the If Then .. Else
Interfacing with Modular Sensor : PIR
..144
Connecting the Motion Sensor to the MCU
.. 145
MCU Detection of Signal from the Motion Sensor
Other Instruction to Detect the Motion Sensor
Motion Sensor Ports
..148
Other Sensors
..148
The Gap Sensor

.. 149

The CLock is Ticking


Part 2 Ends Here

.. 151
.. 152

..116

..124

.. 146
.. 147

[ Keep Continuing ........... ]

myhobby Educational Trainer


Heart
EAF2 Electronics Series
Systems

Heart Systems

CHAPTER 1
The Desktop
Personal Computer (PC)
and the
Micro Controller Unit (MCU)

Chapter 1 will present the :

Hello. I am MCU
your friendly IC

- Desktop PC
- Micro Controller Unit
the difference and use
It discusses the,
Hardware Difference:
- Display
- Processor
- ROM (Read Only Memory)
- RAM (Random Access Memory)
- Storage
- Input / Output
The Tools needed for Programming MCU
- Integrated Development
Environment (IDE)
- Programmer to burn Hex file
- Micro Controller Unit and its dedi
cated motherboard

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

Software
Programming
During my time in the University (in Baguio City),
when I think about Software Programming, I
refer to a Desktop PC Programming and use
BASICA, COBOL, FORTRAN, ASSEMBLY, PASCAL and C language. These are the programming
language available in the 1980s to create a program for Desktop PC running in DOS (DOS = Disk
Operating System). During those days, WINDOWS
operating system is not yet available.

FIGURE 1-3 : Windows Operating System Environment (XP)

Desktop Personal
Computer (PC)
FIGURE 1-1

Laptop or Notebook
Personal Computer
(PC)

: Desktop PC & Notebook PC

There is another Software Programming which


is becoming popular in the Philippines with the availability of Software and hardware Tools. We know in
our Engineering subject as Microprocesor, but
these days some PC base Programming course is
including it as an add-on subject for embedded
controllers. This Software Programming is called
here as : Firmware Programming or simply MCU
programming.
MCU is an abbreviation for Micro Controller Unit.
When I say, MCU Programming, it means you
Program a Micro Controller Unit. The picture in Figure 1-4 is a Microcontroller. Generally, we called it
as IC (Integrated Circuits) or simply uC. To be specific, this IC is a MCU.

FIGURE 1-2 : DOS Operating System Environment


These days, one may think about Software programming is to use Visual Basic, Visual Java, Visual C or C++, Delphi, etc. which is used to create
program to run on WINDOWS XP, ME and the latest Windows Vista and Windows 7 Operating Systems (OS). I like to mention also Linux OS - where
I am a STARTER (its always headache to be a
Starter). There is another OS - the Mac OS or Apple.
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

MCU in different
packages

1
3

1 DIP
2 PLCC
3 TQFP

FIGURE 1-4 : A Micro Controller Unit [MCU]

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

Software and Firmware


Programming
What is the difference? Software Programming
(described in this book) use the PC to create a
program. You can use a Visual Basic, Visual C/
C++, Delphi and other Software to create the program. After creating the program, it will be COMPILED into an EXE file, so it can RUN in Windows.

The Firmware or MCU Programming use the PC


to create a program. You will use a special software to create MCU program. This special program
is called IDE (Integrated Development Environment).
After creating the program, it will be COMPILED
into a HEX or BIN file.

The compiled program is called Software and it is


saved on Harddisk, USB, Diskette or SD card. This
software will only run on a computer in Windows or
DOS.

The compiled HEX or BIN file is called Firmware. It


must be loaded or saved (burned) into the ROM of
the MCU (Read Only Memory). The firmware will
run by the MCU on its hardware - Target Board.

Programmer

Desktop
PC

Save to compiled
Program

Hard Disk

Where to RUN the


compiIed Program

EXE File

Diskette
USB

YOU
You create
software

CD ROM
Use the PC to create a
program and use Visual
BASIC, C/C++ or Delphi
software to create program to run on PC

SD Card

Hello. I am MCU
your friendly IC

Hard Disk

HEX / BIN File

Diskette
YOU
You create
firmware

ROM

USB
CD ROM
Use the PC to create a
MCU program and use
IDE designed for MCU
programming

SD Card

Hardware
Programmer to
SAVE or BURN
the HEX or BIN
file into the ROM
of the MCU

FIGURE 1-5 : Software Programming and Firmware Programming Hardware Configurations


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

PC Versus MCU
A PC is composed of major parts :
- Processor
- RAM Memory
- ROM
- I/O
- Display

A MCU is composed of major parts :

[Pentium IV CPU]
[512 Mbytes]
[80 Gigabytes Harddisk]
[Keyboard, Mouse, USB,
COM Port, Printer,DVD
Drive, PC Cam]
[Flat screen Monitor]

- Processor
- RAM Memory
- ROM
- I/O

[I8051 Core ]
[32 bytes]
[4 Kilobytes]
[32 Input / Output
UART port]

- Display

[LEDs, LCD]

Only Display number and alphabet


[alphanumeric character]

You can watch Movie,


listen to music, make
word processor,
create program, run
your program, Play
Games, etc.

Turn the LED On/Off

Hello. I am MCU
your friendly IC

LCD

LED Light

Monitor

CPU RAM

CPU RAM
Monitor
Mouse
Keyboard

I/O
Motherboard

ROM

Save the program


(Firmware) only
in the ROM

ROM

I/O

Harddisk
DVD
Drive

Save the program


(Software) in any of
these device

SD Card

Integrated Circuit [IC]


Desktop Case
Switch
Mouse

Keyboard

a) Personal Computer (PC)

Sensor
b) Micro Controller Unit (MCU)

FIGURE 1-6 : (a) Desktop Personal Computer (PC) (b) Micro Controller Unit (MCU)
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

Personal Computer versus Micro Controller Unit

Lets see the actual Hardware which made up of a


MCU and compared it to a Desktop PC. When I
say Hardware, it is the Electronic Parts that we
see. A motherboard is a hardware. A USB (Universal Serial Bus) is a hardware. A LCD (Liquid Crystal Display) is also a hardware.
Type

Display

: Chapter 1

The Firmware is the one that makes the Hardware


to work in MCU. A Software is the one that make
the hardware to work in PC. In Figure 1-7, the
MCU is program to Turn ON or Turn OFF the LED.
It is the Firmware that will display the LCD hardware of the sentence : Hello. I am MCU your
friendly IC.

Micro Controller Unit (MCU)

Personal Computer (PC)

Hello. I am MCU
your friendly IC
LCD Display

LED
(Light Emitting Diode)

17 inches Flat Screen Monitor

LCD Display, LED

Type of
Processors

8 bit, 24 MHz 4Kbytes ROM


Microcontroller Unit (MCU)

Switches
Sensors 2

Interface

Temperature
Sensors 2

Sensors 1

Switches, Sensors

Intel (R) Pentium (R) M Processor 1.60 GHz

Mouse

Printer / Scanner

Keyboard

PC
Camera

Mouse, Keyboard, Printer, Scanner, PC Camera

FIGURE 1-7 : Hardware of MCU as compared to the Hardware of a Desktop PC


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

Why we Need MCU?

The Language of MCU

In this Information Age, almost all Electronics devices, like, a simple toys, clock, remote control for
TV / DVD, MP4, Mobile Phone, etc., is running by
an intelligent embedded controller - the MCU.

The Language of MCU is basically 1 and 0, lke


10001110, but human is hard to understqand it. So,
Firware developer developed a more human language like the Assembly Language, yet, it is still
hard to understand (according to some who tried it,
and as experienced by my Kiddy). The book with
complete hardware and software that I wrote in 2003
- as shown in Figure 1-9 uses Assembly Language.

The MCU is programmed to perform a task. We


use MCU, instead of a PC because, a MCU is
smaller and cheaper.
Can you imagine? If your TV/DVD Remote Control
is made up of a Desktop PC - that is too big, you
cannot carry it. So, a MCU is used instead.
A MCU is used when the application is Specific.
For example, A DVD Remote Control or an Tone
control Amplifier remote control, like the one show
in Figure 1-8 is running by a MCU. The use of this
remote control is to adjust the volume - - increase
or decrease, increase the Bass or Treble, power on
or off without necessary to stand up and go nearby
the Tone Control Amplifier.
The specific job of the MCU therefore is to access and control the Tone control Amplifier. Its
Firmware is programmed for Remote Control Applications. Who programmed it? It is the Firmware
Programmer.
So, a MCU, as it name implies, Micro Controller
Unit or MCU, is a small IC that is specially used for
CONTROLLING. We can say therefore that MCU
is a Small Controller as compared to a Desktop
PC or laptop, isnt it?

FIGURE 1-9 : my8051 STARTER


MCU-EB8051 - 2 Trainer Kit
So now, for John, Christian and Christopher who
are still kiddy, I choosed the BASIC Language for
them to explore the wonders of 8051 Microcontroller
programming.
BASIC is simple, easy to use, easy to understand
and fast to follow the syntax.
BASIC - - - for the KIDDY, is for Beginners, which
means Beginners All-purpose Symbolic Instruction Code.

The DATC Remote Control for Tone Control


Amplifier

I am using BASIC language too. I have been using


it since the GW BASIC in 1985 followed by BASICA.
I used to write TURBO BASIC from Borland in the
mid 90s running on DOS. Now comes the VISUAL
BASIC 2008 which runs on Windows OS.

FIGURE 1-8 : Example of Remote Control


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

The other language that can be use to write program for the Intel 8051 MCU is the C Language,
and Pascal and the graphical programming Language
called Ladder works.
These programming Languages are exclusively for
serious Firmware developers. According to the industry, C language is the standard language used
by many professional firmware developer.

BASIC Language for Programming MCU


The BASIC programming for the Intel 8051 MCU is
called BASCOM 8051. It is own and designed by
Mr. Mark Albert - from Holland (The Nederlands).
Please visit his website at :
http://www.mcselec.com
The BASCOM 8051 is a powerful programming Software Tools for the 8051 8-bit MCU. The BASCOM
8051 that runs on Windows 98 and XP is called
BASCOM 8051 IDE. The IDE means, Integrated
Development Environment. BASCOM has also an
IDE for the AVR Micro Controller from Atmel - it is
called the BASCOM AVR IDE.
Figure 1-10 show the BASCOM 8051 IDE on the
Software Simulation environment. It has a Hardware Simulation for LCD, 7-segment and LEDs.

Hardware Needed for


Learning MCU Programming
As in Computer, an Intel Core 2 Duo CPU cannot
run by itself, it need another hardware called
motherboard, it need also a monitor to display the
program, a mouse and keyboard for the user to use
the complete PC.
This is also the same with MCU. The MCU need
also a motherboard, a display like LED or LCD,
and switches. The LED or LCD is used to view the
program response like a monitor. The switches is
used as an input to accept command from the user
- like a keyboard..
So, what Hardware it needed? To run the Firmware
in the real world - means, you can see the LED to
light up or the LCD to display letters and/or numbers, you will need a Target Board. I called it
Target Board to denote to targetted to burn the
firmware you have created in BASCOM 8051 IDE.
Then, you can see your program response by observing the LED : light or no light.
However, it is possible to develop a Firmware without a Target Board by using a special program called
Software Simulator. This Software Simulator program is included in the BASCOM 8051, see Figure
1-10.

BASCOM
8051 IDE
running the
Software
Simulator
Environment

LCD (16x2)
FIGURE 1-10 : BASCOM 8051 IDE - Simulation
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

LED

FIGURE 1-11 : LED and LCD Hardware

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

MCU :
The name of our MCU is called AT89S51, a MCU
manufactured by ATMEL. The 8051 core is the
name of the CPU. See Figure 1-12.
its like saying, the name of the CPU for PC is
Pentium 4. The name of the CPU for the MCU is
8051 core.
Motherboard:
The motherboard of AT89S51 MCU is called
STARTER MCU EB-8051 Trainer Board. The
following denotes the :
STARTER
MCU
EB
8051

= for Beginner
= Micro Controller Unit
= Educational Board
= 8051 CPU

Thus, the STARTER MCU EB-8051 means Educational Board for the 8051 CPU designed for Beginners. It is called Trainer Board because it
uses for Training, practicing to program and running
program. We can call it Trainer Board or Target
Board which simply refer to the one shown in Figure 1-13.

FIGURE 1-12 : The AT89S51 MCU

STARTER MCU-EB80512 Trainer Board

FIGURE 1-13 : The STARTER MCU-EB8051-2 Trainer


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

The STARTER MCU-EB8051 -2 - - motherboard


included the LEDs and dual 7-segment LED Display.
If you program the MCU to light the green LED and
it will not light up, or command the 7-segment LED
to display number 2 and it display zero, it means
that your program has something wrong.
Generally, the LED and the 7-segment display act
as the Monitor or display to see program response.

MCU Programmer/Burner

it is called Flash to denote that it can erased and


save the firmware so fast) and you can only erased
and load again another program by using a Programmer. The Programmer hardware need to be
connected into the PC.
The HS AT89 Flash MCU Programmer comes with
a Tagalog software developed by the Author and
its 8051 circle of friends, that can only run on Windows Environment like Windows 98, Me and XP,
see Figure 1-15. I have not tried to use with Windows Vista and Windows 7. It will not run under
Mac PC (Apple) and Linux Operating System.

There is another hardware needed, called the MCU


Programmer or simply Programmer. This is shown
in Figure 1-14. It is called HS AT89 Flash MCU
Programmer, where :
HS
AT89
MCU

- Heart Systems
- For AT89 series popular
MCU from ATMEL
- Micro Controller Unit

This hardware is used to load or save your firmware into the MCU Read Only Memory (ROM). In
MCU programming, we normally called the
programmign as burn or burn the chip.
The ROM of a MCU is like a diskette which can be
erased and save program (this is called Flash ROM,

FIGURE 1-15 : The HS AT89 Flash Programmer


running on Windows Environment

The HS AT89 Flash Programmer


Hardware
FIGURE 1-14 : The HS AT89 Flash MCU Programmer hardware
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 1

Personal Computer versus Micro Controller Unit

10

Training Setup
1

Target Board + PC
Here is the typical Hardware Setup I recommended
(for the Kiddy) - using the :
1) Samsung Laptop PC
2) STARTER MCU-EB8051-2 Trainer Board
3) HS AT89 Flash MCU Programmer

See Figure 1-16 for the actual setup and Figure 117 for the block diagram.
FIGURE 1-16 : The Hardware System Setup
The Laptop / notebook PC must
run the BASCOM 8051 IDE to
create program - from BAS to
Hex

Laptop /
Notebook PC
1
USB to RS232
Converter (for
burning Hex
file)
USB Cable
(Served as
Power
Supply)

2
3

You must use the HS AT89 Flash MCU


Programmer
USB to RS-232
converter. DO NOT
USE the RS-232
Cable

ISP Cable

STATER MCU-EB8051-2
Trainer Board : Motherboard

FIGURE 1-17 : Block Diagram of the Kiddy Training Hardware


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

Personal Computer versus Micro Controller Unit

This Chapter
What is presented in this Chapter are :
[1]

Introduction to Firmware Programming as


compared to Software Programming

[2]

Introduction and use of Micro controller Unit


- popularly called MCU

[3]

The MCU difference with a Personal Desktop or notebook PC

[4]

The Software and Hardware it needed if you


want to program a MCU.
Hardware :
1) STARTER MCU-EB8051-2 with
the AT89S51 MCU
2) HS AT89 Flash MCU Programmer
Software:
1) BASCOM 8051 IDE DEMO version
with 4Kbytes limitation
2) HS AT89 Flash MCU Tagalog Windows
Software

Hardware

: Chapter 1

Software

11

With or Without the


Hardware
This query is brought into my attention via email,
specifically asking if, someone would like to Learn
MCU Programming but he/she dont have the actual Hardware (1) Target Board and (2) Programmer.
Yes, it is possible to Learn Firmware Programming
- definitely!!!!
But, there is a restriction. You cannot see the actual response of your program in the real world and
real time execution.
Ibig kung sabihin ng real world ay ang nakikita
mismo ng mga mata natin kung paano iilaw ang
LED o mag-display ng number o letter ang LCD.
Kung walang Target Board, ang maari mo lang gawin
ay Simulation. Ang BASCOM 8051 IDE ay
mayroon built-in Software Simulator at kasama na
dito ang Hardware Simulation ng LEDs, LCD at 7segment LEd Display.

Heart Systems 8051 MCU


Programmer
Features:

Hardware Upgrade

- Programming with 8051 core 40 and 20 pins :


AT89C51, AT89C52, AT89C55, AT89S51, AT89S52,
AT89S8252, AT89S8253, AT89LV51, AT89LV52, AT89LV52,
AT89C1051, AT89C2051, AT89C4051
- ISP capable via cable
- ZIF programming via 40 pins and 20 pins
- Windows Environment Control Software on PC
- COM 1 to 5 selection
- USB function via USB to RS232 conversion cable
- USB power or System Power (+5V)

Heart Systems
Electronic Center

http://www.1diycentre.com
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

12

CHAPTER 2
The BASCOM 8051
Integrated Development Environment
(IDE)

Chapter 2 will presents the :


- BASCOM 8051 Software
- Editor Function
- Simulator Function
+5V

It discusses the,
R
560K

AT89S51

GREEN
RED
K

Pin 40
[ Logic 1 ]
[ P1.0 = 1 ]

VCC
+5V

SWITCH Pin 20

Pin 1 P1.0

+5V
GND

0V
Logic 1 for 2 Seconds

myhobby Educational Trainer


Heart
EAF2 Electronics Series
Systems

Editor:
- Writing Program
- Openning a New Program
- Basic Instructions
- Saving a Program
- Compiling a Program
Simulator:
- Openning the Simulation
- Running the Simulator
- Interpert the Simulator Result
- Program Tracer
- Step into Code

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

13

BASCOM 8051 Basic


Operations
Opening the BASCOM

Gaya ng ibang Widows program, ang BASCOM


8051 IDE ay makikita ang icon sa desktop PC.
Tingnan ang Figure 2-1. Heres how to open.
[1]

Itutok ang mouse pointer sa BASCOM icon


at saka double click (double tap on the
mouse pad.

MOUSE

FIGURE 2-2 : Loading the BASCOM 8051 IDE


Demo Version on Windows

Itutok ang Mouse


saka double click
FIGURE 2-1 : Openning the BASCOM 8051
Pagka double click ang mouse, makikita na tatakbo
ang BASCOM 8051 IDE. Tingnan ang Figure 2-2.
Tandaan po na ito ay DEMO version - at FREE ito,
hindi na kailangan pang bilhin. Ito ay mayroon
limitasyon na maka-compile lang ng program
hanggang 4Kbyte of Bin file. Pero sa akin na
medyo serious firmware developer, bumili ako ng
registered BASCOM 8051 na puwede mag-compile
ng malaking program gaya ng 32 Kbytes ng bin file
o mas malaki pa.
Pagkatapos, ang BASCOM 8051 IDE ay tumatakbo
na sa Windows. See Figure 2-3.
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

FIGURE 2-3 : BASCOM 8051 running on Windows

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

14

BASCOM 8051 EDITOR


Window
Ang BASCOM 8051 ay may dalawang Window :
1. Editor Window
2. Simulator Window
Ang makikita sa Figure 2-3 ay ay Editor Window.
Tinawag na Editor kasi, diyan magsusulat ng program. Puwedeng mag-edit, copy, paste at saka
compile ng MCU program.

Ang subdirectory ay isang lugar o isang Folder sa


iyong computer hard disk, na parang maleta o
aparador para doon ilagay ang mga program files.
Heto ang paggawa.
[1]

Ang isang Window na kagaya ng Figure 1-10 at


Figure 2-16 ay ang Simulator Window.

Itutok ang mouse sa My Computer, saka


click ang left mouse. Tingnan ang Figure 24. Pagka double click ng mouse, lalabas
ang My Computer Explorer, tingnan ang
Figure 2-5.

Tinawag na Simulator Window dahil sini-simulate


niya o ginagaya ang function ng MCU. Ang kanyang
purpose ay para ma-ipakita kaagad ang output o
response ng program kahit hindi pa ito inilalagay sa
Target Board. Makikita natin iyan mamaya.

Creating a Subdirectory
For Program Exercises
Bago simulan ang Programming course, gagawa
muna ng Subdirectory o isang FOLDER sa computer Hard Disk para doon i-save ang mga Program
exercises na iyong gagawin.

FIGURE 2-5 : My Computer Explorer


[2]

Mamili ng hard disk kung saan gagawa ng


Subdirectory. Halimbawa, pinili mo ang
Drive D. Itutuk ang Mouse sa drive D saka
Left double Click, Makikita ang loob ng Drive
D, tingnan ang Figure 2-6.

itutuok
ang
mouse sa
My Computer
saka left - double
click

FIGURE 2-4 : Openning My Computer


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Ito ay
isang
halimbawa
ng
nilalaman
ng Drive D
disk, ang
iyong Drive
D ay
magka iba
ang laman
FIGURE 2-6 : Content of my Drive D

Heart Systems

Trainer
No. 05

[3]

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

Right click New - Folder, saka Right Click.


See figure 3-7. Please type BASCOM8051
then press the enter Key.

15

Dito sa directory na :
BASCOM8051 / myPROGRAM / 1..PROGRAM 01
i-save ang mga program na gagawin.

First PROGRAM
i-type ang
BASCOM8051

KIDDY very first Program :


Exercise No 1 - PROGRAM 01
The action......... start here!

FIGURE 3-7 :

Drive D - New Folder

Ang bagong Directory na BASCOM8051 ay nagawa


na sa Drive D. See Figure 2-8.

Ang susunod na gagawin ay gagawa ng Program


sa Editor Window. Ang gagawin ng program ay
Pailawin ang LED. Alin LED?
Ang Green LED sa Target Board, tingnan ang Figure 2-9. Ang pangalan ng LED (Light Emitting Diode) na ito ay LED1. Yes! Hindi iilaw ang LED1
kung hindi uutusan (i-program) ang MCU.

This the
GREEN LED
(LED1)

Bagong
Directory
BASCOM8051

FIGURE 2-8 :

BASCOM 8051 Directory

Hindi pa tapos ang paggawa ng Directory, kasi ang


ginawa niyo lang ay ang main directory. Gagawa
pa kayo ng Folder sa loob ng BASCOM8051 directory kung saan niyo ilalagay ang Program exercises
niyo.
Sa Loob ng BASCOM851 na Directory, gagawa pa
ng Folder na myPROGRAM. At sa loob ng
myPROGRAM ay gagawa kayo uli ng Folder na
1..PROGRAM 01.
myhobby Educational Trainer
Heart
EAF2 Electronics Series
Systems

FIGURE 2-9

: The GREEN LED on the STARTER


MCU-EB8051-2 Trainer Board

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

Introduction To Use the BASCOM 8051 IDE

Openning the
NEW PROGRAM

[2]

16

Writing Program : PROG 01


Isulat ang mga Instruction na :

KIDDY, balikan ang BASCOM 8051 IDE sa iyong


PC screen :
[1]

: Chapter 2

Itutok ang mouse sa FILE, saka click ang


left mouse

P1.0 = 0
END
Tingnan ang Figure 3-12.

Itutok ang mouse sa NEW, saka click ang


left mouse. Tingnan ang Figure 2-10.

Pagka click mo ng mouse, may lalabas na bagong


Window. Tingnan ang Figure 2-11.

P1.0 =0
END

FIGURE 3-12 : Program content of PROG 01

Description of MCU Program


Ang ibig sabihin ng program ay, pa-iilawin nito ang
LED1 na naka-kabit sa paa ng MCU na P1.0.
Ang instruction na:
FIGURE 2-10 : Opening NEW File

P1.0 = 0
ay ilalagay ang P1.0 sa Logic 0. Kung baga, uutusan
nito na ang P1.0 switch na ikabit sa GND.

Bagong
Window
noname 1 ang
pangalan ng
bagong window

Ang instruction na :
END
ay ilalagay sa huli, pagkatapos lahat ng Instruction
para sabihin sa BASCOM 8051 IDE software na,
ito na ang katapusan ng Program.
Simple diba?

FIGURE 2-11 : New Editor Windows


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Tandaan na, ang mga instruction o command sa


MCU ay P1.0=0 at saka END.

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

SAVE your PROG 01

Ang susunod na gagawin ay i-save ang program


sa directory :

17

i-click ang
icon na ito

BASCOM8051 / myPROGRAM / 1..PROGRAM 01


Ang pangalan ng program ay : PROG01
Makikita sa Figure 2- 13 sa loob ng Windows Explorer, na nakasulat na ang PROG01. Makikita rin
na ang TYPE ng program na ito as BAS.

program
extension
program
name

program
size

FIGURE 2-14 : Compile Program


Pagka click sa mouse, tingnan ang directory na :

FIGURE 2-13 : Saving PROG 01

COMPILING your PROG 01


Ang susunod na gagawin pagka-save ng program
ay Compilation.
Ang ibig sabihin ng Compilation o Compile sa MCU
programming ay i-convert (isalin) ang BAS file sa
HEX file. Kasi ang PROG 01.bas ay hindi naiintindihan ng MCU. Ang nai-intindihan lang ng MCU
ay ang PROG.HEX file o kaya PROG.Bin File - ito
ang FIRMWARE na i-save (burn) sa MCU ROM.
Gagamitin ang HS AT89 Flash MCU programmer
para sa pag-burn ng Firmware PROG01.HEX sa
ROM ng MCU.

BASCOM8051 / myPROGRAM / 1..PROGRAM 01


at mayroon makikita na bagong files. Tingnan ang
Figure 2-15. Ang HEX file (PROG01.HEX) ay ang
converted HEXADECIMAL file ng BASCOM program
na PROG 01.bas.
Itong PROG01.hex na ito ang i-program o i-save sa
ROM ng MCU.

Tingnan ang Figure 2-14 kung paano ang pag-compile ng program. Itutok ang mouse sa icon at saka
I-click ang LEFT mouse key.
Puwede din na pindutin ang funtion F7. Ang F7
ang short-cut key para sa Compilation.
FIGURE 2-15 : Compiled HEX File
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

Congratulations KIDDY!!!!!
Mayroon na Firmware - PROG01.Hex. Ang
susunond ninyong gagawin ay gamitin ang Simulator Window, tingnan ang Figure 2-16.

18

i-click ang
icon na ito

Simulator Window
Simulate your First Program
PROG01
Balik tayo sa BASCOM 8051 IDE editor window,
[1]

i-click ang Simulator icon, makikita sa Figure 2-15A.

Ito ang mga instruction ng PROG01. Sa


pamamagitan ng instruction, mauutusan
ang MCU.

Pagka click ang icon, mayroon lalabas na bagong


Window, tingnan ang ang Figure 2-16. Ito ang Simulation Window.
[2]

I-click ang LCD icon at lalabas ang Hardware Simulator Window tingnan ang Figure 2-16.

Makikita ang mga LED. Itong mga LED na ito ay


kunwari naka-kabit sa mga paa ng MCU.

Click
LCD

this

Copy of
your instruction

FIGURE 2-15-A : Simulator Icon to activate the


Simulator Software function of
the BASCOM 8051 IDE

Hardware Simulation
Window

Ang LED na ito ay


naka-connect sa
paa ng MCU na
ang pangalan ay
P1.0
Ito ang mga LED na naka-kabit
sa MCU pins

FIGURE 2-16 : Simulator Window


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

19

Run the Simulator :


Tingnan mabuti ang mga LED sa Hardware Simulator Window lalu na ang P1.0 LED - Tingnan ang
Figure 2-17.
[1]

i-click ang PLAY icon, tingnan ang Figure 218

Pag-ka-click ang PLAY icon makikita na ang P1.0


LED ay magiging Dark Red. Ang P1.0 ay short
name ng Port 1 Bit 0, kadalasan binibigkas na Pee
-one-dot-zero.
FIGURE 2-18 : PLAY Icon

This is
Port 1

This is
Port 0

This is
Port 3
Ang P1.0 (pin1) na paa ng MCU ay
parang naka-connect sa LED sa
Simulator.

P1.0

P1.1

P1.6

P1.0

Port1

Ang P1.7
(pin8) na paa
ng MCU ay
parang
naka-connect
sa
P1.7 LED sa
Simulator.
Port3

1 P1.0
2 P1.1
3 P1.2
4 P1.3
5 P1.4
6 P1.5
7 P1.6
8 P1.7
9
10
11
12
13
14
15
16
17
18
19
20

40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

7
Port0

6 5

4 3 2 1

P1
P1.0 (Pin 1)
P1.1 (Pin 2)
P1.2 (Pin 3)
P1.3 (Pin 4)
P1.4 (Pin 5)
P1.5 (Pin 6)

Port2

P1.6 (Pin 7)
P1.7 (Pin 8)

FIGURE 2-17 : How the Display windows represent the port of MCU
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

20

makikita na ang P1.0 LED ay magiging DARK


RED pagka pindut sa PLAY. Ibig sabihin na
ang LED ay umilaw

Ang signal sa LED ay Logic 1 [P1.0 =1],


ito ang signal bago tatakbo ang Program.
volt
volt
P1.0=1

5
0

Ang signal sa LED ay Logic 0


[P1.0 =0]

time
Ang signal waveform output ng
P1.0 kung ito ay Logic 1, bago
tatakbo ang PROG01

FIGURE 2-19 :

P1.0=1
P1.0=0

time
Ang signal waveform output ng P1.0 kung ito ay
Logic 0, pagkatapos tumakbo ang PROG01

PROG 01 execution result, how the P1.0 LED respond to the program

Result of the Simulator

Verdict PROG 01

Makikita sa Hardware Simulator Window - na ang


P1.0 LED ay magiging Dark Red kung pindutin ang
PLAY Icon. Ibig sabihin nito na, ang program ay
gumagana. Ang instruction na

Mga KIDDY, ang pag-aaral ninyo sa PROG01 ay


successful.

P1.0 = 0

[1]

paano magsimula na gagawa ng program


sa BASCOM 8051.

[2]

magsimulang magsulat ng program at


gagamiting ang instruction na P1.0 = 0 at
saka END.

[3]

mag-compile ng program para i-convert ang


BAS file program sa Hex File.

[4]

paano patakbuhin ang HEX file sa SIMULATOR at basahin ang response ng Simulator
mula sa Hardware Simulation Window.

ay inu-utusan and MCU para mag OUTPUT ng Logic


0.
Kung may mali sa PROG01, hindi sana iilaw ang
P1.0 LED o gagana ang P1.0
Ok mga KIDDY, simple diba? Yes po! Iyan lang
ang ginawa ng PROG01 - pailawin lang ang LED1
(Green na LED). Ang LED1 ay naka-connect sa
P1.0 sa MCU.
Congratulation dahil successful ang iyong kaunaunahang Program.
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Ang mga napag-aralan ninyong ito ay gagamitin


ninyo sa buong pag-program ng MCU. Huwag
ninyong kalilimutan mga KIDDY, ok!

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

PROGRAM
TITLE

: PROGRAM 01
: First Program - Turn On the GREEN LED

SCHEMATIC

: simplified Schematic as shown below

HARDWARE

: STARTER MCU-EB8051-2 Trainer Board

21

DESCRIPTION : This program will turn on the LED [Green LED on the Trainer Board]. Once it light
the LED it will light forever. It can only be turn off when the DC power is remove.
This LED is connected into Port 1, bit 0 [P1.0]
OBJECTIVE

: To learn :
- how to Turn On the LED using MCU control.
- the program command, syntax and instruction to switch the MCU from logic 1 to
logic 0 level
- how to use the BASCOM Simulator and compare it to the real output in real
hardware
R6 = 560 Ohms
Current
LED1
Green

1
P1.0

volt

P1.0=1
P1.0=0

time
Ang signal waveform output ng
P1.0 Bago tatakbo ang program,
ang P1.0 ay logic 1. Pagka-takbo
ang program ang P1.0 ay maging
logic 0.
Ang P1.0 = 0 ay Logic 0.

GND

40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Logic 1
P1.0 SWITCH
Logic 0

IC1
AT89S51 MCU

39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

VCC

5V
500 mA

Ang P1.0 = 1 ay Logic 1

FILE NAME

PROG01.bas

Exercise N0. 1 : Program to Turn on the GREEN LED of the STARTER MCU-EB8051-2 [1/2]
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

22

PROGRAM CODE :

P1.0 =0
END

EXPECTED OUTPUT :

CONCLUSION :

The P1.0 LED will turned into DARK RED when the Program is executed.
This changed of color of the P1.0 LED will demonstrate that the output will change
from Logic 1 to logic 0. This means, the instruction P1.0 = 0 is executed
correctly by the MCU - to put the P1.0 (Port 1 bit 0) to output a Logic 0 causing
to light up the Green LED (LED1) on the STARTER MCU-EB8051-2 Trainer
Board (Target Board).

Exercise N0. 1 : Program to Turn on the GREEN LED of the STARTER MCU-EB8051-2 [2/2]
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

23

Modify the First


PROGRAM
PROG01_LED2 light

LED2

Lets modify PROG01. Ang gagawin ngayon ay


pailawin ang LED2, iyong red LED na kasunod ng
Green LED (LED1). Tingnan ang Figure 2-20 kung
saan matatagpuan ang LED2.
Makikita sa Figure 2-21 ang simplified schematic
diagram, na ang LED2 LED ay naka connect sa
P1.1 (Port 1 Bit 1) o di kaya sa pin 2 ng MCU.
FIGURE 2-20 : Location of LED2 - Red LED

R7 = 560 Ohms
Current

ILED
LED2
Red
P1.0
P1.1

volt

5
0

P1.2

P1.1=1
P1.1=0

time
Ang signal waveform output ng
P1.1

GND

1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

40
2

Logic 1
P1.1 SWITCH
Logic 0

IC1
AT89S51 MCU

39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

VCC

5V
500 mA

FIGURE 2-21 : Simplified Schematic on how LED2 is connected into the MCU
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

24

Balik sa editor window ng BASCOM 8051.


Palitan ang instruction na

[1]

P1.0 = 0 ----->> P1.1 = 0


Tingnan ang Figure 2-22
I-save ang Program sa

[2]

BASCOM8051 / myPROGRAM / 1..PROGRAM01


Ang pangalan ng program ay :
PROG01_LED2 light
[3]

Press F7 (Compile the program)

[4]

Point the mouse on the Software Simulation icon, click it. See Figure 2-15-A.

[5]

Point the mouse on the LCD icon, click it.


See Figure 2-16.

FIGURE 2-22 : Changing instruction


[6]

Point the mouse on the Play icon, click it.


See Figure 2-18.

[7]

See the LED - Hardware Simulation as


shown in Figure 2-23.

Makikita dito sa Software Simulation


Display (Hardware Simulation) na ang
kasunod na LED ng Green LED
(LED1) ay iilaw - ito ay magiging dark
Red. Itong LED (LED2) ay naka
connect sa paa ng MCU na P1.1.

Ang P1.1 = 0 na instruction ang siyang


magpapa-ilaw sa LED2.
volt
5
0

volt
P1.1=1

time
Ang signal waveform output ng P1.1 kung ito
ay Logic 1, bago tatakbo ang PROG01_LED2
light

5
0

P1.1=1
P1.1=0

time
Ang signal waveform output ng P1.1 kung ito ay
Logic 0, pagkatapos tumakbo ang PROG01_LED2
light

FIGURE 2-23 : Hardware Simulation response after running the PROG01_LED2 light program
myhobby Educational Trainer
Heart
EAF2 Electronics Series
Systems

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

25

Modify the First


PROGRAM Again
PROG01_LED5 light
Lets further modify PROG01. Ang gagawin ngayon
ay paiilawin ang LED4. Tingnan ang Figure 2-23
kung saan matatagpuan ang LED5
Makikita sa Figure 2-24 ang simplified schematic
diagram, na ang LED5 LED ay naka connect sa
P1.4 (Port 1 bit 4) o pin 5 ng MCU.
Ok! so guess niyo na ba ang instruction na
gagamitin? Yes! It is P1.4 = 0.

FIGURE 2-23 : Location of LED5 - Red LED

R10 = 560 Ohms


Current
LED5
Red
P1.0
P1.1

volt
P1.4

5
0

P1.4=1
P1.4=0

time
Ang signal waveform output ng
P1.1

GND

1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

40

Logic 1
P1.4 SWITCH
Logic 0

IC1
AT89S51 MCU

39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

VCC

5V
500 mA

FIGURE 2-24 : Simplified Schematic on how LED5 is connected into the MCU
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

26

Balik sa editor window ng BASCOM 8051.


Palitan ang instruction na

[1]

P1.1 = 0 ----->> P1.4 = 0


Tingnan ang Figure 2-25
I-save ang Program sa

[2]

BASCOM8051 / myPROGRAM / 1..PROGRAM01


Ang pangalan ng program ay :
PROG01_LED5 light
[3]

Press F7.

[4]

Point the mouse on the Simulate Program


icon, click it. See Figure 2-15-A.

[5]

Point the mouse on the LCD icon, click it.


See Figure 2-16.

FIGURE 2-25 : Changing instruction


[6]

Point the mouse on the Play icon, click it.


See Figure 2-18.

[7]

See the LED - Hardware Simulation as


shown in Figure 2-26.

Makikita dito sa Software Simulation


Display (Hardware Simulation) na ang
LED5 ay iilaw - ito ay magiging dark
Red. Itong LED (LED5) ay naka
connect sa paa ng MCU na P1.4.

Ang P1.4 = 0 na instruction ang siyang


magpapa-ilaw sa LED5.

volt
5
0

volt
P1.4=1

time
Ang signal waveform output ng P1.4 kung ito
ay Logic 1, bago tatakbo ang PROG01_LED5
light

5
0

P1.4=1
P1.4=0

time
Ang signal waveform output ng P1.4 kung ito ay
Logic 0, pagkatapos tumakbo ang PROG01_LED5
light

FIGURE 2-26 : Hardware Simulation response after running the PROG01_LED5 light program
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

27

LIGHT 2 LEDs
PROG01_LED1_4 light
Subukan natin na pailawin ang dalawang LEDs na
mag-kasabay ang LED1 (Green LED) at saka LED4
(Red LED). Tingnan ang Figure 2-27 kung saan
matatagpuan ang LED1 at saka LED4.
Makikita sa Figure 2-28 ang simplified schematic
diagram. Ang LED1 ay naka-connect sa P1.0 at
ang LED4 ay naka connect sa P1.3 (Port 1 Bit 3).
Here is the TIP: Kailangan na dalawang instructions ang i-declare, kasi, dalawang LEDs ang
paiilawin.

FIGURE 2-27 : Location of LED1 and LED4

R8 = 560 Ohms
R6 = 560 Ohms

LED4
Red

LED1
Green
P1.0
P1.1
P1.3

GND

40

2
3 4

39
38
37
36
35
34
P1.0 33
Logic 0 32
31
30
29
28
27
26
25
24
23
22
21

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

P1.3
Logic 0

VCC

5V
500 mA

IC1
AT89S51 MCU
FIGURE 2-28 : Simplified Schematic on how LED1 and LED4 are connected into the MCU
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

28

Balik sa editor window ng BASCOM 8051.


Palitan ang P1.4 na instruction ng P1.0

[1]

Idagdag ang P1.3 = 0


Tingnan ang Figure 2-29
I-save ang Program sa

[2]

BASCOM8051 / myPROGRAM / 1..PROGRAM01


Ang pangalan ng program ay :
PROG01_LED1_4 light
[3]

Press F7.

[4]

Point the mouse on the Simulate Program


icon, click it. See Figure 2-15-A.

[5]

Point the mouse on the LCD icon, click it.


See Figure 2-16.

FIGURE 2-29 : P1.0 and P1.3 Instructions


[6]

Point the mouse on the Play icon, click it.


See Figure 2-18.

[7]

See the LED - Hardware Simulation as


shown in Figure 2-30.

Makikita dito sa Software Simulation


Display (Hardware Simulation) na ang
P1.0 (LED1) at saka P1.3 (LED4) ay
iilaw na sabay - ito ay magiging dark
Red.

Ang P1.0 = 0 at saka P1.3 na instructions


ang siyang magpapa-ilaw sa dalawang
LED na P1.0 at saka P1.3.
volt
5
0

volt
P1.4=1, P1.3 = 1

time
Ang signal waveform output ng P1.0 at saka
P1.3 kung ito ay Logic 1, bago tatakbo ang
PROG01_LED1_4 light

5
0

P1.0=1, P1.3=1
P1.0=0, P1.3=0

time
Ang signal waveform output ng P1.0 at saka P1.3
kung ito ay Logic 0, pagkatapos tumakbo ang
PROG01_LED1_4 light

FIGURE 2-30 : Hardware Simulation response after running the PROG01_LED1_4 light program
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

29

IMPROVE our Program


PROG01_LED1_4 light
Ang tawag natin sa mga hardware na naka-connect
sa MCU ay P1.0 LED o LED1, P1.3 LED o LED4.
Mayroon instruction para madaling matandaan
(bilang Firmware Developer) ang mga hardware na
kino-control na gaya ng LED.
[1]

Gamitin ang Instruction na ALIAS, ideclare ang pangalan ng hardware na


naka-connect sa mga paa ng MCU, gaya
ng :
GREEN_LED1 Alias P1.0
RED_LED4

[1]

Alias P1.3

Pagkatapos, gamitin ang kanilang Alias


name na:
GREEN_LED1 = 0
RED_LED4

[3]

FIGURE 2-31 : P1.0 and P1.3 Instructions using


their ALIAS name

[7]

Point the mouse on the Play icon, click it.


See Figure 2-18.

[8]

See the LED - Hardware Simulation as


shown in Figure 2-30. Dapat parehas ang
kalalabasan. Pareho na iilaw ang LED1 at
saka LED4 na LEDs.

= 0

I-save ang Program sa


BASCOM8051 / myPROGRAM / 1..PROGRAM01

Ang paggamit ng ALIAS na instruction ay para


madaling matandaan ang pangalan ng hardware
na kino-control. Ang format ay :
Name of the Hardware ALIAS Py.x

Ang pangalan ng program ay :

Kung sann nakaconnect ang hardware.

PROG01_LED1_4 AliasName
[4]

Press F7.

[5]

Point the mouse on the Simulate Program


icon, click it. See Figure 2-15-A.

[6]

Point the mouse on the LCD icon, click it.


See Figure 2-16.

Ang pangalan ng
hardware na nakaconnect sa Py.x

y = Port Number
x = Bit Number
y = 0~3
x = 0~7
ex. Py.x
P1.0
P2.7

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

30

Lets BLINK the GREEN


LED1
PROG01_LED1 Blinking
Ang susunod na gagawin ay BLINKING ang GREEN
LED habang ang RED LED4 ay naka-steady light
lang.
Ang BLINKING na LED ay patay sindi na LED,
iilaw at saka hindi iilaw - - paulit ulit.
Alam niyo na ang pag-control sa LED upang umilaw
ay gamitin ang P1.0 = 0, ibig sabihin na kung pailawin ang Green LED ay ilagay sa Logic 0 kaya
naman ang P1.0 = 0. Kung hindi paii-lawin ang LED,
ito ay - - - - - - - - P1.0 = 1

Logic 1

Kaya, kung ang gustong gawin sa LED1 (ang LED


na naka-connect sa P1.0) ay patay-sindi, i-control
ang P1.0 na mag-output ng signal na logic 1 at saka
logic 0 - paulit-ulit. Ang effect nito ay BLINKING.
Tingnan ang Program :

FIGURE 2-32 : Blink the P1.0 LED1 and Light


steady the P1.3 LED4

GREEN_LED1 Alias P1.0


RED_LED4
Alias P1.3

Ang pangalan ng LED na naka-connect sa P1.0 ay


GREEN_LED1, RED_LED4 naman ang sa P1.3

RED_LED4 = 0

Pailawin ang RED_LED4, i-command na mag-output ng logic


zero ang P1.3.
Instruction na ibig sabihin ay gawin mo ang mga sumusonod

Do
GREEN_LED1 = 0
Waitms 100
GREEN_LED1 = 1
Waitms 100
Loop
End
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

i-command na mag-output ng logic zero ang P1.0 para iilaw


ang GREEN_LED1
maghintay ng 100 miliSecond. Ang instruction na ito ay
para makita na iilaw ang GREEN_LED1
i-command na mag-output ng logic 1 ang P1.0 para hindi
iilaw ang GREEN_LED1
maghintay ng 100 miliSecond. Ang instruction na ito ay
para makita na hindi-iilaw ang GREEN_LED1
Instruction na ka-partner ng Do, ibig sabihin, balik ka at iexecute mo uli ang mga instructions mulas sa Do.
Ending ng Program

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

Introduction To Use the BASCOM 8051 IDE

Balik sa editor window ng BASCOM 8051.


[1]

: Chapter 2

31

I-save ang Program sa

[2]

Isulat ang mga instruction na :


GREEN_LED1 Alias P1.0
RED_LED4
Alias P1.3

BASCOM8051 / myPROGRAM / 1..PROGRAM01

RED_LED4 = 0

Ang pangalan ng program ay :

Do

PROG 01_LED1 Blinking


GREEN_LED1 = 0
Waitms 100
GREEN_LED1 = 1
Waitms 100

Loop
End

[3]

Press F7.

[4]

Point the mouse on the Simulate Program


icon, click it. See Figure 2-15-A.

[5]

Point the mouse on the LCD icon, click it.


See Figure 2-16.

[6]

Point the mouse on the Play icon, click it.


See Figure 2-18.

[7]

See the LED - Hardware Simulation as


shown in Figure 2-33.

Ang LED4 ay steady Light, naging


DARK Red.

volt

Ang LED1 ay Blinking, nagiging Red at


saka DARK Red.

P1.4=1
P1.4=0

time
Ang signal waveform output ng P1.3 kung ito
ay Logic 0, pagkatapos tumakbo ang
PROG01_LED1 Blinking
volt

volt
5
0

P1.3=1
P1.0 = 1

time
Ang signal waveform output ng P1.3 at P1.0
kung ito ay Logic 1, bago tatakbo ang
PROG01_LED1 Blink

5
0

P1.0=1

P1.0=1
P1.0=0

P1.0=1
P1.0=0

time
Ang signal waveform output ng P1.0 kung ito
ay Logic 0 at 1, pagkatapos tumakbo ang
PROG01_LED1 Blinking

FIGURE 2-33 : Hardware Simulation response after running the PROG01_LED1Blinking program
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

32

Makikita dito ang PROGRAM


TRACER na tumatakbo paulit-ulit
sa loob ng Do---- Loop instructions.
Kung ang Program Tracer ay nasa
GREEN_LED1 = 0
Waitms 100
naka-ilaw ang LED1

To STOP the program execution,


put the mouse unto this icon and
LEFT click it
To RUN the program, press again
this icon

Kung ang Program Tracer ay nasa


GREEN_LED1 = 1
Waitms 100
naka-patay ang LED1

FIGURE 2-33 : Hardware Simulation response after running the PROG01_LED4 light program
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

33

LETs BLINK all LEDs


PROG01_All Blinking
Ang susunod ay gawin naman BLINKING ang lahat
ng LEDs, 7 na RED at saka isang GREEN, total 8
LEDS.
May dalawang solution. Ang unang solution ay gaya
ng ginagawa natin na , Solution 1 :
P1.0 =
P1.1 =
P1.2 =
P1.3 =
P1.4 =
P1.5 =
P1.5 =
P1.7 =

0
0
0
0
0
0
0
0

at saka P1.0 = 1
at saka P1.1 = 1
at saka P1.2 = 1
at saka P1.3 = 1
at saka P1.4 = 1
at saka P1.5 = 1
at saka P1.6 = 1
at saka P1.7 = 1

Tingnan ang program sa Figure 2-34. Ang tawag sa


ganitong pag-control ng LED ay BIT Command,
kasi, isa-isang kino-control ang bawat LED sa Port
1.
Save and compile the program as
PROG01_All_LED blinking_1.

FIGURE 2-34 : Solution 1 Program to blink all the


LED

Ang solution 2 ay gagamit ng BYTE Command, para


minsanan lang magbigay ng command sa Port 1
kung saan naka-kabit ang 8 LEDs (LED1 ~ LED7).
Save and compile the program as PROG01_All_LED
blinking_2.
KIDDY, nakita niyo ba ang pagkaka-iba ng dalawang
Command? Ang BIT Instruction ay isa-isang i-control ang bawat Bit-port ng MCU Port 1, gaya ng
P1.0.
Ang BYTE Instruction naman ay sabay-sabay ang
walong Bit-port ng MCU Port 1 na i-control.
Makikita niyo ang instruction na :
ALL_LED = 255
Bakit hindi 1? Kasi, sa BYTE instruction, ang
value ng Port 1 kung ito ay naka logic 1 ay 255,
kasi sabay sabay na naka-logic 1 ang 8 na Bit-port
(P1.0, P1.1, P1.2, P1.3, P1.4, P1.5, P1.6 at saka
P1.7).
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

FIGURE 2-35 : Solution 2 Program to blink all the


LED

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 2

Introduction To Use the BASCOM 8051 IDE

34

Ang response ng solution 1 at saka solution 2 ay


parehas lang. Tingnan ang Figure 2-36. Makikita
dito na sabay-sabay ang Blinking ng 8 LEDs.

volt
P1=1

5
0

time
Ang signal waveform output ng P1 kung ito ay
Logic 1, bago tatakbo ang PROG01_All_LED
Blinking

volt

All LEDs are blinking, nagiging Red


(logic 1 - LED OFF) at saka DARK
Red (Logic 0 - LED ON)

P1=1

P1=1
P1=0

P1=1
P1=0

time
Ang signal waveform output ng P1 kung ito ay
Logic 0 at 1, pagkatapos tumakbo ang
PROG01_All_LED Blinking

FIGURE 2-36 : Hardware Simulation response while running the PROG01_All_LED Blinking program

End of Program Software


Simulation On LEDs
Napag-aral na kung paano sumulat ng Program,
mag-compile ng program para ang BASCOM 8051
program na BAS file ay magiging HEX file, at paano
gamitin ang Software Simulation function ng
BASCOM 8051 IDE.

Pero, ang gusto ko ay mai-pakita sa mga KIDDY


na, ang program na ginawa ninyo ay tumatakbo
talaga sa motherboard - doon sa totoong Target
Board. Makita mismo na iilaw ang mga LEDs na
kino-control at mag-blink ang LED.

Napag-aralan na - - kahit wala pa ang Target Board


ay maari nang i-test ang program.

Iyan ang gagawin natin sa Chapter 3. We will continue to have more and detailed Software Simulation in Chapter 4.

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

CHAPTER 3

35

The Hardware
STARTER MCU-EB8051-2
Trainer Kit - The Motherboard
HS AT89 Flash MCU Programmer
Lets Run the Firmware created in
Chapter 2 on the Real Hardware

P1.0
P1.1
P1.2
P1.3
P1.4
MOSI / P1.5
MISO / P1.6
CLK / P1.7
RST
RXD / P3.0
TXD / P3.1
INT0 / P3.2
INT1 / P3.3
T0 / P3.4
T1 / P3.5
WR / P3.6
RD / P3.7
XTAL2
XTAL1
GND

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

VCC
P0.0(AD0)
P0.1 (AD1)
P0.2 (AD2)
P0.3 (AD3)
P0.4 (AD4)
P0.5 (AD5)
P0.6 (AD6)
P0.7 (AD7)
EA/VPP
ALE/PROG
PSEN
P2.7 (A15)
P2.6 (A14)
P2.5 (A13)
P2.4 (A12)
P2.3 (A11)
P2.2 (A10)
P2.1 (A9)
P2.0 (A8)

Chapter 3 will Lets you run the


Firmware in the Real Hardware.
It will let you see the difference
between the Firmware response on
the :
1. Software Simulator
2. Real Hardware
Alas, you can see your firmware
running in REAL TIME Lighting LED
and Blinking LEDs.

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

36

Lets Connect the


Hardware
Its show time.........!!!!

[7]

Do not Reverse the MCU.

You are about to connect the Hardware.

[8]

Do not connect the STARTER MCUEB8051-2 Trainer Board into the AC wall
socket.

[9]

Do not short the pins on the General I/O


Ports.

You will now going to do the actual Programming of


the MCU. You will see that the program you have
created will run into the motherboard -- running into
its REAL Hardware.
So you have to FOLLOW the INSTRUCTION here
carefully and religiously - MAKE NO MISTAKE. Ok!

CARE of the Hardware :


Bago ang lahat, kailangan malaman niyo muna mga
KIDDY ang mga DOs and DONTs sa paggamit ng
iyong MCU, motherboard o ang STARTER MCUEB8051-2 Trainer Board.
[1]

[2]

Do not put the STARTER MCU-EB80512 Trainer Board near a :


- TV, Washing Machine
- Microwave oven or anything that
will damage it
Do not put a glass of water, juice or beer
bottle and can near the STARTER MCUEB8051-2 Trainer Board.

[3]

Do not study on a metallic surface table,


do not place the STARTER MCU-EB80512 Trainer Board on any metallic table.

[4]

Do not apply strong force on the STARTER


MCU-EB8051-2 Trainer Board

[5]

Do not touch the pins of the MCU.

[6]

Do not use high power adaptor on the


STARTER MCU-EB8051-2 Trainer Board
LIKE 30V or higher. Please use only the
USB cable to power on the STARTER MCUEB8051-2 Trainer Board.

[10] If you nee to remove the AT89S51 MCU


from the board, please touch a GROUND
or use Groundstrap. Be carefull not to damage the MCU.

Connection Diagram with Laptop PC


Thank you KIDDY for observing the Donts and Dos
to prevent your Hardware from breaking down.
Here you go!!! You will now connect the Hardware:
1. HS AT89 Flash MCU Programmer
2. STARTER MCU-EB8051-2 Trainer Kit
(motherboard)
Since you are using a laptop PC na walang COM
Port (RS232 COM Port), kailangan na gagamit ng
USB-to-RS232 converter para sa connection ng
laptop PC at ng HS AT89 Flash MCU Programmer.

FIGURE 3-1 :
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

USB to RS232 Converter

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

37

The Laptop / notebook PC must


run the BASCOM 8051 IDE to
create program.

Laptop /
Notebook PC

Note:
In mid-2010, we have now the Atmel
S51 AVR USB Programmer which work
best with laptop without COM port. This
will replaced the HS AT89 Flash MCU
Programmer.

USB to RS232
Converter
USB Cable

You must use USB


to RS-232 converter.

ISP Cable

STATER MCU-EB8051-2
Trainer Board : Motherboard

HS AT89 Flash MCU Programmer

Programmer

The HS AT89 Flash MCU Programmer


Windows software (Ver. 1.1) must run
into the Windows environment
FIGURE 3-2 :

Motherboard

Notebook PC

The Educational Micro Controller Programming Setup using a Laptop / Notebook PC


without the COM Port

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

38

Run Your Firmware In The Real Hardware

Steps in Connecting the Hardware:


Heto ang recipe para sa successful na Hardware
connections :
[1]

: Chapter 3

Humanap ng lugar na pag-aaralan, hindi


dapat ilagay ang Trainer Board at ang computer sa bakal na lamesa. Gumamit lang
ng lamesa na gawa sa kahoy o plastic.

[3]

Kuhanin ang USB-to-RS232 Cable. Isaksak


ito sa male RS232 socket sa HS AT89 Flash
MCU Programmer. Isaksak ang USB cable
sa laptop PC.

COM PortHS AT89 Flash


MCU Programmer

USB Port of
Notebook PC

FIGURE 3-3 :
[2]

Recommended studying table

Kuhanin ang ISP Cable. Isaksak ito sa female socket ng HS AT89 Flash MCU Programmer at saka doon sa Motherboard,
makikita sa Figure 3-4. Kung mali ang
pagsasaksak mo, hindi ito mailalagay sa
saksakan.
HS AT89
Flash MCU
Programmer

FIGURE 3-5 :
[3]

Connecting the USB-to-RS232


Cable

Kuhanin ang USB cable. Isaksak ito sa


female socket sa HS AT89 Flash MCU Programmer. Isaksak ang USB cable sa laptop
PC.
USB - HS AT89
Flash MCU
Programmer

Motherboard
USB Port
of Notebook PC

FIGURE 3-4 :

Connecting the ISP Cable

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

FIGURE 3-6 :

Connecting the USB Cable

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

Kung nakabukas ang laptop PC,


pagkasaksak ng USB cable sa laptop PC
ay iilaw kaagad ang power LED sa HS AT89
Flash MCU Programmer at saka ang power
LED sa Mother board.

39

Iilaw din ang mga LEDs na LED6 to LED8.

LED6 ~ LED8.

Programmer

FIGURE 3-9 :

ISP Connection indicator

Hardware settings

mother
board

FIGURE 3-7 :
[4]

There is a setting in the MCU Motherboard so that


the LED and 7-segment LED will light. Please follow the setting in Figure 3-10. Ang setting na ito ay
para iilaw ang mga LEDs.

Power On the LED

Pindutin ang dalawang switches na ito.


Dapat iilaw ang Red LED.

This should be the


setting when using the
LEDs and 7-segment
display

mother
board

FIGURE 3-8 :

ISP Switch Setting

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

FIGURE 3-10 : Hardware Setting Switch

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

Run the HS AT89 Flash programmer


Lets run the Programmer. The icon of the HS AT89
Flash MCU Programmer is placed on the Desktop,
you can double click the Icon to run this Software.
[1]

Point the Mouse cursor on the HS AT89


Flash
MCU
Programmer
Icon
(HSAT89Prog.exe). See Figure 3-11.

[2]

Double click the icon. You will see that the


software will load and then appear in the
Windows Environment. See Figure 3-12.

40

Set up the HS AT89 Flash programmer


The next thing to do is to setup the Software Driver
of the HS AT89 Flash MCU Programmer --- we simply called it here now as Programmer.
[1]

Click the MCU (located at the right hand top


of the Programmer software). Choose the
MCU as : AT89S51/52. See Figure 3-13.

[2]

Click the COM (located at the right hand


top of the Programmer software, just below
the MCU). Choose the COM 4, for the Kiddy
PC. See Figure 3-14.

FIGURE 3-11 : Openning the HAS AT89 Flash


MCU Programmer from Desktop

FIGURE 3-13 : Choosing the MCU

If you are using a Desktop PC, use


COM 1 or COM2.
If you are using a notebook PC
without a COM port, you may
need to use USB-to-RS232 converter and may use the correct
port like COM4 or COM5.

FIGURE 3-12 : The HAS AT89 Flash MCU Programmer is running on Windows
Environment (Version 2)
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

FIGURE 3-14 :

Choosing the COM Port

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

TITLE
PROGRAM

: Loading the PROG 01.hex into the motherboard


: Refer to PROG 01

SCHEMATIC

: Not Applicable (N/A)

HARDWARE

: Mother board : STARTER MCU-EB8051-2 Trainer Board

41

DESCRIPTION : The Firmware : PROG 01.HEX will be loaded (or program or burned) into the ROM
of the AT89S51. After a successful programming, the green LED on the motherboard
will light.
OBJECTIVE

: To learn :
- how to program a HEX file into the ROM of the AT89S51 MCU
- how to use the HS AT89 Flash MCU Programmer for the first time
- to compare the Software Simulation versus the actual response of the firmware
to light the LED.

FILE NAME

: PROG01.HEX

PROCEDURE

[1]

Run the Programmer (HS AT89 Flash MCU Programmer) in the Windows Environment of your
laptop PC

[2]

Set up the MCU Type and the COM Port to be use.


MCU : AT89S51 / 52
COM : COM 4 (for the ntebook Pc of the Kiddy)

[3]

Click Bura. The Display will be :


Setting :
MCU : AT89S51/AT89S52
COM : COM 4
Bura :
Successful erase of program
in the ROM of MCU.
Kailangan na i-erased muna
ang dating Hex file bago magsave ng panibagong Hex file.

Exercise N0. 2 :

Loading the PROG 01.hex into the motherboard [1/3]

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

42

[4]

Click A n o n g F i l e . Locate the PROG 01.hex. You can find it from BASCOM
8051\MYPROGRAM\1..PROGRAM 01\ PROG 01.hex. Load PROG 01.hex into the programmer, click Open.

[5]

Click SULAT. The Software will write the PROG 01.hex into the ROM of the MCU. How big the
PROG 01.hex is determined by a moving dot from left to right. Once programmed, the programmer will tell you as :
Kung marami ang dot, ibig
sabihin na mas-mas malaki
ang Hex file na isusulat sa
ROM ng MCU

Sulat :
Successful burning of program in the ROM of MCU.

[6]

Click VERIFY. The Software now will verify the validity of burning the PROG 01.hex into the
ROM of the MCU.

Exercise N0. 2 :

Loading the PROG 01.hex into the motherboard [2/3]

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

43

The Real Hardware Response of PROG01.hex


Congratulations.......!!!!! You just downloaded the
very first firmware you have created into the real
world Hardware. The response of the Target Board
is shown in Figure 3-15. [I should mentioned that
the Crystal used is 3.579545 MHz, if other crystal
is used, the response might be different]
LED1

Applying RESET :
To re-start the program execution, push the RESET button. See Figure 3-16 for the location of
RESET Switch.
You will notice that the GREEN LED will blink while
pushing the RESET button and then continuously
light up.

Removing ISP Connection :


The 3 LEDs (LED6~LED8) as shown in Figure 3-15
is temporarily used by ISP (In-System Programming) in order to program the HEX file into the ROM
of the MCU.
For normal program execution, you can remove this
connection by switching the 2 switches on the Target Board, see Figure 3-16. Once you switch off
by pushing the two switches, the 3 LEDS will power
off (light off) and only the Green LED will light up.
Apply the RESET and ....... here you go!!!! PROG01
is running on the Target Board (motherboard).

ISP LEDs
(LED6~LED8) is
ON

Pressed
this
swtiches so that
LED6~LED8 will
Turn OFF (ISP is
now disabled)
FIGURE 3-15 : Response of PROG01.hex running ont he Target Board
(motherboard)
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

RESET
FIGURE 3-16 : ISP Switch turn off

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

44

RUN all the created firmware in Chapter 2


Lets burn all the firmware you have created in Chapter 2 - run them on the Target Board. Turn off the
ISP connection.

4) PROG01_LED1_3 light.hex

Ang mga ginawang firmware ay :


1) PROG01.hex
2) PROG02_LED2 light.hex

FIGURE 3-19 : Response of PROG01_LED1_4


light.hex

FIGURE 3-17 : Response of PROG01_LED2


light.hex

5) PROG01_LED1_3 AliasName.hex. The response


of this firmware is the same as in Figure 3-19.

3) PROG01_LED4light.hex

FIGURE 3-18 : Response of PROG01_LED4


light.hex
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

FIGURE 3-20 :

Response of PROG01_LED1_4
AliasName.hex

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 3

Run Your Firmware In The Real Hardware

45

Have Fun with Audio


6) PROG01_All_LED blinking_1.hex
The LM4752T Prototype Amplifier

All LEDs are OFF (P1 = 255)

The Digital Audio


Tone Control along
with the LM4752T
Prototype Amplifier

The Voltage swing at


the 8 ohms Load

All LEDs are ON (P1 = 0)


FIGURE 3-21 : Response of PROG01_All_LED
blinking_1.hex
7) PROG01_All_LED blinking_2.hex
Ang response nitong firmware ay parehas sa
PROG01_All_LED blinking_1.hex. Ang pag-kakaiba nila ay ang instruction na ginamit sa pag-command sa MCU.

The Voltage swing at


the 8 ohms Load
start clipping

Ang PROG01_All_LED blinking_1.hex ay gumamit


ng BIT Instruction at ang PROG01_All_LED
blinking_2.hex ay gumamit ng BYTE Instruction.

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

46

CHAPTER 4
The Flow Chart
Program Description Language

Chapter 4 will presents the :


- Flow Chart
- Program Description
Language (PDL)
- Program Logic Flow

Program Struction
More Software Simulation

and More Software Simulation

'********************************************************************************************
' PROG02 : BLINK THE GREEN LED (LED1)
'*******************************************************************************************
'PROGRAM DESCRIPTION :
'This program will blink the Green LED (LED1) on the Target Board continuously
START
'simulating a Car Security LED. The LED light time is 200millisecond (200ms)
'while the LED off time is 2 seconds.

MCU Reset

'AUTHOR :
'F. Dandy Menor
'HSEC, Baguio City
'November 27, 2009

REPEAT

P1.0 = 0

Software IDE : BASCOM 8051 IDE (demo version) 2.0.14.0

http://www.mcselec.com
PROGRAM NAME : PROG02_Blinker_Security_comment.BAS

Delay 2
seconds

Start of Logic

When DC power
is applied, the
MCU RESET
Put P1.0 at
Logic 0

Delay 2
seconds

TARGET BOARD : STARTER MCU-EB8051-2 Trainer Board

'COMPILER DIRECTIVE :
$SIM
'This direct the compiler that it will use only on the
'software simulator and not on the Target Board
'========================================================
'START OF PROGRAM
REPEAT:
P1.0 = 0
Waitms 200

'The Label to jump on


'P1.0 is Logic 0, LEd Turn On
'Wait for 200 ms

P1.0 = 1
Wait 2

'P1.0 is logic 1, LED Turn Off


'Wait for 2 seconds

Goto REPEAT

'Go back to REPEAT

'========================================================
End
'End of Program

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

P1.0 = 1

Put P1.0 at
Logic 1

Delay 2
seconds

Delay 2
seconds

GOTO REPEAT

Power Off

END

When DC power
is remove, the
MCU will off

End of Logic

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

47

Flow Chart - to design


Program Flow
Dahil alam na kung paano gumawa ng simple program at alam na din kung paano i-burn ang firmware (hex file) sa ROM ng MCU, subukan naman
pag-arala ang FLOW Charting para i-represent ang
program logic. Lets see it with PROG02.

START

MCU Reset

Decsription of MCU work


Ang trabaho ng MCU ngayon dito sa Exercise No.
3 - PROG02 ay paiilawin ang LED ng 2 seconds,
pagkatapos papatayin nito ang ilaw ng 2 seconds,
saka paiilawin uli ng paulit ulit - Blinker.

REPEAT

P1.0 = 0

Delay 2
seconds

Flow Chart
Simulan natin gumamit ng Flow Chart. Ang Flow
chart ay PAPER WORK lang at dito makikita ang
LOGIC FLOW. Ang Logic Flow ay ang pagka
sunod-sunod na gagawin ng MCU. Tingnan ang
Flow Chart sa FL1.
Ang paggamit ng Flow Chart ay ang unang gagawin
para sa pag-analize ng gagawin ng MCU.

Start of Logic

When DC power
is applied, the
MCU RESET
Put P1.0 at
Logic 0

Delay 2
seconds

P1.0 = 1

Put P1.0 at
Logic 1

Delay 2
seconds

Delay 2
seconds

START :
Sa Flow chart, magsisimula ito ng
GOTO REPEAT
START

MCU RESET :
Ang MCU RESET ay parang Hoy Gising!!!!. Kung
wala itong supply na 5V, ang MCU ay natutulog.
Kung lagyan ng supply na 5V, magigising ito. Ang
pagka-gising niyan, iyan ang RESET. Kung hindi
maganda ang MCU reset, hindi rin maganda ag
takbo ng firmware, parang tao, kung di maganda
ang gising ay baka nakasimangot.

Power Off

END

When DC power
is remove, the
MCU will off

End of Logic

FL 1 : Flow Chart of PROG02_Blinker

MCU Reset

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

P1.0 = 0 :
Ito ay Instruction na ini-utos ng programmer. Ang
instruction na iyan ay hey! P1.0 mag-output ka
nga ng logic 0.
P1.0 = 0

Delay 2 second :
Habang ang P1.0 ay naka logic 0, maghintay ng
200 milisecond. Ito ay tinatawag na DELAY.
Magkakaroon ng delay para makita na iilaw ang LED
ng 200milisec.
Delay 200
mili second

P1.0 = 1 :
Ito ay Instruction na ini-utos ng programmer. Ang
instruction na iyan ay hey! P1.0 mag-output ka
nga ng logic 1.
P1.0 = 1

Delay 2 second :
Habang ang P1.0 ay naka logic 1, maghintay uli ng
2 seconds. Ito ay tinatawag na DELAY.
Magkakaroon ng delay para makita na hindi nakailaw ang LED na naka-connect sa P1.0
Delay 2
seconds

GOTO REPEAT :
Ang GOTO ay instruction para ulitin excution,
sisimulan uli ang execution doon sa may label na
REPEAT.
GOTO REPEAT

POWER OFF :
Ang MCU ay matutulog muli kung tanggalin mo ang
supply na 5V
Power Off

END :
Dito nagwawakas ang logic ng program.

48

Description of MCU Instruction :


Alam niyo na kung paano pailawin ang LED na naka
connect sa P1.0. Ilagay lang ang instruction na
P1.0 = 0. Kung ito ay patayin ang ilaw sa LED, ito
ay :
P1.0 = 1
Ngayon, mayroong bagong gagawin ng MCU, ito
ay mag-hihintay ng 2 seconds. Wala lang gagawin
ng MCU kundi maghintay - magpalipas ng 2
segundo. Ang Instruction na ito ay WAITMS, example ay
WAIT 2
WAITMS 200

Wait for 2 seconds


Wait for 200 milisecond

Ito din ay madalas na tawagin DELAY, na ibig sabihin


sa tagalog ay ipag-paliban. Doon sa Flowchart,
pagka ouput ng P1.0 ng logic 0 (P1.0 =0) , ang
sumunod ay ipagpaliban muna ng 2 second.
Tingnan natin ang timing diagram.

volt

P1.0=1

RESET
P1.0=0

MCU
sleep

2 sec

5
0

time

Ang isa pang bagong instruction na gagamitin dito


sa Exercise No. 2 ay ang
GOTO
Agn GOTO sa tagalog ay punta ka doon - hindi
iyong kinakain hehehehe, kaya ang GOTO REPEAT
ay ibig sabihin na, punta ka doon sa REPEAT.
Dahil paulit-ulit lang naman ang gagawin, bigyan ng
instruction ng MCU na Goto Repeat.
[Halos kaparehas D0---Loop instruction na ginamit
sa PROG01_LED1 blinking]

END

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

49

PROGRAM
: PROGRAM 02_Blink Security
TITLE
: BLINK the GREEN LED - just like a Car Security LED
DESCRIPTION : This program will going to blink the GREEN LED (LED1)
APPLICATIONS : Use in a car as an LED blinker. See wheres the LED is installed in the Ccar

LED blinker
inside the
CAR
OBJECTIVE:

SCHEMATIC

To learn and understand Flow Chart to represent the logic flow in designng program.
To improved the programming skill and learn more BASCOM programming instructions.

: As shown below
R6 = 560 Ohms
IC2
LM7805

LED1
Green

IN

OUT
+5V

+12V
1
P1.0

GND

40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Logic 1
P1.0 SWITCH
Logic 0

VCC

39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

GND

12V
10 A

CAR
Battery

IC1
AT89S51 MCU

Exercise N0. 3 :

PROG02 exercise to BLINK the GREEN LED [1/2]

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

HARDWARE

: STARTER MCU-EB8051-2 Trainer Board

FILE NAME

: PROG 02_Blinker_Security.bas

50

PROGRAM CODE :

FIRMWARE BURN : Burn the PROG_02_Blinker_Security.hex on the ROM of the MCU sing the HS AT89
Flash MCU Programmer.
EXPECTED OUTPUT (on the Real Hardware - Target Board)

LED is OFF (logic 1)


CONCLUSION :

LED is ON (logic 0)

The P1.0 LED will turned on for 200msecond and off for 2 seconds. It serves as
a blinker.
You can see from this exercise that, the MCU is following the Instruction (command).
The Instruction WAITMS 200 instruction will create a delay of 200 milisecond.
The instruction WAIT 2 will create a delay of 2 seconds.
The P1.0 = 1 instruction will caused a Logic 1 at the P1.0 output. The P1.0=0
instruction will cause a Logic 0 at P1.0.

Exercise N0. 3 :

PROG02 exercise to BLINK the GREEN LED [2/2]

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

51

Software Simulation of
PROG02_Blinker_Security
Subukan natin patakbuhin ang

Sa example na ito, masasabi na hindi REAL TIME


EXECUTION ang takbo ng Software Simulator pero
ipinapakita niya ang takbo ng logic flow.

PROG02_Blinker_Security
program sa Software Simulation ng BASCOM 8051
IDE.
[1]

[2]

[3]

Go back to the Editor Window of


PROG02_Blinker_Security.bas, and then recomplie it by pressing F7.
Open the Software Simulation by pointing
the mouse pointer on the Simulation icon,
then open the Hardware Emulation by
clikcing the appropriate icon, then click the
Play icon.
Obserbahan lang ang GREEN LED (P1.0
LED) pati na ang Program Tracer.

Mapapansin na mas-mabagal ang response ng


Software Hardware Simulator kung i-kumpara sa
takbo ng firmware sa totoong Target Board.

Ang Target Board execution habang running ang firmware ay REAL TIME execution. Dito sa Target
Board response malalaman ang totoong response
ng iyong program.
Kaylan pala ginagamit ang Software Simulator?
Kadalasan na ginagamit ko ito sa pag-check ng
Logic Flow. Dito sa Example na ito, sundan ko
lang ang Flow Chart, patakbuhin ang program sa
Simulator at i-compara ang Logic flow.
Puwede natin patakbuhin ang Simulation sa
pamamagitan ng single execution. I-click ang
mouse at tatakbo ang program sa bawat click ng
mouse.
Ok tingnan natin kung paano.

Real Time
Execution on the
Target Board
This is
L
BASC ED1 on the
OM 80
51
Hardw
are Sim IDE
ulation

Simulated Execution by Software on


the BASCOM 8051
IDE

FIGURE 4-1 :

Real Time Execution on the Target Board and Execution on Software Hardware Simulation

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

52

Software Simulation
Step-into-Code
Gamitin ang STEP-into-CODE para patakbuhin ang
Software Simulator.
[1]

Go back to the Editor Window of


PROG02_Blinker_Security.bas and modify
the code as shown in Figure 4-2.

[2]

Save the progreams as :


PROG02_Blinker_Security_SIM

[3]

Press F7 to compile.

[4]

Open the Software Simulation by pointing


the mouse pointer on the Simulation icon,
then open the Hardware Emulation by
clikcing the appropriate icon.

[5]

Click the STEP-into-CODE icon while observing the Program Tracer and the P1.0
LED on the hardware Simulation Window.

Makikita dito na mayroon nadagdag na instruction :


$SIM
Ito ay compiler directive para sabihin na ang
mga code ay para lang sa Software Simulation. Hindi ito puwede na patakbuhin sa
Target Board.

Keep clicking the STEP-into-CODE icon.

STEP-into-CODE
P1.0

Program
Tracer

FIGURE 4-2 : The use of Compiler Directive $SIM


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

FIGURE 4-3 : STEP-into-CODE

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

Flow Chart - PDL - Simulation - Program Structure

Ngayon, i-compare ang takbo ng program (logic flow)


doon sa Flow Chart. Makikita na tama ang logic
flow, ibig sabihin na tama ang mga instruction na
ini-utos sa MCU.
[5]

Stop clicking the STEP-into-CODE.


Press the Play icon and see the response.
Mapapansin na tatakbo ng mabilis ang Program Tracer at mabilis din ang blinking ng
P1.0.

Tandaan na, hindi ito ang REAL TIME execution ng


program, dahil sa simula pa lang ay sinabihan na
ang Compiler na gamitin lang itong program na ito
sa Simulator sa pamamagitan ng declation na $SIM.
[6]

[7]

: Chapter 4

53

Lack of Instruction
Simulation
Halimbawa, ang PROG02_Blinker_Security_SIM
ay ginagawa na wala ang Target Board, at may
nakalimutan na ilagay ang isang instruction na :
GOTO REPEAT
Data sana mayroon eh, kaso nakalimutan nga!!!
Ang Code ay makikita sa Figure 4-4. Mapapansin
na wala ng Goto REPEAT kung i-kumpara sa Figure 4-2.

Subukan i-burn ang

Save the program as :

PROG02_Blinker_Security_SIM.hex

PROG02_Blinker_Security_wrongcode_SIM

sa MCU at tingnan ang response. Tatakbo


ba ang ayon sa Flow Chart 1?

compile and run on the Software Simulator.

Ngayon, subukan i-burn uli ang


PROG02_Blinker_Security.hex
ito iyong program na wala ang compiler directive na $SIM.
Tatakbo ba ang firmware ayon sa Flow Chart
1?

Alright! Alam na natin ang difference at nadagdagan


pa ang kaalaman natin sa paggamit ng Software
Simulation - na kahit wala pang Target Board ay
puwede na malaman ang kalabasa ng logic flow,
malaman kaagad kung tama ang mga instruction
sa MCU.
Tandaan uli, ang final verdict kung tama o mali
ang ginawang program ay - - kung ito ay tumatakbo
na sa Real Hardware sa Real Time.

FIGURE 4-4 : Intentional Wrong Code


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

[1]

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

54

Use the STEP-into-CODE to run the Software Simulation. Click it untill the Program
Tracer is on the WAIT 2 instruction. See
Figure 4-5

FIGURE 4-6 : Simulation of Wrong Code - the program is terminated by the END Instruction
FIGURE 4-5 : Simulation of Wrong Code
[2]

I-click uli ang mouse at makikita na ang


Program Tracer ay pupunta sa END instruction. See Figure 4-6. Ibig sabihin na tapos
na ang Program.
Mali, diba? Ang gusto natin ay mag-blink
ang LED1 (green LED) ayon sa Flow Chart,
na dapat sana, ang logic flow ay babalik sana
sa REPEAT, pagkatapos ng WAIT 2 na instruction

Kung mayroon kang Target Board at


available ito habang gumagawa ng program, puwede nang i-burn ang hex file,
para makita kaagad kung tama ang
ginawang program, kasi REAL TIME
execution ang takbo ng program sa Hardware.

Ok! Nakuha ba ang gustong ipahiwatig dito? Sa


paggamit ng Software Simulation, makikita na
kaagad kung tama o mali ang logic flow ng program.
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

55

Program Description
Language (PDL)
Aside from the Flow Chart, there is another way to
represent the Program Logic, it is called Program
Description Language or simply PDL.
It is basically similar to Basic Instruction but it is
not a programming language, rather, it is an English language that show the program logic flow.
Kung baguhin ko ang programming style sa
PROG02_Blinker_Security

Gaya ng sinabi ko, binigyan pangalan ang P1.0 na


mas madaling matandaan, gaya ng
LED1
puwede rin GREEN_LED o kaya Security_LED.
Ang

Do
Loop

Naming Hardware to Control

control instruction ay paulit ulit na gagawin ang mga


instructions na nasa loob ng Do - Loop. Ang DoLoop ay ginagamit kung paulti ulit lang na i-execut
ang mga instruction na nasa loob.

START

Ito ay puwede na replacement sa

Heto ang magiging PDL nito sa Figure 4-7.

Turn On LED1
Wait for 200msec
Turn Off LED1
Wait for 2 seconds
Go back to START
End
FIGURE 4-7 : PDL of PROG02_Blinker_Security
The BASCOM Instruction for the PDL in Figure 4-7
is shown in Figure 4-8.
LED1

Alias P1.0

REPEAT:
Goto REPEAT
instruction na ginamit sa Program sa Figure 4-2.
Ang instruction na
Reset LED1
ay katumbas ng LED1 = 0. Sinasabi na ang LED1
o P1.0 na paa ng MCU ay mag-outout ng logic 0.
Ang Instruction na

Do
Reset LED1
Waitms 200
Set LED1
Wait 2
Loop
End
FIGURE 4-8 : Listing of
PROG02_Blinker_Security
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

SET LED1
ay katumbas ng P1.0 = 1, ibig sabihin na inuutusan
ang paa ng MCU na P1.0 na mag-output ng Logic
1.
Finally, ang PDL ay ginagamit lalu na sa
mahahabang program. Maganda kung laging i-sulat
ang PDL sa papel at sundan ang program logic flow
nito sa paggawa ng BASCOM program.

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

56

PROGRAM Comments
Para sa madaling maunawaan ang program, it is
suggested na mag-lagay ng comment. Tingnan ang
Figure 4-9. Ang PROG02_bliker_Security ay
nilagyan gn comment para i-described ang gamit
ng mga instruction.

The complete document of Figure 4-8 is shown in


Figure 4-10.

'********************************************************************************************
' PROG02 :
BLINK THE GREEN LED (LED1)
'*******************************************************************************************
'PROGRAM DESCRIPTION :
'This program will blink the Green LED (LED1) on the Target Board continuously
'simulating a Car Security LED. The LED light time is 200millisecond (200ms)
'while the LED off time is 2 seconds.
'AUTHOR :
'F. Dandy Menor
'HSEC, Baguio City
'November 27, 2009
Software IDE : BASCOM 8051 IDE (demo version) 2.0.14.0

http://www.mcselec.com
PROGRAM NAME : PROG02_Blinker_Security_comment.BAS
TARGET BOARD : STARTER MCU-EB8051-2 Trainer Board
'COMPILER DIRECTIVE :
$SIM
'This direct the compiler that it will use only on the
'software simulator and not on the Target Board
'========================================================
'START OF PROGRAM
REPEAT:
P1.0 = 0
Waitms 200
P1.0 = 1
Wait 2
Goto REPEAT

'The Label to jump on


'P1.0 is Logic 0, LEd Turn On
'Wait for 200 ms
'P1.0 is logic 1, LED Turn Off
'Wait for 2 seconds
'Go back to REPEAT

'========================================================
End
'End of Program

FIGURE 4-9 : The use of comment to document the Program


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 4

Flow Chart - PDL - Simulation - Program Structure

57

'*******************************************************************************
' PROG02 :
BLINK THE GREEN LED (LED1)
'*******************************************************************************
'PROGRAM DESCRIPTION :
'This program will blink the Green LED (LED1) on the Target Board continuously
'simulating a Car Security LED. The LED light time is 200millisecond (200ms)
'while the LED off time is 2 seconds.
'
'This program used the ALIAS instruction to name Bit Port. It also uses the
'Do-Loop control instructions, RESET and SET instructions.
'AUTHOR :
'F. Dandy Menor
'HSEC, Baguio City
'November 27, 2009
'Software IDE : BASCOM 8051 IDE (demo version) 2.0.14.0
'
http://www.mcselec.com
'PROGRAM NAME : PROG02_Blinker_Do_Loop_dox.BAS
TARGET BOARD : STARTER MCU-EB8051-2 Trainer Board
'===============================================================================
'NAMING VARIABLES
LED1 Alias P1.0

'Name the hardware connected at P1.0

'===============================================================================
'START OF PROGRAM
Do

'Do, start execute the following next instructions


RESET LED1
Waitms 200
SET LED1
Wait 2

Loop

'Ouput Logic 0
'wait for 200 milli seconds
'Output Logic 1
'wait for 2 seconds

'Go back to "Do"

'===============================================================================
End
'End of Program

FIGURE 4-10 : An example of program with comment - PROG02_Blinker_Do_Loop_dox


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

58

CHAPTER 5
The WORLD of LEDs
and 7-segment LEDs

Chapter 5 will present more


examples how to control the 8 LEDS
on the Target Board.

More BASCOM Instructions

Display Digit

Decimal

HEXadecimal

192

C0

221

DD

164

A4

162

A2

147

93

138

8Al

136

88

227

E3

128

80

131

83

160

A0

152

98

It also present how to control the


Dual 7-segment LED Diplay.

P3.7

Character

P2.4
P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
P2.3

P2.5
P2.0

P2.2

P2.1

P2.0

P2.7

myhobby Educational Trainer


Heart
EAF2 Electronics Series
Systems

The value that we can obtain is 10001010


This is Binary Number

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

59

The World of LEDs & 7-segment LED Display

Welcome to the World


of LEDs
During the writing of this document, LEDs are becoming now popular. It will be the Energry Saving
LIGHT of the future which replacing the Incandescent bulb and Fluoresecent bulb. There is also the
LED TV which replaced the CRT TV and Plasma.
In the KIDDY Target Board - the STARTER MCUEB8051 Trainer-2, there are 8 LEDs which can be
lighted with the application of firmware as demonstrated in previous Chapters.
Basic of LED :
Light Emitting Diode or LED is a popular means to
denote the presence of electricity, see Figure 51, ito ay isang klase ng LED. Ang kulay ng LED ay
Green. Ang kanyang paa ay hindi parehas, may
mahaba na ang pangalan ay
Anode o A

Dito sa target board, ang LED1~LED8 ay iilaw kung


i-control natin ang MCU (sa pamamagitan ng application program) na mag-output ng Logic 0.
Hmmmm! Paano ba iyan? Eh, sabi ko kanina ay
iilaw ang LED kung mayroon Kuryente, eh........
ang Logic 0 ay walang kuryente?.
Ok! Pag-aralan muna natin ang Hardware kung
paano naka-connect ang mga LED sa MCU, para
malaman natin kung bakit kailangan na Logic 0 ang
i-command (instruct) sa MCU para umilaw ang LED.
Ang Anode (A) na paa ng LED ay dapat nakakabit
sa +5V at ang Cathode (K) na paa ng LEd ay
nakakabit dapat sa GND o kaya minus (parang
Battery na mayroon + ay saka -, hindi puwedeng
baliktarin.
Ang simplified connection nito ay makikita sa Figure 5-2.

at ang maiksi naman ay

+5V

Cathode o K
Ang LED ay iilaw kung mayroon kuryente, kaya
kadalasan ginagamit na POWER ON Indicator.

R6
560

AT89S51
Pin 40
LED1

[ Logic 1 ]
[ P1.0 = 1 ]

VCC
+5V

SWITCH Pin 20

Pin 1 P1.0

Anode
(A)

Cathode
(K)

FIGURE 5-1 : Green LED (Light Emitting Diode)


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

+5V

GND

0V

FIGURE 5-2 : Green LED (Light Emitting Diode)

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

: Chapter 5

The World of LEDs & 7-segment LED Display

Ang Figure 5-2, ang P1.0 ay naka Logic 1 (ang logic


1 ay +5V) kasi makikita dito na naka-connect ang
P1.0 sa +5V. Ibig sabihin nito na
P1.0 = 1
Hindi iilaw ang LED dahil, ang maiksi na paa nito
(Anode - K) ay naka-connect sa +5V, tingnan uli
ang Figure 5-2. Dapat sana, naka-connect ito sa
GND para umilaw, diba?
Ok! Kunwari............. utusan ang paa ng MCU na
P1.0 na mag-output ng Logic 0, ang equivalent
schematic diagram nito ay makikita sa Figure 5-3.
Ang Logic 0 ay equivalent ng GND, kaya naman
ang Anode (K) na paa ng LED ay naka-connect na
ngayon sa GND ---- kaya ito ay iilaw.
Kaya kung gustong pailawin ang LED1, utusan ang
paan ng MCU na P1.0 = 0.

60

Lets Hit the Road of LED


With the analysis on Figure 5-1 and Figure 5-2
bundled with powerful BASCOM 8051 instructions,
it can create a series of program to control the 8
LEDs on the Target Board; LED1 ~ LED8.
Running LED:
Here is the PDL for the running LED
Naming Hardware to Control
START
Turn On LED Sequence1
Wait for 150msec
Turn On LED Sequence2
Wait for 150msec
Go back to START
End
FIGURE 5-4 : PDL of Running LED
-----------------------------------------------------------------PROG03_Running_LED
-----------------------------------------------------------------Running_LED Alias P1
'Let P1 name to be Running_LED

+5V

Do
'Start execute the following instructions

R6
560
GREEN
RED

AT89S51
Pin 40
+5V

SWITCH
Pin 1 P1.0

GND
[ P1.0 = 0 ] Pin 20

Running_LED = &B10101010
Waitms 150
'Let the value of Running_LED to be 10101010
'then wait for 150 millisecond
Running_LED = &B01010101
Waitms 150
'Let the value of Running_LED to be 01010101
'then wait for 150 millisecond

[ Logic 0 ]
+5V
0V

P1.0 = 0,

Logic 0

FIGURE 5-3 : P1.0 of MCU output Logic 0


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

GND

Loop
'Go back to the "Do" to execute again
End
'End of Program
FIGURE 5-4 : BASIC Program for Running LED

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

The World of LEDs & 7-segment LED Display

Ang running LED ay mayroon dalawang Sequence.


Ang Sequence 1 at saka Sequence 2. Kapag
pinagsama mo itong dalawang sequence, ang
kalalabasan ay parang tumatakbo na ang mga
LEDs. Ang instruction na :
Running_LED

Alias

: Chapter 5

61

Sa susunod na sequence, lalagyan uli ng ng


panibagong value anf Running_LED. Ang ilalagay
ang ang Sequence 2 sa Figure 5-5 ng instruction
na :
Running_LED = &B01010101

P1

At automatic na ma-i-transfer ito sa Port 1 o P1.


Ang susunod na instruction ay i-delay ng 150 milli
seconds.

ay para tawagin ng Running_LED and Port 1 (P1).


Imbest na tawagin P1,
tatawagin itog
Running_LED. Ang Instruction na :

Paulit-ulit lang itong gagawin, at ginagawa iyan ng


Do--Loop instruction.

Do
Loop

Alright!

ay endless loop at i-execute lahat ng mga instruction sa lob ng Do--Loop. Ang unang instruction na
nasa loob ng Do-Loop ay :

Its your turn to write the code on the BASCOM


8051 IDE Editor and save it as :

Running_LED = &B10101010

PROG03_running_LED.bas

parang ang Sequence 1 na 10101010 ay ilalagay


na value sa Running_LED. Ang ginamit dito na
paraan para ilagay ang value sa Running_LED ay
Binary Notation.

Compile it and burn the


PROG03_running_LED.hex
into the Target Board.

Ang kalalabasan nito ay alternate na iilaw ang mga


LEDs. Pagkatapos ng malagyan ng value ang
Running_LED ay automatic na i-control ang Port 1,
at saka i-delay ito ng 150 milli seconds ayon sa
instruction na :

Please, de-pressed the ISP switch so that the ISP


connection will turn off the LED6~LED8. You should
see the LED look like running.

Waitms 150

Sequence 1
1

Sequence 2
0

FIGURE 5-5 : LED sequence for the PROG03_Running _LED


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

Right to Left - bar like LED :


Here is another program that will Light the LEDs
from the Right to the Left - looks like Bar LEd.
In this program, we will use the
ROTATE LED1_8, Left
instruction which will rotate the binary data of
LED1_8 variable going into the left.

62

*******************************************************
1..PROG03_Right_Left_running
******************************************************
Dim LED1_8 As Integer
'Reserved LED1_8 to be Integer type
Right_to_Left_LED Alias P1
'Let P1 name to be Right_to_Left_LED
LED1_8 = 255
'Initiate the value of LED1_8
Do

Dimension the Variable, LED1_8


Naming Hardware to Control
Put Initial Value of LED1_8
START
Put LED1_8 into Port
Rotate Left LED1_8
Delay for 100 msec
Goto START
End

FIGURE 5-6 : PDL of Right to Left - bit by bit

Right to Left - bar like LED :


Ang kabaliktaran ng
1..PROG03_Right_Left_running
kung saan gumagamit ng ROTATE X, Left, mayroon
din
2..PROG03_Left_Right_running
na gumagamit ng ROTATE X, Right. Ang lisitng ay
nasa Figure 5-8.
Isulat ang dalawang program na ito, i-save sa kanikanilang pangalan at i-compile.
I-burn ang Hex File sa Target Board para makita
ang real output ng mga LED. Paki-depressed lang
ang 2 ISP switch para di maka-interfere sa
LED6~LED8.

Right_to_Left_LED = LED1_8
Rotate LED1_8, Left
waitms 100
Loop
End
'End of Program
FIGURE 5-7 : Listing for
1..PROG03_Left_Right_running
*******************************************************
2..PROG03_Left_Right_running
******************************************************
Dim LED1_8 As Integer
Left_to_Right_LED Alias P1
LED1_8 = 255
Do
Left_to_Right_LED = LED1_8
Rotate LED1_8, Left
waitms 100
Loop
End
FIGURE 5-8 : Listing for
2..PROG03_Right_Left_running

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

Right to Left - bit by bit :


Here is another program that will Light the LEDs
from the Right to the Left - one-by-one.
In this program, we will use the
For-To-Next
instruction which will execute insruction inside the
For-To-Next with a limited variable as a test for looping back or exiting.

63

*******************************************************
3..PROG03_Left_Right_bit_by_bit
******************************************************
Dim Bit_port As Integer
Do
For Bit_port = 0 to 7 step 1
Reset P1.Bit_port
waitms 100
Set P1.Bit_port
waitms 100
Next P1.Bit_port

Dimension the Variable, Bit_port


Loop
START
For Bit_port, 0~7
Logic 0 -->Port 1 Bit_port
Wait for 100msec
Logic 1 -->Port 1 Bit_port
Wait for 100msec
Next
Goto START
End
FIGURE 5-9 : PDL of Right to Left - bit by bit

End

FIGURE 5-10 : Listing for


3..PROG03_Left_Right_bit_by_bit
*******************************************************
4..PROG03_Right_Left_bit_by_bit
******************************************************
Dim Bit_port As Integer

Right to Left - bit by bit.


This program shown in Figure 5-11 is the reverse of
Figure 5-10. It is basically the same cose except
for the way the FOR-TO-NEXT Count.
In Figure 5-10, the variable
Bit_port
is increment by 1 (For Bit_port = 0 to 7 step 1)
untill it reach the value of 7.
In Figure 5-11, the Bit_port is decremented by -1
(For Bit_port = 7 to 0 step -1) untill it reach the
value of zero.

Do
For Bit_port = 7 to 0 step -1
Reset P1.Bit_port
waitms 100
Set P1.Bit_port
waitms 100
Next P1.Bit_port
Loop
End

FIGURE 5-11 : Listing for


4..PROG03_Right_Left_bit_by_bit
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

Knight Rider - Scanner :


The popular Knight Rider TV series- with scanner
LED infront of the car can be done by our Target
Board with the Do--Loop and For-To-Next instructions, plus the Rotate , Left/Right instructions. the
For-To-Next as a test for looping back or exiting.

64

*******************************************************
6..PROG03_Scanner
******************************************************
Dim LED1_8 As Integer
Dim Counting As Integer
Scanner Alias P1

Dimension the Variable, LED1_8


Naming Hardware to Control, Scanner
Put Initial Value of LED1_8

LED1_8 = 255
Do
For Counting = 0 to 7
Scanner = LED1_8
Rotate LED1_8, Right
waitms 100
Next Counting

START
For Count, 0~7
Put LED1_8 into Scanner
Rotate Left LED1_8
Delay for 100 msec
Next Count

For Counting = 0 to 7
Scanner = LED1_8
Rotate LED1_8, Left
waitms 100
Next Counting

For Count, 0~7


Put LED1_8 into Scanner
Rotate Right LED1_8
Delay for 100 msec
Next Count
Loop
Goto START
End
FIGURE 5-12 : PDL of Scanner

End
FIGURE 5-13 : Listing for 6..PROG03_Scanner

Unlimited Combination
to light the LEDs
Marami pang mga combination ang maaring gawin
para sa pag-papa-ilaw ng LEDs - explore the combinations and youll find it that, there are a lot.
Ang susunod na i-control ay ang 7-segment LED.
Ito ay LED pa rin, ang kagandahan lang nitong LEDs
na ito ay puwede mag-display ng number gaya ng
0, 1, 2, 3, 4, hanggang number 9 at puwede din idisplay ang small letter na c, b o malaking C.

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Tingnan sa Figure 5-14 and 7-segment LED display


na ginamit sa Target Board :
STARTER MCU-EB8051-2 Trainer Kit
Ang 7-segment LED display ay kadalasan nakikita
sa mga appliances para indicator ng number. Ok!
Pag-aaralan natin iyan.

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

65

The World of 7-segment


LEDs
Ang 7-segment LEDs, gaya ng kanyang pangalan
kung isalin sa Tagalog ay pitong LEDs na
pinagdugtong-dugtong. Ang pagka-dugtong-dugton
nito ay porma ng number 8. See Figure 5-14.
Ang bawat segment ay katulad ng isang LED na
gaya ng LED1 o kaya LED2. Ibig sabihin na, ito ay
mapa-ilaw din kung ilagay sa Logic 0 ang MCU port
na kumo-conrol nito, at ilagay sa Logic 1 naman
kung gustong patayin ang ilaw.
Ok, applicable ang napag-aralan sa LED, diba? Ang
pinaka-gamit ng 7-segment LED ay para mag-display ng number, kaya iyan ang pag-aaraan natin.

P3.6
P3.7

FIGURE 5-14 : The 7-segment LED Display

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

IC1
AT89S51 MCU

P2.7
P2.6
P2.5
P2.4
P2.3
P2.2
P2.1
P2.0

1st
Digit
Control

2nd
Digit
Control

FIGURE 5-14-A : The Segment of 7-segment LED


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

SIMPLE MCU Programming

Trainer
No. 05

: Chapter 5

The World of LEDs & 7-segment LED Display

MCU Control :
Ang kumo-control sa mga LED1~LED8 ay ang
Port 1 ng MCU.
P1.0, LED1
P1.1, LED2
P1.2, LED3
P1.3, LED4
P1.4, LED5
P1.5, LED6
P1.6, LED7
P1.7, LED8

66

Para maunawaan kung paano mag-display ng number, tingnan kung paano naka-kabit ang 7-segment
sa Port 2 ng MCU (Figure 5-14-A) at anong Bit_port
ang kumo-control sa bawat segment (Figure 5-15
(a)).
Tingnang ang Figure 5-15, (b) Form Number 5. Ang
gagawin dito para maka-display ng 5 ay i-lagay sa
Logic 0 ang mga segment na bumubo ng number
5. Ang mga bit Port na bumubo ng number 5 ay :

Ang kumo-control naman sa 7-segment LED ay ang


Port 2 ng MCU. Ibig sabihin na, kung pailawin
natin ang 7-segment LEDs, gamitin ang Instruction
na

P2.4
P2.5
P2.6
P2.2
P2.0

=
=
=
=
=

0
0
0
0
0

Para umilaw, kailangan na i-command ang P3.7 na


mag-output ng Logic 0, kaya P3.7 = 0. Ang mga
Segment na hindi kasali sa number 5 ay ang mga :

P2 = 0
Kung saan iilaw lahat ng segment ng 7-segment ng
LED. Parang noong ginawa ng P1 =0, umilaw lahat
ng LED1_LED8.

P2.3 = 1
P2.1 = 1
P2.7 = 1

P2.4

P2.4
P2.3

P2.5

P2.3

P2.5

P2.0

P2.0
P2.2

P2.1

P2.2

P2.1

P2.0

P2.7

P2.0
P3.7

a) Bit Port Designation based on


the connection of the STARTER
MCU-EB8051-2 Trainer Kit

P3.7
b) The segment form digit 5

FIGURE 5-15 : The Segments of 7-segment LED Display a) Bit-port assignment


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

P2.7

b) form the digit 5

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

Light the DOT :


Kung pailawin ang DOT doon sa 7-segment LED,
Ang PDL nito ay makikita sa Figure 5-16.

Display Digit 5 :
Kung digit 5 naman ang i-display, gaya ng nasa
Figure 5-15, ang PDL nito ay makikita sa Figure 518.

Logic 0 on Port 2 bit 7 to light dot


Logic 0 on Port 3 bit 7 to display 1st digit

Logic 0, P2.4
Logic 0, P2.5
Logic 0, P2.6
Logic 0, P2.2
Logic 0, P2.0

End
FIGURE 5-16 : PDL to Light up the dot of a 7segment LED

Logic 1, P2.3
Logic 1, P2.1
Logic 1, P2.7

*******************************************************
1..PROG04_Light_Dot
******************************************************
Reset P2.7

light the dot

Reset P3.7

display on the first digit

End
FIGURE 5-17 : Listing for 1..PROG04_Light_dot
Figure 5-18 shows the real world response of Listing of :
1..PROG04_Light_dot.Hex
Only the first digit dot will Light up, dahil ito lang
ang ini-utos sa MCU.

67

Logic 0, P3.7
End
FIGURE 5-19 : PDL to display digit 5
*******************************************************
2..PROG04_Display_5
******************************************************
Reset P2.4
Reset P2.5
Reset P2.6
Reset P2.2
Reset P2.0

Set all bit ports controlling


the segment to Logic 1
to form digit 5

Set P2.3
Set P2.1
Set P2.7

set all bit ports which do


not form the segment of
digit 5 into logic 1

Reset P3.7

display on the first digit

End
FIGURE 5-20 : Listing for 2..PROG04_display_5
Ang actual response ng 7-segment display sa Target Board ay makikita sa Figure 5-21.

FIGURE 5-18 : Actual display of


1..PROG04_Light_dot.Hex
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Puwede ba I-simulate ito? Yes....! Puwede!


Kayang-kaya ng BASCOM 8051 IDE.

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

68

Setting up the 7-segment on the Hardware


Simulator Window:
Bago magamit ang 7-segment Hardware Simulator,
kailangan i-setup muna ang connections, kung
paano ang connection ng 7-segment LED display
sa Port ng MCU.
[1]

Point the mouse on the digit 8, then Right


Click

FIGURE 5-21 : Actual display of


2..PROG04_display_5

7-segment Software
Simulation
Ang BASCOM 8051 IDE ay mayroon din 7-segment
Hardware Simulator. Tingnan ang Figure 5-22 kung
saan ang 7-segment Simulator na naka-display ng
digit 5

FIGURE 5-23 : Setup the 7-segment LED on the


Hardware Simulator Window
[2]

P3.7

FIGURE 5-22 : Hardware Simulator display of 7segment displaying digit 5


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

A new Window will pop-up as shown in Figure 5-24.

Click each port, chaging all P1.x into P2.x, refer to Figure 5-15 for the correct connection.
Ex. a = P2.4, d = P2.0, b =P2.3, etc... Press
the ENTER key, Click Ok after.
FIGURE 5-24 : Setup the 7-segment LED on the
Hardware Simulator Window

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

69

BYTE Command to

a = P2.4
b = P2.3
c = P2.2
d = P2.0
e = P2.1
f = P2.5
g = P2.6

control the 7-segment LED


Ang ginawang command sa Figure 5-20 ay BIT command dahil isa-isang kino-contorl ang mga paa ng
MCU para mag-outut ng Logic level (1 o kaya 0).

[3]

After setting the correct Bit-port to control


the 7-segment LED, click the OK

[4]

Go back to the Software Simulator window


and click the PLAY icon. The Simulated
response as shown in Figure 5-22 will be
display.

P2.4

Dahil puwedeng i-control ang 7-sgement LED sa


papamigitan ng BYTE command, mas madali itong
i-control. Gawin natin.
Ang gagawin ko ay - uunatin ko (dis-assembled)
ang mga segment ng 7-segment LED. Tingnan ang
Figure 5-25. Ang gusto kung mangyari dito ay icontorl ko ang 8-bit-port ng iisang command, gaya
ng P2 = 0 o kaya P2 =255.

P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0


P2.3

P2.5
P2.0

P2.2

P2.1

P2.0

P2.7

Binary 10001010

The value that we can obtain is 10001010


This is Binary Number

Decimal Equivalent 10001010 = 138

FIGURE 5-25 : 7-segment How to Obtain a one byte value


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

70

The World of LEDs & 7-segment LED Display

Para makuha ang exact value ng Port 2, ginawan


ko ng diagram ang 7-segment LED at ilagay ang
binary value na 1 sa segment na di iilaw at 0 sa
segment na iilaw. Mayroon na series ng 8 bit binary na 10001010. Actually, puwede na itong
ilagay sa Port 2, gaya ng
P2 = &B10001010
Pero, gusto ko makita ang Decimal value, kaya
ginamit ko ang PC calculator at nag-convert ako
mula sa Binary to Decimal. Ang Decimal ng
10001010 ay 138. Puwede nang gamitin sa
P2 = 138
Ang 138 na value ay mag-display ng digit 5. Ang
Listing kung gagamitin ang BYTE command ay
makikita sa Figure 5-26
*******************************************************
3..PROG04_Display_5_Byte_Command
******************************************************
P2 = 138

load 1byte value to P2

Reset P3.7

display on the first digit

End

BYTE Data & Table for


The 7-segment Display
Ang Table 1 ay ipapakita ang Byte value na dapat
ilagay sa P2 para mag-display ng mga number. Ang
mga value na ito ay nakuha sa pamamagitan ng
Figure 5-15 analysis.
Display Digit

Decimal

HEXadecimal

192

C0

221

DD

164

A4

162

A2

147

93

138

8Al

136

88

227

E3

128

80

131

83

160

A0

152

98

Character

FIGURE 5-26 : Listing for 3..PROG04_Display_


5_Byte_command

Table 1 :

The real world display should be the same as in


Figure 5-21.

Figure 5-27 shows the listing to display digit 0~9 on


the first digit of the 7-segment LED in a sequential
manner, keep repeating. The interval between displaying is 1 second.

Basically, if you want to display 5 and dot, the


value of P2 will be
P2 = 10
How? Going back to Figure 5-25, the P2.7 will now
be logic 0 to light the dot, so that, that binary value
will be 00001010, converting it into Decimal will be
10.
See Table 1 how to represent the digit versus data.
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Data to dislay digits 0~9 and the


characted a and b for P2

The Figure 5-29 show the listing for a better way of


BASCOM code using the
READ X
Data Y
instructions. The X is a variable where to store the
value of data after reading the data Y. Just like
putting the Y data into X. See the Listing.

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

The World of LEDs & 7-segment LED Display

*******************************************************
4..PROG04_Count_0_9
******************************************************

71

Here is the PDL for displaying 0~9 on the first digit


of the 7-segment LED, see Figure 5-28.
I created here a variable called

Reset P3.7
Do
P2 =192
Wait 1

: Chapter 5

Read_Data
0

P2 =221
Wait 1

P2 =164
Wait 1

P2 =162
Wait 1

P2 =147
Wait 1

P2 =138
Wait 1

P2 =136
Wait 1

P2 =227
Wait 1

P2 =128
Wait 1

P2 =131
Wait 1

P2 =255
Wait 1

no display

where to store the decimal digit such as 192 to display digit 0, 221 to display digit 1, etc. The Instruction
READ Read_Data
is the exact command to read the data from the
data base. The variable Counting is only used as
general purpose count for the For-To-Next Loop to
display 11 characters, 0~9 plus no display.
Check out the Listing on Figure 5-29.

Loop
End
FIGURE 5-27 : Listing for
4..PROG04_count_0_9

Dimension the Variable


Read_Data->Byte Type
Counting-> Byte Type
Naming Hardware to Control
P2 ->7_segment_Display
P3.7->1st_digit_control
Logic 0--> 1st digit control
Do
Restore the 7_segment_Data
For Counting, 1~11
Read the Data-->Read_Data
Put Read_Data into 7_segment_Display
Delay 1 seconds
Next
Loop
7_segment_Data:
Data 0,1,2,3,4,5,6,,,,,,255
End
FIGURE 5-28 : PDL to display the digits 0~9

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

72

**************************************************************************************************************************
6..PROG04_Count_0_9_Read_Data_1_digit
*************************************************************************************************************************
'DIMENSION VARIABLES
Dim Read_Data As Byte
'Initialized the variable Read_Data to be BYTE type
Dim Counting As Byte
'Initialized the variable Counting to be BYTE type
'NAMING HARDWARE
7_segment_Display Alias P2
'Let P2 to be 7_segment_Display
'This will easilly understand the function of P2 as controlling the 7-segment Display
1st_digit_control Alias P3.7
'Let P3.7 to be 1st_digit_control
'This will denote the control to activate the 1st digit on the dual 7-segment LED
Reset 1st_digit_control
'Put the 1st_digit_contorl to Logic 0 to activate the first digit of the 7-segment LED Display
START OF MAIN PROGRAM
Do
Restore 7_segment_Data
'Instruct the READ instruction to start reading the data
'from the first "data" int he data_base 7_segment_data
For Counting = 1 to 11 step 1
Read Read_Data
'Read the Data from teh data_base 7_segment_data and
'put every data into the Read_Data variable
7_segment_Display = Read_Data
'Now, transfer the vlaue of Read_Data into the 7_segment_Display which caused the 7segment LED display to output the appropriate digit
Wait 1
'Wait for 1 seconds so that we can see the display on the 7-segment LED
Next Counting
'increase counting, if counting is not 11, then continue if counting is equal to 11 then exit the FOR-TO-NEXT
Loop
'Go back to the "DO" and keep repeating
7_segment_data:
Data 192,221,164,162,147,138
Data 136,227,128,131,255
'Here are the data_base which in Decimal which correspond to
'display 0,1,2,3,4,5,6,7,8,9 and no display (255)

End
'End of Program
FIGURE 5-29 : Listing for 6..PROG04_count_0_9_READ_Data_1_digit
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

73

Loop Control Instruction


Do-Loop & While-Wend
Here in Figure 5-32 is another style of instructing
the MCU using the

Another loop control is the


While <condition>
Wend

Do
Loop until <condition>
In here, the For-To-Next loop contorl has been replaced with the Do -Loop Until <condition> and eliminated the variable Counting as in Figure 5-28. The
PDL is shown in Figure 5-30.
The condition to go back in repeating the Loop is
that, 255 is not yet read from the data base. If
the end of data which is 255 has been read, then,
the Do-Loop will exit. The test condition is

The PDL is shown in Figure 5-31. The test condition is


While Read_Data <> 255
. . . .
Wend
The symbol <> means not equal. Kung ang
Read_Value ay hindi 255, i-execute ang mga instructions na nasa loob ng While-Wend.

Loop until Read_data = 255


Dimension the Variable
Read_Data->Byte Type
Dimension the Variable
Read_Data->Byte Type
Naming Hardware to Control
P2 ->7_segment_Display
P3.7->1st_digit_control
Logic 0--> 1st digit control
Do
Restore the 7_segment_Data
Do
Read the Data-->Read_Data
Put Read_Data into 7_segment_Display
Delay 1 second
Loop Until Read_Data = 255
Loop
7_segment_Data:
Data 0,1,2,3,4,5,.............. 255

Naming Hardware to Control


P2 ->7_segment_Display
P3.7->1st_digit_control
Logic 0--> 1st digit control
Do
Initialized Read_Value -->0
Restore the 7_segment_Data
While Read_Data<>255
Read the Data-->Read_Data
Put Read_Data into 7_segment_Display
Delay 1 second
Wend
Loop
7_segment_Data:
Data 0,1,2,3,4,5,.............. 255
End

End
FIGURE 5-30 : PDL to display the digits 0~9
using the Do-Loop Until
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

FIGURE 5-31 : PDL to display the digits 0~9


using the While - Wend

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

74

**************************************************************************************************************************
7..PROG04_Count_0_9_Read_Data_1_digit_Do_loop
*************************************************************************************************************************
'DIMENSION VARIABLES
Dim Read_Data As Byte
'Initialized the variable Read_Data to be BYTE type
Dim Counting As Byte
'Initialized the variable Counting to be BYTE type
'NAMING HARDWARE
7_segment_Display Alias P2
'Let P2 to be 7_segment_Display
This will easilly understand the function of P2 as controlling the 7-segment Display
1st_digit_control Alias P3.7
'Let P3.7 to be 1st_digit_control
'This will denote the control to activate the 1st digit on the dual 7-segment LED
Reset 1st_digit_control
'Put the 1st_digit_contorl to Logic 0 to activate the first digit
'START OF MAIN PROGRAM
Do
Restore 7_segment_Data
'Instruct the READ instruction to start reading the data from the first "data" int he data_base 7_segment_data
Do
Read Read_Data
'Read the Data from the data_base 7_segment_data and put every data into the
Read_Data variable
7_segment_Display = Read_Data
'Now, transfer the vlaue of Read_Data into 'the 7_segment_Display which
caused the 7-segment LED display to output the appropriate digit
Wait 1
'Wait for 1 seconds so that we can see the display on the 7-segment LED
Loop until Read_Data = 255
'
Go back to the inner "DO" and check if Read_Data is 255, if yes then Exit the inner Do-Loop
Loop
'unconditional Loop (repeat forever)
7_segment_data:
Data 192,221,164,162,147,138
Data 136,227,128,131,255
'Here are the data_base which in Decimal which correspond to display 0,1,2,3,4,5,6,7,8,9 and no display (255)
End
'End of Program

FIGURE 5-32 : Listing for 7..PROG04_count_0_9_READ_Data_1_digit_Do_Loop


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

75

**************************************************************************************************************************
8..PROG04_Count_0_9_Read_Data_1_digit_While_Wend
*************************************************************************************************************************
'DIMENSION VARIABLES
Dim Read_Data As Byte
'Initialized the variable Read_Data to be BYTE type
Dim Counting As Byte
'Initialized the variable Counting to be BYTE type
'NAMING HARDWARE
7_segment_Display Alias P2
'Let P2 to be 7_segment_Display
'This will easilly understand the function of P2 as controlling the 7-segment Display
1st_digit_control Alias P3.7
'Let P3.7 to be 1st_digit_control
'This will denote the control to activate the 1st digit on the dual 7-segment LED
Reset 1st_digit_control
'Put the 1st_digit_contorl to Logic 0 to activate the first digit
'START OF MAIN PROGRAM
Do
Read_Data = 0
Restore 7_segment_Data
'Instruct the READ instruction to start reading the data from the first "data" int he data_base
7_segment_data
While Read_Data<>255
'While hte Read_Data is not equal to 255, it will execute the following instructions inside the
WHILE-WEND
Read Read_Data
'Read the Data from the data_base 7_segment_data and put every data into the
Read_Data variable
7_segment_Display = Read_Data
'Now, transfer the vlaue of Read_Data into the 7_segment_Display which caused
the 7-segment LED display to output the appropriate digit
Wait 1
'Wait for 1 seconds so that we can see the display on the 7-segment LED
Wend
'Go back to the "While" and check if Read_Data is 255, if yes then Exit the inner WHILE-WEND Loop
Loop
7_segment_data:
Data 192,221,164,162,147,138
Data 136,227,128,131,255
'Here are the data_base which in Decimal which correspond to display 0,1,2,3,4,5,6,7,8,9 and no display (255)
End
'End of Program

FIGURE 5-33 : Listing for 8..PROG04_count_0_9_READ_Data_1_digit_While_Wend


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

76

The 2nd Digit


Of the 7-segment Display
Ang 7-segment display sa Target Board ay dual
digit, ang mga nakaraan exercises ay ginamit lang
ang first digit.
Para magamit ang 2nd digit, ang

Ang PDL ng program ay makikita sa Figure 5-34.


Ang Listing ay makikita sa Figure 5-35. This program is similar to
7..PROG04_Count_0_9_Read_Data_1_digit_Do_loop

1st_digit_control Alias P3.7


in Figure 5-32, so I high lighted only the IF-Then-Else
insructions.

ay palitan ng
2nd_digit_control Alias P3.6

Ang P3.7 ay para sa control ng 1st digit at ang


P3.6 naman ay para sa 2nd digit.
Ang mga program sa Figure 5-32 at Figure 5-33 ay
applicable na i-display sa 2nd digit, palitan lang ang

Dimension the Variable


Read_Data->Byte Type
Naming Hardware to Control
P2 ->7_segment_Display
P3.7->1st_digit_control
P3.6->2nd_digit_control
Logic 0--> 1st digit control

1st_digit_control Alias P3.7


Reset 1st_digit_control
ng
2nd_digit_control Alias P3.6
Reset 2nd_digit_control
Check it out!!!!

Do
Restore the 7_segment_Data
Do
Read the Data-->Read_Data
Put Read_Data into 7_segment_Display
Delay 400 millisecond
Loop Until Read_Data = 255
If 1st_digit_control---> 0
1st_digit_control-->1
2nd_digit_control --> 0
Else
1st_digit_control-->0
2nd_digit_control --> 1
End If

The If-Then-Else
Condition Flow
Lastly, puwede natin i-display sa digit 1 at saka
digit 2 ang 0~9 na gamit ang If-Then-Else instruction
If <condition> Then
Else
End If
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Loop
7_segment_Data:
Data 0,1,2,3,4,5,.............. 255
End
FIGURE 5-34 : PDL to display the digits 0~9
on the 1st and 2nd Display

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

77

The completed document of Figure 5-25, Listing


9..PROG04_Count_0_9_Read_If_Then_Else
is shown in Figure 5-36. Ang program na tapos na at
gumagana ay dapat lagyan ng completong documento
sa pamamagitan ng comments. Gawin ito para kahit
na taon na ang nakaraan ay madaling maalala ang
ginawang program, at apra madali din maintindihan
ang sinumang babasa.
**************************************************************************************************************************
9..PROG04_Count_0_9_Read_If_Then_Else
*************************************************************************************************************************
Dim Read_Value As Byte
7_segment_Display Alias P2
1st_digit_control Alias P3.7
2nd_digit_control Alias P3.6
Reset 1st_digit_control

Turn On digit 1 -- digit 1 will display first

Do
Restore 7_segment_Data
Do
Read Read_Value
7_segment_Display = Read_Value
Waitms 255
Waitms 255
Loop Until Read_Value=255
If 1st_digit_control = 0 Then
'Kung ang 1st_digit_Display ay naka Logic 0, i-execute ang mga
'susunod na instructions
1st_digit_control = 1
Turn off Digit 1
2nd_digit_control = 0
Turn ON digit 2
Else
'kung hindi logic 0 ang 1st_digit_control, i-execute ang mga
'susunod na instructions
1st_digit_control = 0
Turn On digit 1
2nd_digit_control = 1
Turn Off Digit 2
End If
Loop
7_segment_data:
Data 192,221,164,162,147,138
Data 136,227,128,131,255
End

FIGURE 5-35 : Listing for 9..PROG04_count_0_9_If_Then_Else


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

78

'************************************************************************************************************************
' PROG004 :
Count 0 ~ 9 on the 7-segment Display digit 1 and 2
'************************************************************************************************************************
'PROGRAM DESCRIPTION :
'This program will count 0 to 9 and display on the 1st digit, after that, it will count 0~9 and display on the 2nd
digit , keep repeating the sequence forever.

It used the nexted Do-Loop and If-Then-Else Instructions.

'AUTHOR :
'F. Dandy Menor
'HSEC, Baguio City
'November 27, 2009
Software IDE : BASCOM 8051 IDE (demo version) 2.0.14.0

http://www.mcselec.com
PROGRAM NAME : PROG04_Count_0_9_If_Then_Else.bas
TARGET BOARD :
This program can run only on the Target Board : STARTER MCU-EB8051-2 with AT89S51 MCU
Refer to this Trainer Kit schematic connection wiht the 7-segment LEDs and the book My 8051
DIMENSION THE VARIABLES :
Dim Read_Value As Byte
Let Read_Value to be Byte Type variable
NAMING HARDWARE :
7_segment_Display
Alias P2
Let 7_segment_Display be the name for P2
1st_digit_control
Alias P3.7
Let 1st_digit_control be the name for P3.7 to display on the 1st digit
2nd_digit_control
Alias P3.6
Let 2nd_digit_control be the name for P3.6 to display on the 2nd digit
'==================================================================================
'START OF PROGRAM :
Reset 1st_digit_control
Do

Turn On digit 1 -- digit 1 will display first the counting from 0~9

Start the outer Do -Loop (endless loop)


Restore 7_segment_Data
Read from the first data of the data_base
Do
Start the inner Do -Loop
Read Read_Value
Read data and store in the Read_Value
7_segment_Display = Read_Value
Transfer the Read_Value into
7_segment_Display
Waitms 255
wait for 400 milliseconds, this is the time
Waitms 255
to display each digit
Loop Until Read_Value=255
If the value of Read_Data is 255, it will
exit the inner Do-Loop,otherwise it keep
running inside the ineer Do-Loop

FIGURE 5-36 : Listing for 9..PROG04_count_0_9_If_Then_Else with complete Documents [1/2]


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

SIMPLE MCU Programming

: Chapter 5

The World of LEDs & 7-segment LED Display

79

If 1st_digit_control = 0 Then
'Kung ang 1st_digit_Display ay naka Logic 0, i-execute ang mga
'susunod na instructions
1st_digit_control = 1
Turn off Digit 1
2nd_digit_control = 0
Turn ON digit 2
Else
'kung hindi logic 0 ang 1st_digit_control, i-execute ang mga
'susunod na instructions
1st_digit_control = 0
Turn On digit 1
2nd_digit_control = 1
Turn Off Digit 2
End If
end of If-Then-Else
Loop
unconditional Do-Loop (keep repeating the outer Do-Loop)
7_segment_data:
Data 192,221,164,162,147,138
Data 136,227,128,131,255
Here are the decimal data that denote the decimal digit 0,1,2,3,4,5,6,7,8,9 and blank
'====================================================================================
'END OF PROGRAM
End
Distribution Policy:

--------------------------------------------------------------------------------------------------------------------------------------------------- All BASCOM 8051 PROGRAMS in package with SIMPLE MCU PROGRAMMING

You may use, copy and distribute it FREE OF CHARGE under the following

conditions:

1. For the use with the STARTER MCU-EB8051 Trainer Kit-"2" and NE2 Trainer Kit

2. No changes on the original source file.

3. Exceptions are only possible with a written permission of the author.

Implementation:
All 8051 PROGRAMs in package with the SIMPLE MCU PROGRAMMING have been developed with:

BASCOM 8051 IDE (demo version compiler 2.0.14.0) - visit http:/www.mcselec.com

No formal process has been used during the development, there were no efforts in quality assurance or quality management!
Disclaimer:

-----------------------------------------------------------------------------------------------------------------------------------------------------
All 8051 PROGRAMs in package with the SIMPLE MCU PROGRAMMING is provided as it is, without warranty of any kind.

The author will not be liable for any special, incidental, consequential, indirect or similar damages for any reason in the use

and mis-use of this program.

F. Dandy Menor

Heart Systems, Electronics Center

Baguio City, 2600, The Philippines


myhobby

Http://www.1diycentre.com

Heart

Systems

Educational Trainer
EAF2

Electronics Series

FIGURE 5-36 : Listing for 9..PROG04_count_0_9_If_Then_Else with complete Documents [2/2]


myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

Appendix

Extra

The END of
The 7-segment Display
The introduction of 7-segment LEd Display and
LEDs will temporary end in Chapter 5.
Your interest to explore more on the world of displaying using LEDs and 7-segment LED Display
will walk you to a more interesting adventure of LED
and 7-segment displaying.

The New Target Board


Welcome to LCD Displaying
with NE2ss
As I am writing a section on Chapter 6 - about INPUT Instruction which allow the KIDDY to pressed
the switch to command the program, I am also
debugging the NE2ss Hardware. This is a Hardware dedicated for Personal Home Security Systems - a platform for Programming the Intel 8051
MCU with dedicated for Security Systems. The code
NE2ss means, Night Eyes Security Systems Version 2.

What is good in the NE2ss hardware is that,


mayroon itong
16x2 LCD display
(LCD = Liquid Crystal Display) na gaya ng nasa
Figure 1- 11 at 5-37, na mas-interest ng mga KIDDY,
kasi puwede mag-sulat ng pangalan at number na
mas madali kesa sa pagsulat ng character gaya ng
Hello Baby sa 7-segment display.
Alright! Papalitan ko ang Target Board -- makikita
ang NE2ss hardware sa Figure 5-37. Of course,
ang AT89S51 MCU ay puwedeng tumakbo sa
NE2SS hardwrae at paganahin ang LCD.
Ang Target Board na STARTER MCU-EB8051-2
Trainer Kit ay perfect Target Board, wala nga lang
build-in na LCD, puwede naman dugtungan sa general I/O (Input/Output) sa Con_0_2, pero mas prefer
ng mga KIDDY ang ready made hardware, besides
they are not yet so familiar with lots of Hardware
buffs and havent improved the bad soldering.
The NE2ss is perfect for them for Displaying and to
make a Personal Home Security Project.

315 MHz RF Module

16x2 LCD with amber backlight

MCU (AT89S53, AT89S51 can also be


used)
FIGURE 5-37 : The new Hardware - NE2ss < Night Eyes 2 Security Systems>
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

Heart Systems

Trainer
No. 05

Appendix
Extra

vi

This Page is
Reserved for
Chapter 6

Simple Micro Controller Unit Programming - Part 2

Support the 1DIY Learning


If you have original applications program using the 8051 core, an article about 8051 which is not discussed
here, an enhanced programming technique, a hardware usage of 8051 core, an economical programmer for
the 8051 core specially the ATMEL AT89S series, a preventive measures for MCU breakdown, a start up
topic of MCU, an error you find or comment which will improved the DIY Learning Volume No. 5 - Simple
Micro Controller Programming, I encourage you and everyone to contribute to support the DIY Learning.
Your contribution is like a candle that shines to MCU Hobbyist which lead them to improve themselves to be
a competitive firmware developer / software engineer in the country.

I am 1diyTEAM
myhobby Educational Trainer
Heart
EAF 2 Electronics Series
Systems

my8051_ph@hotmail.com / danztherock@gmail.com

Heart Systems

Appendix

Trainer
No. 05

vii

Extra

-- END of Part 1 - -

THE Simple Micro Controller Unit Programming - Part 1 ends here


but the Adventure with STARTER MCU-EB8051-2
should go on !!!!!
Watch Out for the relesed of - - - -

Part 2

visit

http://www.1diycentre.com

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

myhobby Educational Trainer


Heart
EAF 2 Electronics Series
Systems

Heart Systems