Maker Pro
Maker Pro

PIC: need help with timer

V

Vitaliy

Jan 1, 1970
0
Hello,

I am running the simple program from here (I included the code at the
end for convinience):
http://www.pages.drexel.edu/~kws23/tutorials/PICTutorial/PICTutorial.html
I believe this is the code from some textbook, as I have seen it on
other websites as well.

I would like to doublecheck if my logic is correct.

In the following:

delay clrf tmr0 ; clear TMR0, start counting
again btfss tmr0, 0 ; if bit 0 = 1
goto again ; no, then check again
btfss tmr0, 1 ; if bit 1 = 1
goto again ; no, then check again
btfss tmr0, 2 ; if bit 2 = 1
goto again ; no, then check again
btfss tmr0, 3 ; if bit 3 = 1
goto again ; no, then check again
btfss tmr0, 4 ; if bit 4 = 1
goto again ; no, then check again
btfss tmr0, 5 ; if bit 5 = 1
goto again ; no, then check again
btfss tmr0, 6 ; if bit 6 = 1
goto again ; no, then check again
btfss tmr0, 7 ; if bit 7 = 1
goto again ; no, then check again
return ; else exit delay

1. It the following correct?
tmr0, 0 is true after 1 clock cycle
tmr0, 1 - 3
tmr0, 2 - 7
tmr0, 3 - 15
tmr0, 4 - 31
tmr0, 5 - 63
tmr0, 6 - 127
tmr0, 7 - 255

2. If I want the delay to last 5 clock cycles (for example) - do I
need to do the following? I tried running delay loop twice, and the
delay was indeed 2xdelay, but haven't tried the following yet.

delay1 clrf tmr0 ; clear TMR0, start counting
again btfss tmr0, 0 ; if bit 0 = 1
goto again ; no, then check again
btfss tmr0, 1 ; if bit 1 = 1
goto again ; no, then check again
return ; else exit delay

delay2 clrf tmr0 ; clear TMR0, start counting
again btfss tmr0, 0 ; if bit 0 = 1
goto again ; no, then check again
return ; else exit delay

delay3 clrf tmr0 ; clear TMR0, start counting
again btfss tmr0, 0 ; if bit 0 = 1
goto again ; no, then check again
return ; else exit delay

3. When doing question 1, shouldn't we account for the time it takes
to excecute an instruction (I guess we assume it takes 1 clock cycle
to excecute 'btfss' and 0 clock cycles to excecute 'goto'
instruction?

4. With PIC16F84A, TMR0 runs at 1/4 of crystal oscillator's speed and
then you need to use OPTION register to divide frequency (multiply
time) by 1/2(2), 1/4(4), etc. I could not run at 1/2(2), but all other
prescaler options worked fine. Any ideas why?

5. Going back to the previous question (q.4), I would like to have
higher frequency to have better resolution. I looked up on digikey's
website, and it looks like the highest I can have with PDIP package is
40MHz (which I think will be good enough for me). These are the
following chips that I was able to find: PIC18LF1220-I/P, PIC18LF1230-
I/P, PIC18LF1320-I/P, PIC18LF1330-I/P. A little bit of clarification:
I have Picstart Plus programmer with the latest firmware and would
like to use it without purchasing another one. I would like to
generate PWM signal w/ 50% duty cycle. I haven't gone through the
datasheet in details, but I think it should be doable. Any suggestions
to get the highest frequency?


Thanks,
Vitaliy

---------------------------------------------------------------------------------------------
Clock.asm

; FILE: Clock.asm
; AUTH: (Your name here)
; DATE: (date)
; DESC: 1.0 - Internal timer, blink LED every 32.8 msec
; NOTE: Tested on PIC16F84-04/P.
; 4 MHz crystal yields 1 MHz internal clock frequency.
; "option" is set to divide internal clock by 256.
; This results in 1 MHz/256 = 3906.25 Hz or 256 usec.
; tmr0 bits 0 through 7 (255 decimal) is checked, thus yielding
; 255*256 usec = 65.28 msec delay loop
; REFs: Easy Pic'n p. 113

list p=16F84
radix hex

;----------------------------------------------------------------------
; cpu equates (memory map)
portB equ 0x06 ; (p. 10 defines port address)
tmr0 equ 0x01
;----------------------------------------------------------------------

org 0x000
start clrwdt ; clear watchdog timer
movlw b'11010111' ; assign prescaler, internal clock
; and divide by 256 see p. 106
option
movlw 0x00 ; set w = 0
tris portB ; port B is output
clrf portB ; port B all low
go bsf portB, 0 ; RB0 = 1, thus LED on p. 28
call delay
call delay
bcf portB, 0 ; RB0 = 0, thus LED off
call delay
call delay
goto go ; repeat forever

delay clrf tmr0 ; clear TMR0, start counting
again btfss tmr0, 0 ; if bit 0 = 1
goto again ; no, then check again
btfss tmr0, 1 ; if bit 1 = 1
goto again ; no, then check again
btfss tmr0, 2 ; if bit 2 = 1
goto again ; no, then check again
btfss tmr0, 3 ; if bit 3 = 1
goto again ; no, then check again
btfss tmr0, 4 ; if bit 4 = 1
goto again ; no, then check again
btfss tmr0, 5 ; if bit 5 = 1
goto again ; no, then check again
btfss tmr0, 6 ; if bit 6 = 1
goto again ; no, then check again
btfss tmr0, 7 ; if bit 7 = 1
goto again ; no, then check again
return ; else exit delay

end
;----------------------------------------------------------------------
; at burn time, select:
; memory uprotected
; watchdog timer disabled
; standard crystal (4 MHz)
; power-up timer on
 
T

Tom2000

Jan 1, 1970
0
2. If I want the delay to last 5 clock cycles (for example) - do I
need to do the following? I tried running delay loop twice, and the
delay was indeed 2xdelay, but haven't tried the following yet.

If you want a 5 cycle delay, throw in some nop instructions. Don't
mess with a timer.

The proper way to utilize TMR0 for delays is to set your prescaling
and postscaling, start the timer, then preset TMR0 register's value
for (256-desired number of ticks), perhaps accounting for the 2
instruction cycles it takes to load TMR0. Clear the T0IF flag bit.

When the timer is running, poll T0IF. When the timer rolls over,
this flag is set to 1.

In the code you presented, I'm not sure what the author is doing,
or why. Don't follow his or her example.

Your Picstart+ programmer should handle all the 18F processors
with no problem.

Remember: the data sheet is your friend!

Good luck!

Tom
 
Top