AVR-BOTskol Tutorial

download AVR-BOTskol Tutorial

of 26

Transcript of AVR-BOTskol Tutorial

  • 8/8/2019 AVR-BOTskol Tutorial

    1/26

    Input Output Operations using AVR

    StudioSubmitted by bOtskOOl on Sun, 06/21/2009 - 04:25

    This is the very first tutorial of the AVR (Atmega-16/32) tutorial series. Note that in thisand subsequent tutorials most of the examples will be based on Atmega32, though they

    can be implemented on Atmega16 too without any major changes. If and when some

    changes are required to be made it will be mentioned. So lets begin, first of all we willbe discussing AVR studio which is an editor, assembler, compiler, simulator and much

    more. In this tutorial we will discuss the coding part in assembly language. Many people

    run away from assembly language programming but when you start learning

    microcontrollers properly and thoroughly and write your own programs the assemblygives much easier ability to think, anaylse and apply the logic to make your programs and

    you can directly take advantage of microcontrollers architecture while writing your

    program. If you want you can shift to C very easily after that, it is just a step away afteryou have learnt assembly. We shall be launching AVR C tutorial series in a few days.

    Atmega-16/32 Pin Description

  • 8/8/2019 AVR-BOTskol Tutorial

    2/26

  • 8/8/2019 AVR-BOTskol Tutorial

    3/26

    Physical Ports of Atmega-16/32

    The Atmega-16/32 has four physical ports on the chip as marked in the figure above.These ports are named as PortA, PortB, PortC and PortD. Each port is basically a

    collection of 8 pins grouped together.

  • 8/8/2019 AVR-BOTskol Tutorial

    4/26

    Basic Circuit of AVR

    The pins marked Gnd are to be grounded, Vcc & AVcc are to be given 5V. The resetpin is also high but we usually prefer to put a switch at this point for the reset of the

    chips. If the switch is pressed for a minimum pulse of time then it will reset the chip.

    Note: AVcc should be connected to 5V supply through a capacitor when using PORTA

    pins as ADC, though in simple applications capacitor is not necessary.

    This circuit is used whenever we wish to do anything with the controller. In addition tothis you will have to add other hardware required depending upon what do you want to

    accomplish.

  • 8/8/2019 AVR-BOTskol Tutorial

    5/26

    Now for the given tutorial we will be using the PORTB for connections. We will connect

    8Leds at each pin of PORTB.

    The above picture depicts how each LED will be connected to corresponding pin of

    portB of Atmega32. So lets see the complete circuit

  • 8/8/2019 AVR-BOTskol Tutorial

    6/26

    Complete Circuit Diagram

    Photograph of switch

    The switch is required so that we are able to reset i.e. restart the microcontroller at any

    time if required.

    People who have worked on some other microcontroller must be familiar with word

    Registers.

    In short, registers are used to store information temporarily. That information can be of

    one byte (or two) of data to be processed or an address pointing to data to be fetched.

    The AVR has 32 general purpose registers and many other registers also. There are threeregisters associated with each port that are required for the Input-Output operation for

    AVR. These registers are DDRx register, PORTx register, PINx register where x can be

    (A, B, C, D). These register are very important and are used for reading and writing to theports.

    Note: The physical ports and the register PORTx has same name but the user must notconfuse between them, from now on while discussing register PORTx it will be referred

    as PORTx Register to avoid confusion.

    These registers will be discussed later on in detail but before that lets write our first

    program and try to understand it.

  • 8/8/2019 AVR-BOTskol Tutorial

    7/26

    Download the AVR Studio from Atmel and install it. We will be showing you how to

    start working directly with the software. It is an environment where can write your

    programs, build corresponding hex codes, do debugging and much more!

    You will be also needing the datasheet ofAtmega16orAtmega32.

    Using the datasheet

    If you are beginner do not go through the datasheet now as you will find it quite difficult

    to understand. Luckily you are not supposed to do that, either. The datasheets are

    complete technical documents that you should use as a reference when you are in doubthow a given peripheral or feature works.

    We will be covering the necessary theories and relevant concepts as we move on and in

    no time you will have confidence to write your own programs.

    After you have downloded and installed AVR Studio, just open the it and follow thesteps

    STARTING A NEW PROJECT IN AVR STUDIO

    STEP-1

    When you will launch the AVR Studio a splash screen will appear as shown below. In

    case it doesnt choose New Project option from the Project menu.

    http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725http://www.atmel.com/dyn/resources/prod_documents/doc2466.pdfhttp://www.atmel.com/dyn/resources/prod_documents/doc2466.pdfhttp://www.atmel.com/dyn/resources/prod_documents/doc2503.pdfhttp://www.atmel.com/dyn/resources/prod_documents/doc2503.pdfhttp://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725http://www.atmel.com/dyn/resources/prod_documents/doc2466.pdfhttp://www.atmel.com/dyn/resources/prod_documents/doc2503.pdf
  • 8/8/2019 AVR-BOTskol Tutorial

    8/26

    STEP-2

    Choose New Project as shown above.

    STEP-3

    Choose Atmel AVR Assembler option in Project type:

    Write the Project name (It doesnt accept a space in name). Choose a suitable Location & hit NEXT>>.

  • 8/8/2019 AVR-BOTskol Tutorial

    9/26

    STEP-4

    Choose the debug platform in this case AVR Simulator.

    Choose the device (Here we have chosen Atmega32 for our project) & click on

    FINISH.

    Now we can start our first project and understand the basics of I/O (Input/Output)

    operations.

    We will learn how the value of the I/O registers changes. AVR Studio has a fantastic tool

    for showing step by step execution. Lets get started!

  • 8/8/2019 AVR-BOTskol Tutorial

    10/26

    As shown in the figure above

    Region 1 Write your program here

    Region 2 Menu bars (obviously you know this )

    Region 3 You know what it is yes toolbar section

    Region 4 I/O View

    Region 5 Project directory and distribution

    Region 6 Name of the microcontroller which is currently in use

    When you will write your code you will notice that the instructions, comments, etc will

    be written in different colors. This is a very important feature of an editor. Thisremarkably helps in distinguishing between types of syntaxes and lines.

  • 8/8/2019 AVR-BOTskol Tutorial

    11/26

    Below is a screenshot of our very first AVR code.

    Lets understand the program before going further:

  • 8/8/2019 AVR-BOTskol Tutorial

    12/26

  • 8/8/2019 AVR-BOTskol Tutorial

    13/26

  • 8/8/2019 AVR-BOTskol Tutorial

    14/26

  • 8/8/2019 AVR-BOTskol Tutorial

    15/26

    Status of pin when configured as an output pin:-

    PORTxn= 0, the output is driven high.

    PORTxn= 1, the portx nth pin is driven low (zero).

    PINx Register:-

    To put it bluntly, the PINx register contains the status of all the pins in that port.

    If the pin is an input pin, then its corresponding bit will mimic the logic level

    given to it.

    If it is an output pin, its bit will contain the data that has been previously outputon that pin. (The value of an output pin is latched to PINxn bit, you can observe it

    when we do step by step execution in AVR-Studio shown below)

    Click on Build and Run option present in Build menu bar.

    We can observe the values of registers, Program counter, Cycle Counter, Port registers

    and much more after execution of each instruction!

    We can do step by step execution in AVR Studio by pressing F11 after code has been

    successfully built and is running Lets do it.

  • 8/8/2019 AVR-BOTskol Tutorial

    16/26

    Region 5 - This confirms that are program has been successfully completed,Congratulation!!

    Region 1 - This mark indicates the instruction to be executed in RAM.

    Region 2 - This indicates location of Flash memory (ROM), the program are

    burned in EEPROM and executed in RAM. (The beginners may find this concept

    little confusing but dont worry for now just move on, we shall discuss it somelater on).

    Region 3 - You can look at the current values of register by pressing the (+).

    Region 4 - You can also get the values of the register associated with the

    PORTB.

    Now press F11 once, the arrow jumps to the next position as shown..

  • 8/8/2019 AVR-BOTskol Tutorial

    17/26

    Note the value of R16 by scrolling down, you will find it zero.

    Clock Cycle is shown in blue colour on left hand side. This indicates the number

    of clock cycles after the program started executing. An instruction can take one ortwo clock cycles.

    On Pressing F11 again you will observe this

    And Pressing F11 again...

  • 8/8/2019 AVR-BOTskol Tutorial

    18/26

    The PORTB is set as an output (the register DDRB is set)

    Pressing F11 again we can see that

  • 8/8/2019 AVR-BOTskol Tutorial

    19/26

    The PORTB register is loaded with 255, this output to the pins of PORTB (1111

    1111b) and all the LEDs start glowing

    If we Press F11 again we will observe this

  • 8/8/2019 AVR-BOTskol Tutorial

    20/26

    The PINx register is latched with the value which was the output before this. This

    proves the fact that the output values from pins are latched to register bits PINxnafter one cycles

    Keep on playing with this by pressing F11 again and again & observe things till you

    understand everything properly and are completely satisfied.

    Burning the ROM of microcontroller

    To get the hex file (that you will need to burn to your microcontroller) just open the

    folder where you have saved your project.

    In this case it is - D:\testprog\1

  • 8/8/2019 AVR-BOTskol Tutorial

    21/26

    You can buy a board to burn the AVR or build your own ISP, this topic will be covered

    in next tutorial and by that time you can check out this tutorial - Parallel Port Programmerfor ATMEGA 16/32 - by one of our users - heloitsadi.

    http://www.botskool.com/user-pages/tutorials/electronics/parallel-port-programmer-atmega-1632http://www.botskool.com/user-pages/tutorials/electronics/parallel-port-programmer-atmega-1632http://www.botskool.com/user-pages/tutorials/electronics/parallel-port-programmer-atmega-1632http://www.botskool.com/user-pages/tutorials/electronics/parallel-port-programmer-atmega-1632
  • 8/8/2019 AVR-BOTskol Tutorial

    22/26

    Parallel Port Programmer for ATMEGA

    16/32

    Submitted by heloitsadi on Sun, 06/07/2009 - 07:18

    In-system programmer means we can design a programmer circuit using simple parallelport interfacing such that our controller can be directly burned with the program while in

    the designed system or circuit board.

    Following pins of Atmega16 are used in programmer circuit-

    SCK Port B, Pin 7

    SCK: Master Clock output, Slave Clock input pin for SPI channel. When the SPI isenabled as a Slave, this pin is configured as an input regardless of the setting of

    DDB7.When the SPI is enabled as a Master; the data direction of this pin is controlled by

    DDB7. When the pin is forced by the SPI to be an input, the pull-up can still becontrolled by the PORTB7 bit.

    MISO Port B, Pin 6

    MISO: Master Data input, Slave Data output pin for SPI channel. When the SPI is

    enabled as a Master, this pin is configured as an input regardless of the setting of DDB6.When the SPI is enabled as a Slave, the data direction of this pin is controlled by DDB6.When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the

    PORTB6 bit.

    MOSI Port B, Pin 5

  • 8/8/2019 AVR-BOTskol Tutorial

    23/26

    MOSI: SPI Master Data output, Slave Data input for SPI channel. When the SPI is

    enabled as a Slave, this pin is configured as an input regardless of the setting of DDB5.

    When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB5.When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the

    PORTB5 bit.

    RESET-Pin 9

    Reset Input. A low level on this pin for longer than the minimum pulse length willgenerate a reset, even if the clock is not running.

    VCC -Pin 10

    Digital supply voltage(+5V).

    GND-Pin-11

    Ground.

    Parallel Port-

    Parallel Port interfacing is the simplest method of interfacing. Parallel Ports are

    standardized under the IEEE 1284 standard first released in 1994. It has data transferspeed up to 1Mbytes/sec. Parallel port is basically the 25 pin Female connector (DB-25)

    in the back side of the computer (Printer Port). It has 17 input lines for input port and 12

    pins for output port. Out of the 25 pins most pins are Ground and there is data register (8bit), control register (4 bit) and status register (5 bit).

  • 8/8/2019 AVR-BOTskol Tutorial

    24/26

    Figure-Pin diagram of Parallel Port

    Following Pins are used in parallel port-

    Interfacing -

    In programmer circuit pins of parallel port which are above described has to interface

    with pins of ATmega16 microcontroller which are responsible for in-systemprogramming. The parallel port can be interfaced directly with microcontroller. To avoid

    reverse current we can use Schottkey diodes as safety precaution for pc motherboard.

    Following pins of Parallel Port and ATmega16 are to be interfaced-

  • 8/8/2019 AVR-BOTskol Tutorial

    25/26

    Figure-Circuit Diagram of ATmega16 Programmer

    SOFTWARES USED:

    WinAVR WinAVR is open source package in which we use two sub-programs.

    o Programmers Notepad

    o Mfile.

    Version- 2.0.8.718-basic

    Creator- Simon Steele

    Purpose-

  • 8/8/2019 AVR-BOTskol Tutorial

    26/26

    o To write code.

    o To compile coding.

    o To generate Hex Code.

    o To burn Hex code.

    The next tutorial will make you aware with how to install and use WinAVR forcompiling and burning the program.