====== AVR Studio for ATMega128 ====== ===== Creating new project ===== In order to write a program for the controller you need to create the project space. The project includes typically different files like source codes, header files, compiled program files, etc. It is strongly advised to create new folder for every project (which is offered also by the New Project Wizard). Following steps have to be completed when creating new project with the help of wizard. **1.** Open AVR Studio and press //New Project//. If the dialog box is not opened automatically select //Project - New project// from the menu bar. Press //Next//. {{ :examples:install:windows:studio_welcome.png?400 |}} **2.** Next dialog box is about compiler and initial settings. Select AVR GCC compiler. On the left, insert the name of the project and main source file name. The source file name should have extension ".c". Two check boxes should be also checked, which will create the new folder and initial file. You should also show the folder where project files will be created. After proper selections press //Next//. NB! If AVR GCC is missing on the compiler list, it is not properly installed. In that case the WinAVR software have to be installed before starting to write C source code. {{ :examples:install:windows:studio_new_project.png?400 |}} **3.** On the next dialog box you have to select the debugger platform and microcontroller. HomeLab kit is using JTAG ICE as debugger platform and ATmega128 as microcontroller. After selections press //Finish//. {{ :examples:install:windows:studio_project_programmer.png?400 |}} **4.** Now the project space is created and new window will open where you can start to write the program source code. {{ :examples:install:windows:studio_overview.png?500 |}} **5.** You need to set some project parameters before the first compilation. The important parameters are controller frequency and optimization method. HomeLab controller has frequency 14,7456 MHz (14745600 Hz). The frequency can be set in the project properties with Hz (not MHz): //Project -> Configuration Options -> General//. Optimization method should left -Os, if there is no need for other methods. {{ :examples:install:windows:studio_project_properties_general.png?400 |}} **6.** For using HomeLab library functions the software have to be properly installed. Every new project requires adding library to list of linked objects. To do that go to //Project -> Configuration Options -> Libraries// and add object "libhomelab.a". {{ :examples:install:windows:studio_project_properties_libraries.png?400 |}} If object //libhomelab.a// is missing from the left list the library is not properly installed to the system and it should be reinstalled. ===== Setting's test ===== After set up of the development environment it is wise to test it, for ensuring its correctness. Simplest way is to write a short program, compile it and upload to controller. **1.** Connect the programmer with ATmega128 board. Be sure that the programmer is correctly connected to JTAG connector (cable is directed away from controller board- see next picture). Connect the controller board supply (small green LED should light up if correct power supply is connected). Insert simple C source code: #include #include int main(void) { // Pin PB7 to output DDRB = 0x80; // Endless cycle while (true) { // Pin PB7 invertion PORTB ^= 0x80; hw_delay_ms(500); } } [{{ :examples:install:windows:studio_quick_buttons.png?267 |Compilation and programmer buttons}}] Compile the project with //Build// command (keyboard F7). Make sure that the compilation succeeded. For this you should see the following message on the message window. Build succeeded with 0 Warnings... **2.** Open the controller window //Tools -> Program AVR -> Auto Connect//. Be sure that the tab //Program// is open. {{ :examples:install:windows:studio_programmer.png?400 |}} If the described window does not open and //Connection Failed// window is opened instead you do not have proper connection with the board or programmer. First check that micrcontroller is correctly powered (green LED is on) and the programmer is properly connected to JTAG connector. If this is OK check the COM port number which is assigned by the Windows. If this is greater than 9, the AVR Studio can not recognize the programmer. Follow the instructions given in the beginning of the chapter and assign the port number between 0 and 4. {{ :examples:install:windows:studio_project_programmer_port.png?400 |}} **3.** On the programmer window insert into //Flash//-section textbox //Input HEX File// the location of the compiled program by pressing the "..." button. Compiled program is usually located in the project folders sub folder //default// and has same name as the project but with the extension ".hex", for example "labor1.hex". After selecting correct file press button //Program// which uploads the program to the controller. If all went well you should see the following message on the end of the window: OK Reading FLASH input file.. OK Setting device parameters for jtag programming ..OK Entering programming mode.. OK Erasing device.. OK Programming FLASH .. OK Reading FLASH .. OK FLASH contents is equal to file.. OK Leaving programming mode.. OK According to the program the on-board LED (PB7) should start flashing. If the program works you have successfully set up your programming environment and completed your first program. Congratulations! {{:kit:kit_test.jpg?500|}} ===== Debugger ===== [{{ :examples:install:windows:studio_debugger_io_view.png?280|The list of registers in the debugger of the ATmega128.}}] Debugging a program means searching errors from the program. For that programs called debuggers are created, they allow to execute the program step by step and stopping it where it is needed. Such implementation of the program allows checking the values of the variables at any phase of the program, contents of the registers and the sequence of executing the program. Debugging is especially important while dealing with more complex programs where it is often difficult to find errors. With microcontrollers, it is important that step-by-step implementation of program is done in the controller, which allows seeing change of real outputs in addition to the values of the registers. Two conditions must be met for using a debugger: microcontroller must support debugging and you must have necessary hardware – JTAG programmer which allows debugging. Cheaper programmers using ISP programming interface may upload compiled program into the controller but not necessarily allow debugging. To start the program in debugging mode with the AVR Studio, firstly it should be compiled by pressing button //build// (F7) and the compiled program started with the command //Run// (F5). Before that //break points// (F9) can be added to selected palces in the source code. When implementation of the program reaches the break point, the program is stopped for determining the state of the microcontroller in that point. Implementation of the program may be continued with command //Run// again or use //Step Into// (F11) for implementing the program one command at the time. ===== The usage of floating-point variables ===== Some times in AVR program it is necessary to use floating-point variables. For calculating with them and presenting with //printf//-type functions, the following set-up changes must be done to the configuration of the project: **1.** Open the set-up of the project from the menu //Project -> Configuration Options//. Add //libprintf_flt.a// and //libm.a// after //libhomelab.a// of the library of HomeLab in the configuration tab //Libraries//. **2.** Next, open tab //Custom Options// and chose //[All files]//. Next add lines with "-lprintf_flt" ja "-lm" to the box on the right and line with "-uvfprintf" to the //[Linker Options]// section. **3.** Press OK and close configuration window.