Written by Hop
Published on 03 April 2009
Hits: 24403
Page 10 of 12
Writing the setup code into main.c
I'm sure you are wondering at this point... When am I going to be able to write code? I hear ya. My first C18 project took a week and that was before I even wrote anything worthwhile. The reason for that of course was all this set up, but once you do it a few times it goes a lot faster. Again, as I said before, I'll try to find a way to template all this so you don't have to go through the arduous task of the setup each time, but that is for another day. Even with all this setup work, there are things we haven't explored yet, like setting up the linker for custom ram banking, etc. Thankfully, we don't need to do that with this application so we will save that for another tutorial as well.
If you remember some years back (joking) I mentioned using a file for some of the code you will see here. Since we are going to place it in the main.c file, we will not need that extra file. I still want you to think about this though as we move forward. When you have a proven and tested set of configuration bit settings written, it's good to have that code isolated into a separate file that you can use for other projects. With that said, double click 'main.c' in your project view (under Source Files). When it opens up, place this code into that window via copy and paste.
#include <p18f452.h>
// HS-PLL Enabled, Internal External Osc. Switch Over OFF Disabled
#pragma config OSC = HSPLL, OSCS = OFF
// Power Up Timer: OFF Disabled
#pragma config PWRT = OFF
// Brown Out Reset: OFF, Brown Out Voltage: OFF Disabled
#pragma config BOR = OFF, BORV = 25
// Watchdog Timer: OFF Disabled, Watchdog Postscaler: 1:128
#pragma config WDT = OFF, WDTPS = 128
// CCP2 Mux: OFF Disabled (RB3)
#pragma config CCP2MUX = OFF
// Stack Overflow Reset: OFF Disabled
#pragma config STVR = OFF
// Low Voltage ICSP:OFF Disabled
#pragma config LVP = OFF
// Background Debugger Enable: OFF Disabled
#pragma config DEBUG = OFF
// Code Protection Block 0-3: OFF Disabled
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
// Boot Block Code Protection: OFF Disabled
#pragma config CPB = OFF
// Data EEPROM Code Protection: OFF Disabled
#pragma config CPD = OFF
// Write Protection Block 0-3: OFF Disabled
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
// Boot Block Write Protection: OFF Disabled
#pragma config WRTB = OFF
// Configuration Register Write Protection: OFF Disabled
#pragma config WRTC = ON
// Data EEPROM Write Protection: OFF Disabled
#pragma config WRTD = OFF
// Table Read Protection Block 0-3: OFF Disabled
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
// Boot Block Table Read Protection: OFF Disabled
#pragma config EBTRB = OFF
void main(void){
}
Notice the first line of code containing the #include . This instructs the compiler to include an important header file specific to the device you are planning to use. This header file allows you to address various features of the device by using predefined structures which makes life a LOT easier in code. Microchip includes make device specific header files with your C18 installation, and they are included in the C18 installation at "..\MCC18\h". When looking at this file, great care must be taken not to change anything in that file, whether deliberately or inadvertently.
In the future you may want to 'enhance' your device header file to add structures or modify existing ones. If that is ever the case, it is always a good idea to copy that file to your project folder and make changes to the copy. You should ALWAYS leave the factory header file unchanged. These kind of modifications are beyond the scope of this tutorial and will be addressed in another tutorial.
Also notice the brackets < > around the file name. Presenting the file name to the compiler in this format instructs the compiler to find the file using the search paths we customized earlier in this tutorial. If you should want to include a file that exists in the project root folder and bypass the search feature, simply enclose the file name in double quotes, as in #include "p18f452.h". If the file isn't located in the project root folder, an error will occur during the build process.
The #pramga lines make the setup code fairly large because we include the configuration bits via individual pragma directives. There are other ways to do this that leave a smaller code footprint. I prefer to use this format as it is easier to read and understand. The comments I added should enlighten you to what each line deals with. Make changes accordingly, like the clock settings, etc.
Save the file (CTRL+S). Now we will build the project even though the application doesn't do anything yet. The application should build fine without any errors. Click Project / Build All on the menu bar or use its shortcut (CTRL+F10).
Quite a bit happens in your OUTPUT window in the BUILD tab. This is where you will look for any errors during the build like syntax errors, undeclared prototypes, type mismatches, or any other wonderful coding snags you need to diagnose and fix.
Since the code is pretty simple, the build should succeed just fine, and your OUTPUT / BUILD results should look something like this...
----------------------------------------------------------------------
Debug build of project `S:\mcu\blink_led\blink_led.mcp' started.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed May 13 10:09:42 2009
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "S:\mcu\blink_led\main.o".
Clean: Deleted file "S:\mcu\blink_led\blink_led.cof".
Clean: Deleted file "S:\mcu\blink_led\blink_led.hex".
Clean: Done.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F452 "main.c" -fo="main.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
Executing: "C:\MCC18\bin\mplink.exe" /p18F452 /l"C:\MCC18\lib" /k"C:\MCC18\lkr" "main.o" /u_CRUNTIME /u_DEBUG /z__MPLAB_BUILD=1 /z__MPLAB_DEBUG=1 /o"blink_led.cof" /M"blink_led.map" /W
MPLINK 4.30.01, Linker
Copyright (c) 2009 Microchip Technology Inc.
Errors : 0
MP2HEX 4.30.01, COFF to HEX File Converter
Copyright (c) 2009 Microchip Technology Inc.
Errors : 0
Loaded S:\mcu\blink_led\blink_led.cof.
----------------------------------------------------------------------
Debug build of project `S:\mcu\blink_led\blink_led.mcp' succeeded.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed May 13 10:09:43 2009
----------------------------------------------------------------------
BUILD SUCCEEDED
If you get BUILD FAILED, recheck your work and make sure you do not have any typos. To illustrate what you might see in such a scenario, I removed the pound symbol from the first pragma and then rebuilt the project (CTRL+F10). Of course, the build failed and the output looked like this...
You can try this or just read on to familiarize yourself with debugging build errors.
----------------------------------------------------------------------
Debug build of project `S:\mcu\blink_led\blink_led.mcp' started.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed May 13 11:03:47 2009
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "S:\mcu\blink_led\main.o".
Clean: Deleted file "S:\mcu\blink_led\blink_led.cof".
Clean: Deleted file "S:\mcu\blink_led\blink_led.hex".
Clean: Done.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F452 "main.c" -fo="main.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
S:\mcu\blink_led\main.c:3:Error: syntax error
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `S:\mcu\blink_led\blink_led.mcp' failed.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed May 13 11:03:47 2009
----------------------------------------------------------------------
BUILD FAILED
Double clicking on the blue error line will take you right to that line of code so you can fix the syntax error. A little blue horizontal line will appear before the line containing the error. After fixing the error, rebuild the project to confirm you fixed the error.
{PAGINATION}
{PAGE_NUMBER}