Timers perhaps?


Closed Thread
Results 1 to 13 of 13

Thread: Timers perhaps?

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    14

    Default Timers perhaps?

    Here's one that's got me scratching my head so far.

    I'm interfacing to an LED PWM chip that (as part of the display routine), needs a constant pulsetrain fed to one pin, and on a second pin, one pulse for every 4096 pulses of the first pin.

    Now, the first part is easy, as I just enable clkout, and feed that to the chip. However, the second part is throwing me. I'll be the first to admit that the whole timer concept kinda throws me, but I have a feeling that's where the solution to this lies. I'd really like to avoid interrupts if at all possible, as it adds to the overhead, but if that's how this cat must be skinned, then so be it.

    Here's my test code...it's cobbled together from sources, so don't nail me to the wall (please) if it really doesn't make sense.

    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 xxx0PPP
    TMR0   =  2            'add 2 to timer if you're anal about it starting exactly on the 1st cycle
    INTCON = %10100000      '$A0 enable TMRO interrupt
    pause 20
    
    
    on interrupt goto clkreset
    INTCON = %10100000      '$A0 enable TMRO interrupt
    
    start:
    goto start
    
    clkreset:
        PORTB.7=1
        PORTB.7=0
        TMR0 = 2   'correct timer for missed cycles?
        INTCON = %00100000 '$20 Reset interrupt set T0IE, clear T0IF
    resume
    And so I'm (hopefully) clear, here's what the code would look like if I didn't need it to run in the background constantly.

    Code:
    start:
        for I=0 to 4095
            PORTB.7=1
            PORTB.7=0
        NEXT i
        PORTB.6=1
        PORTB.6=0
    goto start
    -Matt
    Last edited by Doormatt; - 6th June 2007 at 04:51. Reason: Added example code

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    An idea that might be even easier for you...

    'setup code...
    counter var word

    main:
    counter = counter + 1
    portb.7 = counter.0 'portb.7 toggles at the rate of the LSB
    portb.6 = counter.12 'portb.6 toggles at the rate of the LSB/4096
    pauseus xxxx 'add your pause to get the rate down
    goto main

    The loop will always execute at the same speed.

  3. #3
    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!

  4. #4
    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?

  5. #5
    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 05:52. Reason: Was too confusing...

  6. #6
    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.

  7. #7
    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!

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. hardware timers
    By Adam in forum General
    Replies: 3
    Last Post: - 7th March 2007, 00:10
  4. Availability of PIC timers from PBP
    By coda64 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th February 2006, 07:18
  5. Count Down Timers
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th October 2005, 17: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