EMBEDDED SYSTEM BUILDING AND PROGRAMMING IN ASSEMBLY LANGUAGE
EMBEDDED SYSTEM EXAMPLES
Examples of an embedded systems
Cell phones, Traffic light, Pick and drop Robots, All remote control devices, Calculator, Robots, Digital Watches, Televsion, Home theatre, Toy car, Toy helicopter, Bank Doors, Bank ATM, Money counting Machine, Printers e.t.c
Most if not all embedded systems have their hearts as microcontrollers, these are all devices built using programmable integrated circuits.
Note: One may unknowingly buy already programmed integrated circuits (ICs) in the market to build remote control devices, because you did not program the ICs yourself does not mean what you built is not an embedded system.
Embedded systems control many devices around us today and many more enters our world on a daily basis.
BUILDING EMBEDDED SYSTEM
Before one can build and program your own embedded system in Assembly language, a specific programmable integrated circuit (popularly known as microcontrollers) must be studied and well understood. This can be achieved by using google to download the IC manufacturer's datasheet (that specific IC manual) and then take a deligent study of what the Manufacturer says about the microcontroller which you have chosen. Before going into a specific Microcontroller, lets review some of the various makers of embedded system heart (Microcontrollers).
There are countless number of microcontrollers makers in the world but for reference purposes let's mention some of these popular makers of this awesome ICs, they are, Altera, Analog Devices , Atmel, Cypress Semiconductor, Maxim Integrated, ELAN Microelectronics Corp, EPSON Semiconductor, Freescale Semiconductor, Fujitsu, Holtek, Hyperstone, Infineon, Intel, Lattice Semiconductor, Microchip Technology, National Semiconductor, NEC, NXP Semiconductors, Nuvoton Technology, Panasonic, Parallax, Rabbit Semiconductor, Renesas Electronics, Rockwell, Silicon Laboratories, Silicon Motion, Sony, Spansion, STMicroelectronics, Texas Instruments, Toshiba, Ubicom, Xemics, Xilinx, XMOS, ZiLOG.
. e.t.c. These are just few makers in the world, and we also have countless number of assembly languages that each of these IC understands. You are not expected to learn all these various assembly languages and and their ICs to become very good with embedded systems. All you need is a specific Microcontroller from a specific manufacturer and use it to achieve all you could ever imagine to create as far as embedded systems are concern.
CHOSEN FROM THIS ENDLESS LIST OF MICROCONTROLLERS
Just as we have countless number of microcontroller makers so also we have countless number of microcontrollers, the question now is among these chips which one should one chose? Well, this is a very critical question that is not answerable in one direction; this is because various microcontrollers have unique features.
1 If you know very well the features of interest, then chosen is made easier.
2 Complexity or simplicity assembly language of the microcontroller.
Therefore, in most cases chosen a microcontroller is mostly dependent on its features and programming simplicity.
PERSONAL EXPERIENCE AND CHOICE
In this side of the world, virtually all the persons I have seen working with Microcontrollers are either using ATMEL MICROCONTROLLERS (from ATMEL CORPORATION) OR PIC MICROCONTROLLERS (from Microchip Technology). And from personal experience, ATMEL assembly language is easier than PIC assembly language, but due to advance features that PIC has which is not found in ATMEL most persons are currently moving into PIC despite its difficult programming language. ATMEL in order to curtail this current exit of many users of their microcontrollers are building new devices with most features found in PIC.
Finally, due to simplicity in programming ATMEL, I encourage all beginners to start this new adventurous experience of working with microcontrollers using ATMEL.
Out of the numerous microcontrollers that ATMEL Corporation made, we are going to use AT8952 (AT89C52 or AT89S52) for the purpose of this lesson. Things to study about a given programmable IC are stated below:
1. the memory size
2. the pins program identifier for each pin within the integrated circuit.
3. A detailed knowledge about the IC datasheet.
4. the instruction set used by the integrated circuit.
In our case we wish to program ATMEL ICs, with these ic one can build robots, calculator, blinking light, toy car, digital camera, counters and so many.
We want to study ATMEL MICROCONTROLLERS called AT89C52 and its derivatives(AT89S52, AT89C55 e.t.c.), and the language it understands is intel instruction set. As earlier said, we need to study the features of this ic first.
Important Note: Though we are specifically working with AT89C52 microcontroller, but it shoud be noted that we also have AT89S52, AT89C55, AT89S55, AT89S51, AT89C51; with the lessons to be discussed here, all these microcontrollers can also be worked with without any major differences or changes. We are only stating AT89C52 for convenient not that all the lessons are only useful for it alone.
THE HEART OF EMBEDDED SYSTEM
1. It is a 40 pin IC.
2. It has 1 RESET pin.
3. It has 2 Crystal Oscillator pins.
4. It has 2 power supply pins VCC and GND.
5. It has 32 input/output pins simply called the 4 ports.
6. It has 3 external memory pins.
7. It is an 8 bit device.
8. It has 4 ports that is the 32 input/outpins.
9. It has 8kb of ROM.
10. It has 256bytes of RAM (unlike a computer that is 2,000,000,000 byte).
11. It has 128bytes of Special Function Register.
For convenient sake, I will henceforth use AT89C52, but we have others like AT89S52 that use exactly all the things discussed here.
IN DEPTH DESCRIPTION OF AT89C52
1. AT89C52 HAS 40 PINS
As you must have known before now, every IC (Integrated Circuit) has specific number of pins and as such you should know that the IC under study in this lesson has a total of 40 pins. Reading down further will explain these pins and functions in details.
2. AT89C52 HAS ONE RESET PIN (PIN 9)
A RESET PIN is a pin that can be used to clear the AT89C52 RAM, causing the system to be refreshed and start it entire operations from the beginning (start up). This IC we are studying has a single pin used for resetting the entire operations of the embedded system. Lets put it more simply, whenever a computer system is turned on, it keeps taking operational steps (programs) from the ROM and loading it into the RAM as it execute its operations. During this process of data transfer from ROM to RAM atimes errors occur, the error is that data transferred to the RAM may be corrupted and this will lead to the system's misbehaviours (malfunctions). When this happens, you will observe that your system is working in a way that it was not programmed to perform. The option left for you will be to shutdown the system completely and restart, this method of shutting down and restarting is so long and inconvenient. This is why makers of microcontroller decided to put a RESET PIN that accompanishes the same thing that only shutting down would have done.
3. AT89C52 HAS TWO CRYSTAL OSCILLATOR PINS (PIN 18 AND PIN 19)
A crystal oscillator is a component that defines the rate (speed) at which a given devices (embedded system or computer) executes its tasks. Put it simply, for a computer to work, it has thousands of lines of programmed instructions it is using to function. Normally these instructions are not executed simultaneously, rather they are executed sequentially (one after another), the number of instructions it can execute per second defines the speed of the microcontroller (embedded system) project. Simplifying it further by example, if a computer system is rated 2GHZ processor speed, it means that computer can execute 2 billion instructions per second. Because microcontrollers (embedded systems) are dedicated computers such large speeds are not relevant, therefore most of the time our system will be using 4MHz, 8MHZ, 11.0529MHZ, 12MHZ, 16MHZ OR 20MHZ crystal oscillator to make our processor speed of 4MHz, 8MHZ, 11.0529MHZ, 12MHZ, 16MHZ OR 20MHZ respectively. What this crystal oscillator defines are the number of instructions the system we have built can execute per second. Finally, if you use 8MHZ crystal oscillator it means your system can execute 8 million instructions per second.
REAL LIFE ANALOGY OF CRYSTAL OSCILLATOR
In reality, crystal oscillator is an electrical pulse generator (OFF and ON) also called a square wave generator. Just like the heart beats of a living human, embedded system has its own heart beat in the form of OFF and ON generating components. As the OFF and ON are coming and going, the programmed instructions are being excecuted by the help of stack pointer and program counter. Putting it simpler, when you switch ON an embedded system or a computer system, the stack pointer is pointing at the first instruction stored in the ROM, as crystal oscillator begins to generator ON and OFF continously, the pointer is then moved from one instruction to the other, while the program counter keeps record of the necessary information needed by the pointer to know what next instruction to execute. Finally, if the Oscillator stops sending pulses, the computer hangs immediately and no other instruction can be executed. It is just like someone whose heart has stopped beating.
4. AT89C52 HAS 2 POWER SUPPLY PINS (PIN 20 AND PIN 40)
These pins are responsible for energizing (powering) the entire circuit of ATMEL microcontroller. Every IC has minimum of two energizing pins regularly called the VCC/Vdd pin and GND/Vss pin. The voltage acceptable by AT89C52 is 5volts, in other not to damage the IC you must never power it up with voltage more than that specified voltage. The postive terminal of the 5volts power supply (can be 5volts battery) is called the VCC while the negative terminal is what is called the GND.
5. AT89C52 HAS 32 INPUT/OUTPUT PINS
This is the interface between the internal operations of the microcontrollers and the external world. Microcontrollers are known for its ability to process data fed into it and output it again for use by man, this channel upon which this data are fed and output are the output/input pins. No matter how powerful a microcontroller maybe if it cannot take in data and output it there is no way we can know what it is doing, and all its processing is completely useless to us. Example, if you want to build a calculator, you must put keypad in place where users can type in numbers they intend to calculate and that is achieved using the input/output pins; while this inputing of numbers for calculation is being done, the user also wish to see if the numbers he/she has typed are correct therefore, outputting each character as being typed must also be effected and this is done using input/output pins to connect to your output device like LCD, LED, CRT etc. Your television screen at home is directly connected to the input/output pins of the microcontroller that is used in build the television set. Finally, it should be noted that a single pin functions as both input and output, though you have to use your program to specify if a pin will be used as input or output. During programming in assembly language these pins have unique names (identifiers) that you must know so that you can program them correctly.
P0.0, P0.1, P0.2, P0.3,P0.4, P0.5, P0.6, P0.7---(collectively called [Port 0])
P1.0, P1.1, P1.2, P1.3,P1.4, P1.5, P1.6, P1.7---(collectively called [Port 1])
P2.0, P2.1, P2.2, P2.3,P2.4, P2.5, P2.6, P2.7---(collectively called [Port 2])
P3.0, P3.1, P3.2, P3.3,P3.4, P3.5, P3.6, P3.7---(collectively called [Port 3])
IMPORTANT NOTICE:VCC/Vdd is usually a voltage of 5 volts, and GND/Vss is usually a voltage of 0volt. These 32 input/output pins can be made 5volt or 0volt using computer programs. The entire power of microcontroller/microprocessor is simply this singular ability of controllably switching the input/output pins between 5volts (ON) and 0volt (OFF). To make any of these pins 5volts all you need is to send this instruction: SETB Px.x (Px.x can be any of the input/outpin e.g. SETB P2.0 ); this instruction will immediately make that pin to be 5volts. While CLR Px.x (Px.x can be any of the input/outpin e.g. CLR P2.1 ); this instruction will immediately make that pin to be 0volt. We are not yet learning programming so just know that all there is to learn in embedded system is being very able to appropriately switch ON/OFF any of the input/output pins. Though it becomes complicated when what does the turning ON/OFF becomes sensors or mathematical results, but the reality of embedded system is being able to turn ON/OFF correctly irrespective of what triggers it. We will learn more of this when we reach assembly language lesson.
6. AT89C52 HAS 3 EXTERNAL MEMORY ACCESS PINS (PIN 29, PIN 30 & PIN 31)
This pins are responsible for connecting your microcontroller to external memory card or memory IC. AT89C52 has a very limited inbuilt (internal) memory, 8 kilobyte of ROM (very small) and 256 byte of RAM (terribly small), but considering what we intend building with this IC in this lesson it is more than enough for us. But in future you will surely have needs for adding external memory because then you will confirm that there are some embedded systems that the inbuilt memory of AT89C52 cannot possibly serve. When that time comes you will appreciate these three pins being discussed here, but for now simply understand that external memory can be added to enhance inbuilt memory. Finally, PIN 31 must always be connected to VCC, else your system will assume you are using external memory and not the inbuilt internal memory. But when external memory is in use this specific PIN 31 must be collected to GND. In summary, PIN 31 tell the system the memory (internal or external) that it should use for its operations.
7. AT89C52 IS AN 8 BIT DEVICE
Bits are the smallest electrical data that can be stored within the memory of an embedded/computer system in which the system carry from one memory to another by the help of data line. These data lines are usually equal to the devices bit, that is 8 bit devices has its memory units made up of 8 bits and these 8 data bits travel along 8 physical wires (data line) during saving and retrieving of information. Every computer related devices are classified based on number of data line(number of bits) it supports. The earliest computers are just 4 bit devices, before the coming of 8 bit devices which has also advanced to 16 bit and later on 32 bit computers and the most common one invoke that is 64bit computers. We currently also have 128bit computers but very rare due to its cost and sophiscated design we hope in the nearest future just like the earlier ones, 128 bits computers will surely be very common. These pathways within the computer through which data travel defines the speed of the computer to some extent just like crystal oscillator. The more the electrical paths the faster the computer operates. A single bit computer has only one electrical pathways, but 8 bit computers like AT89C52 has 8 electrical pathways therefore when you want to send 8 bit data into it all you need is to place all the 8 bit data into the data line and send it onces using just one crystal oscillator pulse. But if it is to be 1 bit device that the same 8 bit data has to be sent more than 8 times before the reciever can execute its operation, which means more than 8 crystal oscillator pulses, this will also make the system 8 times or more slower than the 8 bit counter part. It is also because AT89C52 is an 8 bit device that the 32 input/output pins are grouped into 4 ports (each make up of 8 bits). And each of the ROM, RAM and other memory registers are all made of 8 bits. Any information in the IC memory can be directly sent to these 4 ports of 32 pins. Finally, it is worthwhile to note that because AT89C52 is basically an 8 bit device, the maximum number each memory location can hold is 255, if ever you try putting a number greater that 255 you will notice that your program will generate errors (255 in decimal is the same as 1111111 in binary).
8. AT89C52 HAS 4 PORTS
Ports are another term used in referring to the 32 input/output, as stated earlier, these pins have been grouped into 4 of 8 bit each (P0, P1, P2 AND P3 that is PORT0, PORT1, PORT2 AND PORT3 respectively and during programming what computer recognizes is P0 not PORT0). Since AT89C52 is an 8 bit device, it will make sense if data can be comfortably moved from one memory to another including to the 32 input/output pins. Grouping these input/output pins into 4 ports made the 8 bit data movement very convenient. Example, one of the important register within AT89C52 is called "A" (meaning accumulator), if you have a value of 130 in it and you wish to send it out to be display outside the world all you need is any of the ports (P0,P1,P2 OR P3), and issue the below command:
MOV Px,A //this command will transfer the 130 in "A" to indicated port.
We will discuss more about these in assembly language progamming lessons.
9. AT89C52 HAS 8 KILOBYTE OF READ ONLY MEMBORY (ROM)
AT89C52 ROM is not very different from the flash drive or memory card we use on daily bases to copy, store and transfer our valuable information from one device to another. We have been saying that embedded systems use written program to perform its operations; this written progams therefore must be stored in the microcontroller permanently so that each time the device is turned ON it will have means of performing it task correctly. ROM stores its information permanently, that is in case of switching OFF your embedded system data/information stored in its flash memory does not get erased. It is also called non-volatile memory which means information store are not deleted in case of power outage. The only observable difference is that the AT89C52 ROM is very small, just about 8192 bytes unlike our common 2GB memory card which is about 2,000,000,000 byte. So comparing AT89C52 8KB to 2GB memory card is like comparing a swimming pool and River Niger.
ROM MEMORY MAP
That instruction ORG 0050, direct your AT89C52 to start storing information from memory location 0050 instead of 0000. We don't have much business with the ROM memory, we simply have to just transfer our written program into it and we are done with it, the memory of much interest and interaction with a programmer of embedded system is the RAM. Though we will still be discussing a little bit of ROM in programming lesson.
10. AT89C52 HAS 256BYTES OF RAM
Unlike the ROM, every computer system does all its data manipulations with the help of the RAM; in fact it should be noted that it is as the result of fetching data from the ROM into the RAM that is responsible for all you see computers do including the embedded systems. So the RAM has a very tricky role that maybe difficult to understand by fresh guys in computers and embedded system; but without the key role of RAM there is nothing that will be called computer or embedded system. The input/output pins are logically connected inbetween the ROM and RAM so as data are been fetched from the ROM into the RAM, the input/output pins are logically manipulated to give us all we see computers do (in fact even the input/output pins are also RAM called Special Function Registers). Another notable functions of RAM is helping the computer/embedded system to operate faster. Example, some information are constantly used by computer/embedded system, and fetching it always from the primary storage which is ROM usually result to slowing down of computer/embedded systems. So these constantly used data can then be kept in the RAM close to point of use so as soon as the computer/embedded system is in need of it, it can access it very fast.
RAM MEMORY MAP
Bit Addressable RAM [032 to 047] :watch closely at picture of RAM memory map above, you will observe the green area, that each memory is divided into 8 (8 bits); that is exactly how all the memory is in real life (that is from 000 to 255). But apart from those 16 bytes marked green you cannot touch a single bit without affecting the remaining 7 bits, unlike those marked green (bit addressable), one can alter a single bit without affecting the remaining seven bits. Any instruction that affects only a single bit, like SETB P1.0 OR CLR P1.1, can also be executed on these green area memory. But such instruction cannot be used outside these memory locations within the RAM Memory Map. If you have no interest using these memories because of their bit manipulation ability you can still use them as other memories we will be discussing shortly.
REGISTER BANKS [000 to 031]: Due to vast memory use in embedded/computer systems during their programmings, some memories are given specialized names just for the purpose of convenient, and not that using their memory number will not give the same results. It should be noted that every memory has position number, and during saving data or retrieving data from it all you need is position number. But for comfortability, some of them are known by their nickname instead of their position number. Example of such memories are the REGISTER BANKS, we have BANK 0, BANK 1, BANK 2 & BANK 3: each bank has its memory locations as R0, R1, R2, R3, R4, R5, R6 & R7.
watch these program that alters the value of RAM memory location 000.
These two above instructions accompanishes the same, the only different is first instruction used the memory position number while the second instruction used the same memory location's nickname. And the instruction itself simply moves 20 (simply a number) into that memory location. Another important note is that we have four the same nickname repeating itself, so how will one differentiate between R0 of BANK 0, R0 of BANK 1, R0 of BANK 2 & R0 of BANK 3? Well, there is another SPECIAL REGISTER that one can use to select between these many banks. But lets talk about this when we get to programming lesson section.
DIRECT ADDRESSABLE [000 to 127]: Within the RAM memory map all memory locations within 000 to 127 are called direct addressable memory (these memories include the register banks, the bit addressable memories and the rest till location 127).
MOV 30, #120
MOV R7, #70
These above instruction directly move numbers to specified memories.
INDIRECT ADDRESSABLE [128 to 255]: Within the RAM memory map all memory locations within 128 to 255 are called Indirect addressable memory.
Example: Let say you want to move number 120 into memory locations that is above 127, for instance memory location 130
MOV R0, #130
MOV @R, #120
These above instruction indirectly move numbers to memory location 130, as you can see You cannot do.
MOV 130, #120
Anyways we will explain it more in Assembly language programming, just wanted to make you understand the programming difference between direct address and indirect address. We cannot directly address memory above 127 because that function has been allocated to addressing another set of memories that are called Special Functin Registers. The next section will throw more light on this reason better.
11. AT89C52 HAS 128 BYTES OF SPECIAL FUNCTION REGISTER (SFR) [128 to 255].
These memories (registers) are specially designed for complex data manipulation that an ordinary registers as we have discussed above cannot do. Example, even the Register Banks cannot be used for mathematical operations like multiplication, division, subtraction e.t.c. So there are specialized registers for such advance bits manipulations. Example of Special Function Registers are A, B, P0, P1, P2, P3, DTPR, TMR0, TMR1, TMR2, INTR0 e.t.c. These registers occupy locations 128 to 255 (exactly the same location as allocated to Indirect addressable memories), because of this fact that SFR and indirect addressable RAM has the same address, we cannot address the indirect addressable directly. if we address these indirect addressable memory direct will will be affecting the special function register unknowingly.
SFR MEMORY MAP (DIRECTLY ADDRESSED)
In summary, make utmost sure that you have understood everyhting we have discussed here about AT89C52 before joining the programming lessons. If there is sections you do not get correctly ensure you asked booktalkers.com for complete clarification; else you will be stressing yourself in vain trying to understand the Assembly language programming. Assembly language programming is all about controlling the various section of the input/output pins, RAM and the SFR; therefore limited knowledge about them makes its programming an impossible adventure.