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??
Prescaling the clock and creating a delay
-
- Posts: 4
- Joined: Tue Nov 27, 2007 10:24 am
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 114
- Joined: Fri Jul 27, 2007 10:50 am
- Been thanked: 1 time
Time delays in Assembler.
For time delays in assembler I recommend PicLoops. It takes all the hardwork away http://www.mnsi.net/~boucher/picloops.html
Keith
Keith
-
- Posts: 4
- Joined: Tue Nov 27, 2007 10:24 am
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Hello Keith
That software looks great. Wish I had something like that back when I was writing assembler code.
That software looks great. Wish I had something like that back when I was writing assembler code.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel