PIC16877A delay need help

Thread Starter

ericyeoh

Joined Aug 2, 2009
58
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.

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:

JDT

Joined Feb 12, 2009
657
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.
 

t06afre

Joined May 11, 2009
5,934
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.
 

Thread Starter

ericyeoh

Joined Aug 2, 2009
58
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.
 

t06afre

Joined May 11, 2009
5,934
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.
You are doing it much harder for your self, than needed. Hi-Tech C do have functions for delays.

__delay_ms(x) // request a delay in milliseconds​
__delay_us(x) // request a delay in microseconds
The usage may be like this
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 4000000
void main(void)
{
__delay_ms(100);
 }
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
 
Top