Monday, September 1, 2008

Online course on Embedded Systems

MODULE -1 (Introduction)

 Embedded Systems is simply the brain of most of the electronics based systems to access, process, store and control the data. Few simple electronics circuits can be intelligently hardware designed without a microprocessor or microcontroller but is not worth the economics except for simple passive operations. So it's more or less must to put this so called silicon brain, which we engineers call as microcontroller in all electronics systems.
Embedded market is experiencing best of it's times and double-digit market growth will continue for some more years. It will continue to grow as long as semiconductor ICs are used for data processing. This may be vague but to be precise, your career growth is assured for another five years. This is only a prediction/extrapolation based on the current trends in the industry. Who knows what may come in future, however next two years, it's going to be very hot and growing field. That too for India, where engineers have extra edge over other regions when it comes to programming will surely lead in the growth. Though programming is a major task in embedded systems. Programming knowledge alone won't help much in getting into this world. The real challenge is in understanding the electronics hardware and also other interface hardware (Automobile engine, heart patient's ECG, to a motor in a satellite).
Here in this free online course, our objective is to train in embedded system design to pass entry-level stage and prepare to deep dive into embedded world if you find it easy and interesting.

We recommend the readers/attendees for this course should be a holder of Bachelor of Engineering/Technology or Bachelor of Sciences in Electronics or it's closely allied branches. Otherwise if you are strong in theoretical parts of analog circuits, digital circuits, and microprocessor can also grasp this content easily. One thing is must; you should love C programming.

What is Embedded System? 
Being an electronic engineer, you might have seen PC desktop's motherboard; it's an embedded system. It has microprocessor (Pentium or Athlon), memory (DRAM DIMM module and onboard SRAM) , I/O interface (keyboard, mouse etc..), Peripheral communication interface (PCI, USB port, etc). This PC system's architecture is designed for application such as net surfing, excel, word, powerpoint, and you know the rest!!!. Say you want to use same computer to monitor the engine of your bike or car. Can you think of using big PC for that purpose? It's so impractical. The i/p and o/p are totally different, here comes customizing your own microprocessor/microcontroller, memory, display, i/o and peripheral interface and also the operating system. This field of designing application specific computer systems is called embedded systems development. If the response of this computer system need to be real time and highly reliable then it's called Real Time Embedded System. The real time means, say in a control system where a speed of motor need to varied the moment some parameter deflect from it's original value, then it's real time; no waiting or hanging.

To define in a sentence, Embedded Systems is a special purpose computer system/board, which encapsulates all the devices such as processor, memory, interface and control in single package or board to perform only a specific application tasks.

Figure 1: Sample block diagram of a typical embedded system.
The most common examples are,
     Cell-phones
     Automatic Teller Machine
     The Digital Interfaced Gasoline Station
     Airborne Flight Control System
     Automotive Engine Health Monitoring System
     Home Security Systems
     Modern Air-conditioners
     Washing Machines
     Medical Equipment
     DVD Players
     Printers
     Medical Equipment
     The list goes on………. Wherever the microcontroller is used it's embedded computer.
 The leading applications of embedded market are,
       Communication
       Computer Peripherals
       Industrial Control and Automotive
       Consumer Electronics
       Test and Measurement
       Medical
       Military/Aerospace
This only is a list of popular applications. The embedded is now getting into lot more interesting applications such as RFID, Agriculture etc.. Each application need some domain knowledge of it's interface hardware. Say the project is to develop a coffee vending machine controller; the embedded programmer/designer has to have knowledge on how the valves dispensing hot water and milk operate and their technical specification.
The user interface design is different for each application. Some application may not need graphic interface at all but some may need audio interface.
Embedded systems - Learning curve
Development for embedded systems is different from common practices in many ways. For new developers in the embedded systems world, there is a learning curve to understand where conventional practices are no longer valid in this new environment. To be an embedded systems developer, s/he need to know many things about the hardware on which software will be executed. Often embedded systems are connected to some sort of control system (activating some switch, rotating a motor) and the developer also needs knowledge of that system as well. If the CPU and/or the operating system are different on the target embedded platform, s/he have to do cross-platform development, which has its own issues. There are different testing techniques as well because most of the embedded systems don't have a monitor screen where error messages or test results can be displayed.
All of these issues make embedded systems development much more complicated than writing a program on a UNIX machine (or a windows PC) and then executing it.

 Module-2(Microcontroller and programming)

  Microprocessor, Microcontroller and System on Chip
Microprocessor:
Microprocessor is the Central Processing Unit (CPU) of embedded system. It does arithmetic and logic operations of the digital binary data.Very old embedded systems circuit/board was generally made up of separate microprocessor (8085), I/P interface, O/P interface, memory, clock and timing devices, power supply devices, and analog/linear devices.
Microcontroller:
In the early days of embedded systems, engineers have built embedded systems with separate set of devices connected on a printed circuit board. The complexity involved in manufacturing and re-engineering was very high with many Integrated Circuits and other components on-board. Also the advance in technology has enabled processor manufacturers to add one device after one into single IC. It started with adding I/O interface and memory, now we see lot more functions inside the processor chip. These microprocessors with all the additional support built-in are called microcontrollers.
To define, Microcontroller is an Integrated Circuit device with CPU, memory, I/O interface and any other logic and analog function on a single chip.

