PDA

View Full Version : Timers have me beat



Doormatt
- 7th June 2007, 04:17
Alright, I give up (for now).

I thought I had timers ALL figured out, but I brought the scope home from work, and it turns out I've got NO idea what I'm doing.

I'm using the following code:


@ device pic16F648A, intrc_osc_clkout, mclr_off, protect_off, lvp_off, wdt_off
DEFINE OSC 4
TRISB = 0 ' Outputs on
CMCON = 7 ' Turn all comparators to fully digital (needed??)
OPTION_REG = %00000011 'Set prescale to 1:16
TMR0 = 2
INTCON = %10100000 '$A0 enable TMRO interrupt

on interrupt goto clkreset

start:
goto start

clkreset:
disable
PORTB.7=1
PORTB.7=0
TMR0 = 2 'correct timer for missed cycles?
INTCON = %00100000 '$20 Reset interrupt set T0IE, clear T0IF
enable
resume


When I run this code on my PIC 16F648A, I get a frequency of 72Khz on B.7, when my math says that I should be getting ~244Hz. AND to make matters more confusing, when I change the line OPTION_REG = %00000011" to another prescaler value, I don't get any change in the output frequency.

I'm obviously missing something very basic here (like last time where I mixed up TRISB and PORTB commands).

Could someone please save what's left of my hair?

Thanks!

T.Jackson
- 7th June 2007, 04:51
How do you figure 244Hz ? (bit puzzzled)

There's a few problems at first glance that I can see ...

<ul>
<li>Disable needs to appear before the routine (not inside of it)</li>
<li>Enable needs to be after resume, not before it</li>
</ul>
I think it's better to just manipulate the bits that you want rather than the whole register. For example; INTCON.2 = 0 clears the overflow flag.
<br/>

Doormatt
- 7th June 2007, 05:01
Here's the math I used to come up with my output frequency.

The internal clock executes at 1Mhz.

If I use a 1:16 Prescaler, then it will take 256*16 clock pulses to overflow Timer0. 1,000,000/(256*16)=244Hz

Math was never my strong suit though...

I moved the enable/disable to where you suggested, and now I'm getting a 10Khz signal.

Getting closer...

EDIT: WOO! I forgot I'd changed another value! It works now! I'm getting my 244Hz signal! Thanks so much!

mister_e
- 8th June 2007, 14:05
Do i have to mention PicMultiCalc ?? :D
http://www.mister-e.org/pages/utilitiespag.html