S215  090803

Lab IG31-19 The Template Program
OEES 215

Back to Main Page
 
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 our programs we'll be using repeated vertical lines (//||||||||||||||||||||||||||||||||||||||||||||) to denote major sections. 
  • The next level will be repeated equal signs: //===============. 
  • The level after that will be repeated dashes: //---------------------- .

In addition to the information provided by the comments in the program below, here is further explanation of the program:
  • In computer programming, we often start counting with zero. There are several reason's why this is the custom. One is that the weight for the right-most digit in the binary number system is 20, which equals 1.  (Any number raised to the zero power is equal to 1.) 
  • Similarly, for a whole number in the decimal number system, the weight for the right-most digit is 100, which also equals 1. This is why we refer to the digit position on the far right as the units column. 
  • Starting at the right-most LED and going left, the eight LEDs are numbered 0, 1, 2, 3, 4, 5, 6, 7. The same is true of the switches, even though the numbers printed next to the eight switches go from 1 to 8. In other words, starting at the right-most switch, the switches are numbered0, 1, 2, 3, 4, 5, 6, 7.
  • A block of  statements begins with a left curly brace ( { ) and ends with a right curly brace ( } ). 
  • A statement represents a complete set of directions to the PIC, and it often contains a command.
  • A command is often a single word (such as if or while) that means something to the PIC.
  • Curly braces are used to mark the body of the main program.
  • Curly braces are used to mark the statements following the while statement.
  • A double equal sign ( == ) is used when we want to compare two pieces of data to determine if they are equal.
  • A single equal sign ( = ) is used to put a piece of data into a variable, or to send it to a port. (A port is basically a set of pins on the PIC that can have input or output wires connected to them.) In the statement x = 3, we're telling the PIC to put the number 3 into x. In programming, the equal sign doesn't mean the same thing as an equal sign in algebra. The programming equal sign is best thought of as an arrow pointing left. You can use the phrase "is replaced by" when you read an equal sign. In other words, the statement x = 3 means "the contents of x is replaced by 3." If x contained a 5 before the PIC executed the x = 3 statement, the 5 that was previously contained in x is replaced by a 3.
  • The while command is used to repeat a block of statements a certain number of times. In the program below, you'll notice that following the while command, there are parentheses with 1==1 inside them. This is a way of telling the PIC that we want it to repeat the block of statements "until Hell freezes over" (or until a break command is encountered, or we turn off the power). After all, 1 is always going to be equal to 1.
  • Functions (secondary programs) always have parentheses immediately following the function name. Sometimes there's nothing inside the parentheses and sometimes there are one or more variables or constants inside the parentheses.
  • Notice that most lines in the program end with a semicolon ( ; ). A semicolon marks the end of a statement. When the PIC reaches a semicolon, it knows it has gotten all the information it needs and can go ahead and execute the statement. An exception to the semicolon rule is when a block of statements follows a command, such as is the case with the while command below. When the PIC sees the left curly brace on the line below the while command, it knows that the information it needs will not be complete until it reaches the right curly brace. Once it reaches the right curly brace, it knows it should now do what the while command says to do, namely go back to the beginning of the block marked by curly braces. The right curly brace at the end of the block takes the place of the semicolon. It's kind of like a higher order semicolon. You can think of there being a giant statement that starts with the letter w of  while and that ends with the right curly brace.
If you've downloaded the files as part of the first lab, you already have a copy of the template.c file. If, for some reason, you want to download the template.c file again, right-click here.
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 ||||||||||||||||||||||||||       
}
}
Back to Main Page