System On Chip (SOC)
Even though MCU holds most of the functions, it still lacks in few special analog functions and application specific functions. The idea of putting entire system (all the semiconductor IC functions) on a single chip is called System on chip. On a printed circuit board, you see a single IC accompanied with few discrete and passive components.
If we look at the recent microcontrollers released in the market, most of them are very close to System On Chip. The concept of SOC is well ticking in the market.
SOC is a common sense solution, that means, why we have to go for a complex board when we can put everything into a single IC. SOC saves board space, ease manufacturing, and score higher in reliability over non-SOC solutions. It's drawback is, the manufacturer profits from this product only if it's used in millions. Also it steals some design flexibility for the design engineer.
 
Processor Architecture
The two most popular architectures used in embedded world are Harvard and Van Neumann. Read this separate article describing the differences between these two architectures.
The Van Neumann V/S harvard processor architecture.

The popular microcontrollers and companies
There are plenty of microcontroller manufacturers all around the world. We in India don't have a LOCAL IC manufacturer who can supply microcontroller chips. However all the major microcontroller vendors in the world have support offices in all our metros. To learn about latest trends in microcontrollers read this report.
Market and technology trends of microcontrollers in the year 2006 

Programming: machine language, assembly language, and C programming
Throughout this course these below books will be suggested to you for further reference. We will mention the page number and book title wherever is required in this course material. Please buy these or refer them in any nearby library.
The books are,
1) Embedded Systems Building Blocks, by Jean LaBrosse
2) An Embedded Software Primer, by David Simon
3) The Art of Designing Embedded Systems, by Jack Ganssle
4) Fundamentals of Embedded software by Daniel Lewis
Assembly and machine language
Now let's start embedding! We will begin training you in programming now, and in coming modules we will be covering on details of functional blocks available on a microcontroller.
The microcontroller is the one, which decides what need to be done, what need not be done, and how to be done. Basic rule we need to keep in mind while "instructing" the microcontroller is - microcontroller is like a very intelligent child. The child (controller) would do exactly what was told it to do - nothing more nothing less. If the instruction is ambiguous then the behavior of the microcontroller would go haywire.
Example: In a bread toaster, the sequence of operations is,
a) Turn on the heater
b) Check whether the bread is properly roasted or not (by checking the temperature or set time)
c) If bread is not yet completely toasted properly again go to step (b)
d) Stop the heater as the bread is toasted properly.

Now how do you tell this sequence to a microcontroller inside a bread toaster? You should tell it (microcontroller) in a way it understands. It is like speaking to a person who knows some language, which you can't speak. The instant option left to you to speak to such person is to catch hold of a translator, who knows both the languages and translate/convert your language to other's language.
The language what all microcontrollers understand is called machine language. Here is just a few lines of machine language for Freescale's 6812 microcontroller.
CF0C00180B8000024D008018030FA008009600847FB1F033260EFE080009
7E080026EE4C008020E918030FA008004D008020DE23F000
Does this jumble of hexadecimal codes dismay you? Obviously you should be! Any way don't get disheartened by this magic series of numbers. But make it very clear this (machine language) is the native language of all the microcontrollers and you should "instruct" them only in their language. Also this machine language is different for each microcontroller families (8051, PIC, ARM etc..).
In the very early stages itself computer scientists/ chip designers noted this problem instantaneously and came out with a solution. For each of the operation that microcontroller can do (execute) they assigned an "English like" word so that programmer/ designer can easily instruct the microcontroller. This is called assembly language.
Here below is table of assembly languages instructions for popular PIC16xx microcontroller. In total it has only 35 instructions.


In this above table, the English type words in the first column are assembly language instructions and the binary codes in the fourth column are machine language instructions.

With this background let us do a small exercise. Let us try to add two numbers, say 3 and 4.
Again remember this,
[Microcontroller is like a very intelligent child. The child (microcontroller) would do exactly what was told it to do - nothing more nothing less. If the instruction is ambiguous then the behavior of the microcontroller would go haywire.]
MOVLW #3; Move value 3 into register W (working register).
What is register?
Register in the context of microcontrollers: Register is some temporary space which it can be used to keep some value temporarily. Generally every microcontroller will have some registers. Some registers have special purpose capability. In this context the register we are using is called "W" register or working register.
ADDLW #4; Now the working register content is added with another value 4.
So now the result 7 is in Working register and it can be used by programmer in any way he/she wants (like display/ store it future arithmetic operations etc)

Basically assembly level language is all about knowing what all the instructions are available with particular microcontroller and write the program (code) according to requirement using the list of available assembly instructions. So by now we know little bit of knowledge on how to speak to microcontroller in their own language.
Now the questions arise. We the programmers use the assembly language as the instructions to "instruct" the microcontroller. But it is already mentioned that microcontrollers only understand machine language as machine language is the native language of all the microcontrollers and we should "instruct" them only in their language.
How does the assembly language become machine language?
Here comes the "Assembler" (You language translator friend): Assembler is a program, which converts assembly instructions to machine language. It is like a translator (dhubashi) who would be used when two persons are communicating in completely different languages.

The C language - very essential
Now we know some basics of machine and assembly language, so that we can instruct microcontrollers in their own language.
Now let us C!
The assembly language programming would work only for simple embedded applications, as you develop bigger and complex applications the assembly language code will be very difficult to manage and the time and effort required to program and debug (fixing errors) rises exponentially with the total code size.

Assembly can still be used for simple programs, only if you wish to experiment. Otherwise C language is the only practical and efficient solution. The thumb rule to decide whether the program is simple or complex is, less than 1000 lines of assembly code than it's simple, greater than 1000 lines of assembly code; better call it complex.
Here few examples of some of the assembly instruction set of few popular microcontrollers.
680x0 based microcontrollers (680x0 are popular 16 bit series of microprocessor/ micro microcontrollers from Motorola)
BRA - Branch;
JNOV - Jump on No Overflow;
DBcc - Test Condition, Decrement, and Branch;
STJ - Store jump-register;
Some of the PIC based assembly instructions.
DECFSZ f,d - Decrement f, Skip if 0
BTFSS - Bit Test f, Skip if Set
IORWF f,d - Inclusive OR W with f
RETLW k - Return with literal in W
Some of the 8051 instruction set
DJNZ - Decrement Register and Jump if Not Zero
JBC - Jump if Bit Set and Clear Bit
LCALL - Long Call
LJMP - Long Jump
XCHD - Exchange Digits
What do you think of these instructions? Seems like some combination of English alphabets isn't it? We immediately feel intimidated by seeing them in the code. How much ever comments/ explanation is provided, it would be very difficult for "new" guy to understand the logic. (New guy is just an acronym here - if you see the code you have written in assembly after 6 months - you will be the "new" guy - In the beginning of my career(author) I have become "new guy" several times and left the difficult portion of the code - simply re-wrote the module - which saved my time and effort). So first problem is its readability. No matter what ever the clarity in description in the form of comments/explanation would make a new guy to feel uncomfortable with the code. So imagine a case of complex embedded application written by multiple developers. Simply it is hell.
Next problem is its (assembly languages) compatibility. Assume with great difficulty the embedded application is developed in assembly language and is fairly working well. Now suddenly market scenario changes and instead of microcontroller-X(which is used by you) microcontroller-Y is cheaper/ affordable (We have seen cases where the microcontroller (and its associated hardware design) changed overnight as another microcontroller was available for 10 cents less). Now in no way you can complete the project as,
--> You need to completely unlearn the assembly language of microcontroller-X and learn that of        microcontroller-Y
-->  Logically design the flow and implement the code using new assembly language
-->  Test the entire setup again.
Here comes the silver bullet - C language. Basically C language is universally known and any "new guy" can learn the basics of C in couple of weeks and understand the design / flow. Also if the hardware (controller) is changed/ redesigned all you need to do is re-compile your program for the new microcontroller :) Life is very easy.
Again - let us remember our postulate. Microcontrollers only understand machine language as machine language is the native language of all the microcontrollers and we should "instruct" them only in their language. So how does the C language code become machine language?
Here comes the "Compiler", compiler is a program which converts C language to machine language. It is like another (high level) translator
But bear in mind - Assembly language (or machine language) is the one which gives fastest and compact code. Basically assembly language is used in these two things.
· The first is for when you need to access hardware. Writing routines to interact with the hardware can be easier and cleaner than the equivalent operation in C. It is not difficult to export the assembly routines to make them callable from C or some other language, so you can get the advantage of having precise control over the hardware without having to write your entire program in assembly.
· The other thing assembly is good for is optimizing certain parts of a program. If you have an extremely time critical routine that is called a lot, then it makes sense to go through it with a fine toothed comb and choke every possible cycle out of it. You have to look at the speed gain and compare it to the time you spent optimizing the code. If you spent three hours optimizing a routine, and you only get a 2 microsecond speed gain, then you have to call that routine billions of times to make it worthwhile. In most cases, it's simply not worth the effort. Embedded applications and DSP are areas where hand optimization might make a significant difference, but unless you doing some extreme number crunching on a PC, it's probably not worth it.
Other than these two things assembly language does not play much role in embedded systems. There are some extreme cases where the complete windows programming is done in assembly. This just shows how much complexity a person can handle and remember this can not be done by next (other) guy.
Ok now we are little bit into the embedded systems. At this point it would be ideal to have some hardware/ assembler/ compiler to play with. We would provide some of the example hardware which we would be using (it would be better if you have them and keep trying the next sections/ examples). Otherwise it will be like hearing a nice story and forgetting everything.
 

Kits to buy to practice this course:

