Results 1 to 9 of 9

Thread: How do i measure the time between each C-code instruction and time taken to....

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    2

    How do i measure the time between each C-code instruction and time taken to....

    Hi ....

    I am a new to this micro-controller world and just practiced few basic programs using 8051 and PIC.
    My project is on temp sensing using ds1820 i.e 5 ds1820 IC's on single wire. i'm using PIC18f452-12MHz Xtal, MPLAB. I need to achieve 1usec time delay.
    PIC18f takes 4 clock cycles per instr. That means each instr will take 3MHz = 0.33usec. Using this value i made a delay for-loop with value 3 to achieve 1usec delay. But i;m not sure what exactly is the delay inside the PIC when i program it. I don't have a CRO to measure the delay.
    So please please help me

    My concern is ....
    'How do i measure the time between each C-code instruction and time taken to get into and come out of any function(returning or non returning any value)....'

  2. #2
    Moderator
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    83
    Rep Power
    2
    Hi Abdul

    Let's not go deep into the architecture and associated data sheets of microcontroller PIC18F452 to address your concern.

    The source code program which you have developed is translated to the target machine language by the assembler. Assuming that you have coded it in a C compiler targeting the PIC series of microcontrollers, the compiler itself compiles the entire source code to the target machine language, allocates memory for storing program, data, stack etc. Thereafter, the microcontroller executes only the machine language code and the clock cycles needed for executing each instruction generally varies from instruction to instruction and controller to controller.

    It is a standard programming practice to create a delay loop by assigning NOP (No operation) for specified number of clock cycles such that the programmer generates the desired delay. In your case, the 1 micro second delay will be achieved by performing 'No OP' for three clock cycles.

    The other probable delay inside the microcontrller is only the propagation delay, which is almost negligible for all practical purposes.

    The best way to measure delay using an oscilloscope is to generate a square wave of desired values (peak, delay) by writing small piece of code, capture the waveform in an oscilloscope and measure the delay thereafter. By doing so, you can compare the expected delay from the program and actual delay observed on the oscilloscope to draw inferences.

  3. #3
    Newbie
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Use keil

    Quote Originally Posted by Vidya1967 View Post
    Hi Abdul

    Let's not go deep into the architecture and associated data sheets of microcontroller PIC18F452 to address your concern.

    The source code program which you have developed is translated to the target machine language by the assembler. Assuming that you have coded it in a C compiler targeting the PIC series of microcontrollers, the compiler itself compiles the entire source code to the target machine language, allocates memory for storing program, data, stack etc. Thereafter, the microcontroller executes only the machine language code and the clock cycles needed for executing each instruction generally varies from instruction to instruction and controller to controller.

    It is a standard programming practice to create a delay loop by assigning NOP (No operation) for specified number of clock cycles such that the programmer generates the desired delay. In your case, the 1 micro second delay will be achieved by performing 'No OP' for three clock cycles.

    The other probable delay inside the microcontrller is only the propagation delay, which is almost negligible for all practical purposes.

    The best way to measure delay using an oscilloscope is to generate a square wave of desired values (peak, delay) by writing small piece of code, capture the waveform in an oscilloscope and measure the delay thereafter. By doing so, you can compare the expected delay from the program and actual delay observed on the oscilloscope to draw inferences.


    Here As you don't have an OSC I would suggest you using some free software's like KEIL and test the register values and then use the counter/timers in it to have the delay u required.
    That would be the best possible way u can go a head...

    Thanks,
    Manoj

  4. #4
    Moderator Knewron's Avatar
    Join Date
    Nov 2012
    Location
    Pune
    Posts
    398
    Rep Power
    2
    There is another way which might be helpful in finding out...that is - sifting through listing file (.lss), it will have assembly codes with mnemonics mentioned (basically assembly program converted from C program by compiler). There you can find which exact instructions are being used for delay and since you know time needed for each instruction, you can find out actual delay being burned into the microcontroller.
    Engineers are not made for complicating things...but to simplify !
    www.knewron.co.in
    Do you like our responses...if yes, please like us on Facebook

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    2

    .lss file Which IDE has this option..???

    Quote Originally Posted by Knewron View Post
    There is another way which might be helpful in finding out...that is - sifting through listing file (.lss), it will have assembly codes with mnemonics mentioned (basically assembly program converted from C program by compiler). There you can find which exact instructions are being used for delay and since you know time needed for each instruction, you can find out actual delay being burned into the microcontroller.
    I was waiting for this response... But can you specify where can i get this .lss file ... i mean which compiler generates it.? As i use MPLAB for PIC... If any other IDE for any micro-controller... Please specify...

  6. #6
    Moderator Knewron's Avatar
    Join Date
    Nov 2012
    Location
    Pune
    Posts
    398
    Rep Power
    2
    where can i get this .lss file
    The .lss file is generated by the compiler during compiling the program and converting it into .hex file. You should check (should have checked already) the directory in which your .hex file is located, in the same directory this listing file should be present. It may be with another extention too. As we don't use MPLAB, can't tell which file exactly, but with Keil and AVR Studio, the extension is .lss.

    Link: https://www.google.co.in/url?sa=t&rc...44770516,d.bmk
    Engineers are not made for complicating things...but to simplify !
    www.knewron.co.in
    Do you like our responses...if yes, please like us on Facebook

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    2

    Can anyone.. give me code for a perfect 1usec or 2usec Delay using 12Mhz-18f ,MPLAB..

    Quote Originally Posted by Abdul View Post
    Hi ....

    I am a new to this micro-controller world and just practiced few basic programs using 8051 and PIC.
    My project is on temp sensing using ds1820 i.e 5 ds1820 IC's on single wire. i'm using PIC18f452-12MHz Xtal, MPLAB. I need to achieve 1usec time delay.
    PIC18f takes 4 clock cycles per instr. That means each instr will take 3MHz = 0.33usec. Using this value i made a delay for-loop with value 3 to achieve 1usec delay. But i;m not sure what exactly is the delay inside the PIC when i program it. I don't have a CRO to measure the delay.
    So please please help me

    My concern is ....
    'How do i measure the time between each C-code instruction and time taken to get into and come out of any function(returning or non returning any value)....'


    hi, all...

    Can anyone.. give me code for a perfect 1usec or 2usec Delay using 12Mhz-PIC18f ,MPLAB.. Which is "verified using CRO.." Please..

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    2
    Quote Originally Posted by Abdul View Post
    hi, all...

    Can anyone.. give me code for a perfect 1usec or 2usec Delay using 12Mhz-PIC18f ,MPLAB.. Which is "verified using CRO.." Please..

    after requesting and running from pillar to post i found 1 CRO.

    this is the verified code...
    void main(void)
    {
    int i;
    TRISBbits.TRISB7 = 0;

    while(1){

    PORTBbits.RB7=0;
    //for(i=0;i<3;i++);
    DS1820_DelayUs(3);
    PORTBbits.RB7=1;
    //for(i=0;i<3;i++);
    DS1820_DelayUs(3);
    }
    }


    void Delay(unsigned char a)
    {
    int i,j;
    for(i = 0; i < a; i++)
    for( j = 0; j < 3; j++);
    }

    void DS1820_DelayUs(int val)
    {
    int k;
    for(k=0;k<val;k++);
    }

    Delay(1); 52usec LOW AND 52USEC HIGH

    Delay(2); 88USEC LOW 88USEC HIGH
    Delay(3); 125USEC LOW 125USEC HIGH
    36 USEC INRCM FOR EACH SINGLE DELAY WITH FUNCTION CALLING TIME 16USEC
    **********************************
    PORTBbits.RB7=0;
    i=7;
    PORTBbits.RB7=1;
    i=5; // 1.4usec low and 2.1usec high.

    **********************************

    for(i=0;i<1;i++); 13usec LOW AND 14USEC HIGH
    for(i=0;i<2;i++); 19usec LOW AND 20USEC HIGH
    for(i=0;i<3;i++); 26usec LOW AND 26USEC HIGH

    EACH FOR LOOP COUNT IS 7USEC DELAY

    **********************************

    DS1820_DelayUs(1); 25usec low and 26usec high
    DS1820_DelayUs(2); 35usec low and 36usec high
    DS1820_DelayUs(3); 44usec low and 45usec high


    ******/

  9. #9
    Newbie
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    1us

    for 1us just put:

    NOP
    NOP
    NOP

    in your program.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •