Replace PBP with Assembly concerning Timer0
Hello,
I was talking to some representatives of microchip personally and they recommended using the timer0, 1 or 2 to count instead of using a loop like
Code:
while x= y
N = N +1
WEND
due to the timer's resolution.
Using the above example how does the assembly replacement with timer0 or timer2 look like ?
Is this statement of MC true ?
What is your application ?
Hi,
It depends on your application. Using any of the timers you can do a counting at the background then depending on the elapsed period you can do other jobs. Normally all the timer registers are accessible through PBP itself. If you need a tight timebase then asm gives you a better control on the actual instruction cycle used. Timer0 may not have a start/stop control. It is clocked from a prescaler or Fosc (external also possible). When timer0 rollsover it sets a hardware interrupt flag TMR0IF. Even if you are not using interrupts you can poll this flag.
Mister_E has done a great utility to make all your timer/pwm/usart calculations a breeze. Find it here http://www.picbasic.co.uk/forum/atta...4&d=1162909841
For your example code something like this can be done in PBP:
Code:
' THIS IS MEANT FOR A 18f452 , 16F PICS HAVE DIFFERENT TIMER0 SETTINGS
X VAR BYTE ' SOME VARIABLE
Y VAR BYTE ' SOME VARIABLE
TIMER_ZERO VAR WORD ' VARIABLE TO STORE TIMER0
T0CON = %00000111 ' OFF,16BIT,PRESCALE=256
INIT:
TMR0H = 0 ' CLEAR THE HIGH BYTE OF TIMER ZERO (UPDATED WHEN TMR0L IS WRITTEN)
TMR0L = 0 ' CLEAR THE LOW BYTE OF TIMER ZERO
T0CON.0 = 1 ' START TIMER0
MAIN_LOOP:
' DO WHATEVER YOU WANT
IF X != Y THEN MEASURE ' IF X AND Y DIFFERS THEN GET THE READING
GOTO MAIN_LOOP
MEASURE:
T0CON.0 = 0 ' STOP TIMER0
TIMER_ZERO.BYTE0 = TMR0L ' READ LOW BYTE FIRST (TMR0H IS UPDATED)
TIMER_ZERO.BYTE1 = TMR0H ' READ HIGH BYTE FIRST
GOTO INIT ' DO IT OVER AGAIN
Please note that since you are checking the condition at the end of the loop, anything consuming time inside would give you erronious results. As for the resolution part it is best to keep the loop running and letting the timer count. The lower the prescale value the greater the resolution. But it seems that you would be doing other tasks as well if x and y are not SFRs or PORT registers. Cause your program need to do something with x and y otherwise they will never end the initially matched condition.
It is best that you post your requirements clearly so that friends here can comeup with something useful for you.
Why don't you use interrupts?
Hi,
I would prefer using interrupts.
Your TIMER_ZERO is already a word variable. So on a timer zero interrupt you can just increment the HighByte of the variable and normally dump the TMR0 to the lowbyte. This would make a pseudo 16bit timer. On a comparator interrupt, get the readings. It should not be very difficult in ASM . By the way what is your clock frequency and your timer0 prescaler value ? If you are using a high prescale then Darrel's Instant Interrupt may work.