From this module onwards our teaching gets more practical. If you own a personal computer at home, you can establish your own lab to practice this course. You got to do little shopping to establish an embedded lab.
You need microcontroller development and support material. We have decided to tailor this course for ARM 7 based microcontrollers. The kit we are using is AME-51 lite (ML67Q4050) from OKI Semiconductor.
Here are brief specs of the kit:
Kit Name: AME-51 Lite, the kit consists of,
AME-51 Lite CPU Board with processor ML67Q4050 (ARM 7)
RS232 Serial Cable
OKI AME-51 Lite CD (GNU Compiler)
Quick Start Guide
The cost of the kit is with in 6000/- Rs (Inclusive of taxes)
The contact details to buy this kit are,
Contact person: Amit Agarwal
OKI Semiconductor Singapore Pte. Ltd.
906 Prestige Meridian -1
29 MG Road Bangalore
Ph: 91 -80- 41530990/91/92
Mobile: 91- 99001-59714
If you have difficulties in contacting them, please let us know we will buy for you.
If you don't have PC or due to other problems can't able to buy this kit. You can use our lab and library facilities in our office to practice and learn under experts. We provide further practical guidance.You can send us email or call us to get more details. Without practicing you can learn through this course, but it's the practice which makes you perfect and complete.

Module -3 (Kit Installation)
As already mentioned in module-2, we are using AME-51 Lite Microcontroller board in our course. It's based on 32 bit ML67Q4051 ARM processor from OKI Semiconductor.
The items bundled with this kit are,
AME-51 Lite evaluation board with ML67Q4051 MCU on board
Serial RS232 cable - 9-pin male/female
OKI AME-51 Lite CD
Quick Start Guide
Kit does't provide 5V DC power suppy. It has to be purchased separately from any electronic shop. The rating of this power supply is,
Output Voltage = 5 to 7.5V unregulated DC Voltage
Current rating = 1Amp
with a 2.1 mm power adapter (Center pin positive)

The components on the AME-51 Lite evaluation board are,


MCU ML67Q4051
Oscillator 32.768 MHz (main clock); 32.768 KHz (sub-clock)
SRAM 1MB – 256K x 32-bit
Serial Ports UART0 (J1-DB9 female pins); UART1 (P1-DB9 male pins)
JTAG interface CONN1 – 20-pin header (10 x 2 dual row)
Power Supply 3.3 V and 2.5 V regulated on board power supply
Power connector J2 – connect optional external 5-to-9 VDC, 400 mA supply
Power indicator D6 – red LED
DIP Switch SW1 - 8 position - configures MCU operating modes
Pushbutton Switches SW2 – EFIQ; SW3 – EXIRQ1; SW4 – RESET
LEDs D2 – green; D4 – yellow; D5 – red
LED display LED1- 7 segment numeric
Inter-board connectors CONN2, CONN3, CONN4, CONN5

Here is the picture of the board

 
1. White cable above number '1' is RS 232 cable.
2. Black plug below number '2' is power plug.
Kit installation
Please follow these below steps to connect the board to your PC.
Place your board at a convenient place next to your PC.
Plug in 5V DC adapter to CPU board. Ensure your 5V DC power supply plug's center pin is positive and it should be of size 2.1 mm. Also make sure your 5V DC adapter current rating is 1Amp.
Connect the provided serial cable from your PC's serial port to the board serial port UART0
There is an 8 position DIP switch to tell the board what to do.
Board can go into four operating modes through four different configurations of the switch.
Here is the switch position table for the four modes.
SW1 Serial Flash Program Stand-alone JTAG debug SRAM debug
FWJ OFF OFF OFF OFF
ROMSEL X ON ON ON
EXBUSE X ON ON ON
EXIROME OFF OFF OFF OFF
BOOT1 OFF OFF ON ON
BOOT0 ON OFF OFF OFF
BOOTCLK X X X X
JTAGE X X ON X
X - Dont care (can be either Off or On)

Default set position is stand-alone mode. If not in stand-alone mode, set it to stand-alone mode.
Installation of software part (compiler):
Load the CD provided in the kit into your PC's CD ROM drive and look for ame51setup.exe and double click to install. Follow the easy instructions and complete the installation. It's must to install it on C drive to avoid the complexity of changing root setting in the makefiles.
Unzip the file ttermp23 and extract them to a folder. In the unzipped files click setup.exe to install Tera Term Terminal Emulator software. Follow the simple instructions and complete the installation.
This software will only work on following Operating Systems
Windows XP professional
Windows 2000 with service pack 1 installed
Windows 98 second edition

At this stage both hardware and software installation is complete.
PS: We have used ARM based kit over 8051 and PIC for the reason of its rising popularity and growing importance.
Module -4


