EmbeddedRelated.com
Forums

How do i create an accurate delay

Started by hybrid_snyper February 14, 2007
On Feb 15, 9:23 pm, David Kelly <n...@Yahoo.com> wrote:
> hybrid_snyper wrote: > > On Feb 15, 4:57 pm, David Kelly <n...@Yahoo.com> wrote: > > >> The first thing to do is to understand the assignment. > > >> Is the loop to repeat every 8us? Just what does "8us occurs before the > >> loop continues" actually mean? 8us between what two events? > > >> Looking in from afar methinks this is not a job for C. That the entire > >> loop needs to be coded in assembly. Last looked at C compilers for the > >> 'F876 family 6 years ago and was not impressed. Never doubted the code > >> would run, just that it was huge. When the compiler issues 10 or 20 > >> instructions for a simple line of C you are not going to be able to hold > >> to 8us timing. > > > the loop is a for loop where, the loop repeats it self dependant on an > > array size. Inside the loop i require RA0 to go high for 8us then low. > > then 7 bits on port B will change. RA0 is signaling a clock and with > > each clock the data on port b changes. The reason for the small time > > delays is to have some sort of control over the refresh rates. > > So the loop doesn't have to repeat every 8us. That loosens things up to > where C is still viable. > > If RA0 is an output strobe then I suggest you think some more about > timing and how something else will be reading your 'F876. I think you > ought to write the 7 bits on port B *then* strobe RA0 for 8us. > > > ASM is new to me so will have to work at it and see what i can come up > > with. > > You should be able to slip the 8 NOP's right in the middle of your C > code something like this: > > for(;;) { > PORT_B = new_value; > RA0 = 1; > ASM("nop"); // 1 > ASM("nop"); // 2 > ASM("nop"); // 3 > ASM("nop"); // 4 > ASM("nop"); // 5 > ASM("nop"); // 6 > ASM("nop"); // 7 > ASM("nop"); // 8 > RA0 = 0; > } > > Of course it varies with different compilers. Dig into your manuals for > the exact syntax your compiler uses. > > And if you want to be picky, 8 NOPs may produce a 9us or longer delay > depending on how the compiler does the RA0 assignments. Even in pure > assembly 7 NOPs would be proper for the delay assuming the output bit > sets at the same phase of the system clock as it clears. One of your > delay clocks is in the set or the clear. > > IIRC the 'F876 has some sort of look ahead that might see a NOP and skip > it in 0 clock cycles. Or maybe I'm thinking of a 0 clock cycle loop > branch instruction?
I've seen that written down somewhere in one of the books ive been looking through, will have a closer look, didn't realise what i was reading may be of some help. thanks for that. I will get back to you all and let you know how i get on.
In article <1171580488.012603.294000@l53g2000cwa.googlegroups.com>,
 "hybrid_snyper" <wayne_tams@hotmail.com> wrote:

> On Feb 15, 9:23 pm, David Kelly <n...@Yahoo.com> wrote:
<big snip>
> > I will get back to you all and let you know how i get on. >
For what it's worth, in the compiler I use, the C directive is: delay_us(8); to give an eight microsec delay in any PIC. The compiler handles all of the peculiarities of each PIC. The proper compiler for the job can make it much easier. Cost, or course, is a factor. Al