Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

1 second delay using PIC Microcontroller

Status
Not open for further replies.

unkn0wn

Junior Member level 3
Junior Member level 3
Joined
Sep 5, 2006
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,482
pic delay

I am using 4 MHz crystal and PIC16F84A.

I need a subroutine for 1 second Time delay. Can anyone please assist me?

I can't use sublw and subtract 3906.25 due to 8-bit registers.

Please, also explain the code if you can.

I would be very thankful.
 

delay pic

The two most common approaches to this are kill time or interrupt.

By killing time, just write a nested subroutine to count down for the appropriate number of cycles. For example:

clrf delay0
decfsz delay0 ,f
goto $-1

will use 768 cycles. When nested:

clrf delay1
clrf delay0
decfsz delay0 ,f
goto $-1
decfsz delay1 ,f
goto $-4

will use 197,376 cycles.

By using an interrupt, other processes can be taken care of. This involves using TMR0 to generate a timer overflow interrupt. I would set it for 1mS resolution, and count the number in two bytes. When 1000 timed interrupts have occurred then the delay is satisfied and the process in question can continue.
 

    unkn0wn

    Points: 2
    Helpful Answer Positive Rating
pic delay generator

you can try the code delay generator :
 
assembly delay loop

use ccsc compiler to use delays of desired value delay_ms(1000)..etc
 

pic16f84a delay

unkn0wn said:
I am using 4 MHz crystal and PIC16F84A.

I need a subroutine for 1 second Time delay. Can anyone please assist me?

I can't use sublw and subtract 3906.25 due to 8-bit registers.

Please, also explain the code if you can.

I would be very thankful.

Think you are using Assembly, yes, for a long delay, it is not easy to calculate the proper delay loop. With PIC16F84, we have the other way is using interrupt, but it is not really easy for beginner either.

Using delay loop in assembly, we can get the most accurate time delay, but that keeps the CPU always busy.

Fortunately, Mr. Boucher provides PIC people with his excellent freeware for calculating delay loop for PICs, you can download the software of only 2MB from the link:
www.mnsi.net/~boucher/picloops.html
Using this software, you can "trim" the loop, or see how to trim the code, to get the best solution for your project.

This is the calculation result from this software:

;PIC Time Delay = 0,9999990 s with 999,999 machine cycles, Osc = 4 MHz include Call + return.
movlw D'6'
movwf CounterC
movlw D'24'
movwf CounterB
movlw D'167'
movwf CounterA
loop decfsz CounterA,1
goto loop
decfsz CounterB,1
goto loop
decfsz CounterC,1
goto loop
return


If you set Counter A = 168, then the delay will = 1.0000020 s with 1,000,002 machine cycles.

Good luck and Happy programming.
nguyennam
 

1 second delay pic

You can also use interrupt based timer routines to increment a counter inside ISR and check for 1 second elapse. You can then set a flag which can be processed in your main routine. I am attaching a free utility programme by Steve (www.mister-e.org) which is very handy in calculating interrupt timings for timers. It gives you loads of other features as well. It is mainly intended for PicBasic Pro for which it generates some codes too. However it will be helpful for calculating other stuffs as well. Check it out and do not forget to thank the author for the nice job.
Regards
 

Attachments

  • picmulticalc_v_1_3_1_2069.zip
    118.2 KB · Views: 397
Think you are using Assembly, yes, for a long delay, it is not easy to calculate the proper delay loop. With PIC16F84, we have the other way is using interrupt, but it is not really easy for beginner either.

Using delay loop in assembly, we can get the most accurate time delay, but that keeps the CPU always busy.

Fortunately, Mr. Boucher provides PIC people with his excellent freeware for calculating delay loop for PICs, you can download the software of only 2MB from the link:
www.mnsi.net/~boucher/picloops.html
Using this software, you can "trim" the loop, or see how to trim the code, to get the best solution for your project.

This is the calculation result from this software:

;PIC Time Delay = 0,9999990 s with 999,999 machine cycles, Osc = 4 MHz include Call + return.
movlw D'6'
movwf CounterC
movlw D'24'
movwf CounterB
movlw D'167'
movwf CounterA
loop decfsz CounterA,1
goto loop
decfsz CounterB,1
goto loop
decfsz CounterC,1
goto loop
return

If you set Counter A = 168, then the delay will = 1.0000020 s with 1,000,002 machine cycles.

Good luck and Happy programming.
nguyennam
===========================
thanks for help! :D

unfortunately, i still cannot comprehend how to solve this example.:?:
can you help me in solving that delay subroutine?.. :!:
:cry:
 

Hi,

Barry Allen unfortunately, i still cannot comprehend how to solve this example.Question
can you help me in solving that delay subroutine?.. Exclamation
Crying or Very sad

Its whats called a nested loop.

First you establish your oscillator frequency - 4Mhz divided by 4 to give the pic clock cycle of 1 Mhz which is the speed most instructions are executed at, although some can take 2 or 3 cycles.

All that then happened is a series of decrementing counters occurs to count down to the 1second or 1,000,000 instruction cycles

Perhaps it will be clearer if you look at a smaller example, this code is generated for a delay of 0.0001 seconds from a 4Mhz osc

The counter is preloaded to hex21 and then decremented untill it reaches hex 00.
which is 100 instruction cycles exactly.

The nested loop just builds on this simple loop


;100 cycles
movlw 0x21
movwf d1

Delay_0:
decfsz d1, f
goto Delay_0
 

Some time ago I wrote a utility to help write assembler code for Pic16 series micros and posted it on eda board here.



It can generate code for delays of various lengths, loops conditionals, etc.

No install is needed, just extract all the files to a directory. Call the directory PicHelp or whatever you like.
Generates code for common constructs in programming.

Please give it a go and see if it helps.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top