| Both sides previous revisionPrevious revision | |
| en:examples:setup:avrstudio:atmega2560 [2012/05/29 14:52] – raivo.sell | en:examples:setup:avrstudio:atmega2560 [2026/02/19 11:30] (current) – external edit 127.0.0.1 |
|---|
| ====== AVR Studio for Windows ATmega2561 ====== | ====== AVR Studio ATmega2561 ====== |
| |
| <note important>Needs translation</note> | |
| |
| ===== Projekti loomine ===== | ===== Creating a project ===== |
| |
| Kui arendustarkvara on paigaldatud saab alustada programmi kirjutamist. | After the development software is installed you can start writing code. |
| AVR kontrolleri programmi kirjutamiseks tuleb luua uus projekt, mis tüüpiliselt sisaldab endas palju erinevaid faile: programmikoodi(e), päisfaile, kompileeritud programmi jne. Selleks et projektid oleks korrektselt eristatavad, tuleks iga projekti jaoks luua uus kataloog (seda võimalust pakub uue projekti loomise abimees). | To write programs for the AVR controller you must create a new project, which typically contains many different files: source code, header files, compiled program, etc. To keep projects separate, it is best to create a new folder for each project (the new project wizard offers this option). |
| |
| Uue projekti loomiseks tuleb läbida järgmised sammud: | To create a new project, follow these steps: |
| |
| **1.** Avada AVR Studio ja vajutada nupule uus projekt (//New Project//). Juhul kui vastav aken automaatselt ei avane, valida menüüst //Project - New project//. Pärast sobivat sisestust vajutada nupule //Next//. | **1.** Open AVR Studio and press the New Project button. If the window does not open automatically, choose //Project - New project// from the menu. After entering the required information press //Next//. |
| |
| {{ :software:avrstudio:avrstudio_newproject0.png?400 |}} | {{ :software:avrstudio:avrstudio_newproject0.png?400 |}} |
| |
| **2.** Avaneb aken, kus tuleb märkida kompilaatori ja failide algseaded. Kompilaatoriks valida AVR GCC ja paremas akna osas sisestada projekti nimi ning algkoodi faili nimi. Algkoodi faili nimi peaks kindlasti lõppema laiendiga ".c". Aknas on võimalik veel märkida valikud automaatseks projekti nimega kataloogi ja algkoodi faili tekitamiseks. Mõlemad valikud on mõistlik ära märkida. Samuti tuleks näidata kataloog, kuhu vastav projektikataloog tekitatakse. Pärast valikute tegemist vajutada nupule //Next//. | **2.** A window opens where you set compiler and file defaults. Choose AVR GCC as the compiler and enter the project name and the main source file name on the right side. The source file name should end with the ".c" extension. You can also select options to automatically create a folder with the project name and the main source file. Both options are recommended. Also specify the directory where the project folder should be created. After selecting options, press //Next//. |
| |
| NB! Kui kompilaatori valikus puudub AVR GCC, siis ei ole WinAVR korrektselt paigaldatud ja seda tuleks enne C-keele programmi kirjutamist kindlasti teha. | NB! If AVR GCC is missing from the compiler list, WinAVR is not installed correctly and must be installed before writing C programs. |
| |
| {{ :software:avrstudio:avrstudio_newproject1.png?400 |}} | {{ :software:avrstudio:avrstudio_newproject1.png?400 |}} |
| |
| **3.** Järgnevalt avaneb aken, kus tuleb märkida kasutatav silumise platvorm ja mikrokontrolleri tüüp. Kodulaboris (v5) on silumise platvormina kasutusel //JTAGICE mkII// programmaator ja mikrokontrolleriks on //ATmega2561//. Projekti lõplikuks loomiseks vajutada nupule //Finish//. | **3.** Next, a window opens where you select the debug platform and microcontroller type. In HomeLab (v5) the debug platform is //JTAGICE mkII// and the microcontroller is //ATmega2561//. To finish creating the project, press //Finish//. |
| |
| {{ :software:avrstudio:avrstudio_newproject2.png?400 |}} | {{ :software:avrstudio:avrstudio_newproject2.png?400 |}} |
| |
| **4.** Nüüd avaneb juba programmeerimise kasutajaliides, kuhu saab hakata kirjutama programmi lähtekoodi. | **4.** Now the programming interface opens and you can start writing the program source code. |
| |
| {{ :software:avrstudio:avrstudio_newproject3.png?500 |}} | {{ :software:avrstudio:avrstudio_newproject3.png?500 |}} |
| |
| ~~PB~~ | <pagebreak> |
| |
| **5.** Enne koodi kompileerimist tuleb määrata projekti seaded. Olulisemad seaded on kontrolleri taktsagedus ja kompilaatori optimeerimismeetod. Kodulabori kontrolleri taktsagedus on 14,7456 MHz ehk 14745600 Hz. See sagedus tuleb hertsides määrata //Project -> Configuration Options -> General// aknas. Optimeerimise meetodiks jätta -Os, kui ei ole konkreetset vajadust teiste meetodite järgi. | **5.** Before compiling the code, set the project options. The most important settings are the controller clock frequency and the compiler optimization method. The HomeLab controller clock frequency is 14.7456 MHz, i.e., 14745600 Hz. Enter this frequency in hertz in //Project -> Configuration Options -> General//. Use -Os for optimization unless you have a specific reason to use another method. |
| |
| {{ :software:avrstudio:avrstudio_newproject4.png?400 |}} | {{ :software:avrstudio:avrstudio_newproject4.png?400 |}} |
| |
| **6.** Kodulabori teegi kasutamine AVR Studio-ga eeldab, et see on tarkvara paigaldamise juhendi kohaselt süsteemi paigaldatud. Iga projekti korral tuleb teek projekti seadetest //Project -> Configuration Options -> Libraries// lisada lingitavate objektide nimekirja. | **6.** Using the HomeLab library with AVR Studio requires that it is installed on the system according to the software installation guide. For each project, add the library to the list of objects to link in //Project -> Configuration Options -> Libraries//. |
| |
| {{ :software:avrstudio:avrstudio_newproject5.png?400 |}} | {{ :software:avrstudio:avrstudio_newproject5.png?400 |}} |
| |
| Kui objekt //libhomelab2561.a// nimekirjast puudub, siis ei ole Kodulabori teek korrektselt süsteemi paigaldatud. | If the object //libhomelab2561.a// is missing from the list, then the HomeLab library has not been installed correctly. |
| |
| ~~PB~~ | <pagebreak> |
| |
| ===== Seadistuste testimine ===== | ===== Testing the configuration ===== |
| |
| Kui arenduskeskkond on esimest korda paigaldatud ja seadistatud, on mõistlik testida, kas kõik sai tehtud õigesti. Selleks on lihtsaim viis teha üks väike programm, see ära kompileerida ja laadida kontrollerisse. | If the development environment is installed and configured for the first time, it is good to test that everything was done correctly. The easiest way is to write a small program, compile it, and upload it to the controller. |
| |
| **1.** Ühendada kontrollerplaat USB kaabliga arvutiga. Peale kontrollerplaadi ühendamist peaks süttima väike roheline toite LED - PWR. | **1.** Connect the controller board to the computer with a USB cable. After connecting the board, a small green power LED (PWR) should light up. |
| |
| **2.** Käivita liidesprogramm HappyJTAG2 ja seejärel AVR Studio. Oluline on sammude järjekord. Esmalt tuleb ühendada kontroller arvutiga ja veenduda, et kontroller lülitub sisse. Seejärel käivitada programm HappyJTAG2 ja AVR Studio. | **2.** Start the HappyJTAG2 interface program and then AVR Studio. The order is important. First connect the controller to the computer and verify it powers on. Then start HappyJTAG2 and AVR Studio. |
| |
| **3.** AVR Studios luua vajadusel projekt ja sisestada lihtne C-kood: | **3.** Create a project in AVR Studio if needed and enter simple C code: |
| |
| Testida võib kahte erinevat koodi. Esimene neist kasutab Kodulabori teeki, teine teeb sama asja ilma teegita. | You can test two different codes. The first uses the HomeLab library, the second does the same without the library. |
| |
| <code c> | <code c> |
| //Lihtne testprogramm, mis kasutab Kodulabori teeki. | // Simple test program using the HomeLab library. |
| #include <avr/io.h> | #include <avr/io.h> |
| #include <homelab/delay.h> | #include <homelab/delay.h> |
| int main(void) | int main(void) |
| { | { |
| // Viigu PB7 seadmine väljundiks | // Set PB7 as output |
| DDRB = 0x80; | DDRB = 0x80; |
| |
| // Lõputu tsükkel | // Infinite loop |
| while (true) | while (true) |
| { | { |
| // Viigu PB7 inverteerimine | // Toggle PB7 |
| PORTB ^= 0x80; | PORTB ^= 0x80; |
| hw_delay_ms(500); | hw_delay_ms(500); |
| |
| <code c> | <code c> |
| //Lihtne testprogramm, mis ei kasuta Kodulabori teeki. | // Simple test program that does not use the HomeLab library. |
| #include <avr/io.h> | #include <avr/io.h> |
| | |
| { | { |
| unsigned int x,y; | unsigned int x,y; |
| // Viigu PB7 seadmine väljundiks | // Set PB7 as output |
| DDRB = 0x80; | DDRB = 0x80; |
| | |
| // Lõputu tsükkel | // Infinite loop |
| while (1) | while (1) |
| { | { |
| // Viigu PB7 inverteerimine | // Toggle PB7 |
| PORTB ^= 0x80; | PORTB ^= 0x80; |
| y=3200; | y=3200; |
| |
| |
| [{{ :examples:install:windows:studio_quick_buttons.png?267 |Kompileerimise ja programmaatori nupud}}] | [{{ :examples:install:windows:studio_quick_buttons.png?267 |Compile and programmer buttons}}] |
| |
| Kompileerida programm käsuga //Build// (kiirklahv F7) ja kontrollida, et kompileerimine õnnestus. Selleks peab teadete aknasse tekkinud kompilaatori väljundi lõpp olema järgmine: | Compile the program using //Build// (shortcut F7) and verify the compile succeeded. The end of the compiler output in the message window should be: |
| |
| Build succeeded with 0 Warnings... | Build succeeded with 0 Warnings... |
| |
| ~~PB~~ | <pagebreak> |
| |
| **4.** Avada koodi laadimise aken nupust //Tools -> Program AVR -> Auto Connect//. Tulemuseks peaks avanema kompileeritud faili mikrokontrollerisse laadimise aken. Kontrollige, et avatud oleks kaart //Program//. | **4.** Open the programming window using //Tools -> Program AVR -> Auto Connect//. This opens the window for uploading the compiled file to the microcontroller. Make sure the //Program// tab is open. |
| |
| {{ :software:avrstudio:avrstudio_prog.png?400 |}} | {{ :software:avrstudio:avrstudio_prog.png?400 |}} |
| |
| |
| Kui eespool toodud aken ei avane ja avaneb aken //Connection Failed//, siis puudub ühendus programmaatoriga. Esmalt tuleks kontrollida, kas mikrokontrolleril on järel toide. Kui see on korras, siis tuleks kontrollida HappyJTAG2 programmi oleku indikaatoreid. FT2232 ja AVRStudio peaks olema rohelised. | If the above window does not open and you see //Connection Failed//, then there is no connection to the programmer. First check that the microcontroller has power. If it does, check the HappyJTAG2 status indicators. FT2232 and AVRStudio should be green. |
| |
| {{ :software:jtag:happyjtag2.png?400 |}} | {{ :software:jtag:happyjtag2.png?400 |}} |
| |
| **5.** Programmaatori aknas sisestada //Flash//-sektsioonis tekstikasti //Input HEX File// kompileeritud fail. Seda saab teha "..." nuppu vajutades. Kompileeritud fail asub tavaliselt projekti alamkataloogis //default// ja see on sama nimega, mis projekt, kuid laiendiga ".hex", näiteks "labor1.hex". Pärast õige faili valimist vajutada nupule //Program//, mis laeb valitud programmi kontrollerisse. Kui kõik õnnestus, tekib programeerimiskeskkonna allosasse teade: | **5.** In the programmer window, enter the compiled file in the //Input HEX File// field in the //Flash// section. You can use the "..." button. The compiled file is usually in the project subfolder //default// and has the same name as the project, but with the ".hex" extension, for example "labor1.hex". Then press //Program// to upload the selected program to the controller. If everything succeeds, a message appears in the lower part of the programming interface: |
| |
| OK | OK |
| Leaving programming mode.. OK | Leaving programming mode.. OK |
| | |
| Programmi mõjul peaks kontrolleri plaadil olev oleku-LED PB7 perioodiliselt süttima ja kustuma. Kui programm töötab, on tarkvara paigaldatud edukalt ja esimene projekt tehtud. Palju õnne! | The program should cause the PB7 status LED on the controller board to blink periodically. If it works, the software installation is successful and your first project is done. Congratulations! |
| |
| {{:kit:kit_2561_test.jpg?400|}} | {{:kit:kit_2561_test.jpg?400|}} |
| |
| ===== Siluri kasutamine ===== | ===== Using the debugger ===== |
| |
| [{{ :examples:install:windows:studio_debugger_io_view.png?280|ATmega registrite loetelu siluris}}] | [{{ :examples:install:windows:studio_debugger_io_view.png?280|List of ATmega registers in the debugger}}] |
| |
| Programmi silumiseks (inglise keeles //debugging//) nimetatakse vigade otsimist programmist. Selleks on loodud eraldi vahendid - silurid (inglise keeles //debugger//), mis võimaldavad programmi täita samm-sammult, peatades seda soovitud kohtades. Selline programmi täitmine võimaldab igas programmi faasis kontrollida muutujate väärtusi, registrite sisusid ja programmi täitmise järjekorda. Eriti oluline on silumise võimaluse kasutamine keerukamate programmide juures, kus vea põhjust on tihti keeruline otsida. Mikrokontrollerite puhul on veel oluline, et tarkvara samm-sammult täitmine toimub reaalselt kontrolleris ja võimaldab nii lisaks registrite väärtuste vaatamisele näha ka reaalsete väljundite muutumist. Siluri kasutamiseks on eelkõige vaja kahte tingimust: kasutatav mikrokontroller peab silumist toetama ja peab olema silumist toetav riistvara - JTAG programmaator. Odavamad programmaatorid, mis kasutavad ISP programmeerimise liidest, võimaldavad laadida kontrollerisse kompileeritud programmi, kuid ei võimalda silumist. | Debugging is the process of finding errors in a program. For this purpose debuggers are used, which allow the program to run step-by-step and stop at desired points. This makes it possible to inspect variable values, register contents, and the execution order at each stage. Debugging is especially important for complex programs where the cause of errors can be difficult to find. In microcontrollers, step-by-step execution runs in the controller itself and also allows you to see real output changes, in addition to register values. To use debugging, two conditions must be met: the microcontroller must support debugging, and you must have debugging hardware, i.e., a JTAG programmer. Cheaper programmers that use ISP can upload the compiled program but cannot debug. |
| |
| Programmi käivitamiseks siluri režiimis AVR Studio-ga tuleks see esmalt kompileerida nupuga //Build// (kiirklahv F7) ja käivitada kompileeritud programm käsuga //Run// (kiirklahv F5). Programmi lähtekoodi võib enne seda soovitud kohtadele lisada katkestuspunkte (inglise keeles //break point//) (kiirklahv F9). Kui programmi täitmine jõuab katkestuspunktini, peatatakse programmi töö, võimaldades nii uurida mikrokontrolleri olekut sellesse punkti jõudmisel. Programmi täitmist võib jätkata jällegi käsuga //Run// või siis kasutada käskhaaval täitmist käsuga //Step Into// (kiirklahv F11). | To run a program in debug mode with AVR Studio, first compile with //Build// (F7) and then start the compiled program with //Run// (F5). You can set break points in the source code before this (F9). When program execution reaches a break point, execution stops and you can inspect the microcontroller state at that point. You can continue with //Run// or step through using //Step Into// (F11). |
| |
| ===== Ujukoma-arvude kasutamine ===== | ===== Using floating point numbers ===== |
| |
| Mõnikord tekib vajadus AVR programmis kasutada ujukoma-arve. Nendega arvutamiseks ja nende esitamiseks //printf//-tüüpi funktsioonidega tuleb projekti seadistustes teha järgmised muudatused: | Sometimes you need to use floating point numbers in AVR programs. To calculate with them and use //printf//-style functions, make the following changes in project settings: |
| |
| **1.** Avada projekti seaded menüüst //Project -> Configuration Options//. Seadete kaardil //Libraries// tuleb sinna, kuhu on lisatud Kodulabori teegi objekt //libhomelab.a//, järgi lisada ka //libprintf_flt.a// ja //libm.a//. | **1.** Open project settings via //Project -> Configuration Options//. On the //Libraries// tab, after //libhomelab.a// add //libprintf_flt.a// and //libm.a//. |
| |
| **2.** Seejärel tuleb avada //Custom Options// kaart ja valida //[All files]// sektsioon. Parempoolsesse kasti lisada read "-lprintf_flt" ja "-lm". //[Linker Options]// sektsiooni lisada rida "-uvfprintf". | **2.** Open the //Custom Options// tab and select the //[All files]// section. Add "-lprintf_flt" and "-lm" to the right-hand box. In the //[Linker Options]// section add "-uvfprintf". |
| | |
| | **3.** Press OK and close the settings window. |
| |
| **3.** Vajutada OK ja sulgeda seadete aken. | |