Inside the DS5000 is a 128 byte memory area called internal ram random access memory.. [This is prob03.asm] .equ switch,144 ;p1 bit 0 is now called switch .equ led,128 ;p0 bit 0 is no
Trang 1Lesson 1: The Language of
Microprocessors
Most people think that computers are some kind of complicated device that is impossible
to learn and infinitely intelligent, able to think better than a person The truth is much less glamorous A computer can only do what the programmer has told it to do, in the form of
a program A program is just a sequence of very simple commands that lead the
computer to solve some problem Once the program is written and debugged (you hardly ever get it right the first time), the computer can execute the instructions very fast, and always do it the same, every time, without a mistake
And herein lies the power of a computer Even though the program consists of very simple instructions, the overall result can be very impressive, due mostly to the speed at which the computer can process the instructions Even though each step in the program is
very simple, the sequence of instructions, executing at millions of steps per second, can appear to be very complicated, when taken as a whole The trick is not to think of it as a whole, but as a series of very simple steps, or commands
Most microprocessors, or very small computers, (here after referred to simply as micro's)
have much the same commands or instructions that they can perform They vary mostly
in the names used to describe each command In a typical micro, there are commands to move data around, do simple math (add, subtract, multiply, and divide), bring data into the micro from the outside world, and send data out of the micro to the outside world Sounds too simple it is
A typical micro has three basic parts inside They are the Program Counter, Memory, and Input / Output The Program Counter keeps track of which command is to be executed The Memory contains the commands to be executed The Input / Output handles the transfer of data to and from the outside world (outside the micro's physical package) The
micro we'll be using is housed inside a 40 pin package, or chip There are many other
actual parts inside our micro, but too much, too soon, would be too confusing right now
We will, however, learn about each and every single one
A Simple Program
As stated before, a program is a sequence or series of very simple commands or
instructions A real world example program might be the problem of crossing a busy street
Step 1: Walk up to the corner of the street and stop
Step 2: Look at the traffic light
Step 3: Is the light green for your direction of travel?
Trang 2Step 4: If the light is red, goto step 2 (otherwise continue to step 5)
Step 5: Look to the left
Step 6: Are there cars still passing by?
Step 7: If yes, goto step 5 (otherwise continue to step 8)
Step 8: Look to the right
Step 9: Are there cars still passing by? (there shouldn't be any by now, but, you never know!)
Step 10: If yes, goto step 8 (otherwise continue to step 11)
Step 11: Proceed across the street, carefully!!
Now this may seem childish at first glance, but this is exactly what you do every time you cross a busy street, that has a traffic light (at least, I hope you do) This is also exactly how you would tell a micro to cross the street, if one could This is what I mean by a sequence or series of very simple steps Taken as a whole, the steps lead you across a busy intersection, which, if a computer did it, would seem very intelligent It is
intelligence, people are intelligent A programmer that programmed these steps into a micro, would impart that intelligence to the micro
The micro would not, however, in this case, know what to do when it got to the other side, since we didn't tell it A person, on the other hand, could decide what to do next, at a moments notice, without any apparent programming In the case of a person, though, there has been some programming, it's called past experiences
Another program might be to fill a glass with water from a faucet
Step 1: Turn on the water
Step 2: Put the glass under the faucet
Step 3: Look at the glass
Step 4: Is it full?
Step 5: If no, goto step 3.(otherwise, continue to step 6)
Step 6: Remove the glass from under the faucet
Step 7: Turn off the water
Trang 3This is a simpler program, with fewer steps, but it solves a problem, to fill a glass with water In a micro, the problems are different (they could be the same if you've made some kind of servant robot), but the logical steps to solve the problem are similar, that is, a series of very simple steps, leading to the solution of a larger problem
Also notice that since the steps are numbered, 1 through 7, that is the order in which they're executed The Program Counter, in this case, is you, reading each line, starting with 1 and ending with 7, doing what each one says In a micro, the Program Counter automatically advances to the next step, after doing what the current step says, unless a
branch, or jump, is encountered A branch is an instruction that directs the Program
Counter to go to a specific step, other than the next in the sequence The branch in this
example is step 5 Not only is this a branch, but it is a conditional branch In other words,
based on whether the glass is full or not, the branch is taken, or not A micro has both branch and conditional branch instructions Without this ability to reuse instructions, in a sort of looping action, a solution would take many more steps, if it would be possible at all
The point of this lesson is to show how a simple set of instructions can solve a bigger problem Taken as a whole, the solution could appear to be more complicated than any of the separate steps it took to solve it Well that wasn't so tough, was it?
The most difficult problem to be solved in programming a micro is to define the problem you are trying to solve Sounds like some kind of play on words, but I assure you, it's not
This is the Logical Thought Process I mentioned earlier The mark of a good programmer
is one who has this ability It is, in my humble opinion, a developed skill, coupled with some talent, and a good understanding of the problem you're trying to solve In most cases, God has endowed you with the talent and the ability to reason, all you need do is
develop the skill of Problem Solving
My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional
teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
Decimal Vs Binary (and Hex)
Most people have learned to use the Decimal numbering system for counting and
calculations But micros use a different system It's called Binary All numbering systems follow the same rules Decimal is Base 10 and Binary is Base 2 The base of a system refers to how many possible numbers can be in each digit position In decimal, a single digit number is 0 through 9 In binary a single digit number is 0 or 1 In decimal, as you
Trang 4count up from 0, when you reach 9 and add 1 more, you have to add another digit
position to the left and carry a 1 into it to get 10 (ten) Ten is a two digit decimal number
In binary, as you count up from 0, when you reach 1 and add 1 more, you have to add another digit position to the left and carry a 1 into it to get 10 (two decimal) While this is exactly what you do in decimal, the result looks like ten What you have to do is get past seeing all numbers as decimal While decimal 10 (ten) looks like binary 10 (two decimal) they represent different decimal values It is still useful to think in decimal, since that's what we're used to, but we have to get used to seeing numbers represented in binary
Another small difference between decimal terminology and binary is that in binary a digit
is called a bit It gets even more confusing by the fact that 4 bits make a nibble Two nibbles make a byte Two bytes make a word Most numbers used in a micro don't go beyond this, although there are others Using what I've just said, if two nibbles make a byte, you could also say that a byte is eight bits
To represent a binary number larger than 4 bits, or a nibble, a different numbering system
is normally used It is called hexadecimal, or Base 16 A shorter name for hexadecimal is simply hex, and that's what we'll use here after In this system there are 16 possible numbers for each digit For the first 10, 0 through 9, it looks like decimal Unlike
decimal, when you add 1 more to 9, you get A I know that having a letter to represent a number is really confusing, but they had to call it something, and A is what they chose
So a hex A is a decimal 10 (ten) The numbers count up from A through F Just to clarify this here is the sequence of counting in hex from 0 to where you have to add another digit position to the left 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (no G) This represents a decimal count of 0 through 15 At a count of F (15 decimal), if you add 1 more you get
10 (oh no! not another 10 that means something else!!) Sad but true
Let's regroup here A binary 10 (one zero) is decimal 2, a decimal 10 is ten, and a hex 10
is decimal 16 If you can get this concept, you will have conquered the most difficult part
of learning micros
I need to get past one more obstacle, the idea of significance In a decimal number like
123, 3 is the least significant digit position (the right most digit) and 1 is the most
significant digit position (the left most digit) Significance means the relative value of one digit to the next In the number 123 (one hundred twenty three) , each number in the right hand most digit position (3) is worth 1 The value of each number in the next most
significant digit position (2) is worth ten and in the most significant digit position (1) each is worth a hundred I'm not trying to insult your intelligence here, but rather to point out the rule behind this The rule is that no matter what base you're working in, as you start adding digits to the left, each one is worth the base times (multiplied) the digit to the right In the decimal number 123 (base 10), the 2 digit position is worth 10 times the 3 digit position and the 1 digit position is worth 10 times the 2 digit position Hence the familiar units, tens, hundreds, and so on For some reason, for most people, this makes sense for decimal (base 10) but not for any other base numbering system
Trang 5The very same is true for binary The only difference is the base Binary is base 2 So in binary the least significant bit (remember bits?) is worth 1 ( this happens to be the same for all bases) The next most significant bit is worth 2, the next worth 4, the next worth 8, and so on Each is 2 times (base 2) the previous one So in an 8 bit binary number (or byte, remember bytes?), starting from the right and moving left, the values for each of the
8 bit positions are 1, 2, 4, 8, 16, 32, 64 , and 128 If you've got this, you have passed a major milestone, you should go celebrate your passage If you haven't, I would re-read the above until you do, and then go celebrate!! ( By the way, if you didn't get this the first time through, join the crowd I didn't either!!)
In hex (base 16) the same rule applies In a 4 digit hex number, starting at the right and working left, the first digit is worth 1 ( hey that's just like decimal and binary!!), the next
is worth 16 (base times the previous digit), the next is worth 256 (16 X 16), and the most significant is worth 4096 (256 X 16) One last note, hex is just binary described another way A hex digit is a binary nibble ( remember nibbles?) Both are 4 bit binary values
Trying to wrap all this confusion up in review, 4 bits is a nibble or a hex digit Two hex digits is a byte or 8 bit binary A 4 digit hex number is a word, or 16 bit binary or 4 nibbles, or 2 bytes You may have to review the previous paragraphs a few times (I did!!)
to get all the relationships down pat, but it is crucial that you are comfortable with all I've said, so that what follows will make more sense
Let's take a few example numbers and find the decimal equivalent of each Let's start with the binary number 1011, a nibble Starting at the right and moving left, the first digit
is worth one, because there is a 1 there The next is worth two, because there is a 1 in it The next would be worth 4, but since there is a 0, it's worth zero The last or most
significant is worth eight, since there is a 1 in it Add all these up and you get eleven So
a binary 1011 is decimal 11 Also, since this could be a hex digit, the hex value would be
B
Let's take a longer binary number 10100101 Starting at the right moving left, the first is worth 1, the next is worth 0, the next is worth 4, the next is 0, the next is 0, the next is worth 32, the next is 0, and the last is 128 Adding all these up you get decimal 165 Dividing this number up into two hex digits, you get A5 So binary 10100101, decimal
165, and hex A5 are all the same value Using hex, the rightmost digit is worth 5, and the most significant digit is worth 160 (10 X 16), resulting in decimal 165 If you understand this, your ready to move on, leaving the different systems behind If you don't, keep reviewing and studying until you do If you don't understand and still continue, you will
be confused by what follows I promise that once you get this, the rest is easier
This ends the second lesson I hope this wasn't too daunting or difficult, but there was a lot to get through The rest of the course should be a little easier Learning a new
numbering system is like learning a new language It's a little cumbersome at first, but it gets easier
Trang 6My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional
teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
A Micro's Commands (or Instructions)
I would like to take a moment and address a few things that will make it easier for me to describe things to you later In a micro we refer to a binary number like 1111 as 1111b, a decimal number like 123 as 123, and a hex number like A5 as A5h So don't be confused
by the letters following numbers, it is easier than having to say the whole words binary, decimal, or hexadecimal, but still indicate it By doing this, there is no doubt what base a multidigit, or multibit, number is in
Also there is another kind of memory, called flags Flags are single bit numbers used to indicate different conditions They are called flags because they flag the program of
events or conditions If a flag is raised, or has a 1 in it, it is said to be SET If it is a 0, it is said to be CLEARED
One other thing, in an 8 bit byte the 8 bits are referred to as bits 0 through 7, with bit 0 being the right most, or least significant, and bit 7 as the left most or most significant In micros, a 0 is just as real a number as 1 or 8, and it should not be thought of as "nothing"
Lastly, there are various Registers inside a micro These vary from micro to micro, but all contain a register called the Accumulator It is also referred to in some as the a register I will be using the accumulator in the following discussion It is a type of memory for storing temporary results and is 8 bits wide, or a byte, as are most places that data can be put inside the micro
In the micro we will be using, there are 5 different types of instructions and several
variations of each, resulting in 110 different instructions These 5 types are
ARITHMETIC, LOGICAL, BOOLEAN, BRANCHING, and DATA TRANSFER
ARITHMETIC
The arithmetic instructions include addition, subtraction, division, multiplication,
incrementing, and decrementing There are two flags used with arithmetic that tell the program what the outcome of the instruction was One is the Carry (C) flag The other is the Zero (Z) flag The C flag will be explained in the following example of addition The
Z flag, if set, says that the result of the instruction left a value of 0 in the accumulator We will see the Z flag used in a later lesson
Addition
Trang 7This is straight forward and is simply to add two numbers together and get the result However there is one more thing If, in the addition, the result was too big to fit into the accumulator, part of it might be lost There is a safeguard against this Take the case of 11111111b (255) and 11111111b (255) These are the largest numbers that can fit into an
8 bit register or memory location.You can add these as decimal numbers, since I gave you their values in decimal also, and you would get 510 The binary value for 510 is 111111110b (9 bits) The accumulator is only 8 bits wide, it is a byte How do you fit a 9 bit number into 8 bits of space? The answer is, you can't, and its called an OVERFLOW condition So how do we get around this dilemma? We do it with the CARRY (C) flag If the result of the addition is greater than 8 bits, the CARRY (C) flag will hold the 9 th bit
In this case the accumulator would have a 11111110b (254) and the C flag would be a 1,
or set This 1 has the value of 256 because this is the 9th bit We haven't covered a 9 bit number, but they come up all the time as overflows in addition Since we are using base
2, and we found out in lesson 2 that the 8th bit (bit 7) in a byte is worth 128, then the 9th bit is worth 2 times that, or 256 Adding 254 and 256, we get 510, the answer, and we didn't loose anything, because of the C flag Had the result of the addition not caused an overflow, the C flag would be 0, or cleared
Multiplication and Division
In the micro we will be using, are multiply and divide instructions, but I will wait till later
to talk about them They do, however, just what the names suggest
Two other instructions are included in the arithmetic group They are increment and decrement These instructions are used to count events or loops in a program Each time
an increment is executed, the value is incremented by 1 A decrement, decrements the value by 1 These can be used with conditional jumps to loop a section of program, a certain number of times We will see these used later
LOGICAL
In micros there are other mathematical instructions called logical instructions These are
OR , AND, XOR, ROTATE, COMPLEMENT and CLEAR These commands are
usually not concerned with the value of the data they work with, but, instead, the value,
or state, of each bit in the data
OR
Trang 8The OR function can be demonstrate by taking two binary numbers, 1010b and 0110b When OR'ing two numbers, it doesn't matter at which end you start, right or left Let's start from the left In the first bit position there is a 1 in the first number and a 0 in the second number This would result in a 1 The next bit has a 0 in the first number and a 1
in the second number The result would be 1 The next bit has a 1 in the first number and
a 1 in the second number The result would be a 1 The last bit has a 0 in the first number and a 0 in the second number, resulting in a 0 So the answer would be 1110b The rule that gives this answer says that with an OR, a 1 in either number results in a 1, or said another way, any 1 in, gives a 1 out
AND
AND'ing uses a different rule The rule here is a 0 in either number will result in a 0 , for each corresponding bit position Using the same two numbers 1010b and 0110b the result would be 0010b You can see that every bit position except the third has a zero in one or the other number Another way of defining an AND is to say that a 1 AND a 1 results in a
1
XOR (eXclusive OR)
XOR'ing is similar to OR'ing with one exception An OR can also be called an inclusive
OR This means that a 1 in either number or both will result in a 1 An eXclusive OR says that if either number has a 1 in it, but not both, a 1 will result A seemingly small
difference, but crucial Using the same two numbers, the result would be 1100b The first two bits have a 1 in either the first or the second number but not both The third bit has a
1 in both numbers which results in a 0 The fourth has no 1's at all, so the result is 0 The difference may seem small, even though the OR and XOR result in different answers The main use of an XOR is to test two numbers against each other If they are the same, the result will be all 0's, otherwise the answer will have 1's where there are differences Compliment
Complimenting a number results in the opposite state of all the 1's and 0's Take the number 1111b Complimenting results in 0000b This is the simplest operator of all and the easiest to understand It's uses are varied, but necessary, as you'll see later
Trang 9This instruction clears, or zero's out the accumulator This is the same as moving a 0 into the accumulator This also clears the C flag and sets the Z flag
BOOLEAN
There are other commands that deal with single bit values In a program, an 8 bit byte location may be used as 8 separate flags or indicators, defined by the programmer, and used to signal events or conditions between separate parts of a program This might be a bit that indicates whether a door is open or closed, or whether a key has been pressed or a switch is closed or open These bits or flags are usually the mechanism that keeps law and order in the program, and insures that the program does not get "lost" or misdirected We'll see how this works later
There are instructions to do the same things with bits that are done with bytes by other instructions You can AND, OR, SET, CLEAR, or MOVE bits You will see in a later lesson how these are used
BRANCHING
There are also program flow commands These are branches or jumps They have several different names reflecting the way they do the jump or on what condition causes the jump, like an overflow or under flow, or the results being zero or not zero But all stop the normal sequential execution of the program, and jump to another location, other than the next instruction in sequence
Jump on Bit
These instructions let you make a jump based on whether a certain bit is set (a 1) or cleared (a 0) This bit can be the CY (carry) flag, the Z (zero) flag, or any other bit Compare and jump
These instructions compare two values and jump based on the result In lesson 1 we looked at a glass filling with water and compared it to a full one in our mind, and if it wasn't full we looked at it some more This caused that program to loop, or wait, until the glass was full before continuing on
Call
There is also a variation on a jump that is referred to as a CALL A CALL does a jump, but then eventually comes back to the place where the CALL instruction was executed and continues with the next instruction after the CALL This allows the programmer to
create little sub-programs, or subroutines, that do repetitive tasks needed by the main
program This saves programming time because once the subroutine is written, it can be used by the main program when ever it needs it, a kind of way to create your own
instructions
Trang 10This is a variation on the exchange instruction Here the lower nibble, or digit, is
exchanged with the lower nibble of the other location The upper nibble is the left 4 bits (bits 4 through 7) and the lower nibble is the right 4 bits (bits 0 through 3)
possible ways to get to a memory location
My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional
teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
The DS5000 Micro controller
Well, finally, this is what all the previous lessons have been getting you ready for To start looking at the micro we will be using in this course All the previous lessons have been laying the ground work and basic concepts common to most micros The one we will be using is the Dallas Semiconductor DS5000 micro controller This chip, in my mind, is one of the neatest ones ever made Dallas Semiconductor knew what to put in a chip There are a few external parts needed along with the DS5000, but they are few, and
Trang 11cheap to buy I will give you a schematic and a parts list, along with the places to get all the parts later
I would recommend that you get the Secure Micro controller Data Book (in PDF format)
from Dallas Semiconductor, as soon as you can The paperback version is out of print
now The telephone number is 972 - 371 - 4448 They are located in Dallas Texas at 4401
South Beltwood Parkway, zip 752444-3292 It's loaded with information and is
absolutely crucial for a good understanding of the DS5000 The book actually covers several versions of the 5000 series of microcontroller chips
My hope is that you will buy one of the DS5000 parts and really start building things with it I think it costs about $75 Cheap, considering what you can do with it But don't buy it yet, I'll tell you when There is still lots to learn before you will be able to start building something with it
The DS5000 is a microcontroller based on the Intel 8051 microprocessor It looks like an
8051 as far as programming and the general pinout of the package The pinout refers to
the physical package and what each pin on the package is used for But the DS5000 is much more There are 40 pins on the package Of these pins, 32 are connections to the outside world that information can be brought into or out of the DS5000 There are a few other pins, but more on them later
These 32 pins show up inside the package as 4 ports Each port is 8 bits wide and have the names p0, p1, p2, and p3 Each port can be inputs or outputs to or from the outside Besides being inputs and outputs, p0, p2, and p3 have special functions assigned to them P0 and p2 are used for external memory interface, which we will not use since there is plenty of memory inside the DS5000 These 16 pins will be used for input / output But if
we were talking about an 8051, this is where memory would connect Half of the pins of port 3 will be used for their special functionality The other four pins of p3 will be used
as regular inputs and outputs, not using their special functions These functions will be discussed later, however
Two pins of p3 are for serial communications with the DS5000 Through these pins, serial data can be sent or received with another computer This is how we will load a program into the DS5000 from a PC
Two more pins are for a different kind of input called interrupts In our example in lesson
1 of the program where we are standing at the street corner, watching the light and the traffic, if a person walked up and tapped us on the shoulder and asked what time it is, this would be an example of an interrupt It doesn't alter the program we are doing, it just temporarily stops us while we tell the time to the person As soon as we tell the time, we
go back to watching the lights and traffic as before This describes the action of an
interrupt
The interrupt has a program associated with it to guide the micro through a problem In the case of the above example, this program would be to look at our watch, read the time,
Trang 12and then tell it to the person This is called an interrupt service routine Each time an
interrupt occurs, the current program is temporarily stopped and the service routine is executed and when complete, returns to the current program We will spend a lot more time later describing interrupts and how we'll use them
Inside the DS5000 is a 128 byte memory area called internal ram (random access
memory) The first 32 bytes are used for 4 banks of registers They are called bank 0, bank 1, bank 2, and bank 3 When the micro is first powered up, bank 0 is the selected bank Afterwards there is a command which allows switching to another bank, but we won't do that right now Inside each bank there are 8 seperate registers They are called r0, r1, r2, r3, r4, r5, r6, and r7 These registers are used for temporary storage of whatever
is needed by the program There is also a register that is the accumulator called a This
register is different from the other 8 registers It is used to accumulate the results of
various instructions like add or sub (subtract)
The next 16 bytes of internal ram are for bit storage and retrieval This allows for 128 different bits that can be used by the program Most early micros didn't have this very useful feature
The last 80 bytes of internal ram are for general storage and is used for many different things
There is another 128 byte area of memory called the special function registers This is
where p0, p1, p2, and p3 are located The accumulator and program counter are also in this area along with other registers that control various aspects of the operation of the DS5000 More on these as they come up later
Lastly there is one more area of memory that is divided into two parts The program
memory and the data memory This is in reality a block of 32K (32768) bytes of ram that,
through the programming of one of the special function registers, can be divided into the two sections In most of our work we will divide this up into 16K (16384) bytes of
program and 16K (16384) bytes of data The program memory is write protected so that
an errant program can't change itself accidently and really make a mess out of things The data memory can be written to or read from as needed by the program
There is a lithium battery inside the DS5000 that keeps all the memory alive when power
is turned off This is one of the neatest features of the DS5000, and I don't know of another micro that has this In most micros there is PROM ( programmable read only memory) or EPROM (erasable programmable read only memory), as the program
memory PROM can be programmed once and that's it EPROM can be programmed and then erased with ultra violet light through a glass window in the micro's package But this requires a special programmer and an ultra violet light source for erasure (over $125 worth of stuff!!)
But in the DS5000, 2 of the pins can be placed in a special state, and the program
downloaded to the chip through the serial port from a PC There is a special program
Trang 13inside the DS5000 that allows all this to happen, that goes away when these 2 pins are returned to their normal state This special program can be accessed anytime to change the user program by, in our case, flipping a switch, downloading the program, and then returning the switch to its normal position Neat, huh? It takes just a few seconds to do this, where it used to take a half hour to go through the erase / program cycle of an
EPROM based micro And no special hardware is needed, other than the serial port This speeds up program development and allows a change to be made to the program, downloaded to the micro, and executed, in under a minute Slick!!! It will still take plenty
of time to develope and debug a program, but very little time or effort to get the program into the DS5000, ready to run
I have also located a free 8051 simulator for your use that will allow you to test small programs out on your PC without downloading them to the DS5000 You can see all the registers change as the program is stepped through each instruction This greatly speeds
up the debugging process, and is an educational tool that shows how each instruction affects the various parts of the DS5000 We will use this in later lessons to illustrate the different instructions and programs that we will write
There are other features to be covered later, as they come up In the next lesson we will start looking at assembly language, the method we will use to write a program
My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.net/personal/index.html
What is Assembly Language?
Inside the DS5000, instructions are really stored as hex numbers, not a very good way to
look at them and extremely difficult to decipher An assembler is a program that allows
you to write instructions in, more or less, english form, much more easily read and
understood, and then converted or assembled into hex numbers
The program is written with a text editor ( I'll furnish you with one), saved, and then assembled by the assembler (I'll furnish you with one) The result is the file you
download to the DS5000 Here is an example of the problem of adding 2 plus 2: [This file will be called prob01.asm]
mov r0,#2
mov a,#2
add a,r0
The first line moves a 2 into register r0 The second moves a 2 into the accumulator This
is all the data we need for the program The third line adds the accumulator with r0 and
Trang 14stores the result back into the accumulator, destroying the 2 that was originally in it The accumulator has a 4 in it now and r0 still has a 2 in it
Assembly language follows some rules that I will describe as they come up With most instructions, especially those involving data transfer, the instruction is first, followed by
at least 1 space, then the destination followed by a comma, and then the source The
destination is where the result of the instruction will end up and the source is where the
data is coming from
Next we will read a switch, and light an led if the switch is pressed Bit 0 of p1 will be the switch When the switch is closed or pressed, bit 0 will be a 1, and if the switch is open or not pressed, bit 0 will be a 0
Bit 0 of p0 will be the led If bit 0 is a 0 the led is off and if bit 0 is a 1, the led will be on All the other bits of both p0 and p1 will be ignored and assumed to be all 0's, for the sake
of discussion [This is prob02.asm]
start: mov a,p1 ;read the switch
mov p0,a ;write to the led
sjmp,start ;go to start
The first line has something new It's called a label In this case it is start: A label is a
way of telling the assembler that this line has a name that can be referred to later to get back to it All labels are followed by the symbol : , which tells the assembler that this is a label Also a comment can be added to the line to remind you of what that line does A comment is always started with a ; which tells the assembler to ignore all that follows on that line because it is a comment
In the first line we also read the switch by reading p1 (the source) and putting it into the accumulator (the destination)
The next line writes the accumulator, which has the switch in it (the source) to p0, which has the led attached (the destination)
The last line jumps back to start This completes the loop of reading the switch and writing to the led
I hope you see that if the switch is closed, the first line will result in a 1 in the
accumulator In the second line, writing a 1 to p0 will light the led If the switch is open
in line 1, there will be a 0 in the accumulator which, when written to p0, will turn off the led This loop would continue, endlessly, reading the switch and writing the led, until the micro is turned off, or a new program loaded If the switch is pressed, the led will be lit, otherwise it won't
This particular problem could have been solved with just a switch connected to an led, like a light is connected to a wall switch in your house But with a micro in the loop, much more could be done We could have a clock that also turns on and off the led based
Trang 15on time Or we could monitor the temperature and turn the led on and off based on what temperature it is Or we could monitor several switches and turn the led on and off based
on a combination of switches
In the above example we assumed that the other bits of p0 and p1 were all zeros But in reality, each of these bits could have a function assigned to them Then we would need to look only at bit 0 in p0 and bit 0 in p1 This further complicates the problem
In assembly we can assign a name to a bit and refer to it by that name, instead of a bit in a
port This is done with an equate directive Directives are assembler commands that don't
result in program but instead direct the assembler to some action All directives start with
a period
In the DS5000 there are many bit locations 128 of them were discussed in the previous lesson There are others like all the bits in the accumulator, all the bits in the p0, p1, p2, and p3 ports Each one is assigned an address by the DS5000 and can't be changed The first 128 are the 16 bytes of internal ram following the first 32 bytes Bit 0 of p0 is
address 128 (80h) and bit 0 of p1 is address 144 (90h) Now let's look at what this looks like in assembly [This is prob03.asm]
.equ switch,144 ;p1 bit 0 is now called switch
.equ led,128 ;p0 bit 0 is now called led
start: mov c,switch ;get the state of the switch and put in carry
mov led,c ;mov the carry flag to the led
sjmp start ;jump to start
This has the same result as the previous program, but doesn't assume anything about the other bits in p0 and p1 Also the equate only has to be made once at the start of the
program, and thereafter the name or label is used instead of the bit number This makes things much simpler for the programmer Also the carry flag is used with bit instructions like the accumulator is used for byte instructions All bit moves must be through the carry flag All equates must be defined before they are used in a program This holds true for labels also Another advantage of naming bits with an equate is that if, later in the design process, you decide to use a different bit for the led or the switch, only the equate has to
be changed, not the program itself
Another way to do the 2 plus 2 problem is to use a similiar technique This directive is
the reserve storage directive Here is how this would look in assembly: [This is
prob04.asm]
first: rs 1 ;reserve a byte called first
secnd: rs 1 ;reserve a byte called secnd
mov first,#2 ;put a 2 into first
mov secnd,#2 ;put a 2 into secnd
mov a,first ;mov first into accumulator
add a,secnd ;add secnd to accumulator
Trang 16In the first line we see the rs directive The first part is the name or label, the second is the directive, and the third is the amount of storage to be assigned to the name In this case we only needed 1 byte for each variable so only 1 was reserved for each Also you will notice that everything lines up vertically This is only for readability for the
programmers sake of clarity The rule that the assembler uses is that at least 1 space must
be between each part of the line Also the names can be much longer, but I prefer short names so I don't have to type so much Plus by using short names, more space is left for the comment field Comments are very important When you initially write a program, the tendancy is not to write much in the comment field because you're in a hurry But if you have to come back to it a few weeks later, it's much easier to understand what you've written if you've taken the time to write good comments Also good comments help in debugging
In this example, we reserved two bytes and stored a 2 in each one Then we got the first one (first) and put it into the accumulator Then we added the second (secnd) to the accumulator Now the accumulator has a 4 in it, and first and secnd still have a 2 in them This method also has the advantage of being able to not worry which byte is first and which byte is secnd, because the assembler takes care of it for you The difference
between a rs and a equ is that with the equ you tell the assembler exactly which bit you want to name, where with the rs, the order of the reserve storage determins which byte is which It really doesn't matter which is which, because you refer to them as names
instead of actual locations
In the DS5000, there are several ways of addressing variables One is the register
addresses, r0, r1, r2, and so on Another is by direct addressing This uses the actual
address in internal ram where the variable is Using a name with the rs directive is a form
of direct addressing Here the assembler assigns the actual address of the named variable
by which order the rs's are in If the variable first was actual location 0 then the variable secnd would be actual location 1 Location and address mean the same thing When the assembler assembles the program, the names are dropped (to protect the innocent) and the actual addresses are used
Another form of addressing is called immediate addressing This is the form used in the
original 2 plus 2 problem It is indicated by the # symbol in front of the number This tells the assembler that the number is in the instruction, not somewhere else Also this method was used in the above example where the variables first and secnd were loaded with a 2 The 2 was in the instruction not somewhere else But when we started getting the
numbers to add we used direct addressing, using the names Here the 2's were in locations and not in the instruction Here are those instructions and what type of addressing was used in each:
first: rs 1 ;this is a directive not an instruction
secnd: rs 1 ;so is this one
mov first,#2 ;direct,immediate
mov secnd,#2 ;direct,immediate
mov a,first ;implied,direct
add a,secnd ;implied,direct
Trang 17Here's the original 2 plus 2 problem and the addressing modes of each instruction:
mov r0,#2 ;register,immediate
mov a,#2 ;implied,immediate
add a,r0 ;implied,register
Notice that each instruction can have two different addressing modes, one for the
destination and one for the source Also notice that the a is an implied address Register
refers stictly to the registers r0, r1, r2, and so on, even though I've referred previously to the accumulator as a register This comes partly from my experience with the Zilog Z-80
microprocessor, where the accumulator is a register In the DS5000 the accumulator is in
the special function registers but the instruction implies that the accumulator will be used
as either the source or the destination, depending on the instruction Implied is a method
of addressing that shortens the number of bytes any particular instruction assembles into
To digress just a little here, an instruction like add a,r0 is a one byte instruction In other words this instruction would end up inside the DS5000 as one byte Part of the byte
is the opcode and the other part is which register is affected or used The reason for this is
that a prime concern in programming a micro is how may bytes the program will actually take up inside the micro, after it's been assembled The idea is to cram as much as
possible into as few bytes as possible This is why implied addressing is used It limits choices in the use of the instruction, you always have to use the accumulator as either the source or the destination, but it shrinks the size of the instruction, so that more
instructions can fit inside the micro This is a choice made by the maker of the micro, and
is not up for discussion It's a trade off of flexibility vs size That's why you'll see lots of instructions that use the accumulator This is the best way to describe implied addressing
In the case of an instruction like add a,secnd ,two bytes are assembled The first byte says that this is an add instruction and that the accumulator is implied as the destination The second byte is the direct address of the source variable, secnd This is transparent to the programmer because we are using an assembler, but the underlying results are
noteworthy when trying to cram the most into the DS5000 Well enough of that We will probably get into this again, later
Another form of addressing variables is called register indirect or just plain indirect
addressing This is a little more complicated Here the address is held in a register, either r0 or r1 The following is another example of the 2 plus 2 problem using register indirect addressing [This is prob05.asm]
buffer: rs 2 ;reserve two locations for the data
mov r0,#buffer ;set r0 to the start of the buffer
mov @r0,#2 ;put a 2 into the first location of the
buffer
inc r0 ;increment r0 to point to the second byte
mov @r0,#2 ;put a 2 into the second location of the buffer
mov r0,#buffer ;set r0 to the start of the buffer
mov a,@r0 ;get the first 2
inc r0 ;step to the second 2
add a,@r0 ;add the second 2 to the first 2
Trang 18Line 1 is again a reserve storage directive, but this time we are reserving two locations, one for each 2 in the problem Line 2 sets r0 to point to the first location of the buffer we've created with the rs directive As stated before, the # means immediate, but in this case the assembler sees that the first character after the # is not a number, and, instead finds the label buffer and gets the direct address of where it is in the internal ram and puts that address in r0, immediately That's a mouthfull but that's what happens on that one line!
Line 3 stores a 2 in the first location of buffer The @ symbol tells the assembler that the following register holds the address (indirect) of where to put the 2 into So after this instruction, the first byte of buffer has a 2 in it Line 4 increments or steps r0 to the second location in buffer If r0 had the address of the first byte, then incrementing it by
1 now results in the address of the second byte being in r0
Line 5 does the same thing that line 3 did, except that the 2 is stored in the second byte of
buffer Line 6 does the same thing that line 2 did, getting r0 to point to the first location
in buffer Line 7 moves the first byte of buffer (the first 2)into the accumulator Line
8 steps r0 to the address of the second 2 in buffer Line 9 adds the second 2 to the first 2 and stores the result in the accumulator
Repeating what we did for the other example programs, here are the addressing modes of each line:
buffer: rs 2 ;this is a directive, not an instruction
mov r0,#buffer ;register,immediate
mov @r0,#2 ;indirect,immediate
inc r0 ;this is arithmetic, adding 1 to r0
mov @r0,#2 ;indirect,immediate
mov r0,#buffer ;register,immediate
mov a,@r0 ;implied,indirect
inc r0 ;arithmetic, adding 1 to r0
add a,@r0 ;implied,indirect
Here is another example of the 2 plus 2 problem done still a different way: [prob06.asm]
buffer: rs 2 ;reserve 2 bytes for the data
mov #buffer,#2 ;put a 2 in the first location in buffer
mov #buffer+1,#2 ;put a 2 in the second location in buffer
mov a,buffer ;move the first 2 into the accumulator
add a,buffer+1 ;add the first 2 to the second 2
Line 1 reserves 2 bytes and names it buffer Line 2 puts a 2 into the first location of
buffer Line 3 puts a 2 in the second location of buffer Line 4 moves the first 2 into the accumulator Line 5 adds the first 2 to the second 2 and stores the result into the accumulator There are still other ways to solve this problem In fact, if a problem was given to 100 programmers, there would probably be 100 different programs, all with the same results
Trang 19Lastly, I want to explain something else about the assembler Since there are two distinct memory areas in the DS5000, there must be a way to describe to the assembler which one
is being referred to at any particular place in the source file The source file is what the above program, or any program that has been written, is referred to It is the source for the assembler, or the file that is going to be read by the assembler to generate the object file (the object of the assembler) from The object file is the file that will be download to the DS5000 They are two different files One you've written with a text editor (the source file) and the other is created by the assembler (the object file) when you assemble the source file You use an assembler with the object in mind of generating a file to download to the micro, hence the name, object file
I've left out some directives from the previous programs, for simplicities sake, that I need
to mention now One is the org directive It is the originate or origin directive This tells the assembler at what address the first byte of assembled code is to be placed inside the DS5000 It is the origin of the program or the beginning Here's how this would look for our last example program:
.org h'0000
mov r0,#buffer ;set r0 to the start of the buffer
mov @r0,#2 ;put a 2 into the first location of the
buffer
inc r0 ;increment r0 to point to the second byte
mov @r0,#2 ;put a 2 into the second location of the buffer
mov r0,#buffer ;set r0 to the start of the buffer
mov a,@r0 ;get the first 2
inc r0 ;step to the second 2
add a,@r0 ;add the second 2 to the first 2
memory named .memory will be used now This represents the internal ram The next line has a .memory directive This switches to the internal ram memory The .org following this is telling the assmbler that the first .rs is located at address 00h So the address of
buffer is 00h or just 0 This is the value or address that is loaded into r0 when the mov r0,#buffer is executed
Well we've covered quite a lot in this lesson, and I hope you've gotten most of it If not, I would suggest re-reading it until you do I would also suggest that you print out all of these lessons so you can refer to them later In the next lesson we will actually be
assembling these programs and running them in the simulator for a closer look at what happens inside the DS5000 This is where it really gets good
Trang 20My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional
teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
Using an Assembler and Simulator
I would suggest that you create a directory somewhere on your hard drive called
COURSE and put the following files in this directory They are all zipped and you will need PKUNZIP from PKWARE or a similar unzipper to extract the files All these
programs are DOS based and should be ran under DOS If you are using WIN3.1 or
WIN95 you'll need to get to DOS If you're a power user you could setup icons to run these programs, but my explanations and examples will assume that you are at the DOS command line C:\COURSE prompt
I've spent several hours searching the net to find all these files All are either shareware or freeware and there should be no ethical problems using them for this course You should, however, purchase all of them in the event you continue using them after the course I can't give you license to use them, but for educational purposes, I don't think that there will be any problems Most of these programs have manuals that explain how they work Print these out for future reference I've lost the docs for QEDIT and BR but if you've ever used a text editor or browser, you shouldn't have any problem using them
The assembler we are going to use is made by Pseudo Corp and can be downloaded here The simulator is made by HiTech Corp and is here The examples from lesson 6 are here The text editor QEDIT , by SemWare, can be downloaded here A text browser can be downloaded here Any text editor can be used to create the assembler source files You could even use notepad in Windows I am giving you the ones in lesson 6 already typed
up Also, a handy 8051 reference can be downloaded here It is a TSR that can be loaded once before starting the assembler or simulator, and then invoked by pressing a couple of keys together It has some good info and might be useful in the process
For those of you who just can't wait, here is a rough draft of the system we will use later
on in this course for training purposes It's isn't finished (all the parts are not marked) but all the pin numbers and the general connections are complete, and could be built, but don't I'll give you the real thing later on There is a text file describing how to print it out, read it before trying to print it!
The first step in writing a program is to define the problem as completely as possible You will always think of things as you go along that you left out After thinking out the problem, you start by typing up a source file I have already typed up all the sample files
in lesson 6 and assembled them The source files all end with the asm extension The extensions created by the assembler are .obj which is the object file and .lst which is
Trang 21the listing file The listing file shows all the addresses resolved by the assembler, all the
code generated and all the comments This file is very important in using the simulator
In the simulator, there are no labels shown because the simulator really dis-assembles the object file as it is loaded into the simulator No names are listed in the simulator so the listing file is used to keep up with where the simulator really is You'll see all this as we
go along The listing file also shows any errors in the assembly process so that you can correct them It is crucial that you look at the listing file to be sure that there are no errors listed
One note, the assembler creates object files with the extension .obj but the simulator looks for object files with the extension .hex So you have to rename them after
assembly before loading them into the simulator Sorry about that, but that's the way it works I have already renamed the problem object files for you, but any you create will have to be renamed each time you assemble a file before loading them into the simulator
The simulator is a way to test your programs without downloading them to the DS5000 This means, for our purposes, you don't have to have a DS5000 to learn to use a DS5000 The way you would use it in real life is to write each program, assemble it, and then load
it into the simulator After all the different pieces of the whole program have been tested, you would combine them into one file, possibly test it again, and then download it to the DS5000 This particular simulator isn't fancy, and there are better ones, especially the one from Pseudo Corp, the one I use when I'm making something ($199) But this one is free and will do for our purposes The only thing that it can't do is simulate an interrupt, but this can be worked around and I'll show you how later
We are going to look at some of the examples from the last lesson and watch them in action in the simulator We're not going to look at all of them, but you can look at the others on your own The first we will look at is PROB01.ASM I am assuming that you followed my directions and created a directory to hold all the files listed on this page These should include the assembler program, the editor program, the simulator program, and all the example files from lesson 6 Also you are in that directory at the DOS prompt
The first thing to do is start the simulator Do this by typing SIM51EM and press ENTER You are now looking at the simulator screen with all the various sections of the 8051 The screen is divided into six sections Each section is separated by dashed lines The top section is the Registers section.On the left side starting on the 8th line are the names Addr, Opcode and Instructions This is the Code section On the Right starting on line 4
is the Internal RAM section Below that is the External RAM section Below that is the Program memory section Below that is the Function Key assignments and what each does To the left of that is two boxes, one that says File and the other says Select Below that is the command line with the flashing cursor
Of these sections, we will be concerned with the Register, Code, and Internal RAM sections, the function keys, and the command line I will describe each as this discourse continues Notice that in the function key section, the F7 File IO is highlighted Also on the far left of that, a box that says FILE is shown Inside that box are Name file, Read
Trang 22file, and Write file These are the commands that can be entered on the command line Only the capital letter of each is used So to name a file you press N, to read a file you press R and to write a file you press W
First we must name the file we want to load into the simulator Press N and enter
PROB01.HEX and press enter Now PROB01.HEX should appear on the line Press enter
to accept it Now press R(READ) and Y to accept PROB01.HEX PROB01.HEX is now loaded into the simulator
Looking at the Code section we see under Addr a column of 4 digit numbers On the first line we see a 0000 followed by 7802, MOV R0,#02 The 0000 says that this is location
0000 in Program memory ( the first location) The 7802 is the actual two byte Opcode that is assembled from the MOV R0,#02 instruction The 78 says that this is a MOV immediate into R0 and the 02 is the value moved into R0 Also notice that this line is highlighted This is the instruction that will be executed next Notice above the Code section is R0 and the value below it is 00 This is what is currently in R0 Also notice that
at the top of the screen in the Register section is ACC and below that a 00 This is what is currently in the accumulator And lastly notice the PC and the 0000 below it This is the current value in the Program Counter
The next thing we must do is to start executing the program To do this first press F5 Notice the left two boxes have now changed to Breakpoint and Misc These are the commands that can now be entered on the command line Notice the Single step in the right box To single step the program one instruction, press S Do that now, but only once Notice that now in the Code window the highlight is on the next line Also notice that R0 now has a 2 in it We have just executed our first instruction, to put a 2 in R0 Notice that the ACC is still 0 Also notice that the PC is now 0002 Remember that the
PC always points to the instruction that will be executed next Now press S again Now
the ACC has a 2 in it as does R0 Also the PC is now 0004 We have just executed the second instruction, to move a 2 into the accumulator We are now ready to add 2 plus 2.Now press S again Now the accumulator has a 4 in it This is the result of adding 2 plus 2 This also finishes this program
Notice that in the Code window below the three instructions that were in this program that the rest of the lines have NOP in them for the Instruction Also notice that the
Opcode is 00 A NOP is an instruction that does nothing and happens to have an Opcode value of 00 These are here because all the rest of Program memory has 0's in them, which we didn't load with anything, but are interpreted as NOP's This is normally what you will see following the last instruction in a program
In review, the Opcode is the actual value that the assembler assembles for an instruction and is what the micro understands and can execute The Instruction is the english form of the Opcode and is for the benefit of the programmer
To exit the simulator, first press F1 and then E and Y This completes the first session with the simulator You can simulate this program again by repeating the previous steps
Trang 23Next we will simulate PROB04.ASM Start the simulator, name PROB04.HEX, and read
it Also you will need the list file for this program (PROB04.LST) printed out for
reference You need to set your printer for condensed because the lines are longer than 80 characters
About the listing file PROB04.LST, you'll notice that this resembles the source file PROB04.ASM but with three extra columns of numbers to the left The left most column
is line numbers and is just for reference The next column is addresses or memory
locations They mean different things depending on what part of the listing your looking
at Notice that on line 3 there is an org 8 This is telling the assembler to start reserving internal ram locations with location 8 The next line has an 8 in the second column This
is the location reserved for the variable first Line 5 has a 9 in this column Location 9
is reserved for the variable second I mention these because in the simulator there are no labels shown but instead you'll see the numbers 8 and 9 used This is some of what the assembler takes care of for you You get to reference the variables by name in the source file, while the micro uses numbers to reference them Now look at line 7 Here is an org
0 This tells the assembler to start assembling code at location 0000h Notice that on line
8 there is a third column of numbers This is the actual opcode generated by the
assembler for a given instruction You will see these numbers in the simulator in the code window along with the numbers in the second column
I tell you this to show you the relationship between the listing file and the code window
in the simulator Since there are no labels in the simulator, the listing file can be used to interpret the code window in the simulator and keep up with where you are in the
simulator
With the simulator started and PROB04.HEX loaded, notice that all the registers have a 0
in them to start with Also notice the first 4 lines in the code window These are the 4 instructions in the listing file See the simularity between the 2nd and 3rd colums in the listing file and the code window Also notice that none of the lines in the listing file above the instructions are listed in the code window All these lines are for the assembler only and don't generate any code
Now single step once Notice that in the Int Ram window, on the second line there is an
0008 and beside that an 02 This is the 02 that was loaded into first which had location
8 reserved for it by the assembler So the result of MOV first,#2 was to put a 2 in
location 8 of the internal ram The layout of the Int Ram window is the location followed
by 8 bytes of data The 8th byte on that line is location 07h The next line starts with location 08h and goes through location 0fh The next line starts with location 10h and ends with location 17h, and so on
Now single step once Notice that location 09 in the internal ram now has a 2 in it Single step again and notice that there is now a 02 in the accumulator We have just moved the 2 from location 8(first) and put it in the accumulator Single step again and notice that the accumulator has a 04 in it We have just added location 9(secnd) to the accumulator with
Trang 24the result in the accumulator We are now done with the program, and the second session
of using the simulator Exit the simulator to get ready for the next and last example
Now start the simulator and load the PROB03.HEX file In this example we will modify port lines to simulate a switch being closed Remembering back to PROB03.ASM, p1 bit
0 was where we connected the switch and p0 bit 0 is the line connected to the LED If the switch is closed we will have a 1 on p1 bit 0 If we have a 1 on p0 bit 0, the LED will be lit
Notice that in the Register window in the simulator, on the left side line 3 is P0 followed
by FF and 11111111 The FF is the hex representation of 11111111b The 1's are the individual bits or lines of p0, with the left most being bit 7 and the right most bit 0 The same holds true for p1 So currently there is all 1's on p0 and all 1's on p1 So the switch
is closed and the LED is lit Also notice that across the top of the register window is the
CY or carry flag and it is currently a 0 Also the first line in the code window has the instruction MOV C,P1.0 This says to move port 1 bit 0 into the carry flag Single step once Now see that the CY has a 1 in it We have just moved the 1 on bit 0 p1 into the carry flag Single step again This moved the 1 in the carry flag to p0 bit 0 But since we already had a 1 on that bit, no difference was noticed Notice that the highlighted line in the code window is a SJMP 0000 Notice that in the listing file PROB03.LST there is a label on line 8 called start Also notice in the address column is a 0000h So the label
start is at location 0000h On line 10 of the listing file is a sjmp start This says to jump to the label start Since start is at 0000h, that is the address shown in the code window This says to jump to location 0000h This again illustrates the relationship of the listing file to the code window Single step once more Now we are back to the first instruction
Now we are going to set bit 0 of p1 to a 0 Do this by first pressing F6 Now press I for internal ram Now F for fill Now enter 90 for the start, 90 for the end, and 00 for the byte Answer Y to the following prompt Now see that p1 has a 00h or 00000000b in it
We have not only put a 0 in bit 0 but a 0 in all the bits We put a 90h for the start and stop address for the fill because p1 is at address 90h in the 8051 and can't be changed If you notice in the listing file all the names and numbers that follow the program listing These are all the reserved names in the assembler and the corresponding addresses of each inside the 8051 Find P1 and you will see the address 0090 to the right of it This is the address of p1 in the 8051 So we entered a start for the fill of 90 and a 90 for the end of the fill This selected the one location for the fill You can specify a range of addresses and fill all of them at once That is why it asks for a start and an ending adress for the fill But since we only wanted to alter p1, we entered the same address for the start and end Now the switch would be open Press F5 to switch back to Execute Now single step once Notice that the carry flag now has a 0 in it We have just moved the 0 in p1 bit 0 into the carry flag Single step once Notice that bit 0 of port 0 now has a 0 in it and the rest of the bits are still 1's The LED would now be off Single step once Now we're back
to the first instruction
Trang 25We can now put all 1's back into p1 to simulate the switch being closed again Do this by repeating the above steps only when it asks for the byte of data, enter FF (make sure CAPS is set on your keyboard or it won't accept the entry) instead of 00 Now single step through the instructions again and you will see a 1 in p0 bit 0 This would again lite the LED You can now exit the simulator We have finished this program example
Well this has been somewhat sticky, having to look at the listing file and the code
window, navigating the simulator, loading programs and altering register contents But I hope this has given you some insight into the simulator, the relationship of the listing file
to the code window, and some differences between the assembly source file and the hex object file that is generated by the assembler The fact that labels are dropped in the assembly process and replaced by absolute addresses for the micro to use How the assembler lets you write a program using english names and labels in a form much easier for the programmer to keep up with Without an assembler, the task of writing a program would be extremely difficult, since what ends up inside the micro is just numbers
In my early days of micro building I hand assembled each instruction into the hex
opcodes and programmed an EPROM with them The time consummed this way was enormous It was an exercise in converting assembly language into opcodes that, in and
of itself, was a total waste of time I had to keep an intense concentration level while doing this, and one mistake would cause the program not to work I would then have to
go through my hand written program and not only check the logic of my instructions, but whether I had properly assembled them into opcodes I never want to do that again The tools I have furnished you with, I would have given practically anything to have had back then There were such tools but they cost several thousand dollars, way beyond my price range I considered my time to be cheap and went the route I did for that reason But time isn't cheap in reality, and anything you can use to better utilize it is a must I probably should have came up with the money to buy these tools back then but I didn't One
project I worked on for Levi Strauss, took me 18 months to finish With these tools I could have done it in a few weeks, I'm sure Well enough of that
In the next lesson we will look at the second phase of writing a complete system The first phase is to define the system and what you want to accomplish with it The next is to write all the subroutines and functions that will be repeated throughout the system These are called the BIOS (Basic Input/Output System) routines and OS (Operating System) routines
My email address is here and I would welcome your questions or comments as you proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
Trang 26
BIOS and OS Routines
When writing software a common practice is to decide what functions or routines will be needed to complete the project These can include conversion routines, routines to scan different pieces of the system, or any routine that can be written and then called by either the operating loop or the interrupt routines Typical examples could be a routine to
convert binary to decimal, convert decimal to binary, convert binary to ascii, and convert decimal to ascii Others might be to get current time, scan an analog point and convert it
to binary, output a character on the serial port, write a character to an LCD display, read a character from a keyboard, and many others By writing each of these routines, and debugging them separately, the development time is generally reduced Plus the task of debugging the final software is simpler, because each of these routines have been
debugged previously and are known to work In a PC these routines are called the BIOS and DOS BIOS stands for Basic Input/Output System and DOS stands for Disk
Operating System In the devices we will build there won't be a disk but there will be an Operating System and BIOS In this lesson we will look at these more closely and start writing them
The first we will write is a routine to convert binary to decimal In the micro, all numbers used are binary, but we usually want to be able to display them in decimal for easy
reading.The following is a binary to decimal conversion routine It takes a binary number
in the accumulator and returns with the decimal number in r3, r4, and r5 R3 will hold the hundreds digit, r4 the tens digit, and r5 the units or ones digit In an 8 bit location, the largest number is FFh or 255 Following is the routine bin2dec (bin2dec.asm)
bin2dec: mov b,#100 ;set to divide by 100
div ab ;divide acc by 100
mov r3,a ;move the hundreds digit into r3
mov a,b ;move remainder into acc
mov b,#10 ;set to divide by 10
div ab ;divide acc by 10
mov r4,a ;move the tens digit into r4
mov r5,b ;move the ones digit into r5
ret ;done, exit the routine
To this we add up front:
org h'0000 ;start assembly at 0000h
start: mov a,#h'ff ;load a FFh(255) into acc
acall bin2dec ;convert to decimal
mov a,#h'64 ;load a 64h(100) into acc
acall bin2dec ;convert to decimal
sjmp start ;jump to start
By adding this you can see how a function or subroutine is called by the operating
system Now download bin2dec.zip , unzip it and start the simulator and load (read) BIN2DEC.HEX As before, all the registers are 0's to start with Print out the listing file
Trang 27BIN2DEC.LST for reference Using the listing file is even more crucial here because we are calling a subroutine To follow the code window's contents, while single stepping the program, the listing file is a must
Notice that there are two labels in this listing One is the start of the operating loop and the other is the start of the binary to decimal subroutine Also notice that the subroutine
ends with a ret instruction This instruction is similar to a sjmp except that it returns to
the instruction after the one that called it (acall bin2dec) In this way we can do several binary to decimal conversions with the same routine Also when a call is performed another register comes into play It is called the Stack Pointer (SP)
The stack is a device to hold the return address for a call What happens is that before
branching to the subroutine, the location of the next instruction after the call is pushed
onto the stack Pushing is another word for storing, but it relates to the stack only and
usually return addresses When the return is encountered the return address is popped off
of the stack and placed into the PC so that the next instruction fetched is the one after the call This is sort of like stacking (pushing) books in a pile, one on top of another with the most recent addition on top of the stack Then as a book is taken off of the stack (popped) the next one is on top As each byte of the two byte return address is pushed onto the stack, the stack pointer is incremented once Then when the return is encountered and each byte of the two byte return address is popped off of the stack, the stack pointer is decremented.once for each byte The end result is that the stack pointer is always pointing
to the current position on the stack where the next byte of address would be popped off
To explain this again a little more explicitely, when the call is encountered, the stack pointer is incremented and the first (low) byte is pushed onto the stack Then the stack pointer is incremented again and the second (high) byte is pushed When the return is encountered, the first (high) byte is popped off the stack and the stack pointer is
decremented Then the the second (low) byte is popped off of the stack and then the stack pointer is decremented The stack pointer provides the address in internal ram where the first byte that is to be popped is located This is a little confusing to some but it happens automatically for calls and returns
But the stack can be used to pass data between routines and is commonly used in C and other languages The data to be passed is pushed onto the stack and the other routine is called or jumped to and the data is popped off of the stack into registers for use by the second routine Care must be taken to make sure there are an equal number of pushes and pops One of the hardest bugs to find is a mismatch in pushes and pops It causes the program to act in wild and mysterious ways that is difficult to decipher I don't want to dwell on this too much but, simply put, there must be a return for every call and a pop for
every push or there will be problems
I mention the stack pointer here because you will notice it's value changing as we single step through the program Each time we call the bin2dec routine, it will be incremented
by 2 and each time we return from the routine, it will be decremented by 2 Also the stack
Trang 28usually starts after any variable storage, usually residing towards the end of the internal ram
In the 8051 there are two call instructions, the acall (Absolute Call) and the lcall (Long Call) The acall is a shorter instruction (2 bytes) but is limited to use where the entire program is in the first 2048 (2K) bytes of program memory The lcall is longer (3 bytes) but can call to anywhere in program memory The only reason to use the acall is to save space in program memory It should be avoided usually and the lcall used instead
Back to the simulator Single step once The accumulator now has an FFh in it Notice the value of the stack pointer (SP) Single step again Now we have called the bin2dec
routine and the stack has been adjusted Single step again Now the b register has a 64h (100) in it
To digress a moment, the div instruction uses the b register and the accumulator The accumulator is divided by the b register and the dividend ends up in the accumulator and the remainder is left in the b register So to get the hundreds digit, the accumulator is divided by 100 The hundreds digit that ends up in the accumulator is then stored in r3 The remainder in b is moved into the accumulator and the b register is loaded with a 10 Then the accumulator is divided by 10 The tens digit is now in the accumulator and the remainder, which is the ones digit is in the b register The routine is finished by moving the tens digit from the accumulator into r4 and the ones digit in the b register into r5 The routine is now finished and the return is executed
Single step again Now a 2 is in the accumulator The remainder is in b Single step again The 2 has been moved into r3 Single step again The remainder in b is now moved into the accumulator Single step again The b register has a 10 in it now Single step again A
5 (the tens digit) is in the accumulator and a 5 (the ones digit) is in the b register Single step again R4 now has the tens digit Single step again R5 now has the ones digit
Notice in the internal ram window that location 08 has a 4 and location 09 has a 0 Now look at the location of the next instruction after the acall in the listing file It is 0004h You're looking at the stack with the return address in it Single step again The return has been executed and the PC is now pointing to the next instruction after the acall
You can now single step at your leisure and watch the next conversion take place, starting with a 64h (100) in the accumulator and ending with a 1 in r3, a 0 in r4 and a 0 in r5 You can exit the simulator when you want and this session with the simulator is over
Next we will write a routine to convert decimal to ascii Ascii is used by most display systems and by the LCD display that we will eventually use in our Micro Lab Kit Since
as before, all the numbers used by the micro are binary We have written a routine to convert the binary to decimal and now we will convert that to ascii so that we can display
it
The routine we are going to write will take the 3 digit decimal in r3, r4, and r5 and return with 3 digit ascii in r3, r4 and r5 To convert a decimal number to ascii, all you have to do
Trang 29is add 30h to it In other words a decimal 0 is ascii 30h and a decimal 9 is ascii 39h Following is the dec2asc routine
dec2asc: mov a,r3 ;get the hundreds digit
add a,#h'30 ;add 30h to it
mov r3,a ;move it back into r3
mov a,r4 ;get the tens digit
add a,#h'30 ;add 30h to it
mov r4,a ;move it back into r4
mov a,r5 ;get the ones digit
add a,#h'30 ;add 30h to it
mov r5,a ;move it back into r5
ret ;done, return
We will now add this to the previous routine and call it bin2asc.zip You can download it and unzip it, print the listing file, start the simulator and load BIN2ASC.HEX You can see that we have added a call to do the conversion from decimal to ascii and the decimal
to ascii routine Also I changed the two numbers to be converted to FEh (254) and 7Bh (123) This is done to better see the results since each digit is different Single step
through the instructions and take note of the internal ram window at the far right As the numbers are converted to ascii, the area where there are normally a bunch of periods will now show the ascii numbers 254 or 123
There are many more routines to write, but the time is getting near where we need some hardware to experiment with I hope that in this lesson you've seen how an operating system operates and how we write subroutines to be used by the operating loop The loop
in our example began at start and looped at the end back to start In the finished operating loop there will be a lot more code and many more subroutines, but the concept is
identical to our examples
In the next lesson we are going to discuss interrupt routines and how to use them In the meantime I am going to start work on a printed circuit board to make the main processor board for our Micro Lab Kit
My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
Interrupts and the Operating System
Trang 30In a typical system, the software can be divided into 3 possible groups One is the
Operating Loop, another is the Interrupt Service Routines, and the last is the BIOS and
OS functions and subroutines The Operating Loop is the main part of the system It will usually end up being a sequence of calls to BIOS and OS subroutines, arranged in an order that accomplishes what we set out to do, with a little manipulation and data transfer
in between At the same time, at least it LOOKS like it's happening at the same time, the interrupts are being serviced as they happen In the DS5000, there are seven possible events that can trigger an interrupt Two of them are from external interrupt inputs, that can be from whatever hardware we've added to the DS5000 that we deem to need
servicing as soon as they happen Two more are from internal timers or counters that cause an interrupt when a certain count has occurred One is a Power Fail that happens when the power to the DS5000 has dropped below a certain voltage The last two are from transmitting or receiving data through the serial port
To digress just a moment, there are two ways to service, or act on, events that happen in the system One is to scan them and the other is to use interrupts Scanning is just what is sounds like Each possible event is scanned in a sequence, one at a time This is ok for things that don't require immediate action Interrupts, on the other hand, cause the current process to be suspended temporarily and the event that caused the interrupt is serviced, or handled, immediately Remember, from a previous lesson, that the routine that is
executed as a result of an interrupt is called the interrupt service routine, or the interrupt handler routine
In most systems, and ours in particular, there is a mixture of both The choice of which to use depends on how long you can wait to take action on an event For instance, when a character is received through the serial port, it must be read and stored somewhere for later use, and done quickly enough that the next character received doesn't overwrite the
previous one The serial port is double buffered This means it can be receiving one character, while still holding the last character it has previously received Remember, or
understand, that the serial port gets it's name from the fact that characters are received as
a serial stream of individual bits, one bit at a time When 8 bits have been received, this byte is transferred to a one byte receive buffer, at which time an interrupt is generated, and the serial receiver can start receiving the next character If the operating loop is in some conversion routine that takes a while to finish, and a character comes in on the serial port, this conversion needs to interrupted for long enough to get the received
character and store it in a buffer area, and then return to finish the conversion routine, without loosing any serial data
The number of interrupts available for use is limited and part of the system designer's job
is to figure out which events need interrupts and which ones can be polled, or scanned
Some, like the serial port and the power fail, are dedicated and can't be used for anything else But one of the two timer/counter interrupts and the two external interrupts can be used for whatever the designer sees fit I normally like to take one of the two external interrupts and apply the 60 hz line frequency to it so that the micro is getting interrupted
60 times a second, all the time This allows for, what appears to be, two separate pieces
of software to be running at the same time The operating loop, doing its scheduled tasks,
Trang 31and an interrupt routine that is doing several jobs that need to be done during the same time period An example of one of these jobs is to update a real - time system
clock/calender that can be used by the operating system, to schedule tasks based on time
of day/date Another job is to poll some parts of the system hardware often enough to
appear to be interrupt driven, when there isn't a dedicated interrupt that could be used
What happens is that when the 60 hz interrupt happens, certain events are scanned to see
if they have happened These events are therefore scanned every 1/60 th of a second In this manner a combination of polled - interrupt driven response to an event can be used to expand the effective number of interrupts available to the designer It's faster than just polling, but not as fast as a dedicated interrupt
In the DS5000, as with any micro that has interrupt capability, there is a method by which the interrupt gets serviced in a timely manner Remember how a call works, as we saw in the previous lesson When the call is executed, the next instruction's address that would have been executed, is pushed onto the stack, and a jump is made to the start of the called routine At the end of that routine, a return is executed, which pops the previously pushed address, and causes a jump back to the next instruction after the call An interrupt does essentially the same thing When the interrupt occurs, and the current instruction that is being processed is finished, the address of the next instruction to be executed is pushed onto the stack Then a jump is made to a dedicated location This particular jump is called
a vector
It's referred to as a vector, to differentiate it from a regular jump, although it's essentially the same thing Each interrupt has it's own vector, or unique location where it's service routine starts These are hard coded into the DS5000 and can't be changed These vector addresses start at 0000h in program memory Following is a map of these locations and the interrupt assigned to each
002Bh Power Fail Warning
These are the starting addresses of the service routines for each of the interrupt sources in the DS5000 Notice one that I haven't mentioned before called Reset/Power Up When power is applied to the DS5000, program execution starts at location 0000h Another way
to get to this location, other than jumping to it, is to do a reset of the DS5000 There is a pin on the DS5000 called Reset Triggering this input causes the DS5000 to vector to 0000h and start executing just like power up The two differences between this and a regular interrupt is that it doesn't push any address onto the stack, and it is recognized any time it happens and can't be turned off The other interrupts can be disabled by the
program, and while disabled, do not generate any interrupts Also when power up occurs
or a reset occurs, the interrupts are automatically disabled and must be enabled before they can be used Reset does several other things that I will mention later
Trang 32Again let me digress a moment About the jump instructions In some of our previous programming examples we had a jump instruction called sjmp (Short Jump) This
instruction can only jump to addresses relatively close to it There is another jump called ajmp (Absolute Jump) that can get to anywhere, as long as anywhere is in the first 2K bytes of program memory There is a third jump called ljmp (Long Jump) It can get to any place in program memory The short jump is good for small loops inside a routine The absolute jump is good when the entire program fits in less than 2K bytes But to get
to any place, regardless of how far away it is, the long jump is used The sjmp and the ajmp are 2 byte instructions and the long jump is a 3 byte instruction
Back to interrupts You will notice that there isn't many locations between vector
addresses What is normally done is that at the start of each vector address, a long jump instruction (3 bytes) is placed, that jumps to the actual start of the service routine This way the service routines can be anywhere in program memory The vector address jumps
to the service routine There is more than enough room between each vector address to put a long jump instruction Looking at the table above, there are 3 locations for the Reset / Power-Up jump instruction and at least 8 locations for each of the other vectors When actually writing the software, at address 0000h will be a jump instruction that jumps around the other vector locations I usually jump to 0030h and continue with the rest of the program This leaves 5 locations for the Power Fail vector, more than enough room for the 3 byte long jump instruction needed to get to it's service routine
Besides being able to disable all of the interrupts at once, there is a way to enable or disable them individually There is a special function register (SFR) called the interrupt enable register Each bit in this register controls one of the interrupts When a power-up
or reset occurs, all of these bits are cleared to 0's, which disables all the interrupts Setting anyone of them to a 1, enables that interrupt Also there is a bit, that if 0, disables all of the interrupts at once, regardless of the state of the individual enables It is also cleared by power-up or reset So to enable any one interrupt, the global interrupt enable must be set (enabled) and the individual enable bit for each of the interrupts must also be set I
usually set each of the individual enables that I want to be active, and then, just before entering the operating loop, I enable the global enable, enabling all of the interrupts at once
When the DS5000 is powered up or reset, after jumping around the interrupt vectors, the first things that are done are called housekeeping functions, like setting the stack pointer
to the start of the stack, clearing out various locations, setting up the baud rate for the serial port, and any other things that must be done to get ready for the operation of the system This is called booting up The same thing happens in your desktop PC This code only runs once for a power-up / reset It's also called initialization It sets up a known condition to start from, so that the system always (hopefully!) starts off the same way
Following is a program segment that shows the first few locations of program memory with a typical example of the interrupt vectors
org h'0000 ;start assembling at address 0000h
boot: ljmp cont ;jump around interrupt vectors
Trang 33org h'0003 ;assemble at address 0003h
ljmp exint0 ;jump to external interrupt 0 routine
org h'000b ;assemble at address 000bh
ljmp timer0 ;jump to timer 0 routine
org h'0013 ;assemble at address 0013h
ljmp exint1 ;jump to external interrupt 1 routine
org h'001b ;assemble at address 001bh
ljmp timer1 ;jump to timer 1 routine
org h'0023 ;assemble at address 0023h
ljmp serio ;jump to serial I/O routine
org h'002b ;assemble at address 002bh
ljmp pwrfl ;jump to power fail routine
org h'0030 ;assemble at address 0030h
cont:
This is how our system will look when we're done At the label cont: we continue with housekeeping and initialization, having jumped around the interrupt vector locations Remember that the .org is an assembler directive that tells the assembler that the next instuction assembled will be at the address in the .org By using the .org, we can place the jumps at the appropriate vector addresses that are hard coded in the DS5000
There are other things about interrupts that we will cover as they come up, but this lesson was to get you used to the idea of interrupts and what they're used for in a typical system Remember in a previous lesson we were standing at a busy intersection waiting for the traffic light to change, when a person came up and tapped us on the shoulder and asked what time it was It didn't stop us from going across the street, it just temporarily
interrupted us long enough to tell them what time it was This is the essence of interrupts
They interrupt normal program execution long enough to handle some event that has
occurred in the system
Polling, or scanning, is the other method used to handle events in the system It is much slower than interrupts because the servicing of any single event has to wait its turn in line while other events are checked to see if they have occurred There can be any number of polled events but a limited number of interrupt driven events The choice of which
method to use is determined by the speed at which the event must be handled
Also, we have a third option and that is a combination of polling and interrupts by using a continuously interrupting event and polling certain other events during each iteration, or occurrance, of the interrupt This method is faster than straight polling but slower than a dedicated interrupt It also has other limitations that we will discuss later on
In the next lesson we will start looking at the various parts of our system
My email address is here and I would welcome your questions or comments as you
proceed through this course Depending on the volume of mail I get, it may take a couple
of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional
teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
Trang 34System Resources
In a typical system there are various parts, the choice of which depends on what the design goal of the system is There must be a serial interface to allow the downloading of the program into the DS5000 There also must be an oscillator to supply the DS5000 with
a clock source To permit trouble shooting problems, we need a way to break out of any program that is currently executing, and examine various locations in the DS5000 to help
in the debugging process To view the states of various pins on the DS5000 we also need
a logic probe These are the parts that we will have on the basic processor board of our system There will also be a power supply to provide +5 volts for the system, and a connector to take all the pins of the DS5000 off of the basic board so that they can be connected to a proto board to allow us to connect up what ever we want to the DS5000 Also there will be a connector that can be connected to an X10 interface to allow us to use any of the inexpensive X10 modules that can be found at various suppliers like Radio Shack
If I wasn't clear about this before, the idea I am working from is to have the basic
processor board and a proto area connected to it to allow connecting any parts we want to the DS5000 A proto board is just a matrix of holes, with connectors, on 1" centers that a chip, or a part, can be plugged into and wire jumpers used to interconnect that chip to the appropriate DS5000 pins These proto boards come in a variety of sizes and shapes They are available at Radio Shack and other consumer stores that handle electronic parts They work very well for temporary construction, or proto typing, of circuits, hence the name, proto boards This will allow us to add things as we go along in this course and
eventually have a prototype system that will be able to do lots of things It, however, won't be a finished product, but merely a prototype It won't be in a case, and the proto board doesn't allow for much in the way of mechanical vibration stability But it will serve as a training platform and to allow you to design your own projects in the future
In addition to the basic processor board and the proto boards, we will be using an A/D converter and a 4 line by 20 character liquid crystal display (LCD) We will also have a 4
by 4 keyboard This will be the extent of our training system
I'm sure, at least for some of you, that all this jargon is fairly meaningless But I will now start explaining it in further detail
First, all microprocessors need a system clock to drive them This is not a clock like the one beside your bed but, instead, an oscillator that produces a very stable frequency of pulses These pulses are applied the the micro on the clock-input pin What happens with
it inside the micro is not important other than to say that without a clock, the micro won't work at all We will be using an oscillator that is in it's own chip package, that doesn't require any support parts to operate Just apply power to it and connect it to the DS5000 and we're done with the clock
Trang 35Next we need an interface that allows us to download a program to the DS5000 This is implemented with a chip from Maxim called the MAX232 chip It has 2 transmitters and
2 receivers inside it To digress just a moment, we've talked about 1's and 0's in our earlier lessons Then they were just numbers But when looking at the pins of the
DS5000, they are really voltage levels Ideally a 0 is 0 volts and a 1 is +5 volts But in
real life a zero is close to 0 volts and a 1 is close to +5 volts In the DS5000 specs, a 0 is
any voltage less than about 8 volts and typically about 1 volts or less and a 1 is any voltage greater than about 2 volts and typically about 4.8 volts These are called logic levels Levels between 8 volts and 2 volts are indeterminate and usually indicate a bad chip or an open connection
But a serial port uses a different set of levels to transmit and receive with They conform
to the levels set out in the specs for RS-232C where a 0 is close to -5 volts and a 1 is close to +5 volts To connect the serial port from a PC to the DS5000 we must convert these levels from one spec to the other This is the job of the MAX232 chip It can take the RS-232 levels in from the PC, through one of it's receivers, and convert them to logic levels to the DS5000 It can also take logic levels out of the DS5000, through one of it's transmitters, and convert them to RS-232 levels to the PC In our system, a serial
connection requires 3 wires, a transmit, a receive, and a ground or common The transmit out of the DS5000's serial port goes through a transmitter of the MAX232 chip through a connecting cable and into the receive of the PC's serial port The transmit out of the PC's serial port goes through a connecting cable through a receiver in the MAX232 chip and into the receive of the DS5000's serial port The ground or common on the PC's serial port is connected to the ground of the DS5000 This completes the serial port
connections There are 4 external capacitors connected to the MAX232 chip that allow it
to function properly but my idea here is not to go into the field of electronics and
capacitors Just know that they are needed for proper operation of the MAX232 chip The next chip in our arsenal is a hex inverter chip It gets the name hex from the fact that there are 6 gates inside Each one is an inverter An inverter inverts what ever level is at the input pin and puts that out on it's output pin In other words, if a 1 is on the input, a 0 will be on the output Conversly if a 0 is on the input, a 1 will be on the output How this chip is used would be difficult for me to explain to you in terms that you would
understand So just accept for now that we need it to complete the system
The last chip, besides the DS5000, is the power supply regulator It takes a voltage of about 9 Volts DC and outputs a very stable +5 volts DC for the system power The +5 volts is normally referred to as Vcc, a term from the transistor days, but still used today
I must take care not to get too deep into electronics here, because that is a another course
in itself So some of what I tell you, you'll just have to accept on blind faith Maybe you will decide to get further into electronics on your own That's how I started out
Computers came later for me You, on the other hand, are getting the cart before the horse I will impart on you some aspects of electronics as they come up, but I will try, for your sakes, to keep it to a bare minimum After all, this course is for people who have very little knowlege of computers or electronics To keep your interest level up on the
Trang 36micro side, I must avoid getting too deep on the electronics side This will make it
impossible to explain everything I would like to But, like I've said earlier, chips are like Tinker Toys, you just connect them together and go In most case, not a lot of electronics knowlege is needed, just the knowlege of how to connect them together logic
There is another device commonly used in micros called an A/D converter A micro only understands 1's and 0's and to allow a micro to look at a varying voltage level, an analog
to digital converter must be used It does just what the name implies, it converts a varying voltage (analog) level into a corresponding digital number The one we will use is a National Semiconductor ADC0808 It has 8 analog inputs and gives an 8 bit value out as
a result of the conversion It accepts a voltage in the range of 0 volts to +5 volts If the voltage is 0 the digital value will be 00h If the voltage is +5, the digital value will be FFh This means that there can be 256 levels, including 0, that can be measured by the micro Doing a little math, this means that each one bit change in the digital value is equal to a 02 volt or 20 millivolt change in the analog voltage So as an example, if there
is a 02 volt level into the A./D, there will be a count of 01h as a result If there is a 2 volt input, there would be a count of 64h (100)
An example of an analog input is a temperature sensor A temperature sensor outputs a voltage that is directly proportional to the ambient temperature The one we will use is made by National Semiconductor called an LM34 It gives a 01 volt per degree farenheit output For instance, if the temperature is 100 degrees, the ouput will be 1 volt (100 times 01) Another example could be a potentiometer or variable resistor The volume control
on your radio is a potentiometer Another example might be to measure the voltage of a battery Another is to measure light intensity to determin when it's night or day A
microphone in each room to determin if the room is occupied, to turn lights on
automatically when you enter a room
Another device we will talk about is a liquid crystal display The one we will use has 4 lines of 20 characters each, for a total of 80 characters It can display all the alphabet and numbers plus some special characters like @#$% It takes an ascii number and displays
it This is why we wrote a binary to ascii routine in a previous lesson, to get ready to use this display This display can be used to display the time or temperature or anything you want displayed
The last thing I would like to mention is something called X10 It has been around for several years and is an assortment of modules that allow anyone to control lights and just about anything else plugged into 120 volt house power There are also door sensors, keypads, infrared motion sensors and many other accessories New ones are introduced occasionally and they are very cost effective and inexpensive to own They communicate over the power lines in your house, without any other connection Just plug in a control module, plug in the appliance to be controlled into the module and your done There is a particular module that lets a computer communicate with the other modules in your house You plug this module into an outlet, plug the computer into it, and your ready to control lights, monitor doors, motion sensors, even your telephone! Part of our design project will be to make an interface that will allow us to connect our micro to the X10
Trang 37computer interface The rest will be up to you and the modules you want to control An example of the costs: a module to control a light is $15 The computer interface is $60 A door sensor is $20
The rest of the parts in our system will be odds and ends A few resistors, capacitors, transistors, switches, cable and connectors
With all these components, we will be able to build lots of useful functions into our system Here is a list of possible uses we can put our system to performing
1 A full-featured set-back thermostat to control your home airconditioning, to save you money in power costs!
2 A home security system
3 With the X10 capabilities, control of all of your appliances and lights
4 A clock/calendar that can be used by all the other functions to add time/date to the control functions For instance the setback thermostat can adjust the temperature in your house after you leave for work, but not do it on weekends Or turn on security lights at night and turn them off in the morning Turn your children's TV off at bed time Set your coffee pot up the night before and have hot coffee ready for you when you wake up You'll probably think of other uses that are combinations of these or new ones that I haven't thought of yet The possibilities are limited by your imagination, and your pocket book, of course Other chips can be added that allow the system to TALK to you
All these applications can be purchased off of the shelf at stores, but the cost is more and you don't get the satisfaction of building and customizing them to suit your needs I hope this prospect interests you If it dosen't, this is probably as far as you need to go in this course The rest of it is devoted to reaching this conclusion Unless you intend to buy the hardware described in the next lesson, the rest of this course will be hard to keep up with
It will assume that you have bought the needed hardware and are following along
The next lesson isn't really a lesson, but instead, the parts list, schematic, and all the sources of the parts and their prices We are just about ready to start building the
hardware, something I've been anticipating from the beginning I am going to build all this for myself, along with you
For those of you who are going to end the course here, I hope I've given you food for thought and an insight into the world of microprocessors and computers in general I am planning to offer a free course in basic electronics after I finish this one, so check back at
my home page in a few months and see if it has began Good luck to you and God bless
My email address is here and I would welcome your questions or comments as you proceed through this course Depending on the volume of mail I get, it may take a couple
Trang 38of days or so for me to get back to you, but be assured, I will get back to you I really do
want you to understand the information I'm presenting, and not being a professional
teacher, I might not always explain things to the satisfaction of everyone, but with your patience, I'll do my best My home page is http://www.hkrmicro.com/personal/index.html
The Basic System
Now starts the good stuff Here begins the description and construction of the basic
development system I've been referring to in previous lessons The source for most of the parts will be Digikey Corporation Understand that I'm neither affiliated with nor do I have a preference as to which you use and, depending on where you live, there are
probably other sources I will try to list the electrical characteristics of each part so that where ever you get them, you will get the right one There are some parts that can't be obtained at either place and I will list them as they come up
One more thing You should, if you don't already have one, get a multimeter to use along with this course It will prove invaluable in trouble shooting problems with the system, and you are basically blind without one One can be obtained for around $20 that is
digital and is quite accurate These used to cost over a hundred dollars at the least and several hundred for a good one CARE SHOULD BE EXERCISED WHEN USING ONE TO MEASURE VOLTAGES GREATER THAN +5 VOLTS! especially when measuring AC power outlets or the like
For more information on electronics, check out this link This is a nice tutorial on electonics, and should prove useful
We will begin with the system power supply This consists of a transformer, a bridge rectifier, a filter capacitor, a +5 volt regulator chip and a heat sink for the regulator The transformer needs to be a UL listed wall mount variety with at least a 7.5 volt RMS
output and at least a 5 amp capacity
The parts I bought were from DigiKey Corp and the part numbers are:
Qty Part# Description Price
1 T702-ND WALL TRANSFORMER 9VAC 600 MA 6.70
1 HS121-ND PLUG-IN HEAT SINK FOR TO-220 44
1 W005G-ND 1.5 AMP 50 V BRIDGE RECTIFIER GP .42
1 P5234-ND NHE RADIAL ELECT CAP 16V 470UF 66
1 NJM7805FA-ND IC 5V POSITIVE REGULATOR TO220 88
The processor parts consist of the microcontroller, an oscillator, 2 switches, 2 resistors, a hex inverter and a capacitor All but the microcontroller I got from Digikey Here are the Digikey parts
1 SE1232-ND CRYSTAL OSC 11.0592MHZ CMOS 8 D 3.45
1 CD74HCT04E-ND HEX INVERTER 39
Trang 392 P2040-ND 22UFD 16VDC TANTALUM CAP 1.72
2 CKN1059-ND TOGGLE SWITCH SPDT VERTICAL RT 8.88 The processor I bought from Dallas Semiconductor directly The part number may vary from the one shown, but it should be the 16 MHz, 32K X 8, 40 pin DIP, part
1 DS5000-32-16 Soft Microcontroller ~70.00
Dallas Semiconductor can be reached for credit card orders at 1-800-336-6933 for US orders or at 972-371-4000 for orders outside the US Another source is Newark at 1-800-4NEWARK for $62.50
The rest of the parts I bought at Digikey I've included a description so that you could get some of these parts at another location Radio Shack has many of these parts, including solar cells Also the prices I've listed here, will, no doubt, vary over time
Also the LCD Display can be bought from Allelectronics.com for $7.00 Their part number is LCD-46 The solar cell can be substituted with a part number PVD-2, from the same place Allelectronics.com has many of these parts, and a whole lot more, at GREAT prices Check them out at http://www.allelectronics.com
6 P2040-ND 22UF 16VDC TANTALUM CAP 5.13
10 P4551-ND 001UF 50VDC POLYESTER B CAP 1.08
2 CD74HCT123E-ND DUA RETRIG MONO MULTIVIBRATOR 1.60
1 ADC0808CCN-ND 8 BIT A/D CONV 8 CH MUX 28-DIP 6.60
1 MAX232CPE-ND IC 5 VOLT DUAL RS232 X/R 16-DIP 2.94
3 2N3904-ND NPN SML SIG G.P AMP&SWITCH TO-92 .87
1 2N3906-ND PNP SMLL SIG G.P AMP&SWITC TO-92 .29
5 100EBK-ND 100 OHM 1/8W 5% CARBON FILM RESISTOR .28
5 330EBK-ND 330 OHM 1/8W 5% CARBON FILM RESISTOR .28
5 470EBK-ND 470 OHM 1/8W 5% CARBON FILM RESISTOR .28
5 4.7KEBK-ND 4.7K OHM 1/8W 5% CARBON FILM RESISTOR .28
18 10KEBK-ND 10K OHM 1/8W 5% CARBON FILM RESISTOR .56
5 27KEBK-ND 27K OHM 1/8W 5% CARBON FILM RESISTOR .28
1 4N35QT-ND TRANSISTOR OPTOISOLATOR .43
2 923252-ND SUPER STRIP ALLOY CONTACTS 38.50
1 P276-ND SOLAR CELL 1.3V 55.0 X 11.0 MM 1.16
1 73-1098-ND LCD MODULE 20 X 4 CHARACTER 31.92
1 MC14M-X-ND 5 FT 14 CONDUCTOR RIBBON CABLE 4.80
3 HLMP-1700QT-ND T-1 DIFFUSED RED LED 90
2 P4887-ND 1UF CAP 64
1 CD74HCT393E-ND DUAL 4 BIT COUNTER 83
10 BAV21DICT-ND 2 AMP 250 VOLT DIODE 1.00
2 LM34DZ 32 TO 212 PREC F TEMPERATURE SENSOR 5.70
1 P2105-ND 1UF 16VDC TANTALUM CAP 28
I also put a connector on my LCD display which involved the male connector on the display, a female connector to plug on it and the pins and crimper for the female
connector You can solder the 14 conductor ribbon right to the display, saving this cost, if you are careful Digikey doesn't sell a 14 pin version so you have to get a 12 pin and a 2 pin connector and use them together to get 14 pins total
Trang 401 WM4210-ND 1 INCH 12 PIN HEADER 1.38
1 WM4200-ND 1 INCH 2 PIN HEADER 29
1 WM2000-ND 1 INCH 2 PIN TERMINAL HOUSING 14
1 WM2010-ND 1 INCH 12 PIN TERMINAL HOUSING 86
30 WM2200-ND 1 INCH PINS FOR HOUSINGS 3.15
1 WM9900-ND 1 INCH PIN HAND CRIMP TOOL 13.57 You will need a cable and 9 or 25 pin female serial connector to connect to your PC serial port The number of pins depends on the number of pins on your PC Some PC's have both, with the 9-pin being COM 1 and the 25-pin COM 2 But things are further
complicated if you have an internal modem Many times the 25-pin port, or COM 2 will
be disabled and the installed modem will be set for COM 2 The cable between the PC and the microcontroller only needs 3 wires I have used standard flat telephone cable (called silver satin) for this cable It is cheap and you can get some at many different places It needs to be at least 10 Ft long so that the micro doesn't have to be too close to the PC The schematic I will furnish will show you what pins to tie together on the female serial connector to avoid having to use more than 3 wires I also bought a hood to cover the female connector to hide the wires on the connector It's not necessary but improves the looks and gives you something to grab hold of when connecting or disconnecting the connector with your PC The connectors are available at Digikey
1 109F-ND 9 PIN D FEMALE CONNECTOR 1.65
1 125F-ND 25 PIN D FEMALE CONNECTOR 3.43
1 909Z 9 PIN D HOOD 2.00
1 925Z-ND 25 PIN D HOOD 3.03 Some of the above parts are for constructing the X-10 interface You will need the
computer interface made by X-10 to complete the interface It can be obtained from Home Automation Systems Inc Their WEB site is http://smarthome.com/ You will need a 10 Ft piece of flat telephone cord with a standard phone connector on one end to connect to the interface module Their order phone number is 1-800-SMARTHOME or 1-714-708-0610 for questions The part number is
1 1135 TW523 TWO WAY INTERFACE MODULE 22.95
I think that this is all the parts needed to complete the course You might want to wait to buy the X-10 module if you are short of funds It will be several lessons before we start
on that project, but you will eventually need it to complete the course It is a very useful interface and fairly priced You will be able, through it, to control any light, appliance, air-conditioning, or receptacle in your house that you buy a corresponding module for A module for controlling a light with dimming capabilities is about $10 To control the other equipment in your house you will need units that cost a little more but none are very expensive Check out the WEB site mentioned above and you can get on their mailing list and get a catalog of the various modules available and the prices or view them on-line
You will need a soldering iron to do any soldering It should be as small a wattage as you can get, around 15 watts, with as small a tip as you can get, to prevent overheating the