|

楼主 |
发表于 2010-12-26 19:49:42
|
显示全部楼层
now, we are going to create our first PIC project.
1) launch hi-tide;
2) the first time it launches, it will ask you for a "workspace": essentially how / where you want your projects to be stored. Pick a spot or go with the default setting.
3) once in hi-tide, you can create a new project, by clicking the "new project" icon on the upper left, or File->New->Project. This launches the new project wizard.
4) you will see that a few "lines" are there on the first screen, like "CVS", or "Hi-Tech C", etc. Expand "Hi-Tech C" and you will see a hi-tech compiler (if you have installed it / them). I prefer to use picc9.60pl2. but use whatever you feel comfortable. Click "Next".
5) You will need to give your project a name. whatever works for you is fine. Hi-tide (Eclipse really) allows spaces as a valid character. I use "CR 12F675 Ex1" as my project name. Click "Next".
6) you need to chose the project type. We want to produce a hex file to be burned into the pic. Either "Debug" or "Release" will work. Click "Next".
7) Now, you need to pick your mcu. 12F675 is under "Microchip" so expand it and pick PIC12F675. Click "Next".
8) you can pick a packaging now, and/or main.c. Click "Next".
9) under the "C/C++ Project" window on the left, you will see that now you have a project called "CR 12F675 Ex1". Expand it and you will see a few lines and one of them is main.c. Double-clicking it will bring up the editor window, with the content of main.c in it that looks like this:
========beginning================
#include <htc.h>
void
main(void)
{
while (1){
//TODO Auto-generated main function
}
}
========end==============
that's the basic template for a mcu code.
10). On the lower left, you will see a "Project Information" window. it shows some information pieces, including how big the compiled code is.
11). We are going to enter a simple code, below, into the editor window.
============beginning============
#include <htc.h>
__CONFIG( //fuse configuration bits
MCLRDIS & //disable mclr pin - use it as a regular port pin
WDTDIS & //disable watch dog
BORDIS & //disable brown-out
PWRTEN & //enable power-up timer
INTIO //enable internal rc oscillator
);
#define LED_PORT GPIO //led on gpio, the only port for this chip
#define LED_DDR TRISIO //output direction register
#define LED0 (1<<0) //led on gpio.0
void mcu_init(void) { //reset the mcu
ANSEL = 0x00; //all pins as gpio
CMCON = 0x07; //analog comparators off
LED_PORT &=~LED0; //clear LED0
LED_DDR &=~LED0; //led0 as output
}
void
main(void)
{
mcu_init(); //reset the mcu
while (1){
//TODO Auto-generated main function
LED_PORT ^= LED0; //flip led0
}
}
=========end==============
this simple piece of code contains 4 important sections, for any mcu.
a) the header section: it tells the compiler which header file to include. here, we are using a htc compiler to generate code for 12F675, so we will let the compiler sort it through by including just <htc.h>
b) the fuse section: it tells the compiler how we want the fuse to be set on this particular mcu. which fuse to set and how to set them will depend on the chip and the tools you use.
c) the mcu initialization section, via "mcu_init()": this sets up the mcu in a way that we want. many mcu chips boot - up with its port in the analog input mode, analog comparators on, etc. so we need to turn that off.
d) the code execution section, via "main()": this is where the actual work is done.
your mcus may differ, your compilers may differ, how you implement the four sections are differ (for example, many modern mcus don't have the fuse concept but uses run-time enable/disable bits to turn on / off peripherals. and many avrs boot up with pin default to output so the mcu_init() is usually empty), but it is always a good idea to keep the four sections in place.
12): while in the editor window, press <Ctrl-S> will initiate the compiler, or Project->Build/Build All, etc. will compile the code and you will see the information in the Project Information window is updated accordingly. As the code has no error, it should compile flawlessly and generate the desired .hex / .coff files for us. |
|