Prescaling the clock and creating a delay

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
DaveRoberts
Posts: 4
Joined: Tue Nov 27, 2007 10:24 am
Contact:

Prescaling the clock and creating a delay

Post 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??

User avatar
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:

Post 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.

KeithSloan
Posts: 114
Joined: Fri Jul 27, 2007 10:50 am
Been thanked: 1 time
Contact:

Time delays in Assembler.

Post by KeithSloan »

For time delays in assembler I recommend PicLoops. It takes all the hardwork away http://www.mnsi.net/~boucher/picloops.html

Keith

DaveRoberts
Posts: 4
Joined: Tue Nov 27, 2007 10:24 am
Contact:

Post 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

User avatar
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:

Post by Benj »

Hello Keith

That software looks great. Wish I had something like that back when I was writing assembler code.

User avatar
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:

Post 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.

Post Reply