OK - Let us start now with "real" hardware (board) and real "software".
One thing you should make sure is that you have the hardware board and the necessary software installed on your PC.(if at all you have purchased the OKI development board) and read about the various documents available with that.
Reading of Module-3 is good enough to install h/w and s/w. However if you need further details, search for AME-51 lite on www.okisemi.com for user manual docs of this kit. Click on this link to dowload the same from our website.
First thing you need to have is an "editor". Basically editor is a program by which we can "see" and "alter" source codes. There are plenty of editors available from normal notepad to highly sophisticated editors, which would be part of IDE (Integrated Development Environment). To start with let us start using an editor called "Notepad ++". This is freeware (no need to pay any money for this). You can download this from following link. In case if you can not find this freeware, the ordinary notepad of any windows operating system can be used.
http://sourceforge.net/project/showfiles.php?group_id=95717&package_id=102072
Once you have installed the development environment provided by OKI - you would have "ame51gnu" directory (folder) in C:\ drive. You can browse through various folders in this "C:\ ame51gnu\" and get to know how the source code is organized for examples etc.
674051 Directory
There are at least 4 sub directories within 674051 Directory. Below figure explains the meaning of each of this item
Name of the folder Explanation
COMMON Directory for common assembler and C Sources
NewProj Template project directory
Hello “Hello world” sample program
Bootimage SRAM downloaded binary
LED LED Sample program
TestLED Created to for this discussion purpose (you will not have this folder)
Some other different example folders Would contain corresponding source files.
Common Directory holds two more subfolders (i) INC and (ii) SRC.
INC folder holds all the header files for source programs.
SRC holds the common assembler and C programs.
They would be locate at "C:\ame51gnu\Examples\674051\COMMON\INC" and "C:\ame51gnu\Examples\674051\COMMON\SRC" respectively.
The contents of these folders would be shown as below.
Project Directory (example Hello)
This "Hello" directory is taken as an example folder (as most of you would be aware of "Hello world" program in the beginning of your C language learning days). This folder (as well others also) consists of "hello.c" (this would be different for other example programs), "flash.ld" and "Makefile" - this would be same (at least the name of these files) in all other example programs also.

 
Hello.c : This is the main source program.
Flash.ld : Linker script which defines how the program places into RAM and ROM of the chip
Makefile : It defines the compiler and linker setting. It automates the whole compiling process by just typing "gnumake"
With this background of understanding file structure, how they are organized, and how the typical source code directory would look like (like hello directory), let us start embed ourselves in to our first embedded program.
You should know how an output device is connected (and what is the output device used), intricacies of this output device etc… So let us start our first program with LED. Let us turn ON an LED. Here are the basic steps to be followed.
1) First copy the LED directory as TestLED. Now you would have TestLED folder as well in "C:\ame51gnu\Examples\674051" directory
2) Rename the LED.c in this directory as TestLED.c (you can use dos command "ren" or press "F2" in windows to rename).
3) Open the TestLED.c file with "Notepad++" and delete all the content.
4) Insert the following code in this file (TestLED.c)
int main(void)
{
int i;
volatile unsigned char * ModeRegister;
volatile unsigned char * OutputRegister;

ModeRegister = 0xB7A04008;
OutputRegister = 0xB7A04000;

*ModeRegister = 0x01; // Configure Port E bit 0 as outuput
while (1)
{
*OutputRegister = 0x01; // Set the Port E bit 0 as 1
//Delay
for (i=0; i<1000000; i++)
;
*OutputRegister = 0x00; // Set the Port E bit 0 as 0
//Delay
for (i=0; i<1000000; i++)
;
}
}

5) Open the "Makefile" (in the same directory) and replace the line number 12 as OUT = TestLED . Basically      this command tells the compiler to name the outfile as TestLED.hex

6) Now compile the code -  
     To compile, open the DOS prompt of your system (to open the dos prompt click on the start button/icon of      your window OS and look for run in the menu list  and click on it. Type "cmd" or "command" in the entry space      and click ok). Now the DOS prompt will open to a default location. Now type the DOS command
      "cd    \ame51gnu\examples\674051\TestLED\" at the DOS prompt.
     Type "gnumake" at the DOS prompt to compile. Now the program should compile (any error! read the                 embedded_kit_manual.pdf. for detailed program compiling and running guidance).

7) After compiling check in the folder  \ame51gnu\examples\674051\TestLED\, you could see a new file called      TestLED.hex is created. This stores the hexadecibal machine langauge code of this program.

8) Load this TestLED.hex file into the board through serial port connected from PC to board using Tera term pro software already loaded on your PC. To learn how to load the program read embedded_kit_manual.pdf.

9) After loading the program change the switch positions from stand alone mode to SRAM mode and press the reset button.
10)Now you would see the RED LED blinking. It will turn ON and OFF - continuously.
 
Here is the explanation of the code.
1) Generally any processor or microcontroller would have some ports. These are called General Purpose Input Output (GPIO).
Here you may ask - why these are called GPIO or Ports? If you recall - in earlier days any "goods" that should enter or leave a country would be transported through ships and these ships would be entering or leaving ports of that country. In the same way if you want to send any signal you should put the signal in the port and it would be send. Conversely, if you wish to receive any signal - you should receive (or read) using the ports.
2) The ARM Chipset used in this board has Port 0 to Port 15. Each port has different bit width(some ports has 8 bits, some have 7, some have 6 and some are with only 5, which could be used for any purpose (so called as GPIO).
3) In this example we are using Port E - bit (0). Each port can contain any number of individual bits that can be used (generally port would have 8 bits). This is like saying 8 ships can arrive or depart from this port. Always note that these bits (or anything in embedded world) would be counted from 0! So 8 bits means bit 0 to bit 7 are available.
4) In a way - we are forced to use this Port E - bit( 0), because this is the bit, which is connected to RED LED in our kit. So it is always required for embedded engineers/ programmers to have the complete understanding of the hardware - how it is connected like? what is connected to? where it is connected ?and why. Read the manual embedded_kit_hw_manual.pdf to know how the LEDs are connected.
5) Now we know that, we need to make this Port E - bit (0) high (called some times logical 1 - or simply the voltage becomes +5V) to make the LED to glow and Port E - bit (0) to Low (called some times logical 0 - or simply the voltage becomes 0V) to turn off the LED.
6) You cannot use a port to write and read just like that! You need to tell the processor (in our case ARM chipset) that we are using the particular port and particular bit as output or input. This is like having two-way line - we need to "go" out in left and "come" in back in right. ARM chipset provides a "Mode control" register, which does this job.
7) So in summary - we need to "tell" the processor that we are using Port E - bit (0) as output and make this bit high and low in a continuous loop.
8) Now look at the code once again.
     I.  "int i" is used as general purpose variable (used for delay).
     II. ModeRegister and OutputRegister are used as 8 bit pointers (both have 8 - GPIOs)
    III. ModeRegister is at 0xB7A04008 and OutputRegister is at 0xB7A04000 (Note any ports would   have          address - This address is used to access particular port)
    IV. Now configure this Port e - bit 0 as out put by writing 0x01 (as last bit is made 1).
    V.  Now write 0x01 to OutputRegister to glow the LED and write 0x00 to turn off the LED.
    VI. Do this in a loop so that LED turns ON and OFF continuously.

Module -5 (Processor Architecture)
Architecture of ARM7 TDMI processor core and ML67Q4051 Microcontroller

The background of ARM:
ARM is the acronym for Advanced RISC Machine, an UK based company, which has pioneered the growth of RISC processor Architecture.
What is RISC? RISC stands for Reduced Instruction Set Computer. RISC based architecture although invented quite earlier but has become popular and overtook its rival architecture Complex Instruction Set Computer (CISC) somewhere during late nineties. Most popular CISC architecture is 80x86 processors from Intel. Recently Intel too has adopted RISC kind of features and architecture in latest CPUs.
The advantage of RISC is in the simplicity (in terms of processor resource consumption) of the instructions and processing time. Each instruction takes only single clock cycle. Overall power consumption is very less. Due to this fast response, low power consumption and coding flexibility, RISC architecture is highly suitable for embedded systems. However there is one drawback with RISC, that is the instruction set code is longer and takes more memory. This issue is no more a concern with the growth in the memory technology.
As said earlier in the previous modules, we are using OKI Semi's ML67Q4051 microcontroller in this course material. The processor core used in ML67Q4051 is ARM7TDMI. ARM7TDMI is the most used RISC core from ARM.

The architecture of ARM7TDMI is shown in figure below

Highlights of ARM7TDMI:
    --There are 37 registers of 32 bit wide in this processor core. 16 registers are available for the  programmer.
    --It's pipeline architecture; that is 3 instructions are processed simultaneously at 3 different stages.
    --The bus architecture is of Von Neumann type where single 32-bit data bus carry both instructions  and data.
    --The data-types can be of 8 bit /16 bit/32 bit wide.
    --Processor can run on seven different modes based on the application requirement.
    --Has built in 32x8 multiplier and a 32 bit barrel shifter (both needed much for DSP functionality)
    --This processor can also execute another instruction set called THUMB state (16 bit) to give the        programmer an option to use this processor like CISC processor. The total instruction set can be tidier and takes less memory space.

To study in-depth, the architecture and other capabilities of ARM7DMI, please read the pdf file from the link below. The content of this pdf file is simple and self-understanding.
http://www.arm.com/pdfs/DDI0210B_7TDMI_R4.pdf
OKI'S Microcntroller ML67Q4051:
Now let us read about the OKI Semiconductor's MCU ML67Q4051. It has quite a good list of latest features.
The features of this MCU are,
   --Has built in SRAM of 16 KB and FlashROM of 128 KB.
   --Has robust clock network
   --Interrupt controller supports 41 interrupt resources
   --External memory controller to access ROM, SRAM, and I/O connected to the external memory  space.
  --Has system timer of 16bit auto reload timer with its interrupt given high priority.
  --Built-in DMA controller enables direct data transfer between memory-memory, I/O -memory, and  between I/O -I/O devices to spare the CPU from simple data transfer burden.
   --Watchdog timer to monitor the program from running out of control and generate interrupt or reset  signal.
  --The built-in 4-channel, 10-bit resolution analog-to-digital converter supports two modes of  operation:  Scan mode sequentially converts input from the selected range of channels; select mode  converts input   from a single channel.
  --Has 15 general purpose I/O ports: 8channels of 8-bit, 3 channels of 7-bit, 3 channels of 6-bits, and  one channel of 5 bits.
  --Integrates one channel of I2C bus interface, one I2S (serial audio interface) bus interface, one UART  interface, one SIO interface and one SPI interface.
  --Real time clock (RTC) with 10,000-year calendar with resolution down to 1 second.
  --Flexible timer block with 6 channels of 16 bit timer.
  --Has JTAG interface to debug the program from a host computer

