Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay Function

Status
Not open for further replies.

EN0

Member
Hi,

I'm creating a delay function that offers the capability of having the timing configuration in milli-seconds or seconds. See the code below (C18 compiler):

Code:
void delay(int delay, char degree = 'm')
{
    int cntr;
    
    if(degree == 'm' || degree == 'M')
    {
        // Our timing configuration is in milli-seconds
        
        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < delay; cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (63536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 63536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
    else if(degree == 's' || degree == 'S')
    {
        // Our timing configuration is in seconds

        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < (delay * 2); cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (64536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 64536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
}

When I build the program it says that this line gives me a syntax error:

Code:
void delay(int delay, char degree = 'm')

This is presumably due to the "char degree = 'm'", but I'm wondering why that is so? I'm more familiar with PHP, and in that particular language, this makes it easy to assign a default value; if no value is entered in the second parameter when the function is called, it defaults to 'm'.

I'm hoping this is possible somehow in C?

Thanks,

Austin
 
No its not possible to initialize a variable in the function call in C, (This can be done in C++ by overloading), but if you compare the 's' || 'S' first, you can the default to milliseconds if degree contains anything but 's' or 'S'.
 
Okay, thanks. I revised my code to have it as a default (else statement), but it adds more lines than one would deem necessary.

Code:
void delay(int delay, char degree)
{
    int cntr;
    
    if(degree == 'm' || degree == 'M')
    {
        // Our timing configuration is in milli-seconds
        
        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < delay; cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (63536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 63536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
    else if(degree == 's' || degree == 'S')
    {
        // Our timing configuration is in seconds

        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < (delay * 2); cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (64536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 64536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
    else
    {
        // Our timing configuration is in milli-seconds (default)

        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < delay; cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (63536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 63536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
}
 
Last edited:
like this

Code:
void delay(int delay, char degree)
{
    int cntr;

    if(degree == 's' || degree == 'S')
    {
        // Our timing configuration is in seconds

        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < (delay * 2); cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (64536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 64536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
    else
    {
        // Our timing configuration is in milli-seconds (default)

        OVF = 0;
        T3CON |= 0x80;      // 16-bit configuration set

        for(cntr = 0; cntr < delay; cntr++)
        {
            // Load TMR3H and TMR3L bytes
            TMR3H = (63536 >> 8) & 0xFF;     // Load TMR3H byte first
            TMR3L = 63536 & 0xFF;        // Load TMR3L byte next
            while(!OVF);     // Wait for timer to complete transaction
            OVF = 0;
        }
    }
}

Then you don't need to specify milliseconds as this is the default, BUT 's' or 'S' will set seconds
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top