Using timing functions I – startDelay() and isDelay()

swThis post demonstrates, how to use functions startDelay() and isDelay(). It is a response for questions from visitors of this website. At beginning I am going to show you, how to use simple startDelay() and isDelay() functions combination for red LED blinking. Then we will do some simple but useful temperature measurement application.

We are going to use functions startDelay() and isDelay():

startDelay(): – This function preset and start Delay timer.

void startDelay(ticks)

 ticks is number of ticks of 10ms. It is 8 bit value, so it can be 0-255.

isDelay()  – This function get status of Delay timer (elapsed, not elapsed)

bit isDelay()

This simple example show how to control red LED blinking with these two functions:

// ************************************************
/*
    This example demonstrates, how to us 
    startDelay() and isDelay() functions	
*/
// ************************************************
#include "includes/template-basic.h"
// ************************************************
void APPLICATION()
{
    // period of measurement is 100 Ticks = 1s
    meas_period = 100; 
 
    pulseLEDG();
 
    while (1)
    {
        startDelay(meas_period);
        while (isDelay());
        pulseLEDR();			
    }		
}
// ************************************************

 while loop wait for 1s, and then red LED pulse is executed.

Timed temperature measurement

Now, we know how to use startDelay and isDelay functions. So we are able to make some simple application for temperature measurement with specified measurement period. This example is not based on DPA, it is simple P2P application.  We will use Command R as a “read command” and attribute which specifies number of measurement. Measurement period is set in code, but of course you can change source code for period change possibility.

// *********************************************************************
/*
    This example demonstrates, how to us startDelay and isDelay() functions
 
    Just use commands: "R#num" where:
    'R' is Real time measurement command
    #num is number of measurement (not zerro), is decimal
    example: R#10 executes 10 real-time temperature measurement
 
    Just use commands: "R$num" where:
    'R' is Real time measurement command    
    $num is number of measurement (not zerro), is in hexadecimal
    example: R$0A executes 10 real-time temperature measurement
 
    There are responses for your requests:
    "A0" - No error
    "A1" - Unknown command
    "A2" - Busy, some process is running now
    "A3" - Bad params, some parameter of command is wrong
 
*/
// *********************************************************************
#include "includes/template-basic.h"
 
#define STATE_NONE        0    // No operation
#define STATE_RTM        1    // Real time measurement
#define STATE_STM        2    // Storage measurement
 
#define ERROR_NOERR        '0'    // No error
#define ERROR_CMD        '1'    // Unknown command
#define ERROR_BUSY        '2'    // Busy, I am not able to do it
#define ERROR_PARAMS    '3'    // Bad params
 
// *********************************************************************
void APPLICATION()
{
    uns8 meas_period;
    uns8 meas_num;
    uns8 meas_state;
    uns8 meas_val;
    uns8 ans_val;
 
    meas_period = 100;            // period of measurement is 100 Ticks = 1s
    meas_num = 0;                // number of measurements to do
    meas_state = STATE_NONE;    // state machine state
 
    pulseLEDG();
 
    while (1)
    {
        // request processing
        if (RFRXpacket())
        {            
            ans_val = ERROR_CMD;    // command not parsed yet
 
            if (DLEN == 2)    // command with one parameter
            {
                if(bufferRF[0]=='R') // real time measurement
                {
                    // State Idle and some measurement to do?
                    if ((meas_state == STATE_NONE) && (bufferRF[1]>0))
                    {                        
                        // state change indication
                        pulseLEDG();        
                        // copy num of measurement
                        meas_num = bufferRF[1];
                        // change state
                        meas_state = STATE_RTM;
                        // start measurement
                        startDelay(meas_period);
                        // set NOERR flag
                        ans_val = ERROR_NOERR;
                    }
                    else    // Error message
                    {
                        // R command bad param
                        if (bufferRF[1] == 0)
                            ans_val = ERROR_PARAMS;
                        // R command busy flag
                        else
                            ans_val = ERROR_BUSY;
                    }                
                }            
            }            
            // answer to request
            bufferRF[0] = 'A';         
            bufferRF[1] = ans_val;
            DLEN = 2;
            PIN = 0;
            RFTXpacket();
            pulseLEDR();            
        }        
 
 
        // I am in STATE_RTM state 
        if (meas_state == STATE_RTM)
        {
            // Delay elapsed
            if (!isDelay())
            {                
                // Need I some data?
                if (meas_num>0)
                {
                    // meas it and send it
                    meas_val = getTemperature();
                    bufferRF[0] = 'T';         
                    bufferRF[1] = meas_val;
                    DLEN = 2;
                    PIN = 0;
                    RFTXpacket();
                    pulseLEDR();
 
                    // decrease meas_num                    
                    meas_num -= 1;
                    if (meas_num == 0)
                    {
                        // meas_num elapsed?
                        // clear state
                        meas_state = STATE_NONE;                        
                    }
                    else
                    {
                        // not elapsed yet?
                        // restart measurement period
                        startDelay(meas_period);
                    }
                }
            }
        }
 
    }
}
 
// *********************************************************************

As a firts step, DLEN and command are checked. If DLEN is correct and command is R (read), then state and number of measurement are checked. If reading has been executed before, busy flag is set. If not and number of measurement is greater than 0, state is changed and Delay timer is set and restarted. After all these steps, answer is generated and sent.

        // request processing
        if (RFRXpacket())
        {            
            ans_val = ERROR_CMD;    // command not parsed yet
 
            if (DLEN == 2)    // command with one parameter
            {
                if(bufferRF[0]=='R') // real time measurement
                {
                    // State Idle and some measurement to do?
                    if ((meas_state == STATE_NONE) && (bufferRF[1]>0))
                    {                        
                        // state change indication
                        pulseLEDG();        
                        // copy num of measurement
                        meas_num = bufferRF[1];
                        // change state
                        meas_state = STATE_RTM;
                        // start measurement
                        startDelay(meas_period);
                        // set NOERR flag
                        ans_val = ERROR_NOERR;
                    }
                    else    // Error message
                    {
                        // R command bad param
                        if (bufferRF[1] == 0)
                            ans_val = ERROR_PARAMS;
                        // R command busy flag
                        else
                            ans_val = ERROR_BUSY;
                    }                
                }            
            }            
            // answer to request
            bufferRF[0] = 'A';         
            bufferRF[1] = ans_val;
            DLEN = 2;
            PIN = 0;
            RFTXpacket();
            pulseLEDR();            
        }

In main measurement block, Real Time measurement state is checked. Then Delay timer elapsed flag is checked and then number of measurements to do is observed. Then temperature is measured and sent. If there are some another measurement to do, Delay timer is restarted.

       // I am in STATE_RTM state 
        if (meas_state == STATE_RTM)
        {
            // Delay elapsed
            if (!isDelay())
            {                
                // Need I some data?
                if (meas_num>0)
                {
                    // meas it and send it
                    meas_val = getTemperature();
                    bufferRF[0] = 'T';         
                    bufferRF[1] = meas_val;
                    DLEN = 2;
                    PIN = 0;
                    RFTXpacket();
                    pulseLEDR();
 
                    // decrease meas_num                    
                    meas_num -= 1;
                    if (meas_num == 0)
                    {
                        // meas_num elapsed?
                        // clear state
                        meas_state = STATE_NONE;                        
                    }
                    else
                    {
                        // not elapsed yet?
                        // restart measurement period
                        startDelay(meas_period);
                    }
                }
            }
        }

Both source codes of these examples are attached. If you have some question, please feel free to ask me.

Download Attachments

  • zip Delays.zip
    Delays project
    File size: 90 KB Downloads: 160

Leave a Reply

Your email address will not be published. Required fields are marked *