S215  090806  

Lab IH06-21 Using the 10ms Delay Function
OEES 215

Back to Main Page
Table of contents for this page
Explanation of the example program Example Run the example Modify the example and run the new program Extra credit
 
Explanation of the example program
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:
  • void means that the function won't be sending back any number to whatever program called it.
  • Delay10ms is the function name.
  • unsigned char dly means that the function expects to receive an unsigned character, which in this case actually means it expects to receive an unsigned byte (8 ones and zeroes). unsigned means that the number will always be a positve one. (After all, a negative delay would mean travelling back in time!) In the C Language, there is no byte variable type, instead we use char, which means the same thing.
  • unsigned char i is a variable declaration, and it tells the compiler to reserve a space in memory for a one-byte variable named i.
  • The if statement checks to see if a delay of zero is being asked for (a strange thing for someone to ask for). A delay of zero causes problems, so the if statement tells the PIC to return to the calling program if it sees such a delay. (Also, returning to the calling program right away will result in a delay close to zero). return means, go back to whatever program called the function.
  • The for command is something we haven't covered yet. Basically the (i=25; i>0; i--) following the for means that  Delay100TCYx(dly) will be repeated 25 times.
  • Delay100TCYx(dly) is a function that was furnished along with the PIC C compiler that MPLAB IDE uses. In the function name, 100TCY means 100 instruction cycles. CY stands for cycles, and T stands for instruction, and I'm guessing they didn't want to use I for some reason so they used T instead.  The x stands for "times". 
  • We are using an instruction cycle time of 4 us (microseconds), so if dly contains a one, the Delay100TCYx(dly) statement would give us a delay of 100 x 4 x 1, or 400 us. 
    • The instruction cycle time is the amount of time it takes the PIC to execute one machine-language instruction, such as adding two numbers together.
  • Because the for statement causes Delay100TCYx(dly) to be repeated 25 times, the delay obtained is 25 x 400 us x dly. If dly contains a two, the delay works out to 25 x 400 x 2, which equals 20000 us, or 20 ms.
  • In other words, the Delay10ms( ) function takes whatever number is sent it, puts that number into the dly variable, and multiplies it by 10 ms.
The good thing about functions such as Delay10ms( ) is that you don't need to be concerned with their inner workings. Thus, in order to use the Delay10ms( ) function, all you need know is that it multiplies whatever number you send it by 10 ms, and gives you a delay of that length.

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:
  • LED0 is turned on.
  • The PIC waits for 0.1 seconds.
  • LED0 is turned off.
  • The PIC waits for 0.1 seconds.
  • The previous four steps are repeated over and over again.
 
Example
//******************************************************************************
//   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 ||||||||||||||||||||||||||       
}
}


Run the example
  1. Download ih01-18-flashing-LED.c by right-clicking the preceeding underlined link, and put it into your C Programs folder.
  2. Load, compile, and run the program. (Here's how.)
  3. LED0 should flash on and off quite rapidly.

Modify the example and run the new program
  1. Save the example program with the new file name of IH06-21 Using the 10ms delay function.c . ( Here's how.)
  2. Change the two delays in the example program so that you will get 1 second delays instead of 0.1 second delays.
  3. Add an if statement right above the line LED0 = 1 and set things up so that the LED will flash only when switch 0 is on. 
Hints:
  • You'll need to make a block out of the four lines in the main loop of the example. Do this by putting a left curly brace before the line LED0 = 1 and a right curly brace after the second delay.
  • You won't need an else statement.
When you run the program, the LED should flash only when switch 0 is on. When switch 0 is off, the LED might stay on, but this is OK.

Extra credit
Note: Extra credit projects are designed to give you something to do if you're caught up in lab and want to earn extra points. We won't give you extra credit points unless you're caught up with all the other lab projects. So, if you're not caught up, first get yourself caught up. Then, come back and do this extra credit project.
  • Add an else statement that will turn off the LED when switch 0 is off. This will ensure that the LED doesn't remain on some of the times when switch 0 is off.
  • Add code that will make LED 1 flash when switch 1 is on and LED 2 flash when switch 2 is on. All three LEDs and all three switches should operate independently.

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