Page 1 of 1
Prescaling the clock and creating a delay
Posted: Tue Nov 27, 2007 10:34 am
by DaveRoberts
Hello,
I am totally new to PICS and would like to create a time delay of between 5 or 10 seconds. Obviously with a clock speed of 3MHz a simple count is a big number. I am aware that the clock can be prescaled to give a 1:256 ratio meaning the count would be substantially less. The only problem is I do not know how to write the code for this. Can anyone help??
Posted: Tue Nov 27, 2007 10:48 am
by Benj
Hello
The easiest way to create a second delay is to create a function with a byte and an integer variable.
byte - inner_loop
int - outer_loop
then do something similar to the following.
for (outer_loop=0;outer_loop<3000;outer_loop++)
{
for (inner_loop=0;inner_loop<250;inner_loop++);
}
//Note this code is designed to work with a 3MHz crystal.
To create delays of more then one second simply add another for loop around the second delay structure.
If you need a very precise second timer then your better off using the Timer0 interrupt.
Time delays in Assembler.
Posted: Tue Nov 27, 2007 7:44 pm
by KeithSloan
For time delays in assembler I recommend PicLoops. It takes all the hardwork away
http://www.mnsi.net/~boucher/picloops.html
Keith
Posted: Wed Nov 28, 2007 8:10 am
by DaveRoberts
This does not seem familiar to me. I did forget to mention that I am using the PIC16F84A. I assume the code you have sent me here is in C?
Dave
Posted: Wed Nov 28, 2007 10:23 am
by Benj
Hello Keith
That software looks great. Wish I had something like that back when I was writing assembler code.
Posted: Wed Nov 28, 2007 10:31 am
by Benj
Hello Dave
The example code that Keith sent is in assembler. It should work fine with MPLAB or other similar PIC compilers. It will also work with your 16F84A chip.