Timers perhaps?


Closed Thread
Results 1 to 13 of 13

Thread: Timers perhaps?

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Very interesting!

    But sadly enough, I can't use it. I need the PIC to generate these two clock pulses independently of the rest of my program. The second chunk of code I had was just an example of what I need to accomplish.

    I'm still reading, and from what I garner, I need to set the prescaler on one of the timers, and trigger it from the internal clock...or something to that effect. I'm honestly quite confused at this point.

    Thanks for the bit trick there though! VERY cool!

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Doormatt View Post
    Very interesting! But sadly enough, I can't use it. I need the PIC to generate these two clock pulses independently of the rest of my program. The second chunk of code I had was just an example of what I need to accomplish. I'm still reading, and from what I garner, I need to set the prescaler on one of the timers, and trigger it from the internal clock...or something to that effect. I'm honestly quite confused at this point. Thanks for the bit trick there though! VERY cool!
    How fast does that bit on portb.7 have to toggle? What freq do you want?

  3. #3
    Join Date
    Aug 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Let's abstract for a sec.

    I need two pins, call them A and B.

    If Port A is pulsing at 4096Hz, then Port B needs to pulse at 1Hz. (In the real world, Port A needs to be a minimum of 245Khz)

    But, I need to do this in the background.

    Ideally, port A would be driven by the clock out, and port B would pulse once for every 4096 pulses coming out of the external clock.

    Does that make more sense (sorry if I'm being confusing!)

    -Matt
    Last edited by Doormatt; - 6th June 2007 at 06:52. Reason: Was too confusing...

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Doormatt View Post
    Let's abstract for a sec.
    I need two pins, call them A and B.
    If Port A is pulsing at 4096Hz, then Port B needs to pulse at 1Hz.
    But, I need to do this in the background.
    Ideally, port A would be driven by the clock out, and port B would pulse once for every 4096 pulses coming out of the external clock.
    Does that make more sense (sorry if I'm being confusing!)
    -Matt
    Ok, so you need the fast pin at 4096Hz and the slow pin to pulse at 1Hz? Yes?
    Is that what you actually need?
    It is definitely a doable project, interrupt driven pulses and all...just need to know your exact numbers.

  5. #5
    Join Date
    Aug 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Yay!

    It's doable!

    I need One pin at a minimum of 245Khz, and then the other pin at a 1:4096 ratio.

    As long as that one pin is over 245Khz, I'm happy.

    (Detail warning)
    Basically, the chip I'm controlling has 16 PWM outputs, each with 4096 brightness levels. You serially clock in your data, and then feed it the aforementioned pulse train. That pulse train makes it step through the brightness levels, and then the single pulse resets the display chip. I need a minimum of 247Khz, as I have to update the display 60 times a second, and with 4096 levels, thats 60*4096Hz = 245Khz.

    Thanks SO much for helping me with this!

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Doormatt View Post
    I need One pin at a minimum of 245Khz, and then the other pin at a 1:4096 ratio.
    Woof!
    You initially said 4096hz, that's doable...
    245khz is a bit much...still doable, probably not at 4mhz though...

    Quicky explanation: Take your 4mhz clock rate, divided by 4 because of the instruction rate, gives you 1mhz. You need 250khz, that gives you 4 instructions per tick. That's just enough for the jump to the interrupt and the resume (at 2 cycles per instruction), not including any PBP overhead. If you were able to run your PIC at 40Mhz, that would give you 40 instructions per tick to play with, actually 36 after the jump/resume (again, not including any PBP overhead). Not a lot to work with...since you'd have to reload TMR0 with a reload value to get it to kick the overflow interrupt at the 250Khz rate. So, you can't really use a PIC to keep track of an input pin reliably and to output a pulse unless that's ALL it's doing, and you have really tight code. Otherwise, you might miss a pulse here and there...

    EDIT: Disregard the paragraph that previously talked about the 74LS92...

    Take that same idea above and feed 3 cascaded 4 bit counters and use the high bit of the high counter as your 1/4096 pulse generator. (for some reason I was thinking of the above paragraph in terms of divide-by-12 vs. divide-by-12-bits.

    Actually, I think this is the first time I've suggested using logic chips instead of a PIC, instead of the other way around.
    Last edited by skimask; - 6th June 2007 at 07:15. Reason: EDIT: My bad...

  7. #7
    Join Date
    Aug 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    Riiiight! I forgot that the PIC is "running" at 1Mhz, not 4.

    After reading over the datasheet a few more times, I think I've wrapped my head around the timers.

    This code now seems to work (with the inclusion of a enable and disable), and should be outputting the right ratios (With the full output coming off of Clkout, and the rest coming out of B.7).

    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 = %10000011  '$84 assign prescaler to TMR0, set prescale to 1:16
    TMR0   =  2            'add 2 to timer if you're anal about it starting exactly on the 1st cycle
    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
    I don't have my scope handy (left it at work), so I can't measure the output, so I'm trying to figure out what frequency they're actually firing at. I know someone who's gotten this to work on the PIC I'm using (16F648A) with the internal clock, so I know this isn't a hopeless case...maybe there's something wrong with my math somewhere...math was always my weak point.

    Thanks again for your ongoing patience and help!

Similar Threads

  1. Timers
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th November 2008, 21:08
  2. Reading Timers
    By kevj in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th August 2007, 09:19
  3. hardware timers
    By Adam in forum General
    Replies: 3
    Last Post: - 7th March 2007, 01:10
  4. Availability of PIC timers from PBP
    By coda64 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th February 2006, 08:18
  5. Count Down Timers
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th October 2005, 18:34

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