PDA

View Full Version : timer1 help



robertpeach
- 9th September 2009, 15:04
hey sorry need some help on this... been working with timer1 to make leds blink from someone code example in the past. unforunately im a bit stuck


define OSC 20

TRISA = 0
TRISB = 0


PORTA = 0
PORTB = 0

T1CON = %00110001 ' 0.524ms
TMR1IF VAR PIR1.0 ' Overflow bit of Timer1.

Output1 VAR PORTB.1 ' Blink 6secs.
Output2 VAR PORTB.0 ' Blink 0.5secs.

PreLoad VAR WORD
TimeCount VAR BYTE
TimetoBlink VAR BYTE

PreLoad = 0 ' to get 500ms.
TMR1H = PreLoad.HighByte

TimeCount = 0
TimetoBlink = 12 '12 = 6secs.

PAUSE 50 ' OSC to Settle.


Start:


IF TMR1IF THEN Blink

GOTO Start



Blink: 'Each int is 0.500 sec.
TMR1L = PreLoad.LowByte ' Load the timer with preload value.
TMR1H = PreLoad.HighByte
TimeCount = TimeCount + 1 ' Count interrupts.
Output2 = Output2 ^ 1 ' Toggle fast blinking output.

IF TimeCount = TimetoBlink THEN '6Secs ON, 6secs OFF.
TimeCount = 0
Output1 = Output1 ^ 1
ENDIF

TMR1IF = 0 ' Clear TMR1 int. flag.

GOTO Start

END

unfortunately the blinks are far too fast.... about 8 to 10x too fast.

my pic is oscillating at 20mhz so i thought id just use that internal oscialltion. when u choose that oscillator it does Fosc/4 so its at 5mhz. with a word size timer1 i can measure up to 65536 ticks; with a 1:8 prescaler on that only takes about 0.7 seconds or something to complete. how do i get it so it will be 6 seconds? cant change prescaler anymore... and preloader wont make a difference since it only speeds up the cycle. i think anyway

any help on this? thanks

Melanie
- 9th September 2009, 15:34
If you run out of Ticks then you simply run out of Ticks... because somebody will come along and say "How can I have Timer1 count an HOUR or a DAY or a WEEK"?

Eventually you run out of capacity for the Registers for the Time period you wish to count. There is nothing you can directly do about that...

So the usual solution is to preset the Timer to count say 0.5 Seconds (which is within range), and when the period has elapsed you simply increment a BYTE. When that BYTE has incremented to 12 (for 12 x 0.5 Seconds = your desired 6 Seconds), zero the byte (for the next 6 Second timing period) and toggle your LED. So basically you are supplimenting Timer1 with another eight or sixteen bits, but those you have to increment yourself rather than have it done for you.

robertpeach
- 9th September 2009, 16:03
ok thank you mel,

additionally i just wanna check my calcs are along right lines...

ive timed the blinks and it takes 19 seconds for 10 "on and offs". on is 6 cycles and off is 6 cycles, so total is 120 cycles.

each cycle is 65536 bits.. so 120x65536/19 = 410000 hz

Fosc/4 means that its 410,000x4 = 1,640,000 hz

this seems a bit of a random clock frequency? especially when the deault internal oscialltor is 4mhz and im running the actual pic itself on an external 20mhz oscillator.

maybe i have missed something, which is the most likely thing but i cant see it at the moment

Melanie
- 10th September 2009, 09:05
Well, I was going to go through your code and verify your Timer settings, but not knowing what chip you are using that makes it kinda difficult.

So, 'assuming' you are genuinely running at 20MHz (this now depends on your OSC and Configuration setting for your chosen PIC - and if you don't get that correct then everything is screwed thereafter...)...

I tick = 200nS (FOsc/4), so an empty 16-bit Timer will execute in 65536 x 200nS = 13.1mS then multiplied by any scalers you have previously enabled.

If you think your PIC speed is running oddly, then a simple blinky with PAUSE 10000 and your wristwatch should verify that your speed settings (which includes the DEFINE OSC 20) are all correct give or take.