I have pretty much difficulty studying how does the delay loop work. From the training kit sheets from the PIC Kit 2, it said something about the cycles,counts and clocks. But the explaination is really confusing and I kinda got stuck here. Sometimes they overlap each other.
Also, I noticed some goto $ + 1. What do they mean?
I have pretty much difficulty studying how does the delay loop work. From the training kit sheets from the PIC Kit 2, it said something about the cycles,counts and clocks. But the explaination is really confusing and I kinda got stuck here. Sometimes they overlap each other.
Also, I noticed some goto $ + 1. What do they mean?
it said something about the cycles,counts and clocks. But the explaination is really confusing and I kinda got stuck here.
Its not that bad -
First of all there is your Xtal or internal osc. frequency, let say 4meg.
The Pic chips system clock uses that frequency but after dividing it by 4, so the system clock is F/4 or 4meg / 4 = 1meg.
( you can work out what that is as a faction of a second )
Given these figures you can work out how many times it has to loop around to cause a delay of say 1ms.
For long delays, say 250ms, you often see the 'loop' made up of two or more loops within it.
However thats a bit much for most folk, so thats why you have those handy little delay calculators are mentioned earlier.
Ok, rereading back the tutorials supplied and with the help of the Microchip's tutorial webpage given just from a few posts back, I could calculate the number of cycles provided.
Right now it's easy for me to make a simple delay from nested loops which involves just going back to the loop twice, or using the "goto $ - 1" technique, which could be simpler.
However, I can't figure out how they get 249998 cycles from the "goto $ + 2" method..
However, on the second time round this loop, count1 is now set at 00, so it counts down by 256 time on all subsequent passes .
This means you have 1 pass at 4F x 5 and all others at FF x5.
This calculates to 1281, then x count2 of C4, which equals 250,000
As you can now see, count1 for the first pass is just used as an adjusting method
If you can, run your code in MPLAB-Debugger -Sim and open the StopWatch in the Debugger dropdown -this will let you see each step down to a single instruction.