1. 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.
    Dismiss Notice

Delay10KTCYx in C18

Discussion in 'Microcontrollers' started by Urahara, Jul 20, 2008.

  1. Urahara

    Urahara Member

    Joined:
    Jun 9, 2008
    Messages:
    157
    Likes:
    0
    Hi

    I hv a 4MHz crystal on my board. Would like to do a 0.1sec delay using the Delay10KTCYx function found in delay.h of C18. My calculation as follows :

    Instruction Cycle = 4,000,000/4 = 1,000,000 per sec
    No of 10KTCY needed for 1 sec = 1,000,000/10,000 = 100
    Therefore No of 10KTCY for 0.1 sec = 10

    I hence pass the value of 10 when I call this function.

    But it seems to be slower than the actual timing.

    Did I do wrong somewhere? Comments welcome. Thks!
     
  2. bryan

    bryan Member

    Joined:
    Jun 21, 2003
    Messages:
    416
    Likes:
    3
    Location:
    Vancouver, Canada
    I assume you are using the MPLAb stopwatch to test the timing?
     
  3. Urahara

    Urahara Member

    Joined:
    Jun 9, 2008
    Messages:
    157
    Likes:
    0
    nope...wrote the codes under MPLAB IDE environment and programmed a 18F4550 uC. The pgm basically waits for 0.1sec, displays the elapsed waiting time on a LCD, then waits again, etc. I think displaying it on the LCD introduces some overheads, but the difference is too huge.

    Am I correct in my calculations of 10x10KTCYx ? Thks!
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada

    Use one of the built in timers of the 18F4550 for accuracy. You can also use the ECCP module in "trigger special event" mode with the timer to get precise timing intervals.
     
    Last edited: Jul 20, 2008
  6. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    Your calculation is correct. Can you provide your code?
     
  7. Urahara

    Urahara Member

    Joined:
    Jun 9, 2008
    Messages:
    157
    Likes:
    0
    Here's the code :

    Code (text):

    #include <p18f4550.h>
    #include <delays.h>
    #include <string.h>
    #include <stdlib.h>
    #include <xlcd.h>

    #pragma config FOSC = XT_XT
    #pragma config PWRT = OFF
    #pragma config BOR = OFF
    #pragma config WDT = OFF
    #pragma config MCLRE = ON
    #pragma config PBADEN = OFF
    #pragma config LVP = OFF
    #pragma config USBDIV = 1

    #define CLEAR_DISPLAY 0b00000001 /* Clear Display */

    void main( void )
    {
        int subsecond=0;
        int second=0;
        char string1[8];
        char string2[2];
        char dot[]=".";
        int i;

        init();
        LCD_init();

        while(1)
        {
            Delay10KTCYx(10);  // Delay of 0.1sec

            if (subsecond<9) {
                subsecond+=1;
            }
            else {
                second+=1;
                subsecond=0;
            }

            WriteCmdXLCD(CLEAR_DISPLAY);
            itoa(second,string1);
            strcat(string1,dot);
            itoa(subsecond,string2);
            strcat(string1,string2);
            putsXLCD(string1);
        }
    }

     
    Hv timed the code with a stopwatch. The former starts to visibly lag slightly behind the stopwatch around 18-20 seconds onwards. Certainly the LCD, string conversion codes as well as the delay function call add some overhead, but seems pretty high.

    Or is my expectation wrong? ie,the overheads are not trivial. If so, is there a way to determine how much instruction cycle they would take so that I can factor these overheads into the delay?

    Thks!
     
  8. Urahara

    Urahara Member

    Joined:
    Jun 9, 2008
    Messages:
    157
    Likes:
    0
    Hi kchriste

    By internal timers, do you mean modules such as timer0, timer1? For timing to say 60secs at 0.1sec resolution, function Delay10KTCYx is not good enough?
     
  9. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    3v0 here. Yes I am sure that is what he means, TMR0 etc. The problem with the delay function is it create the delay by executing a fixed number of instructions. If interrupts are serviced the delay time is extended by the time used for the interrupts.

    If you setup a TMR timer it counts independantly of the code executed. Hardware independant of the processor count clock cycles. It is very accurate.
     
    Last edited: Jul 21, 2008
  10. Urahara

    Urahara Member

    Joined:
    Jun 9, 2008
    Messages:
    157
    Likes:
    0
    Hv written another set of codes that use Timer1.

    It is confirmed. The delay function is inaccurate, while that of Timer1 is, based on empirical observation between my stopwatch and the LCD output.
     
  11. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    One nice thing about using the timer is that you can do other work while the timer is counting.
     
  12. Urahara

    Urahara Member

    Joined:
    Jun 9, 2008
    Messages:
    157
    Likes:
    0
    yup, looking for an opportunity to try that out later.

    Thks!
     

Share This Page