Timers have me beat


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2006
    Posts
    14

    Default Timers have me beat

    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:
    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!

  2. #2
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post 244Hz ?

    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/>
    Last edited by T.Jackson; - 7th June 2007 at 04:54.

  3. #3
    Join Date
    Aug 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    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!
    Last edited by Doormatt; - 7th June 2007 at 05:56. Reason: Got it working!

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Do i have to mention PicMultiCalc ??
    http://www.mister-e.org/pages/utilitiespag.html
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Timers
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th November 2008, 20:08
  2. Reading Timers
    By kevj in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th August 2007, 08:19
  3. Timers !
    By n qwerty in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd August 2007, 11:39
  4. hardware timers
    By Adam in forum General
    Replies: 3
    Last Post: - 7th March 2007, 00:10
  5. Availability of PIC timers from PBP
    By coda64 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th February 2006, 07:18

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts