On Thu, 27 Feb 2003, Wouter van Ooijen wrote: > coding challenge: > > - 14-bit core PIC > - forget about paging and banking issues > - smallest possible code > - assembler > > TMR1 is a free-running 16-bit timer that can be read as two file > registers TMR1H and TMR1L. Note that there is no buffering in the > reading, so for 00FF you might read FF as low byte and then 01 as high > byte (just after the rollover). How about this for reading the clock: movf TMR1L,W ; grab TMR1 movwf TMR1L_buff movf TMR1H,W movwf TMR1H_buff ; Check for rollover. If the low byte increments from ff to 00 then ; there's a rollover and we need to advance the high byte by one count ; However, the low byte may be any value between 0xfd and 0xff at ; the point we read it above. If it was then, well we have a rollover. ; One way to capture this condition is by monitoring the upper bit ; of TMR1L. If it is high when we read it above, and low when we read ; it next (just below), then a rollover has occurred. In fact, we ; have about 255 cycles to capture this rollover condition - ; anywhere from the first read being 0x80 to 0xff and the second ; read being 00 to 0x7f. If interrupts are enabled, then we need ; to ensure no isr takes longer than 255 cycles. ; comf TMR1L,W ; andlw 0x80 skpz incf TMR1H_buff,F > > I have two file register bytes tH and tL that represent a TMR1 value > that lies in the future. I want to wait until this 'moment' has arrived > (or passed). Note that due to the above reading problem, the possibility > of interrupts and one more issue (think for yourself) it is no use to > loop and wait for a 'perfect match'. Looking for (tH,tL) >= > (TMR1H,TMR1L) won't do either, because the tH,tL 'moment' might be > beyond a roll-over from FFFF to 0000. > > Note that there are actually two challenges: first find a suitable > algorithm, then encode it in assembler. > > The application of such code is to create a timing loop with zero > cumulative error. Now check to see if you're close to the target value. if( abs(target - current) < threshold) you're close. If threshold is 255, then this untested code might do the trick: movf T1L,W subwf TMR1L_Buff,F movf T1H,W skpc incfsz T1H,W subwf TMR1H_buff,F skpnz goto close skpc incfsz TMR1H_buff,W goto not_close This can be optimized.... Scott -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics