S215 090803
Lab IG31-19 The Template Program
OEES 215
| The program below was written in the C
language. It's a language for writing programs of a technical nature,
such as programs to control machines and application programs such as
Microsoft Word. It got it's name from being the third crack at
developing a new language. The first two languages were called A and B.
There is a fancier version of C called C++. (Evidently they figured that
C+ would seem like a grade.) C++ has features that are important for
very large programming projects, but C itself is veryCCpowerful. It's
also the language that Microchip (manufacturer of the PIC line of
microcontrollers) has decided to make available to its customers. We'll use "PIC" interchangeably with "microcontroller." (The word microcontroller is a bit long.) In the program below, actual program statements are boldfaced and in blue. Text that's in black will have no effect on the program that's sent to the PIC microcontroller. This text is information to explain the program. Such lines of text are called comments. Good programming practice is to use plenty of comments, both for the person writing the program and for anyone else who might have need of understanding the program (such as someone at a later date that needs to make modifications). There are two ways of denoting that text is a comment. One is to precede it by two slashes. The other is to start the comment with a slash and a star ( /* ) and end it with a star and a slash ( */ ). This latter method allows you to write comments that are more than one line in length. The double slash method works only for comments that are no more than one line long. A common practice is to divide programs into sections (kind of like paragraphs) by separating them with lines of stars or lines containing other repeated characters.
In addition to the information provided by the comments in the program below, here is further explanation of the program:
Some Peculularities of LEDs 5, 6, and 7 When you run the program, LEDs 6 and 7 will be on, even though the program didn't tell them to be on. The is because the same PIC output lines that operate LEDs 5, 6, and 7 are also used to operate part of the alphanumeric display. For the present program, the alphanumeric display is operated in a such a way as to always leave LED 5 off. In other programs, this LED may be dim or flashing rapidly. This is because of a design compromise that resulted from the fact that there simply weren't enough input and output lines to go around. However, if a program doesn't use the alphanumeric display, all eight LEDs are totally under control of the program. Even when the alphanumeric display is being used, you can often send numbers to LEDs 5, 6, and 7, and be able to tell whether they are on or off. When they are supposed to be off, they may be lighted dimly or be flashing very fast. |
| //****************************************************************************** // Template for Use With IF24-20 PIC18 Microcontroller Board S215 09 0731 // ***************************************************************************** #include "s215-header8.h" //Includes the header file in this program. /* Contains definitions for SW0, SW1, ..., LED0, LED1, ..., PB, etc. */ #pragma code //This marks the beginning of the program area. void main (void) //This marks the beginning of the main program. /* When the microcontroller gets the RUN signal, it will start at the beginning of the main program, and call up functions as requested in the main program. DispN() is a function (secondary program) that displays a number in the alpha- numeric display. ADC8msb() is a function that gets an 8-bit number from the analog to digital converter. */ { InitPorts(); //Initialize I/O ports, channel 0 ADC, and // alphanumeric display. //---------------------------------- while (1==1) { //|||||||||||||||||||||||||| Main Loop |||||||||||||||||||||||||||||||| LED0 = SW0; //Switch 0 turns LED 0 on and off. LED1 = PB; //The pushbutton turns LED 1 on and off. DispN(ADC8msb(), 3, 0); //Display the output of the analog to digital converter // that's connected to the potentiometer. Three digits // will be displayed, and they will start at position zero // (far right digit of the display). //|||||||||||||||||||||||| End of Main Loop |||||||||||||||||||||||||| } } |