Professional Documents
Culture Documents
CPU
General
Purpose
MicroProcesso
r
DATA BUS
RAM
ROM
Timer
I/O
Port
ADDRESS BUS
BUS
Figure-1: Microprocessor (only the leftmost box). You have to connect other components like
RAM, ROM etc. using data BUS and Address BUS
CPU
Timer
RAM
ROM
Serial
COM
Port
I/O Port
Figure 2 : Microcontroller (The whole Box). All components are inside the
microcontroller. When you buy microcontroller, you get all components (RAM, ROM
etc.) as built-in inside the chip.
In many applications, for example, a TV remote control, there is no need for the computing
power of an 80486 or even an 8086 microprocessor. In many applications, the space used, the
power consumed, and the price per unit are much more critical considerations than the
computing power. These applications most often require some I/O operations to read signals
and turn on and off certain bits. So, microcontroller will be a good choice in these cases.
Microcontrollers for embedded systems:
Microprocessors and microcontrollers are widely used in embedded system products. An
embedded system is controlled by its own internal microprocessor (or microcontroller) as
opposed to an external controller. Typically, in an embedded system, the microcontroller's
ROM is burned with a purpose for specific functions needed for the system. A printer is an
example of an embedded system because the processor inside it performs one task only;
namely, getting the data and printing it.
In contrast with a Pentium-based PC (or any x86 PC), which can be used for any
number of applications such as word processor, print server, bank teller terminal, video game
player, network server, or Internet terminal. A PC can also load and run software for a variety
of applications. Of course, the reason a PC can perform myriad tasks is that it has RAM
memory and an operating system that loads the application software into RAM and lets the
CPU run it. In an embedded system, typically only one application software is burned into
ROM. An x86 PC contains or is connected to various embedded products such as the
keyboard, printer, modem, disk controller, sound card, CD-ROM drive, mouse, and so on.
Each one of these peripherals has a microcontroller inside it that performs only one task. For
example, inside every mouse a microcontroller performs the task of finding the mouse's
position.
Today, embedded systems are found in cell phones, digital cameras, camcorders,
portable video games, calculators, and personal digital assistants, microwave ovens,
answering machines, home security systems, washing machines, lighting systems, fax
machines, copiers, printers, and scanners, cash registers, alarm systems, automated teller
machines, transmission control, cruise control, fuel injection, anti-lock brakes, active
suspension and many other devices/ gadgets. Hence, learning about the embedded system is a
doorway to learn controlling these devices.
Criteria for choosing a microcontroller:
1. The first and foremost criterion in choosing a microcontroller is that it must meet the task
at hand efficiently and cost effectively. In analyzing the needs of a microcontroller-based
project, we must first see whether an 8-bit, 16-bit, or 32-bit micro controller can best
Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB)
handle the computing needs of the task most effectively. Among other considerations in
this category are:
a) Speed: What is the highest speed that the microcontroller supports?
b) Packaging: Does it come in a DIP (dual inline package) or a QFP (quad flat
package), or some other packaging format? This is important in terms of space,
assembling, and prototyping the end product.
c) Power consumption. This is especially critical for battery-powered products.
d) The amount of RAM and ROM on the chip.
e) The number of I/O pins and the timer on the chip.
f) Ease of upgrade to higher-performance or lower-power-consumption versions.
g) Cost per unit: This is important in terms of the final cost of the product in which
a microcontroller is used. For example, some microcontrollers cost 50 cents per
unit when purchased 100,000 units at a time.
In this respect, we have chosen either Atmenga32 (or ATmega16) because it can be
operated at moderate speed (up to 8KHz internal clock, also higher external clock), it has
sufficient amount of RAM, ROM for any decent project and has many features like ADC
etc. on a single chip.
2. The second criterion in choosing a microcontroller is how easy it is to develop products
around it. Key considerations include the availability of an assembler, a debugger, a codeefficient C language compiler, an emulator, technical support, and both in-house and
outside expertise. We have chosen ATmega series because a lot of community support is
available online. Many free and efficient compiler and code samples from different
projects are present for ATmega32.
3. The third criterion in choosing a microcontroller is its ready availability in needed
quantities both now and in the future. For some designers this is even more important
than the first two criteria. Currently, AVR and PIC are two most used microcontrollers. In
recent years, companies have begun to sell Field-Programmable Gate Array (FPGA) and
Application-Specific Integrated Circuit (ASIC) libraries for the different microcontrollers.
Familiarization with ATMEGA32:
Note that:
Total 4 PORTs (A, B, C, D)
Each PORT has 8 PINS, e.g.,
PORT A has PA7PA0 pins.
PIN 10 is power supply (VDD
= 5V), PIN 11 is GND (0V).
Make sure
is
connected to 1.
Experimental Procedure:
SET A PIN OF MICROCONTROLLER AS INPUT OR OUTPUT:
Suppose, a LED is connected to pin 40 and a Button is added to pin 5. How will you set the
controlling register?
TO SET A PIN AS OUTPUT:
1. First set the pin 40 (which is PA0) to Output. To make any pin as output set a 1 at that bit
position. Since PA0 is the 1st bit, make the 1st bit of DDRA as 1. Use the following line:
DDRA = 0x01;
// in Hex
Or,
DDRA = 0b00000001;
// in Binary
2. Once the PA0 is set as output, decide what you want to show by this pin. You can show
either 0 or 1.
To set PA0 as high
PORTA = 0x01;
Or,
PORTA = 0b00000001;
To set PA0 as low
PORTA = 0x00;
Or,
PORTA = 0b00000000;
Button
LE
D
2. Once the PB4 is set as input, check whether the pin is high or low using if clause
if ((PINA&0b0001000) == 0) // If PB4 is zero, then using AND with 1 will be 0
{
// code when button sends zero (PB4 = 0)
}
else if ((PINA&0b0001000) == 1) // If PB4 is 1, then using AND with 1 will be nonzero
{
// code when button sends high (PB4 = 1)
}
___________________________________ _______________
Our first Project: we will write a code that will blink a LED.
Circuit Connection: Suppose, a LED is connected to PORTB at PIN B0 (i.e. at PIN 1, see fig.
3).
__________________________________________________
First we have to select the language; we can use either assembly or C to write the code. But
the microcontroller only understands Intel Hex format. So, we will use a compiler that will
convert the code (Written in assembly or C) into a Hex file. Then we will download the Hex
file into the flash memory of the microcontroller. The microcontroller will run according to
the code.
The size of the hex file produced by the compiler is one of the main concerns of
microcontroller programmers because microcontrollers have limited on-chip Flash memory.
For example, the Flash memory space for the ATmega32 is 32K bytes.
What should you choose between C and Assembly depends on the need of the project. The
following table shows the difference between the languages (Assembly and C)
Assembly
hex file generated by the assembler is
much smaller
1. Programming in Assembly language is
often tedious.
2. Programming in Assembly language
takes a lot of time and patience.
1.
2.
3.
4.
C
Hex file generated by the compiler is
larger
It is easier and less time consuming to
write in C than in Assembly.
C is easier to modify and update.
You can use code available in function
libraries.
C code is portable to other
microcontrollers with little or no
modification
Since, we have 32KB of memory to use (It is actually a lot of memory), Hex file size will not
be a concern for our LED blinking program. For all the benefit promised from C language,
we will use the C programming language to code.
Second, we have to use a compiler that will convert our C code to Hex file so that
microcontroller can read the Hex file and work accordingly. There are several compilers like
AVR GCC & AVR Studio, Micro C, Raisonance Kit (Ride) etc. AVR GCC & AVR Studio
are a good compiler you can use, but we will use MicroC because MicroC has a lot of built-in
libraries that will help you a lot, as you will see later.
Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB)
Finally, before start coding, lets review about the data types of C language.
No.
1
2
3
4
5
6
7
8
Data Type
unsigned char
char
unsigned int
int
unsigned long
long
float
double
Size in Bits
8-bit
8-bit
16-bit
16-bit
32-bit
32-bit
32-bit
32-bit
Data Range
0 to 255
-128 to + 127
0 to 65,535
-32,768 to +32,767
0 to 4,294,967,295
-2,147,483,648 to +2,147,483,648
1.175e-38 to 3.402e38
1.175e-38 to 3.402e38
As you can see, there are total 8 data types. Use data types wisely. Suppose, you want to save
your age in a variable, which data type will you use? Suppose, you declare a variable named
age like this:
unsigned int age;
Now, from the table you can see, the age variable you declared can vary from 0 to 65,535.
But, you can expect to live up 120 years at best. Unless you have found some way to live
over thousand years (!), you should use the 1st data type (unsigned char, 8 bit range up to
255).
Whats the benefit of using smaller data types?
Instead of using 16 bit unsigned data, you are using only 8 bit char. So, you have saved (16
bit 8 bit) = 8 bit data (1 byte). You may think, in a world of terabyte, saving 1 byte is not a
big deal. But in a world of microcontroller where you have 32Kbytes of code memory, saving
1 byte is a big deal. Even if you were coding a microcontroller with 1 Terabyte memory, you
should always try to use less memory as you can. Never waste the resources, even it is 1 bit.
This will make the difference of an efficient coder and a bad coder.
Using MicroC for AVR:
1. Open Micro C compiler. Go to: Project menu > new project (Shortcut: Ctrl + Shift + n).
Press next to continue.
Always try to learn the shortcuts commands. They will increase your efficiency.
2. Step 1 & 2: Select your microcontroller as Atmega32. Select clock source as 8 MHz.
This will be the clock source for the CPU instructions.
3. Step 3: Save your project with a name that shortly says the function of the code (e.g.
led_Blinking).
It is wise to create a folder with your section and group no (like section B Group 8)
inside some directory. Some of your project may be needed later. So, if you are
organized and have saved your work properly with proper name, it will help you in your
report writing and in completing future projects.
4. Step 4, 5 & 6: For this point, just press next to step 4 (i.e. you dont want to add any file
in your project). For step 5, select Include all and press next. Press finish (step 6).
From your experience with the C programming language, you know that the main function
is the principal function that will be executed by the Microcontroller once.
Write the following code in the code window:
2
3
We will always use a forever running while loop and keep the microcontroller
busy. If we do not use it, main function will run only once. Then the
microcontroller will get out of the main function and will not do anything (be
idle).
Note 2:
you can also use for (;;) or do-while (1) code block to make an infinite loop.
They are equivalent and different programmer like different styles.
3. PORTB = 0x01;
Up to now, you have set PORT B0 as output. That means you can set the output either 0 or 1
and use that port to drive a LED. If you set 1, the output port will get power (+5V). For
Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB)
10
setting 0, output port will have no power (0V). So, PORTB = 0b00000001 will make the
output LED to be on. To make the LED off, set PORTB = 0b00000000.
LED connected
at PB0
ON
OFF
PORTB
PB7
0
0
PB6
0
0
PB5
0
0
PB4
0
0
PB3
0
0
PB2
0
0
PB1
0
0
PB0
1
0
4. Delay_ms(1000)
This is a built-in function in MicroC that will give a delay of 1000ms (or 1s).
Why you have to use that delay? Consider the following two cases of delay:
Case I: If you give the delay, the code will run like this
LED: on Keep LED on for 1s LED off Keep LED off for 1s LED on ..
The change will be slow and your eye will be able to see the blinking (on-off of LED).
Case II: If you do not give any delay, the code will run like this
LED:
on off on off on off on off on off
This means the LED will turn on and off too fast (since there is no delay between on and off).
Our eyes cannot detect too fast change and we will see the LED is always on (although the
LED is actually blinking very fast).
You can now see the reason of using MicroC. It has built-in Delay_ms function that
automatically gives delay in milisecond; you do not have to write the function by yourself. If
you want to create a delay function by yourself, you can use for loop
unsigned int i;
For (i=0; i <= 65535; i++)
{
// empty for loop that does nothing, but will
// keep microcontroller busy
}
The problem of using for loop to create delay is that it is difficult to set delay time like 1s, 5s.
It depends on clock source, processor etc. Using Timer is a good alternative to create delay.
For now, use Delay_ms ( ) or Delay_us ( ) to create time delay in ms or s respectively.
5. // .
Anything after the double slash sign (//) will be ignored by the compiler. You will use this for
commenting throughout your code. A good coder always comments in his/her code in a
meaningful way.
Do not comment something obvious like:
PORTB = 0x01;
// Making PB0 = 1
Rather use comment so that it can help others in reading & understanding your code:
PORTB = 0x01;
// setting PB0 as output, driving a LED
11
PORTB = 0x01;
Delay_ms(1000);
PORTB = 0x00;
Delay_ms(1000);
}
}
JUMP TO WHILE
AGAIN
while (1) {
Algorithm
Main function to be run by Microcontroller
Wait a second
After you have entered the code in the compiler, go to menu: Project build (ctrl+F9). This
will create a hex file in the file location where you have saved your project. The Hex file has
to be loaded to microcontroller to see the effect of LED blinking code.
Simulating the Code in Proteus:
Open Proteus (Go to Start Menu, find Proteus and click ISIS 7 professional). You will see a
window like below. You can see in the left panel, there are many tools. The tools that we will
use mostly are shown in the image below. We will discuss only the tools we will need for this
LAB.
12
13
After pressing P, you will see a search box, enter text LED and search. You will find a
variety of LEDs. For our purpose, select LED-RED and press ok (see figure below).
Subcircuit mode: you can divide the circuit in different sections and represent each
section/block in a subcircuit. This makes the design simpler to observe and manage in
hierarchical way. We will not use subcircuit for such simple LAB, but for complex design,
e.g. if you are designing a 4 bit PC in Proteus, you have to use subcircuit to keep your design
neat and accessible.
Terminal Mode: When you want to give
POWER and GROUND to any pin of
device, select this mode. Suppose, you
want to give ground to your LED, look the
following image how it can be done. Just
click on the GROUND and click on the
design space on the right. You will see
your cursor is now a GROUND. Place the
ground where you want.
If cursor does not turn into pencil automatically, make sure you have selected
the component or terminal mode.
press
P
(in
keyboard)
Search
for
ATmega32
2.
Complete
connection: by moving
cursor near the pins.
Remember, you wrote
the code to blink a
LED using PB0 pin.
Give a ground at the
other end of LED
Figure-8: Connection of LED with microcontroller
14
You are almost done! Double click the microcontroller in your design a dialog box name
edit component will open. (See figure-9)
Select the folder icon in the program file and show the hex file you generated
earlier.
Select clock source (CKSEL FUSES): (0100) Int. RC 8 MHz.
15
If the oscilloscope does not show the display window, right click on the
oscilloscope and select Virtual Oscilloscope from the pop-up menu.
Press one shot and change voltage magnitude and time axis to see the pulse you are
generating by the code that actually makes the LED blink. The analog voltage level can also
be seen at voltage probe.
16
Simulation in Proteus is quite real time (i.e. if you face a problem in Proteus, you will also
see the same problem in Hardware). Also, you can check your code over and over again
before building your circuit. But when you give connection of your circuit and download
your Hex file to Microcontroller, you have to consider certain conditions. Lets upgrade our
simple project.
Upgrade 1: Use microcontroller as source or sink:
+5V
Microcontrolle
r as
Source
PB0
LE
D
Microcontroller
as
Sink
PB0
17
In real life, microcontroller is used only for controlling (As you can see, it has controller in
its name!). It is not good for driving components (i.e. to give currents). Taking too much
current will damage the microcontroller. So, always use to design your circuit where
microcontroller works as sink.
Careful:
look at the table above that when PB0 = 5V, the LED is off and when PB0 =
0V, the LED is on (Inverse logic: Our code says when PB0 is 5V, LED will be
on). Since our code is to blink the LED (on and off alternately), we do not
have to change the code here. But, you may want to change your comments so
that you can remember you are using Microcontroller as sink:
PORTB = 0x01;
18
19
Place your microcontroller in TOPWIN and use the software supplied with TOPWIN to write
the hex file. Be sure you have set the clock source as internal and same as you used in your
C code.
Simulation and Measurement:
1. Suppose a push switch is connected at PB0 and A LED is connected to PD0.
a. Write a complete C code in MicroC that will blink the LED when the switch is
pressed only.
b. Simulate the code in Proteus and ensure it works perfectly.
c. Implement the project in Breadboard
Equipment: ATmega16 or ATmega32 (1), wires, Resistors (220 ohm) (1), Topwin
Burner, Push button (1)
2. Implement a project named Knight Rider LED. In this project use total 8 LEDs in
PORTD. When the switched is pressed, the middle LED will turn on. Then the right LED
will only be turned on, till the end. Then the lighting LED will come to left side.
Use the 8 LED table below. Follow the numbering to note how the LEDs are turning on (Start
from 1, end at 16, then again start from 1)
13
14
15
16
2
3
4
1
12
11
10
9
7
6
5
8
N.B. only one LED will be on at a time.
Hint:
1. Search 7SEG-BCD in the Proteus library and complete the connection as shown
above.
2. Note that 7SEG-BCD is connected to PB3-PB0. So, first set these PINs as output.
3. Then in your infinite while loop, first show first digit of your ID (e.g. 1). Note that the
7SEG-BCD takes input as hex and give the output. To show 1, you have to set PB3PB0 = 0001. If you want to show 9, set PB3-PB0 = 1001 (binary of 9).
4. Do not forget to give delay between the digits.
After showing your ID, show the letters of your department character by character (EEE, CSE
etc.
Questions for report writing:
Bonus:
Add a switch at PB7 (search Proteus model Logic State It will give 0 or 1 to PB7 pin).
When PB7 is 1, the operation will be as before. If PB7 = 0, your ID and department will be
seen in reverse order (e.g. 1209-1359-2 CSE will be 2-9531-9021 ESC in reverse).
Submission:
During the project show at next LAB, bring
1. Proteus file containing circuits (.DSN) and MicroC project file with C code (To show
your effort to the faculty)
2. Printed copies of your code (.C) file and design file (Proteus file)
3. Your short Report explaining your code and design
20