Maker Pro
Maker Pro

PIC Delay

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
I 'm using a PIC microcontroller 18f2520 with CCS compiler,

I want to create a delay of 1 sec using Timer1. I have a crystal of 4Mhz and controller itself divides internally with 4,(Given in datasheet).

Now i use a prescaler of 8, and since it is a 16bit timer, i get a maximum delay of approx. 524ms.

I want to use a timer interrupt every 1 sec , How do i do that??

Regards,
Sid
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,613
Joined
Nov 17, 2011
Messages
12,613
Set timer1 such that the delay is 500ms. Have the timer call an ISR (interrupt service routine) that counts the number of interrupts and only after every second interrupt calls the intended function. This will result in a 1 second (2*500ms) call intervall for the function.

In fact, you don't have to "count" within the ISR. just set a flag at the fisrt interrupt. Clear the same flag at the second interrupt. Call the function only when clearing the flag.
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
Ahh yeah..tat was easy...thanx! myself should have thought about it!

My bad :(

thanks man once again!

Regards,
SId
 

foTONICS

Sep 30, 2011
332
Joined
Sep 30, 2011
Messages
332
Why dont you just force a value into the timer and compensate for the missing instruction times?
 

sid2286

Aug 24, 2011
102
Joined
Aug 24, 2011
Messages
102
oh I tired that, but as per my spec I'm using a pic18f2520 with 4Mhz crystal and max perscalar that is available is 8, on timer1 which is 16bit.(so max count is 65535)

So maximum delay I can provide is 524ms.

I cannot load anyother value.

:(

Rgrds,
Sid
 

foTONICS

Sep 30, 2011
332
Joined
Sep 30, 2011
Messages
332
Then just nest a timer that is a factor of the total time you need inside another loop which counts the number of times the inner loop runs
 
Top