3.2 Delay Driver

3.2.1 Introduction

3.2.1.1 Overview

The MPLAB® XC8 and XC16 compilers have built-in delay functions or macros to make the microcontroller (MCU) wait for a certain amount of time. For these delay functions, time is measured in microseconds or milliseconds, and for PIC32 devices, in timer ticks as well. The format for invoking these macros will vary from device to device, but this delay driver helps abstract the format differences.

3.2.1.2 Features

The Delay driver supports pausing for a number of milliseconds or microseconds.

3.2.2 How to Use the Delay Driver

To use the delay drivers, include the delay.h file wherever the delay function is needed and call the DELAY_milliseconds(time) or DELAY_microseconds(time) function, where time is the number of milliseconds or microseconds to delay.

3.2.2.1 Use Cases

The Delay driver is an interface for abstracting the built-in delay macros.

3.2.2.1.1 Use Case 1: Delay for 1s before Toggling an LED

Use the DELAY_milliseconds function defined by the Delay driver:
  • This requires 1000 ms to get a 1s delay:

int main(void)
{
    SYSTEM_Initialize();

    while(1){
        DELAY_milliseconds(1000); //Delay for 1s
        LED_Toggle();
    }
}

3.2.2.1.2 Use Case 2: Delay for 1 ms before Toggling an LED

Use the DELAY_microseconds function defined by the Delay driver:
  • This requires 1000 μs to get a 1 ms delay:

int main(void)
{
    SYSTEM_Initialize();

    while(1){
        DELAY_microseconds(1000); //Delay for 1 ms
        LED_Toggle();
    }
}

3.2.3 Module Documentation

3.2.3.1 Delay Driver

3.2.3.1.1 Module description

This file contains the API to generate delays in the range of milliseconds and microseconds.

Version: DELAY Driver Version 1.1.0
Functions
  • void DELAY_milliseconds (uint16_t milliseconds)

    Delays the execution of the program for a certain number of milliseconds.

  • void DELAY_microseconds (uint16_t microseconds)

    Delays the execution of the program for a certain number of microseconds.

3.2.3.1.2 Function Documentation

DELAY_microseconds()

void DELAY_microseconds (uint16_t microseconds)

Delays the execution of the program for a certain number of microseconds.

Parameters:
in microseconds

- Number of microseconds to delay

Returns:

None.

DELAY_milliseconds()

void DELAY_milliseconds (uint16_t milliseconds)

Delays the execution of the program for a certain number of milliseconds.

Parameters:
in milliseconds

- Number of milliseconds to delay

Returns:

None.

3.2.4 File Documentation

3.2.4.1 source/delay.h File Reference

#include <stdint.h>

3.2.4.1.1 Functions

  • void DELAY_milliseconds (uint16_t milliseconds)

    Delays the execution of the program for a certain number of milliseconds.

  • void DELAY_microseconds (uint16_t microseconds)

    Delays the execution of the program for a certain number of microseconds.

3.2.4.1.2 Detailed Description

DELAY Generated Driver API Header File