This is an old revision of the document!
PAra escrever um programa para o controlador é necessário criar um novo projecto. O projecto inclui tipicamente diferentes ficheiros como código fonte, ficheiros de cabeçalhos, programas compilados, etc. É fortemente acomselhado criara uma nova directoria para cada projecto (uma das opções do Assistente de Novo Projecto).
Os seguintes passos tem de ser completo quando é criado um novo projecto com a ajuda do assistente:
1. Inicializar Eclipse (Robotic HomeLab IDE) e selecionar novo projecto. Se o software foi instalado de fresco, a directoria Workspace terá de ser criada. Para fazer isto selecione a localização da directoria Workspace na janela principal onde o repositório será criado.
Para criar um novo projecto, selecione no menu File → New → C project. Insira o nome do projecto e prima Next.
2. Configuração do projecto na caixa de diálogo deverá ser selecionada como only Release.
3. Na janela seguinte, o tipo de controlador e a frequência de utilização tem de ser selecionados. O microcontrolador do Robotic HomeLab (v5) é o ATmega2561 funcionando há frequência 14745600 Hz. Para concluir a configuração do novo projecto prima Finish.
4. Depois disto a interface de utilizador para desenvolvimento do programa irá abrir. Antes de começar a escrever código é preciso criar um novo ficheiro para conter o código fonte do programa. Para fazer isso, clique com o botão direito do rato no projecto, selecione New → Source File e insira um nome para o ficheiro, que deverá acabar com a extensão “.c”. É recomendado que o nome principal do programa seja “main.c”. Depois de primir Finish, o novo ficheiro é criado e aberto no ambiente de desenvolvimento.
5. Antes de começar a escrever código, é aconselhável fazer algumas alterações às preferências do ambiente de desenvolvimento para maior comodidade. Selecione do menu Window - Preferences e navege no menu de àrvore da esquerda até General → Workspace e selecione a opção Save automatically before build
6. Para testar o correcto funcionamento do Eclipse, o seguinte código fonte pode ser copiado para o ambiente de desenvolvimento e compilado. Para compilar pode-se usar a combinação de teclas de CTRL+B
/CONTINUAR AQUI For testing the correct operation of Eclipse, the following source code can copied to environment and compiled. For compilation the keyboard combination of CTRL + B can be used. <code c> A simple test code which is not using HomeLab library #include <avr/io.h>
int main(void) {
unsigned int x,y; // Set pin PB7 as output DDRB = 0x80;
Infinite loop while (1) { Invert pin PB7
PORTB ^= 0x80;
y=3200;
while(y--){
x=260;
while(x--){
asm volatile ("nop");
}
}
}
} </code>
For successful compilation, the code is needed to be error free and the compilator must be able to find all necessary header files. A successful compilation is indicated in Problem window as emptiness (or at outermost cases some warnings Warning) and in Console window as the output of compilator which will for example be in this case:
AVR Memory Usage ---------------- Device: atmega2561 Program: 308 bytes (0.1% Full) (.text + .data + .bootloader) Data: 0 bytes (0.0% Full) (.data + .bss + .noinit) Finished building: sizedummy
If the code has errors or header files are not found, then the Console window displays the number of errors on end line and describes possible faults above it.
../main.c: In function 'main': ../main.c:23: error: expected ';' before 'while' ../main.c:13: warning: unused variable 'x' make: *** [main.o] Error 1
Alsothe Problems window displays several error messages and corresponding location in code is marked with error icon.
7. For using HomeLab library, it is assumed to be correctly installed into operation system with the help of instruction. For every project the library must be included into list of linkable objects in project options. To do this, open at first: Project → Properties and then from leftside tree C/C++ Build → Settings, further Tool Settings → AVR C linker → Libraries. Choose in window that opened in the right under Libraries (-l) icon Add… and input to the dialog box “homelab2561”. Then OK and one more OK.
8. For testing HomeLab library the following source code can be used when copied besids the previous code and compiled. You can again use CTRL + B combination on keyboard.
//A simple test code which uses HomeLab library #include <avr/io.h> #include <homelab/delay.h> int main(void) { // Set pin PB7 as output DDRB = 0x80; // Infinite loop while (true) { // Invert pin PB7 PORTB ^= 0x80; hw_delay_ms(500); } }
If compilation of this code is also successful, the development environment was set up correctly.
1. Connect the microcontroller board to PC using USB cable. After successful connection a small green LED labeled as PWR will light up (after some time on first connection).
2. Set options for program loader (AVRDude). For this again open project properties Project → Properties and from leftside tree AVR → AVRDude, further Programmer configuration → New. The dialog box that now opens needs no changes and you can press OK button. If it is selected press Apply and then OK.
It is important to check that in list Programmer configuration something can be choosed and by default this will be: New Configuration.
3. If HomeLab microcontroller module is connected with PC, you can now try to load the compiled program into microcontroller. For this simply press AVR icon or use CTRL + ALT + U in keyboard.
As an effect of this 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!
It is sometimes necessary in AVR programs to use floating-point variables. For calculating with them and presenting with printf-type functions, the following changes are necessary in the configuration of the project:
1. Open the configuration of the project from the menu File → Properties. On the leftside panel open AVR C Linker → Libraries, where in addition to HomeLab library other library objects must be included printf_flt and m (libprintf_flt.a ja libm.a).
2. Then open AVR C Linker → General and into box of Other Arguments add line -uvfprintf.
3. Press OK and close configuration window.