Difference between revisions of "PIC Microcontrollers with C18 Compiler"

From Mech
Jump to navigationJump to search
 
(69 intermediate revisions by the same user not shown)
Line 1: Line 1:
__TOC__
__TOC__
==PIC Microcontroller==
The microcontroller used for this project is the PIC18F4431 made by [http://www.microchip.com 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.


==Compiler and IDE==
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 C18 compiler is a C-language compiler for the Microchip PIC18 family of microcontrollers. The student version of this compiler is available for free at Microchip's website. The difference between the student version and the commercial version is the level of optimization available. For most purposes, the student version should suffice.


Be aware that there are some differences between Microchip's C18 compiler and C compilers for other platforms. For example, an <tt>int</tt> data type in C18 is 16 bits, instead of the usual 32 bits. Refer to the C18 user guide for more information.
The PIC is interfaced with the XBee modem via the RS-232 serial interface.


Microchip's IDE, MPLAB, is also available for free at Microchip's website.
===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.


==Connecting the Programmer==
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 (<tt>p18f4431.h</tt> (or <tt>p18f4431i.h</tt> if you are using the ICD2 programmer) for the PIC18F4431 microcontroller).
[[media:Using_ICD2_Poster.pdf|Connecting the ICD2 Programmer]]


==Writing the Code==
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 <tt>.c</tt> or <tt>.h</tt> 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.
[[Writing Code with the C18 Compiler]]


==Example Projects==
====Reading and Writing to Registers====
[[PIC Analog-Digital-Converter Example]]
The I/O ports and hardware peripherals on the PIC are controlled by SFRs, or special function registers. The values in these registers determines the behavior of the hardware. For example, Port B is an 8 channel digital I/O port that has corresponding SFRs TRISB, PORTB, and LATB (TRISB stands for Tri-State B, PORTB stands for Port B, and LATB stands for Latch B). Each of these registers is eight bits long, and each bit corresponds to a certain pin (channel) on the PIC. In this case, the least significat bit of the register affects pin RB0, and the most significant bit affects pin RB7. To write values to SFRs, we simply treat them like a global variable. Individual bits can usually be accessed by using the convention '''[SFR_name]bits.[bit_name]''', for example, '''PORTBbits.RB0'''. The name of the SFRs and bits can be found in the datasheet and the header file for the PIC (e.g. <tt>p18f4431.h</tt>).


[[PIC Motor Control and Serial Port Example]]
TRISB determines whether the pins on Port B are configured as inputs or outputs. If we wanted RB0, RB1, and RB5 to be inputs and the rest to be outputs, we would write:
<pre>
TRISB=0b00100011; //a "0b" prefix denotes that the following is a binary number
</pre>
Of course, we could have used a hexademical or decimal number instead.


[[PIC Motor Control with EEPROM and Interactive Menu Example]]
If we then wanted to set all the output ports high, we could write:
<pre>
LATB=0b11111111;
</pre>
LATB will not affect the pins designated as inputs.


For more examples, see the C18 User's Guide and the application notes on Microchip's website.
If we wanted to read the state of pin RB0 and store the result into a variable named <tt>RB0_Status</tt>, then we would write:
<pre>
RB0_Status=PORTBbits.RB0;
</pre>


==Tips and Troubleshooting==
Notice that when we read an input, we used PORTB, and when we wrote to an output, we used LATB. LATB will give us the values that we ''want'' the output pins to be, while PORTB will give us the actual state of the pin. Writing to PORTB will usually do the same thing, but could also lead to a [http://www.piclist.com/techref/readmodwrite.htm| Read-Modify-Write problem] that could distort the states of the pins.
[[PIC/C18 Compiler Tips and Troubleshooting]]


===main.c===
==Documents==
===MPLAB and C18 Compiler===
In your main source file, which we will call <tt>main.c</tt>. At the top, you should include the line
[[media:MPLAB_GS.pdf | MPLAB Quick Start Guide]]
<pre>
#include <p18f4431.h>
</pre>
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 <tt>.h</tt> header files.


[[media:MPLAB_UG.pdf | MPLAB User's Guide]]
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
<pre>
#include <usart.h>
</pre>
For this project, we will also include a file called <tt>main.h</tt>. This file will contain macros and miscellaneous functions.


[[media:Microchip_C18_GS.pdf | C18 Compiler Getting Started]]
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.


[[media:Microchip_C18_UG.pdf | C18 Compiler User's Guide]]
====Recommended Reading====


[[media:Microchip_C18_libraries.pdf | C18 Compiler Libraries]]
===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.


==Code==
===PIC18===
[[media:PIC18_config_settings.pdf | PIC18 Configuration Settings Addendum]]


==PCB==
===PIC18F4431===
==Documents==
[[media:PIC18F4431_datasheet.pdf | PIC18F4431 Datasheet]]
[[media:PIC18F4431_datasheet.pdf | PIC18F4431 Datasheet]]

[[media:PIC18F4431_errata.pdf | PIC18F4431 Errata]]
[[media:PIC18F4431_errata.pdf | PIC18F4431 Errata]]
[[media:PIC18F4431_config_settings.pdf | PIC18F4431 Configuration Settings]]

Latest revision as of 15:23, 11 October 2007


Compiler and IDE

The C18 compiler is a C-language compiler for the Microchip PIC18 family of microcontrollers. The student version of this compiler is available for free at Microchip's website. The difference between the student version and the commercial version is the level of optimization available. For most purposes, the student version should suffice.

Be aware that there are some differences between Microchip's C18 compiler and C compilers for other platforms. For example, an int data type in C18 is 16 bits, instead of the usual 32 bits. Refer to the C18 user guide for more information.

Microchip's IDE, MPLAB, is also available for free at Microchip's website.

Connecting the Programmer

Connecting the ICD2 Programmer

Writing the Code

Writing Code with the C18 Compiler

Example Projects

PIC Analog-Digital-Converter Example

PIC Motor Control and Serial Port Example

PIC Motor Control with EEPROM and Interactive Menu Example

For more examples, see the C18 User's Guide and the application notes on Microchip's website.

Tips and Troubleshooting

PIC/C18 Compiler Tips and Troubleshooting

Documents

MPLAB and C18 Compiler

MPLAB Quick Start Guide

MPLAB User's Guide

C18 Compiler Getting Started

C18 Compiler User's Guide

C18 Compiler Libraries

PIC18

PIC18 Configuration Settings Addendum

PIC18F4431

PIC18F4431 Datasheet

PIC18F4431 Errata