Student Manual Lab 6

Embed Size (px)

Citation preview

  • 7/25/2019 Student Manual Lab 6

    1/20

    Experiment 6 Lab Manual

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 1

    American International University- BangladeshDepartment of Electrical and Electronic Engineering

    EEE 4219: Computer System Architecture Laboratory

    Title:Microcontroller Based System Design

    Introduction:

    In this section, we will learn

    1) Difference between a microprocessor and a microcontroller

    2) How to code a simple program in C for ATmega32 Microcontroller

    3) How to simulate the code in Proteus and observe the results

    Theory and Methodology:

    By microprocessor we mean the general-purpose microprocessors, for example, Intel's x86family (8086, 80286, 80386, 80486, and the Pentium) or Motorola's PowerPC family. These

    microprocessors contain no RAM, no ROM, and no input/output ports on the chip itself. Forthis reason, they are commonly referred to as general-purpose microprocessors. If you have

    to design a general-purpose microprocessor like the Pentium and make them functional, you

    must add RAM, ROM, I/O ports and timers externally and place in a board (mother board).

    Although the addition of external RAM, ROM, and I/O ports makes these systems bulkier

    and much more expensive, they have the advantage of versatility, enabling the designer to

    decide on the amount of RAM, ROM, and I/O ports needed to fit the task at hand. For

    example, you can upgrade your Laptop RAM from 2GB to 4GB easily.

    This is not the case with microcontrollers. A microcontroller has a CPU (a microprocessor) in

    addition to a fixed amount of RAM, ROM, I/O ports and timers all on a single chip. In

    other words, the processor, RAM, ROM, I/O ports, and timer are all embedded together on

    one chip; therefore, the designer cannot add any external memory, I/O port or timer to it. The

    fixed amount of on-chip ROM, RAM, and number of I/O ports in microcontrollers makes

    them ideal for many applications in which cost and space are critical. The difference of the

    microcontroller and microprocessor can best be demonstrated using the following figures

    DATA BUS

    RAM ROM TimerI/O

    Port

    CPU

    General

    Purpose

    Micro-

    Processo

    ADDRESS BUS

    Figure-1: Microprocessor (only the leftmost box). You have to connect other components like

    RAM, ROM etc. using data BUS and Address BUS

  • 7/25/2019 Student Manual Lab 6

    2/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 2

    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, thepower 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 signalsand 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 anynumber 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 taskat 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

    RAM

    ROMTimer

    I/O Port

    Serial

    COM

    Port

    CPU

    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.

  • 7/25/2019 Student Manual Lab 6

    3/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 3

    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 ROMon the chip.

    e) The number of I/O pinsand the timer on the chip.

    f) Ease of upgradeto 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 ADCetc. 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 code-

    efficient 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:

  • 7/25/2019 Student Manual Lab 6

    4/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 4

    About The Microcontroller:

    Atmega32 has total 40 pins(Look at the pin description of fig. 3). As you can see the pins are

    arranged in two sides of microcontroller. This type of package is called Dual-In-Line package

    (DIL-40).

    There are 4 ports in ATmega32: PORT A, B, C and D. Each port has 8 pins (0-7). For

    example, PORTB has 8 pins from PB0 to PB7. In ATmega32, PB0-PB7 is arranged from 1 to

    8 positions.

    You can use any port & their pins as output or input. In output mode, you can drive anything

    (e.g. LED) from the pins. In input mode, microcontroller can understand the input you give at

    the pins (e.g. using a button). So, you have to first set the pin either as input or output.

    To control I/O pin of microcontroller, there are three controlling register:

    DDRx (Data Directional Register for PORT x),PORTx (For port Output)

    PINx (For Port Input).

    Note: they are uppercase. If you are controlling PORT D, then x = D (i.e., DDRD, PORTD

    and PIND).

    Apparatus:

    1.

    Computer System for simulation

    2. Proteus Software

    3.

    Atmega-32 Microcontroller

    Figure-3: PIN configuration of 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.

  • 7/25/2019 Student Manual Lab 6

    5/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 5

    LE

    Button

    Figure-4: Microcontroller as input and

    output

    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 1stbit, make the 1stbit 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 highPORTA = 0x01;

    Or, PORTA = 0b00000001;

    To set PA0 as low

    PORTA = 0x00;

    Or, PORTA = 0b00000000;

    TO SET A PIN AS INPUT :

    A button is connected to pin 5 (PB4). When the button is pressed, either 0 or 1 is connected

    with this pin. To use this pin as input

    1. First set the pin 5 (which is PB4) to INPUT mode. To make any pin as input, set a 0

    at that bit position. Since PB4 is the 4 thbit, make the 4thbit of DDRB as 0. Use the

    following line:

    DDRB = 0x00; // in HexOr, DDRB = 0b00000000; // in Binary

  • 7/25/2019 Student Manual Lab 6

    6/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 6

    2. Once the PB4 is set as input, check whether the pin is high or low using ifclause

    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 non-

    zero {

    // 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 willconvert the code (Written in assembly or C) into a Hex file. Then we will download the Hexfile 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 C

    hex file generated by the assembler is

    muchsmaller

    Hex file generated by the compiler is

    larger

    1. Programming in Assembly language is

    often tedious.

    2.

    Programming in Assembly language

    takes a lot of time and patience.

    1. It is easier and less time consuming to

    write in C than in Assembly.

    2.

    C is easier to modify and update.

    3. You can use code available in function

    libraries.

    4.

    C code is portable to other

    microcontrollers with little or no

    modification

    Since, we have 32KBof 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 Studioare 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.

  • 7/25/2019 Student Manual Lab 6

    7/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 7

    Finally, before start coding, lets review about the data types of C language.

    No. Data Type Size in Bits Data Range

    1 unsigned char 8-bit 0 to 255

    2 char 8-bit -128 to + 127

    3 unsigned int 16-bit 0 to 65,535

    4 int 16-bit -32,768 to +32,7675 unsigned long 32-bit 0 to 4,294,967,295

    6 long 32-bit -2,147,483,648 to +2,147,483,648

    7 float 32-bit 1.175e-38 to 3.402e38

    8 double 32-bit 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 1stdata type (unsigned char, 8 bitrange 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

    bit8 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, saving1 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.

  • 7/25/2019 Student Manual Lab 6

    8/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 8

    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.

  • 7/25/2019 Student Manual Lab 6

    9/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 9

    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).

    You will see the following window:

    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:

  • 7/25/2019 Student Manual Lab 6

    10/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 10

    Linebyline Explanation of the code:

    1.

    DDRB = 0x01;

    You have added LED to port PB0. So, you will want to make that port as output port.

    To set PB0 as output, change data directional register for that port to 1 . Look at the tablebelow to see how it is done:

    DDRB (Data Direction Register for PORT B)

    PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0

    0 0 0 0 0 0 0 1

    You need to set PB0 as output. So, making PB0 = 1 (keeping PB7-PB1 = 0) will result in

    00000001 in binary or 01 in hex. This tells the compiler that set only pin B0 as output andset others as input

    Note that 0x indicates the number following 0x is in hexadecimal. You can use also binary

    notation like: DDRB = 0b00000001; (note 0bindicates binary)

    2. while (1){ }

    The purpose of the while codeblock is to engage the microcontroller in running the code

    repeatedly. If you do not use while, the microcontroller will run the code for one time, this

    means LED will blink for only one time. But we want to blink it again and again. Hence, wehave to use an infinite loop that will blink the LED repeatedly . Look the argument inside

    while is 1 (or true), this indicates that the condition of while is always true. So, the code

    inside the while will run forever.

    Note 1: We will always use a forever running while loop and keep the microcontrollerbusy. 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

    5

    43

    2

    1

  • 7/25/2019 Student Manual Lab 6

    11/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 11

    setting 0, output port will have no power (0V). So, PORTB = 0b0000000 1 will make the

    output LED to be on. To make the LED off, set PORTB = 0b00000000.

    LED connectedat PB0

    PORTB

    PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0

    ON 0 0 0 0 0 0 0 1OFF 0 0 0 0 0 0 0 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 forloop

    unsigned int i;

    For (i=0; i

  • 7/25/2019 Student Manual Lab 6

    12/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 12

    Lets summarize the algorithm and code:

    Code Algorithm

    void main() { Main function to be run by Microcontroller

    DDRB = 0x01;

    // Set direction to be output

    Set the PB0 pin as output (now, it can give

    either 0 or 1)

    while (1) { Run the code inside the while block

    infinitely. (So, inside the block we will write

    the code of blinking LED for one time,

    while will run that repeatedly)

    PORTB = 0x01; Turn the LED on that is connect to PB0

    Delay_ms(1000); Wait a second

    PORTB = 0x00; Turn the LED off

    Delay_ms(1000);

    }

    }

    Wait a second. Thus 1 blink is complete.

    Then go the beginning of while loop and

    start again.

    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 willuse mostly are shown in the image below. We will discuss only the tools we will need for this

    LAB.

    JUM

    PTOWHILE

    AGA

    IN

  • 7/25/2019 Student Manual Lab 6

    13/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 13

    Selection mode:To select the parts or components in your design space.

    Component mode:There are a lot of components in Proteus. If you want a component, say

    you want a LED in your simulation, then click component tool and select P at under the

    device entry (or shortcut: Select component, then press p from your keyboard). Note that,P means you are going topick a component from a list of components stored in Proteus

    library.

    Fi ure-6: Pickin a com onent from librar

    Figure-5: Familiarization to Proteus Interfaces and tools

  • 7/25/2019 Student Manual Lab 6

    14/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 14

    Figure-7: Terminal modeto pick a ground or

    Power supply

    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-REDand 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 inhierarchical 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, youwant to give ground to your LED, look the

    following image how it can be done. Justclick on the GROUND and click on the

    design space on the right. You will seeyour cursor is now a GROUND. Place the

    ground where you want.

    To connect the LED and the GROUND,place your cursor at the end of ground or at

    the end of LED terminal. Your cursor willturn in a pencil. Click (do not press and

    hold, just click once) and complete theconnection.

    NOTE: If cursor does not turn into pencil automatically, make sure you have selected

    the component or terminal mode.

    Now, complete a connection like the following

    Hint-1. Pick

    microcontroller:

    select component mode

    press P (in

    keyboard)

    Search for

    ATmega322. Complete

    connection: by moving

    cursor near the pins.

    Remember, you wrote

    the code to blink aLED using PB0 pin.

    Give a ground at theother end of LED

    Figure-8: Connection of LED withmicrocontroller

  • 7/25/2019 Student Manual Lab 6

    15/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 15

    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.

    Now, run the simulation by pressing the Play button at the bottom of the design window.

    ADD MEASURING INSTRUMENTS:

    Add a voltage probe (see figure below) and an oscilloscope (in the virtual instruments mode)

    to PB0 pin. Use channel A (or any other) of the oscilloscope.

    Fi ure-9: Connection Indication

    Fi ure-8: Editin com onent

  • 7/25/2019 Student Manual Lab 6

    16/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 16

    Inside the virtual oscilloscope (see image below), you will see three options: auto, one shot

    and cursor. Auto will always update the channel. One shot will take the voltage reading

    from the channel only for one time. Selecting cursor, you can measure different points in

    the display pulses.

    NOTE: 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.

    Fi ure-10: Addin Measurin Instruments

  • 7/25/2019 Student Manual Lab 6

    17/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 17

    PART III: IMPLEMENTING THE PROJECT IN THE HARDWARE

    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 downloadyour Hex file to Microcontroller, you have to consider certain conditions. Lets upgrade our

    simple project.

    Upgrade 1:Use microcontroller as source or sink:

    Microcontrolle

    r as

    Source

    PB0

    Microcontroller

    as

    Sink

    LE

    PB0

    +5V

    Figure-12: Microcontroller can supply current as driver or take current

    from source as sink

    Figure-11: Oscilloscope connection

  • 7/25/2019 Student Manual Lab 6

    18/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 18

    Microcontroller (fig. 12) When PB0 = high (+5V) When PB0 = low (0V)

    As Source (Left

    diagram)

    LED is on. Current will flow

    from microcontroller to the

    ground through LED. So,

    microcontroller is the source

    (supplying current)

    LED is off, because voltage

    difference of the LED is zero.

    As Sink (Rightdiagram)

    LED is off, because voltagedifference of the LED is(5V-5V) = 0.

    LED is on. Current will flowfrom power supply (+5V) to themicrocontroller pin (PB0)

    through LED. So,microcontroller is the sink

    (Taking current)

    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; // Turn OFFdiodes on PORTB

    Upgrade 2: Always Use current limiting resistors:

    Running the LED at its rated normal forward current will produce its rated light and assure a

    long prosperous life for it. Running a LED above its maximum ratings will destroy it.

    In this LAB, we are using 5V across the LED to light it up. This will not cause problem for

    now, but in the long run, it might damage the LED. See your text book, to learn how to

    calculate the current limiting value of resistors. For this LAB, use 220resistor in series withLED so that excess voltage is taken by the resistor and the current in that branch is limited by

    the resistance. Your modified schematic will be like this

  • 7/25/2019 Student Manual Lab 6

    19/20

    Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 19

    HARDWARE IMPLEMENTATION:

    In simulation, we did not give power to microcontroller. The simulator knows the power pin

    of microcontroller and use power internally. But, in actual hardware connection, you have to

    give power to ATmega32.

    Look at the pin diagram of microcontroller (fig. 3).

    Connect +5V to PIN 10.

    Connect GND to PIN 11. Connect +5V to RESET pin 9.

    pin is used to reset. If given 0, it will reset the code in microcontroller and start again

    from main function. We do not want to reset the controller. So, make this pin high (inactive).

    Good Reset circuitry is another feature of good project. Good reset circuit ensures that when

    reset is pressed, the processor will reset only one time (sometimes the reset pin senses spikes

    or bounces in input and reset several times). Think about your PC. If you press reset (Restart)

    and the PC restarts several time, that will be a faulty design. Hence, in real time project, use

    good reset circuit with capacitors so that it can prevent any spikes in reset pin. For this simple

    LAB, the reset will be simple: connect directly to HIGH.

    Connect your circuit with microcontroller. The microcontroller still does not know what to

    do. We know what to do: lets burn some microcontroller! By burning (means writing) the

    hex file generated from our code to its ROM, the microcontroller will know how to behave

    according to the code. This is similar to Proteus. We loaded the hex file to ATmega32, now

    we will load it to hardware.

    To download the Hex file, we will use a device named TOPWIN, which is a universal burner

    (means it can handle microcontroller from different family) . But here also a lot of varietyexists. You can use different burning software like Extreme Burner, software with Micro C or

    AVR studio. The downloading hardware can also vary. TOPWIN is just one of the many

    universal programmers. You can even build a programmer/burner/MC writer of your own.Several such circuits exist over Internet.

    Figure-13: Use of current limiting resistor in series with LED

  • 7/25/2019 Student Manual Lab 6

    20/20

    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

    12

    14

    11

    15

    10

    16

    9

    1

    8

    2

    7

    3

    6

    4

    5

    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 PB3-

    PB0 = 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 beseen 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