⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quicksta.htm

📁 一个PIC的demo程序包
💻 HTM
字号:
<html><head><title>Getting Started</title></head>
<body bgcolor=#ffffff>

<a name="1000407"> </a>
<h1>Getting Started</h1>
<hr><p><a name="1000408"> </a>
For new users of the PIC C compiler, the following section provides a step-by-step guide to getting your first program running in a target system. You'll need a working PIC system, with RAM and ROM, and some means of programming the ROM. And, of course, you'll need to have installed the compiler as described in the previous chapter.
</p><p><a name="1000414"> </a>
You will find a complete guide to using HPDPIC and PICC in the chapters <em><a href="../hpd.htm#">Using HPDPIC</a></em> and <em><a href="../c.htm#">PICC Command Line Compiler Driver</a></em>.
</p><p><a name="1000416"> </a>
One thing should be made clear; with embedded programming there really is no such thing as a "quick start". There are several variables, e.g. the hardware, memory, I/O devices and the software, all of which must be exactly right or the program will simply not work. There are no error messages when your embedded program crashes - it is a black box. Be prepared to check everything carefully, and if possible start with known working hardware. Debugging hardware and software at the same time squares the degree of difficulty.
</p><a name="1000418"> </a>
<h1>4.1   A Sample Program</h1>
<pre><a name="1000419"></a>#include	&lt;pic.h&gt;<br><br>/*<br> *	Demo program - flashes LEDs on<br> *	Port B, responds to switch press<br> *	on RA1. Usable on PICDEM board.<br> */<br><br>#define PORTBIT(adr, bit) ((unsigned)(&amp;adr)*8+(bit))<br><br>static bit button @ PORTBIT(PORTA, 1);<br><br><br>main(void)<br>{<br>	unsigned i;<br>	unsigned char j;<br><br>	TRISB = 0;			/* all bits output */<br>	j = 0;<br>	for(;;) {<br>		PORTB = 0x00;			/* turn all on */<br>		for(i = 16000 ; --i ;)<br>			continue;<br>		PORTB = ~j;			/* output value of j */<br>		for(i = 16000 ; --i ;)<br>			continue;<br>		if(button == 0)			/* if switch pressed, increment */<br>			j++;<br>	}<br>}</pre>
<p><a name="1000420"> </a>
The small sample program shown is written for the PIC16C84 processor running on the Microchip PICDEM 1 demo board which has 8 LEDs attached to Port B. It loops forever flashing the LEDs. If your hardware is different, as it almost certainly is, you should write a similar program tailored for your particular hardware. Flashing LEDs is however a good place to start, as it provides a visual indication of program function. If you don't have LEDs attached, then you could monitor an output port line with an oscilloscope or logic probe. Alternatively, you could even monitor address lines with an oscilloscope or logic analyser. The idea is to be able to determine that the program is running correctly using a minimum of resources, so as to remove as many variables as possible from the problem.
</p><p><a name="1000421"> </a>
Once the first program is running, it is easier to progress from that point than to try and run a complex program from the beginning. To get this program running, you will need to compile it, either using HPDPIC (the integrated development environment) or PICC (the command line compiler driver).
</p><a name="1000423"> </a>
<h1>4.2   Using HPDPIC</h1>
<p><a name="1000424"> </a>
To enter this program, simply follow these steps:
</p><ul><a name="1000425"> </a>
<li>Start HPDPIC by typing <em>hpdpic</em>, then press <em>enter</em>. If you have installed HPDPIC properly, it will be in your search path. You should have on screen a <em>menu bar</em>, a large<em> edit window</em>, and a smaller <em>message window</em>.</ul>
<ul><a name="1000426"> </a>
<li>Start typing the program text in the edit window. The editor command keys allow either the standard PC keys (arrow keys etc.) or WordStar-compatible keystrokes.</ul>
<ul><a name="1000427"> </a>
<li>After typing the complete program (with any modifications necessary for your hardware) save the file by pressing <em>alt-s</em>. A dialog box will appear asking you to enter a name to save the file to. Type the name <em>sample.c</em> and press <em>return</em>. The file will be saved to disk.</ul>
<ul><a name="1000428"> </a>
<li>Press <em>alt-p</em> to open the <strong>Options</strong> menu. Use either the mouse or arrow keys to select the item <strong>Select processor...</strong>. This will open a dialog box enabling you to select the processor you are using. Press <em>return</em> to exit the dialog box.</ul>
<ul><a name="1000429"> </a>
<li>Press <em>alt-p</em> again and select the <strong>Output file type...</strong> item. This will open a dialog box allowing you to select an output format for your executable file. Choose a file format compatible with your programming process. Bytecraft COD and Intel HEX are the most commonly used formats. Press <em>return</em> to exit the dialog box.</ul>
<ul><a name="1000430"> </a>
<li>Press <em>F3</em> to compile the program. If you haven't saved the edit file, you will be prompted to do so now. Save it as <em>sample.c</em>. HPDPIC will compile the program. Any errors found will stop the compilation, and the errors will be listed in a window that appears at the bottom of the screen. The cursor in the edit window will be positioned on the error line. Correct the error, then press <em>F3</em> again. HPD remembers the processor type and output type you selected previously, so you will not have to re-enter this information.<br><br>On completion of compilation, an output file called <em>sample.cod</em> (or <em>sample.hex</em> for Intel hex) will be left in the current directory.</ul>
<ul><a name="1000431"> </a>
<li> Exit HPDPIC by pressing <em>alt-q</em>.</ul>
<a name="1000433"> </a>
<h1>4.3   Using PICC</h1>
<p><a name="1000434"> </a>
To use PICC to compile your sample program, you will first need to create a file containing the program. You can use whatever text editor you are familiar with, as long as it can create a plain ASCII file. The MS-DOS <em>edit</em> command is satisfactory. Save the file as <em>sample.c</em>. To run PICC, type:
</p><p><a name="1000435"> </a><tt>
picc sample.c

</tt></p><p><a name="1000436"> </a>
If you are compiling for the 16C84, for example, you should add the -16C84 option, e.g.
</p><p><a name="1000437"> </a><tt>
picc -16c84 sample.c

</tt></p><p><a name="1000438"> </a>
This generates code for the 16C84 instruction set.
</p><p><a name="1000439"> </a>
If you have correctly entered the sample program, no error messages should result. If you do get error messages, edit the program to correct the errors, and recompile with PICC as before. Here is sample output from PICC:
</p><pre><a name="1000469"></a>C:\HT-PIC\SAMPLES&gt;<strong>picc sample.c<br></strong>HI-TECH C COMPILER (Microchip PIC) V7.70<br>Copyright (C) 1984-1997 HI-TECH SOFTWARE<br>Serial no: CPIC-12345; Licensed to:<br>Jeremy Bennett, HI-TECH Software<br>Select chip type from one of:<br>12C508 12C509 14000 16C52 16C54 16C54A 16C55 16C554 16C556 ...<br>Type: 16c84<br><br>Memory Usage Map:<br><br>Program ROM:  $0000 - $002A   $002B (43) bytes<br>Internal RAM: $000C - $000E   $0003 (3) bytes</pre>
<p><a name="1000470"> </a>
Points to note:
</p><ul><a name="1000442"> </a>
<li>The processor was not specified, so PICC prompted for the processor.</ul>
<ul><a name="1000443"> </a>
<li>PICC has printed a short summary of memory usage.</ul>
<a name="1000444"> </a>
<h2>4.3.1  Output File Format Selection</h2>
<p><a name="1000445"> </a>
The compiler supports various output file formats. The two most commonly used for input into an EPROM programmer are Intel HEX and binary. The default is both Bytecraft COD and Intel HEX, but can be changed with one of the following options to PICC:
</p>
<dl><a name="1000446"> </a>
<dd> <em>-motorola</em>	Produce Motorola S1/S9 HEX file</dl>
<dl><a name="1000447"> </a>
<dd> <em>-bin</em>	Produce a binary output file</dl>
<dl><a name="1000448"> </a>
<dd> <em>-ubrof</em>	Produce an IAR Ubrof file</dl>
<dl><a name="1000449"> </a>
<dd> <em>-aahex</em>	Produce HEX records with symbols for American Automation emulators</dl>
<dl><a name="1000450"> </a>
<dd> <em>-tek</em>	Produce Tektronix HEX file output</dl>

<a name="1000451"> </a>
<h1>4.4   Running your program</h1>
<p><a name="1000452"> </a>
Once you have compiled the program, you will have a file called <em>sample.cod</em> and a file called <em>sample.hex</em> in the current directory. How you get this into your hardware will vary depending on just what you have to work with, but generally speaking you will need an EPROM/EEPROM programmer to allow you to get the program into the memory of your target system The exact procedures for doing so are beyond the scope of this manual.
</p>
<hr><br>



<address>
<a href="mailto:hitech@htsoft.com">hitech@htsoft.com</a>
</address>

<i>Copyright &#169; 1997, HI-TECH Software.   All rights
reserved.</i>

<!-- This file was created with Quadralay WebWorks Publisher 3.5.0 -->
<!-- -->
<!-- For more information on how this document, and how the rest of -->
<!-- this server was created, email hitech@htsoft.com -->
<!-- -->
<!-- Last updated: 07/30/97 12:44:24 -->

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -