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 sec delay in pic16F877A

Status
Not open for further replies.

ranasinghe.um

Newbie level 3
Joined
Jan 21, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,313
please can some one tell how to get an one sec delay in PIC16F877A.
i'm glad if anyone can post me the assembly coding for that.

i used the following cording to get that delay.but unfortunately it was too fast.

1st i defined two counters and put the following function to call when I want delay.

delay
decfsz counterL,1
goto delay
decfsz counterH,1
goto delay

return


please help me to come up with this..because im stucked with this problem. :???:
 

;PIC Time Delay = 1.0000000 s with Osc = 4.000000 MHz

DelayOneSecond
movlw D'6'
movwf CounterC
movlw D'24'
movwf CounterB
movlw D'168'
movwf CounterA
loop
decfsz CounterA,1
goto loop
decfsz CounterB,1
goto loop
decfsz CounterC,1
goto loop
retlw
 
Code:
; Delay = 1 seconds
; Clock frequency = 4 MHz

; Actual delay = 1 seconds = 1000000 cycles
; Error = 0 %

	cblock
	d1
	d2
	d3
	endc

			;999997 cycles
	movlw	0x08
	movwf	d1
	movlw	0x2F
	movwf	d2
	movlw	0x03
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;3 cycles
	goto	$+1
	nop

; Generated by http://www.golovchenko.org/cgi-bin/delay (December 7, 2005 version)
; Sat Jan 22 09:25:39 2011 GMT

Here is the code generated by the site in the code.
 

If you intend to use delay several times, will waste microcontroller processing.
The best alternative is to use delay under interrupt conception.

+++
 

If you intend to use delay several times, will waste microcontroller processing.
The best alternative is to use delay under interrupt conception.

+++


I do not follow your reasoning

How will this waste processing time, if it is a routine that is called?
Using the interrupt would create an issue when you wanted a sec delay, what would you use for the interrupt trigger?
 

It's a blocking routine, what Andre_teprom means is that while in the delay loop you are wasting time that could be used more profitably.
If you use one of the hardware timers to generate periodic interrupts, you can create almost any delay you want and still run other tasks at the same time.
Looking at it from the opposite angle, if you use software loops to create delays and you also use interrupts for something else, every interrupt will make to delay longer than you want it to be.

Brian.
 

It's a blocking routine, what Andre_teprom means is that while in the delay loop you are wasting time that could be used more profitably.
If you use one of the hardware timers to generate periodic interrupts, you can create almost any delay you want and still run other tasks at the same time.
Looking at it from the opposite angle, if you use software loops to create delays and you also use interrupts for something else, every interrupt will make to delay longer than you want it to be.

Brian.


I understand that whilst it doing the delay the pic is doing nothing else, but the original post only states that they want a 1 sec delay, but not in what context (from his status and the nature of the question, I would assume he is a beginner and interrupts may be too in depth at this moment in time).

John
 

thank you evrybody..means a lot..
can anyone tell me how to calculate that time delay using counter variable..
 

var1 equ 0Dh
var2 equ 0Eh // define the variable in registers

your code



call delay

Delay movlw 0xFF ;move 255 to w reg
movwf var1 ;move 255 w reg to var1
movwf var2 ;move w reg to var2
Loop1 decfsz var1,1 ;check var1 is zero skip if zero
goto Loop1 ;goto loop 1
decfsz var2,1 ;check var2 is zero skip if zero
goto Loop1
return
 

Hi

If you use the code above (in my earlier post), you should be able to get an idea of how the calculation works.

On a pic running at 4mHz, most instruction take 1 clock cycle to execute (1uS). There are a few that take 2, ie Call, Return and Goto are a few. If you download the data sheet for the pic, they contain the instruction set and the time taken to complete.

Using the above details you can calculate how long each loop run will take to process, and then calculate the overall time to complete.

If you use this link **broken link removed**, you can play around with delay loops and look at the code produced to achieve the required delay.

Hope that helps
 

visit

compizspec.blogspot.com
may be helpful
 

u can also use built in function in mikro c pro compiler

Delay_ms(2000);

ms=milli seconds... just calculate and insert it in the function ..............

---------- Post added at 16:28 ---------- Previous post was at 16:28 ----------

u can also use built in function in mikro c pro compiler

Delay_ms(2000);

ms=milli seconds... just calculate and insert it in the function ..............
 

u can also use built in function in mikro c pro compiler

Delay_ms(2000);

ms=milli seconds... just calculate and insert it in the function ..............

Yes you can, but the original post is using assembler, so it would appear like that is the route he wants to take. And his request is for assembly code.

---------- Post added at 11:47 ---------- Previous post was at 11:29 ----------

visit

compizspec.blogspot.com
may be helpful

The only problem with your site is that it all appears to be using 'C' and as previously stated the OP is after assembler. Your site does not really explain anything it just contains some code with the 'C' delay commands in them.
 

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, if, while, 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.
 

thanks alot everyone. I think i can manage it now, cz of ur helps. thank you very much.. :lol:
 

One better approach is to avoid the uP to hang only in the delay routine and just to wait the 1000ms period to expire. The best solution is to use RTOS - in this case you can run several programs into a single uP (I had in one of my projects with 16F876A 6 inndent tasks simultaneosly running). The best RTOS for PIC uP is SALVO but you can find also many other RTOS (some even free). You'll be surprised how powerful becomes the uP when you use RTOS and also your productivity will jump because all programs become simpler and easier for maintanance.

Once you start using RTOS you'll find out that even without RTOS you could create a program structure which makes possible many different tasks to executed in the same time. The main challenge is to convert the structure of the program in such way that it always passes through some common point (you should not loop in single place or branch). In this place you can put a check for the timer and if 1s expired ypu can execute something.

The structure of the program should look like:
Code:
void main(void)
{
  while(1)
{
  if (TIMER1_Overflow)
 {
   ResetFlagTimer1();
   prescall++;
   if (prescall > TIME1000ms)
   {
      prescall=0;
     //--------- here we pass every 1000ms
     DoSomething();
   }
  
  DoTask1();
  DoTask2();
  .....
 } 
}  // endless loop

}

Separate tasks like DoTask1() could have individual state machine and they never hold the program flow (like wait 1000ms) for long time. It will take some time to modify your program but later you'll enjoy smooth performance
 

You'll be surprised that SALVO is not that expensive you you're a student or you work for the university. There is an academic version which is about 150$ but you need to claim for it. It's fully functional version without any limitations but has no sources (only compiled libraries).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top