Lab IH06-21 Using the 10ms Delay Function
OEES 215
| Explanation of the example program | Example | Run the example | Modify the example and run the new program | Extra credit |
| The example program flashes
LED0 by repeatedly turning it on for a tenth second and turning if off
for a tenth second. The Delay10ms( ) Function This program uses the Delay10ms( ) function, one of the custom functions written for this course. Go to the Custom Functions Contained in the s215-libraryxx.lib File page to see a description of what the Delay10ms( ) function does. Basically, whatever number you put inside the parentheses gets multiplied by 10 milliseconds. In the example, we send a 10 to the Delay10ms( ) function, which gives us a delay of 100 milliseconds, which is the same as 0.1 second. Functions are little programs that are used by the main program (or by other functions) to perform a task that is often needed. In many cases, functions are also used to keep the main program short and easy to understand, with many of the details hidden away in functions. On a calculator, when you take the square root of a number, you are using the calculator's square root function. Functions in computer programming are the same idea, we can send them a number, and they can send us back a number. Actually, some functions don't get sent any numbers and don't send back any numbers. The InitPorts( ) function in the example is one such function. We still need the parentheses after the function name, even though we don't put anything inside them. When the compiler sees a name followed by parentheses, it knows that it's dealing with a function. The Delay10ms( ) function needs to have a number inside the parentheses, but it doesn't send back any number. All it does is make the computer waste time for however long we want the delay to be. The Delay10ms( ) function is contained in the s215-libraryxx.c file. (The xx stands for the version of the s215 library file.) If your were to look inside s215-library6.c, or any version after this, you'd see the following code (programming lines) for the Delay10ms( ) function. void Delay10ms(unsigned char dly) { unsigned char i; if (dly == 0) return; for (i=25; i>0; i--) Delay100TCYx(dly); } Here's an explanation of this code:
The Main Loop For most of our programs, the portion between //|||||||||| Main Loop ||||||||||| and //||||||||| End of Main Loop ||||||||||||| is where the important work is done by the PIC. We are calling this portion of the program the main loop, and it is a block of statements that are repeated over and over again by the whilecommand. This is typical of almost all control programs, i.e., programs that control machines (which can include electronic circuits controlled by a microcontroller). If we were to look at the software contained in the microcontroller chip in a cell phone, we'd see that it has such a loop. Inside this main loop, the microcontroller would be checking to see if there's an incoming call, or if we've pressed any buttons, or if the battery is getting low, etc. In the example, here's what's happening in the main loop:
|
| //****************************************************************************** // IH01-18 Flashing LED S215 09 0731 // ***************************************************************************** #include "s215-header8.h" #pragma code void main (void) { InitPorts(); //---------------------------------- while (1==1) { //|||||||||||||||||||||||||| Main Loop |||||||||||||||||||||||||||||||| LED0 = 1; Delay10ms(10); //10 x 10 ms = 100 milliseconds = 0.1 seconds LED0 = 0; Delay10ms(10); //|||||||||||||||||||||||| End of Main Loop |||||||||||||||||||||||||| } } |
|
|
When you run the program, the LEDs might behave a little differently than you expect if more than one switch is on. Hint: You'll want to use copy and paste to save yourself some typing. If you're not sure how to use copy and paste, ask us to show you how. |
| Back to Main Page |