PIC16877A delay need help

Discussion in 'Embedded Systems and Microcontrollers' started by ericyeoh, Feb 18, 2011.

  1. ericyeoh

    Thread Starter Active Member

    Aug 2, 2009
    58
    0
    Guys,i;m using PIC16F877A with crystal value of 20Mhz and High speed frequency.
    How am i going to create a 50mili second delay? if i want to use delay looping with the NOP();.

    unsigned int a;

    For(a=0;a=<10000;a++)
    {
    NOP();
    }

    Is there any ways to calculate the number of looping the NOP to create a 50milisecond?
    i;m using C language to write the code.
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016

    You will need to use trail and error to get 50ms. The problem is the for loop. You cannot be certain of how many instructions it is processing.

    You could view the assembly and count the instructions.

    Each instruction consumes 4 clock cycles. So a with a 20mhz clock, each instruction would take 5MHZ or 5us.
     
    Last edited: Feb 18, 2011
  3. JDT

    Well-Known Member

    Feb 12, 2009
    658
    85
    The best way is to use a timer to generate regular interrupts. Say every 1ms. In the interrupt code have a register that decrements every interrupt until it reaches zero. Then stops.

    In your main code if say you wanted a 50ms delay, set this register to 50 and wait until it reaches zero.

    Doing it this way also means you can do other things in your code (if needed) while waiting. As long as your main loop comes round and checks the state of this register regularly.
     
  4. JDT

    Well-Known Member

    Feb 12, 2009
    658
    85
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Most C-compilers. Have functions for delays. Which C compiler do you use? The latter functions may be good none critical timing functions. However if you want to control say a sample interval. You should go for a solution using a timer and a ISR.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Thanks for the catch! Too early in the morning for me. :) Now that I think of it that is a bit slow for 20mhz :)
     
  7. ericyeoh

    Thread Starter Active Member

    Aug 2, 2009
    58
    0
    i;m using Hi-Tech PIC compiler.

    In the delay.h library, i change my xctal frequency to 20Mhz and the delay function is inaccurate. Thats is why i try to use for lopp with NOP();

    If the instruction cycle is 5Mhz, how should i write in C code to provide 50mS delay?
    I've seen PIC delay calculator which can find easily in the web, but all the code is generated in asm format.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    I explained the reason for using asm above. With C, you results will be unpredictable. You will need to experiment if you want to do only C. But just because it works now doe not mean it will work in the future, should you upgrade your compiler or change compilers.

    HiTech should allow for inline ASM.
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Sorry I was totally screwed up on my math this morning. Your instruction clock will indeed be 5MHZ but that is a period of .2usec or 0.0002ms .


    I hope I got it right this time.

    Period = 1 / Frequency.
     
  10. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You are doing it much harder for your self, than needed. Hi-Tech C do have functions for delays.
    The usage may be like this
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 4000000
    4. void main(void)
    5. {
    6. __delay_ms(100);
    7.  }
    No need for the dealy.h
    Which version do you use of the compiler. If you only are a hobbyist using the free version. You can always download the latest version from here. Up to around version 9.6x it was some problems with the delay functions then using long delays. But it is fixed now. If you are a MPLAB user. I will strongly recommend reading the quickstart.pdf in the docs folder ....(install folder)\HI-TECH Software\PICC\9.81\docs. And of of course the manual.
    http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en542849
     
Loading...