One of the characteristics of the evaluation board is that comes with a bootloader already programed, this allows us to load the program to the microcontroller without a programmer's necessity, because the bootloader takes charge of receiving the “hex” file through the USB port and to make that the microcontroller to program itself with the program of our application after the space that occupies the program of the “Bootloader.”
The used bootloader is the denominated HID that is the one that Microchip uses in its last application libraries MAL(Microchip Aplication Library).
This bootloader is declared to the system like a human interface device (HID), which Windows recognize directly without driver necessity.
1. Execute “HID bootloader” application and will appear us this window:
The PICDEM FS USB evaluation board normally executes the program that has been loaded and if we maintain pressed one of its push buttons (S2) and reset it enters to the bootloader mode. When entering this mode it will be recognized by the system and by the Bootloader application, remember that it doesn't need driver, and the button will be enabled “Open Hex File” of the application in the PC.
2.
Clicking in button “Open Hex File” we can load the file with extension “hex” that generate previously.
3.
Click in “Program/Verify” to load the microcontroller with our program. To confirm that the operation was carried out with success in the informative part of the main window.
4.
Press “Reset” push button of the board that makes it to leave the bootloader mode and execute our program. They should light the 4 LEDs that the board has connected in the Port D.
next we analyze the program so that you understands it and can use it as template and to modify it according to their necessities.
Analysis of the program.
1.
What is contained among / * and * / are comments and they appear in MPLAB of clear gray color:
/*Ejemplo 2 capítulo
"Getting started" "Light LED"
modificado para puerto D que tiene LEDs en tarjeta USB_BOARD_4550
XTAL = 20MHz
versión: 10 para bootloader HID
Modificado: 13 Agosto 2012
*/
2.
“//” it is used to add comments since from it appears until the end of the line:
#pragma config PLLDIV = 5 // 20 MHz crystal/5 = 4MHZ para PLL
96MHz
Sometimes is used also to disable a line of the program without deleting it:
//#pragma config CP2 = OFF
3.
The commands “#pragma config” they indicate to the programer's program the value of the different bytes of configuration fuses that has the microcontroller:
#pragma config PLLDIV = 5 // 20 MHz crystal/5 = 4MHZ para PLL
96MHz
4.
Since the bootloader program occupies a space permanently at the beginning of the program memory where are the reset vectors and interruption, we should re-address these vectors:
// (PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
#define
REMAPPED_RESET_VECTOR_ADDRESS 0x1000
extern void _startup
(void); // See c018i.c in your C18
compiler dir
#pragma code
REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
void _reset (void)
{
_asm goto _startup _endasm
}
5.
With “void main(void)” our main program begins. It is contained among key parenthesis "{}"
6.
“TRISD = 0;”
it configures the port D like output
7.
The value that we assign to “PORTD” it will be reflected in the output:
PORTD = 0x0f;
With this the 4 less significant bits are activated, therefore the LEDs will ON.
Using MPLAB simulator.
1.
If we don't see the source code of our program at this time and we only see the tree scheme. Click in the file with extension. c to open it.
2.
If we select “Debugger/Select tool” we can see several debugging tools. “MPLAB SIM” it doesn't require additional hardware and it allows us to simulate on screen. Choose this option.
3.
A bar will be added with icons that allow us to execute the program step by step, all at once, skip functions, to leave these, etc.
Note:
F5, F6, F7, F8, F9 they correspond to the icons of the debugging bar.
4.
Having open our project to press “F5” (Halt) and then “F6” (Reset) to make sure that there is not an execution in progress.
Note:
It is necessary to make click in the white part of the window that contains the code to focus it and then to execute the debugging commands.
5.
Press “F7” to execute step by step.
We will go by several sentences of functions that C18 implements and that they appear in a separate window. We will concentrate on arriving to the function “main” of our program.
6.
In the first line after the main opening key can make double click to put a “Breakpoint”, what will allow us to execute until this point at once next time that we run the program from the beginning with F9.
7.
Press F5 and F6 for reset, then F9 and we will arrive to the “Breakpoint”. Then we can follow step by step.
8.
To see how change the the microcontroller registers execute “View/Watch”: