Você está na página 1de 7

NEX Robotics

Fire Bird II Using AVR Studio

Using AVR Studio Before installing the AVR Studio4 make sure that you have installed WinAVR-20060421 on your PC; you can download WinAVR from the following website. http://winavr.sourceforge.net/download.html After installing WinAVR, now install AVR Studio4 and after installation run the software by browsing the following path from the Start Menu Start> All Programs > Atmel AVR Tools> AVR Studio 4 Now follow the steps below to start using the AVR Studio Step1: Creating a new project Click on the Project tab and select New Project

Step2: Specifying the Project Name and Path Select Project Type as AVR GCC and specify the project name and the path to a folder where you want all files, related to this project, to be stored. Click on Next when you are done.

www.nex-robotics.com

NEX Robotics

Fire Bird II Using AVR Studio

Step3: Selecting the Debug platform and the microcontroller Select the Debug platform as AVR Simulator and in the Device window select ATmega128

Step4: Configuring the Project Settings Click on the Project tab and select Configuration Options. Here check into the Create Hex File checkbox.

www.nex-robotics.com

NEX Robotics

Fire Bird II Using AVR Studio

Step5: Getting started with coding 5.1 Adding the header files #include <avr/io.h> #include <avr/interrupt.h> #include <avr/signal.h> #include <avr/iom128.h> 5.2 Defining the I/O port directions and using the optional Pull-up resisters To do this we need to write proper values into the following two registers. So lets take a small diversion to understand what they do. 1. Data direction register (DDRx) The purpose of the data direction register is to determine which bits of the port are used for input and which bits are used for output. If DDRx is written logic one, port pin is configured as an output pin. If DDRx is written logic zero, port pin is configured as an input pin. DDRA = 0xF0; //sets the 4 MSB bits of portA as output port and //4 LSB bits as input port 2. Port drive register (PORTx) If the port is configured as output port PORTx register drives the corresponding value on output pins of the port.

www.nex-robotics.com

NEX Robotics

Fire Bird II Using AVR Studio

DDRA = 0xFF; PORTA = 0xF0;

//set all 8 bits of PORTA as output //output logic high on 4 MSB pins and logic low on 4 LSB pins

For pins configured as input microcontroller supply the pull up register by writing a logic 1 to the corresponding bit of the port driver register DDRA = 0x00; //set all 8 bits of PORTA as input PORTA = 0xFF; //pull up register is assigned to all the pins; Having understood this, lets go ahead and write a subroutine which will initialize the I/O port directions. void port_init(void) { DDRA = 0xFF; PORTA = 0x00; DDRB = 0xF7; PORTB = 0x7F; DDRC = 0xFF; PORTC = 0x00; DDRD = 0x00; PORTD = 0xFF; DDRE = 0x7C; PORTE = 0xFF; DDRF = 0x00; PORTF = 0x00; DDRG = 0x00; PORTG = 0x1F; } 5.3 Writing the interrupt subroutine SIGNAL (SIG_INTERRUPTn) { ISR Code } //ISR Interrupt n;

Check out the iom128.h file for various interrupts vector addresses Step6: Compiling and Building the code Click on the Build tab and select the Build or Compile option or else select the following shortcuts on the toolbar Compile (Alt+ F7) Build (F7)

www.nex-robotics.com

NEX Robotics

Fire Bird II Using AVR Studio

Step7: Writing a simple code to turn on the buzzer

#include <avr/io.h> #include <avr/interrupt.h> #include <avr/signal.h> #include <avr/iom128.h> void port_init(void) { PORTA = 0x00; DDRA = 0xFF; PORTB = 0x7F; DDRB = 0xF7; PORTC = 0x00; //m103 output only DDRC = 0xFF; PORTD = 0xFF; DDRD = 0x00; PORTE = 0xFF; DDRE = 0x7C; PORTF = 0x00; DDRF = 0x00; PORTG = 0x1F; DDRG = 0x00; } void buzzer_on (void) { unsigned char portb_restore = 0; portb_restore = PORTB; // reading the PORTB original status portb_restore |= 0x80; // setting the MSB to turn on the buzzer PORTB = portb_restore; // executing the command } void buzzer_off (void) { unsigned char portb_restore = 0; portb_restore = PORTB; // reading the PORTB original status portb_restore &= 0x7F; // resetting the MSB to turn off the buzzer PORTB = portb_restore; // executing the command } void main(void)

www.nex-robotics.com

NEX Robotics

Fire Bird II Using AVR Studio

{ port_init(); buzzer_on(); } Step8: Programming the microcontroller Note: Before starting the programming make sure that the BIOS setting of your PCs parallel port is configured in ECP mode Open the avrdude-gui.exe from the folder C:\WinAVR\bin

If you are running this software for the first time make sure that you install the IO driver by clicking the Install tab in GiveIO Driver section. www.nex-robotics.com 6

NEX Robotics

Fire Bird II Using AVR Studio

Adjust the following settings in the avrdude gui DeviceAtmega128 Programmer- stk200 Portlpt1 Check into the Write and Verify checkboxes in the Flash column and specify the location of the hex file to be loaded. Click the Execute button in the gui to load the program on microcontroller and then check the Output window for successful download of the code.

www.nex-robotics.com

Você também pode gostar