Difference between revisions of "PIC Microcontrollers with C18 Compiler"
(→main.c) |
(→main.c) |
||
Line 28: | Line 28: | ||
#include <usart.h> |
#include <usart.h> |
||
</pre> |
</pre> |
||
For this project, we will also include a file called <tt>main.h</tt>. This file will contain macros and miscellaneous functions. |
|||
If we want to implement real-time operations (for example, having some take performed exactly once every second), then the speed of our oscillator is important. The clock speed is often used to calculate periods and frequencies, so in main.h, we define the frequency of the oscillator clock as '''FOSC'''. It takes four instruction cycles to execute one instruction, so the clock frequency divided by four is also a useful number. We define this number as '''FOSC4''' in <tt>main.h</tt>. If you change your clock frequency, be sure to remember to edit this section. |
|||
====Recommended Reading==== |
====Recommended Reading==== |
Revision as of 17:59, 12 July 2007
PIC Microcontroller
The microcontroller used for this project is the PIC18F4431 made by Microchip. This is an 8-bit microcontroller with motor PWM generators, a quadrature encoder interface (QEI), and other "peripherals". The microcontroller uses an external 40MHz oscillator.
The microcontroller controls two wheel motors with PWM driving external H-bridges. The H-bridges draw their power directly from the batteries, not regulated power.
Each of the wheels also has a quadrature encoder. The left wheel encoder is connected to the PIC's QEI via a schmitt trigger buffer. The right wheel encoder is wired to a U.S. Digital LS7183 chip (because the PIC only has 1 QEI) which outputs pulses on one of two pins, depending on whether the wheel is spinning backwards or forwards. The two pins are then each connected to a counter on the PIC, which keeps track of the number of pulses. The location of the wheel can be determined by subtracting the reverse-pulse counter from the forward-pulse counter.
The PIC is interfaced with the XBee modem via the RS-232 serial interface.
Programming the PIC
Setting Up
For this project, Microchip's free MPLAB IDE and free C compiler were used to program the PIC18F4431. If you are using the ICD2 programmer, be sure to follow the installation instructions carefully, or Windows may attempt to install the wrong drivers. After installing both MPLAB and C18, create a new project with the project wizard. Follow the prompts, and you should end up with an empty project if you did not add any existing files.
We must now add a linker script to your project. Make sure View>Project is checked, then right-click on Linker Scripts in the project panel and click on Add Files.... The linker scripts are in folder MCC18\lkr. Find the file that corresponds to your microcontroller (p18f4431 (or p18f4431i if you are using the ICD2 programmer) for the PIC18F4431 microcontroller).
We now add source and header files in the same manner. To make a new source or header file, go to File>New, and then save the file as a .c or .h file. With the default settings, all the source and header files that you write must be in the same folder or MPLAB will give you an error message stating that it cannot find the files. You can change the environment variables by going to Project>Build Options...>Project.
main.c
In your main source file, which we will call main.c. At the top, you should include the line
#include <p18f4431.h>
This header file contains the definitions and names of the Special Function Registers (e.g. PORTA, TRISA, etc). By default, MPLAB will search the MCC18/h folder and the folder your main source file is in for .h header files.
Microchip's C18 compiler also comes with a library of functions. The documentation for the library can be found in the MCC18/Doc folder, if you choose to install them when installing C18. Otherwise, they can be found on Microchip's website. Since our program will be using the serial port, we include the serial port functions with
#include <usart.h>
For this project, we will also include a file called main.h. This file will contain macros and miscellaneous functions.
If we want to implement real-time operations (for example, having some take performed exactly once every second), then the speed of our oscillator is important. The clock speed is often used to calculate periods and frequencies, so in main.h, we define the frequency of the oscillator clock as FOSC. It takes four instruction cycles to execute one instruction, so the clock frequency divided by four is also a useful number. We define this number as FOSC4 in main.h. If you change your clock frequency, be sure to remember to edit this section.
Recommended Reading
Troubleshooting
- If MPLAB has trouble connecting with the ICD2, open the Device Manager (Control panel>System>Hardware>Device Manager) and disable then re-enable the ICD2. Also be sure that you followed in installation instructions in the manual when you installed the ICD2.
XBee
Code
PCB
Documents
PIC18F4431 Datasheet PIC18F4431 Errata PIC18F4431 Configuration Settings