4.3.1 Delay

The Delay functionality is used by the ATWINC15x0 driver for synchronizing with ATWINC15x0 modules.

  1. Create delay.c and delay.h files under the “source files” of the project.
  2. Copy the following to the delay.c file:
    #ifndef DELAY_H
    #define	DELAY_H
    #ifdef	__cplusplus
    extern "C" {
    #endif
      // 1ms Count = 1ms / (1/((FOSC/2)/TMR Prescaler))
    #define DELAY_1MS_COUNT     16 
    // Given FOSC/2 = 4MHz, TMR  //Prescaler = 256:
                                   // TMR Period = 64us
                                   // 1ms / 64us = 15.6 => 16    
    /* * Timer control functions for the delay function
     * Set these values to the MCC generated functions for the TMR peripheral used for the delay. */
    #define DELAY_TMR                   TMR1
    #define DELAY_TMR_START()           TMR1_Start()
    #define DELAY_TMR_STOP()            TMR1_Stop()
    #define DELAY_TMR_PER_SET(period)   TMR1_Period16BitSet(period)
    #define DELAY_TMR_COUNT_SET(count)  TMR1_Counter16BitSet(count)
    void delay_timer_callback ();
    void delay_ms(); 
    #ifdef	__cplusplus
    }
    #endif
    
  3. Copy the following to the delay.h file:
    #include <stdio.h>
    #include "mcc_generated_files/mcc.h"
    #include "delay.h"
    
    
    static bool delay_timer_expired = false;
    
    void delay_timer_callback (void)
    {
        delay_timer_expired = true;
    }
    
    void delay_ms(uint16_t ms)
    {
        DELAY_TMR_STOP();
        DELAY_TMR_PER_SET ( (uint16_t)(ms * DELAY_1MS_COUNT) );
        DELAY_TMR_COUNT_SET ( 0 );
        delay_timer_expired = false;
        DELAY_TMR_START();
        
        while (!delay_timer_expired)
            ;
    }