Académique Documents
Professionnel Documents
Culture Documents
http://zjembedded.blogspot.in/2012/05/pic18-getting-started.html
Embedded Projects
Wednesday, May 30, 2012
After installing the IDE and PICkit 2 driver, I populated enough of my circuit board to start testing with the PIC. I added the LDO regulators for 5V, 3.3V, and 2.5V, a power LED, the reset circuit, crystal, LEDs, and DIP socket. My first mistake was soldering on a 48 MHz crystal when I saw 48 MHz as the max clock speed. I started up the LED blink program, and the timing wasn't even close. After some digging, I figure out that 48 MHz was the max clock speed, but to get that you either need an external clock or a 12 MHz crystal with the 4x PLL activated. I'm not finding it in the datasheet right now, but I seem to recall 25 MHz was about the max crystal frequency the PIC could drive. I'm using 16 MHz for now.
When you download the Microchip C18 compiler, be sure to check out the included documentation. It's located in the install directory in a "doc" folder (for me it is at C:\Program Files (x86)\Microchip\mplabc18\v3.40\doc). The "Getting Started" document and "User Guide" are obviously helpful, but the "hlpPIC18ConfigSet.chm" and "PIC18F Peripheral Library Help Document.chm" are also extremely useful. The first one lets you select your microcontroller and then list all of the configuration settings and possible values. The configuration settings do similar things as fuses in AVRs, but it seems to be a little smoother with PICs to use them. The issue I had at first is that the configuration settings names between chips vary, so if you're trying to get sample code for a different PIC to work with yours and are seeing compiler issues it'd be a good idea to reference this document.
1 of 3
2/20/2013 6:11 PM
http://zjembedded.blogspot.in/2012/05/pic18-getting-started.html
For example, the sample LED blink program I found in the user guide used "WDT" to set the watchdog timer to off, but for the PIC18LF26J11 , I needed to use "WDTEN" instead. The library help document shows all of the functions available in the C18 library, and again they're broken down by what's available for your specific microcontroller. The following code is what I wrote as a sample program to make sure I had everything working correctly and could program the PIC. I wired an LED each to pins 2, 3, & 4 on the PIC (PORTA0-2), and set them blinking at a 1, 2, & 4 Hz rate. This is where I figured out about the crystal frequency being off, because the blink rate was much slower than in should have been. I'm using two of the libraries that come with the C18 compiler, and had to add them to the project to get it to work (though I just referenced them from their default location).
#include <p18lf26j11.h> #include <delays.h> #pragma config OSC = HS //High speed crystal #pragma config WDTEN = OFF //Disable watchdog timer #pragma config XINST = OFF //Disable Extended CPU mode #define #define #define #define #define #define char i; void main() { //Set LED Pins data direction to OUTPUT LED1hzTris = 0; LED2hzTris = 0; LED4hzTris = 0; //Set LED Pins to OFF LED1hzPin = 0; LED2hzPin = 0; LED4hzPin = 0; i = 0; while(1) { Delay10KTCYx(50);//Delay 1/8 sec LED4hzPin = ~LED4hzPin;//Toggle LED Pin if (i%2==0) LED2hzPin = ~LED2hzPin;//Toggle LED Pin if (i%4==0) LED1hzPin = ~LED1hzPin;//Toggle LED Pin i++; } LED1hzPin LATAbits.LATA0 LED1hzTris TRISAbits.TRISA0 LED2hzPin LATAbits.LATA1 LED2hzTris TRISAbits.TRISA1 LED4hzPin LATAbits.LATA2 LED4hzTris TRISAbits.TRISA2
This step was pretty simple, but it's one that can take some time tracking down silly errors when you're starting with something new, and now I'm ready to move on to my actual project. Next up will be getting the RFID module working. Posted by Zach at 4:08 PM Labels: Dev Board, PIC
Comment as:
2 of 3
2/20/2013 6:11 PM
http://zjembedded.blogspot.in/2012/05/pic18-getting-started.html
3 of 3
2/20/2013 6:11 PM