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.

PIC16F84A delay generation and adjusting help

Status
Not open for further replies.

Tracid

Member level 3
Joined
Nov 1, 2006
Messages
57
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Neded, Slovakia
Activity points
1,756
Hello :)

It was 4 years ago when i last worked with PIC16F84A and that was only a few lessons in school. Yesterday my brother asked me to write a code to control some sort of machine. I managed to write the code and simulate it by using LEDs on outputs. The aim is to control four electromagnets and code time for they should be turned on. This is now at test phase and it is 2seconds (routine generated by online delay generator found on internet)
Here comes the hardest part for me as i was not working with PICs only a few times: we do not know how long the electromagntets should be energised so we decided to use pushbuttons to adjust time. The actual routine in ASM looks like this:

Delay
;1999996 cycles
movlw d'17'
movwf twosec_d1
movlw d'93'
movwf twosec_d2
movlw d'5'
movwf twosec_d3
Delay_0
decfsz twosec_d1, f
goto $+2
decfsz twosec_d2, f
goto $+2
decfsz twosec_d3, f
goto Delay_0

;4 cycles (including call)
return

this is only for fixed time(2sec). We want the time to be adjustable between 1-4secs in say 50msec steps using 4MHz crystal. In this routine it would be difficult for me since i had to insert one more variable and calculate which to modify to which value.... Could anybody recommend me a simple routine or idea how to make delay adjustable by pushbuttons (up down) between 1sec and 4secs? PIC16F84A, 4MHz, ASM code
thank you :))

Added after 2 minutes:

sorry for the unsorted code, i just copy pasted it and it got unsorted

Added after 1 hours 44 minutes:

the code doesnt need to be optimised, i do not need much space for the remaining code...
 

Hi,

Think the simplest way for your routine would be to have a 50ms delay.

Then for each press of the switch it increments a counter by 1.
( don't forget to debounce your switch by 20 -30ms)

After each increment, or when you want to 'run' - then copy the counter value down to the 50ms routine which should modified to do the 50ms delays x the copy value of the counter.
The copy value of the counter will be decremented to zero, but your orignal counter value should be preserved ready for the next push of the switch
 

    Tracid

    Points: 2
    Helpful Answer Positive Rating
hehhh what a simple idea :DD and it did not came up my mind :D Thank you very much :) I will make a constant 1sec routine then after that the counter x 50msec to do 1sec+ routine :)) Thank you again :)
 

OK, another little help i need... After the 1sec delay part of routine i put the following to count X times the 50msec delay:

movf M13Time,W
movwf M13Timì_TMP
movf M13Time_TMP,w (this one line may be unnecessary if previous alredy have the value in W, i put it just for sure)
btfsc STATUS,Z
goto $+4
call FiftyMsec
decf M13Time_TMP,F
goto $-4
return


So i just want to know if it is good, if i test the Z flag of STATUS whether the counter alredy reached zero? If i put a register into W and it equals to zero, will the Z flag become 1?
And sorry i just dont know how to TAB here on the forum so the code would look sorted :S
 

Hi,

I would do it this way - excuse coding - just done it here - dinner calling !

main rouitne...

movwf M13Time_temp ; load in copy of number of 50ms to do
call Extratime



Extratime call FifyMsec ............ ; do a 50ms delay
decfsz M13Time_temp,F ........... ; when done decrement file M13Time _temp and test if it is zero, (stores the decremented value back into the file)
goto Extratime ........... ; if not Zero do again
return ............ ; is zero so finished -return
 

:D :D thanks, much simpler :D i think i will use yours :p Otherwise, was i correct with my code? so when i move a register into W and its value is zero then it will set Z flag of SATUS?

Added after 7 minutes:

but your code makes an extra 50msec delay which is the very first. but if my counter is zero from the beginning so i do not need any 50msec delay then it is not good (so in my program i will do only 1sec{this is another routine} delay and no additional 50msec increments) ... however, my project is not critical so it doesnt make problem to afford that extra time:)
 

Hi,

Yes, your BSFSx is correct in its use, as you say the instruction before will have actually set the Zero bit if the register is 00.

I would actually do this test in the main code rather than do it in the delay subroutine - but thats just personal style.

While the use of the $+ or $- operators do seem handy, it can lead to problems, particulalry if you modify your code or say upgrade it to the larger chip which do things in 2 byte instructions etc.
Suggest you use 'goto label' instead.
 

hmm i checked the routine again and i think skip if clear is OK because i want to call FiftySec subrutine if the M3Time_TMP value is not zero. if that registers value reaches zero then i want goto $+4 ... Am i wrong?
 

Hi,

Yes you are right, - I amended my last reply a minute later when I realised I had misread your code - sorry about that.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top