The internal architecture of this MCU is shown below.
 
To learn in-depth about this MCU please visit following link:
http://www.okisemi.com/eu/docbox/ML67Q4050_4060-DS_rev1.2.pdf

Module - 6 (Sample programs 2)
Sample Program 2: Display decimal numbers from 0-9 in the seven segment display.

As we learn the basic idea about programming and how to run and execute it in the previous module (module -4), its time to move on to next level of programming. Instead of turning ON & OFF an LED, let us use the seven segment display device in the kit to display or count the numbers from 0 to 9.
Before going into programming steps, let us have an idea about the seven segment display and its connectivity to the driver & the MCU unit.
The following fig (Fig 6.a) shows various elements of a 7 - segment display and how it is connected to different pots of the MCU GPIO (General Purpose Input and Output). Please note segment A- F are connected to one port(PF) and elements G and DP are connected to other port (PD)

 
Fig 6.a
For circuit details download the circuit diagram file. Though reading into circuit is little stressful but gives more insight into how the ports are connected.
The display used is a common anode type RED display unit , which is connected to the MCU through a Low-Voltage Octal Bus Buffer (inverted), TC74LCX240F.
So now what we have do is, to find out the Hexadecimal code word for each number (from 0 to 9) to get displayed on the 7-segment LED display unit.

Port Segment 1 2 3 4 5 6 7 8 9 0
PF 0 A OFF ON ON OFF ON ON ON ON ON ON
PF 1 B ON ON ON ON OFF OFF ON ON ON ON
PF 2 C ON OFF ON ON ON ON ON ON ON ON
PF 3 D OFF ON ON OFF ON ON OFF ON OFF ON
PF 4 E OFF ON OFF OFF OFF ON OFF ON OFF ON
PF 5 F OFF OFF OFF ON ON ON OFF ON ON ON
PD 3 G OFF ON ON ON ON ON OFF ON ON OFF
PD 4 dp X X X X X X X X X X
Chart 1
The above chart (chart 1) provides the information on which output lines/pins to be made high or low to display the corresponding numbers. We have not used dp (decimal point) segment and so is the status given as 'X' meaning don't care.
Now, let us figure out the hexadecimal code for each number.
For example let us take the number "2". From the chart given above, it can be understood that, to display 2, we need to make a, b, d, e & g segments of the display "HIGH" and the remaining segments "LOW".
Thus the GPIO output registers PF & PD should hold the data values as shown below.

Bit PF5 PF4 PF3 PF2 PF1 PF0
Data X 1 1 0 1 1

Bit PD5 PD4 PD3 PD2 PD1 PD0
Data X 0 1 X X X
X - Don't care
Here let's assume to substitute X with 0 (zero). The hexadecimal equivalent of "011011" is "1B" and that of "001000" is "08". So to display the digit  "2" we need to load 1B to the register of the port PF and 08 to the register of the port PD.
Similarly we can find out the hexadecimal equivalent code for each digit to be displayed.
Once we got the Hexadecimal code for each number, it is time to move to the programming section. Here are the basic steps to be followed.
  1. As we did in the Module-4 , copy the TestLED directory as COUNTER. Now you would have COUNTER folder as well in "C:\ame51gnu\Examples\674051" directory
  2. Rename theTestLED.c in this directory as counter.c
  3. Open the counter.c file with "Notepad++" or any other text editor and delete all the content and insert the following code in this file
int main(void)
{
int i;
volatile unsigned char * ModeRegister1;
volatile unsigned char * OutputRegister1;
volatile unsigned char * ModeRegister2;
volatile unsigned char * OutputRegister2;
ModeRegister1 = 0xB7A05008; // PF GPIO mode register address in Hex
OutputRegister1 = 0xB7A05000; // PF GPIO output register address in Hex
ModeRegister2 = 0xB7A03008; // PD GPIO mode register address in Hex
OutputRegister2 = 0xB7A03000; // PD GPIO output register address in Hex
*ModeRegister1 =0x00FF; // Configure Port F bit 0 as output
*ModeRegister2 =0x00FF; // Configure Port D bit 0 as output
while (1)
{
*OutputRegister1 = 0x003F; // Display 0
*OutputRegister2 = 0x0000; // Display 0
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0006; // Display 1
*OutputRegister2 = 0x0000; // Display 1
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x001B; // Display 2
*OutputRegister2 = 0x0008; // Display 2
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x000F; // Display 3
*OutputRegister2 = 0x0008; // Display 3
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0026; // Display 4
*OutputRegister2 = 0x0008; // Display 4
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x002D; // Display 5
*OutputRegister2 = 0x0008; // Display 5
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x003D; // Display 6
*OutputRegister2 = 0x0008; // Display 6
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0007; // Display 7
*OutputRegister2 = 0x0000; // Display 7
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x003F; // Display 8
*OutputRegister2 = 0x0008; // Display 8
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0027; // Display 9
*OutputRegister2 = 0x0008; // Display 9
for (i=0; i<1000000; i++); //Delay
}
}

5. Now compile and run the program by following the same steps as given in module -4.
6. Follow the procedures to load the program into the board as mentioned in module-4 or read this     user manual.
Now you should see the 7-segment LED display, counting from 0 to 9 and repeats the same.
You might have already got an idea about the ARM chipset used in this kit, different ports, bits, different type of modes in which it can operate (or can be configured), and how to use the mode register & output register to make the MCU to work according to our needs. Same kind of configuration is done in this sample program also, except the registers used are PF & PD.
  
Sample Program 3: Simultaneously display decimal numbers from 0-9 in the seven segment display and lighting the three LEDs available on the board/kit.

The following fig (Fig 6.b) shows the connectivity of the 3 LEDs (Green, Red & Yellow) to the Micro controller ports.

 
Fig 6.b

Hope you remember that we used the port PE in the sample program in module 4.
Same port is used here too to connect the three LEDs to the MCU.
And also, as you saw in the previous sample program in this module (module 6), the ports PF & PD are used for the 7-segment display unit to get connected to the MCU.
So let us do it in a different way. Make the green LED to glow when the 7-segment displays digits '2' and '4', both green & yellow LEDs to glow while displaying '5', '6' and '7' and all the three LEDs to glow while displaying '8' and '9' on the 7-segment display.
So let us configure the output register of the port PE for the three LED to work along with the 7-segment display unit.
As you can see in the fig 6.b, the port bitsPE0, PE1 & PE2 are used to drive the red, Yellow & green LEDs respectively. And remember they are wired (in the kit) as "active low".
 
Bit PE6 PE5 PE4 PE3 PE2 PE1 PE0
Data X X X X 0 0 0

Suppose we want to glow the three LEDs at a time. For this, all the three bits PE0, PE1 & PE2
are made Low (0) (bits PE3 to PE6 kept as "Dontcare" in this case)
Its clear that the hexadecimal code to display all the three LEDs is 00 and to make all of them OFF, the code is 07.
Now its time to focus on the program code. Let us change the previous program as show below.

int main(void)
{
int i;
volatile unsigned char * ModeRegister1;
volatile unsigned char * OutputRegister1;
volatile unsigned char * ModeRegister2;
volatile unsigned char * OutputRegister2;
volatile unsigned char * ModeRegister3;
volatile unsigned char * OutputRegister3;
ModeRegister1 = 0xB7A05008; // PF GPIO mode register address in Hex
OutputRegister1 = 0xB7A05000; // PF GPIO output register address in Hex
ModeRegister2 = 0xB7A03008; // PD GPIO mode register address in Hex
OutputRegister2 = 0xB7A03000; // PD GPIO output register address in Hex
ModeRegister3 = 0xB7A04008; // PE GPIO mode register address in Hex
OutputRegister3 = 0xB7A04000; // PE GPIO output register address in Hex

*ModeRegister1 =0x00FF; // Configure Port Fbit 0 as outuput
*ModeRegister2 =0x00FF; // Configure Port D bit 0 as outuput
*ModeRegister3 =0x00FF; // Configure Port E bit 0 as output
while (1)
{
*OutputRegister1 = 0x003F; // display 0
*OutputRegister2 = 0x0000; // display 0
*OutputRegister3 = 0x0007; // all the three LEDs OFF
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0006; // Display 1
*OutputRegister2 = 0x0000; // display 1
*OutputRegister3 = 0x0007; // all the three LEDs OFF
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x001B; // display 2
*OutputRegister2 = 0x0008; // display 2
*OutputRegister3 = 0x0003; //Green LED ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x000F; // display 3
*OutputRegister2 = 0x0008; // display 3
*OutputRegister3 = 0x0003; //Green LED ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0026; // display 4
*OutputRegister2 = 0x0008; // display 4
*OutputRegister3 = 0x0003; //Green LED ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x002D; // display 5
*OutputRegister2 = 0x0008; // display 5
*OutputRegister3 = 0x0001; // Green & Red LEDs ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x003D; // display 6
*OutputRegister2 = 0x0008; // display 6
*OutputRegister3 = 0x0001; // Green & Yellow LEDs ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0007; // display 7
*OutputRegister2 = 0x0000; // display 7
*OutputRegister3 = 0x0001; // Green & Yellow LEDs ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x003F; // display 8
*OutputRegister2 = 0x0008; // display 8
*OutputRegister3 = 0x0000; //green, Yellow, & Red LEDs ON
for (i=0; i<1000000; i++); //Delay
*OutputRegister1 = 0x0027; // dis9
*OutputRegister2 = 0x0008; // dis9
*OutputRegister3 = 0x0000; //green, Yellow, & Red LEDs ON
for (i=0; i<1000000; i++); //Delay
}
}
Now what you have to do is just edit the "counter.c" file in the COUNTER folder and alter the program as shown in the above code, or copy this program entirely to the counter.c file after deleting the previous code . compile and run the code as explained along with the previous sample programs. Now you can see the LED getting on in the planned sequence while the seven segment is displaying 0 to 9.

Now its times to write your own program?
Write an easy program to display hexadecimal numbers F to 0 in decremental order

1 comments:

Thanks for sharing the information!

Post a Comment

post the comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites