Announcement

Collapse
No announcement yet.

Millisecond Counter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Keith55555
    replied
    Not at all a spoiler Richard. It's always nice to compare and then recognize similarity in the proper direction one is going in. I thank you again for that.

    Leave a comment:


  • richard
    replied
    if it doesn't spoil your fun there is worked solution here
    http://www.picbasic.co.uk/forum/show...312#post147312

    Leave a comment:


  • Keith55555
    replied
    Well that's some sweet code Richard. Thank you.

    Leave a comment:


  • mpgmike
    replied
    I guess I tend to use interrupts more than most here. What I love about software is that there is always 2 if not 20 different ways to tackle any challenge.

    Leave a comment:


  • richard
    replied
    or something easy and accurate like

    Code:
    ;[email protected]
    #CONFIG
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
    __config _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_19 & _LVP_OFF
    #ENDCONFIG
    
    DEFINE OSC 4
    OSCCON=$68
    ANSELA=0
    
    TRIGGER VAR PORTa.4 ;timer1 gate
    timer var word ext
    @timer = TMR1L ; set shortcut alias to timer count
    T1CON=0
    T1GCON=$c0
    
    
    
    measure: ; can measure 1uS to 65535 uS in 1 uS increments
    'reset timer
    T1CON=0 ;timer off
    pir1.0=0 ;clear overflow ? optional maybe
    TIMER=0
    T1CON=1 ;timer on
    'wait for rising edge of pulse
    WHILE TRIGGER == 0 : WEND
    WHILE TRIGGER : WEND
    ;stop timer no retriggering
    T1CON=0
    ------timer now holds pulsewidth in uS -----------
    ;now do something with it

    Leave a comment:


  • Keith55555
    replied
    Thanks Mike. I'll look into that.

    Leave a comment:


  • mpgmike
    replied
    An approach I'd try is set up Timer 2/4/6 for 5 ms using T2CON for clock Pre/Post scaler and PR2 (T2PR on newer devices) to get my 5 ms. Enable interrupts, and in the ISR merely increment a variable and set a Ready flag. Your 5 ms would give a value of 1 to your variable, 10 ms a value of 2, 40 a value of 8. In my main routine, I'd poll for the Ready flag, then SELECT CASE the variable value. If 40 (variable value of 8) is the highest you need, part of the SELECT CASE execution is to reset the counter. Whatever you need to do to trigger your photo capture can be done in a separate subroutine called from your SELECT CASE.

    Leave a comment:


  • richard
    replied
    there are no pbp native commands to control pic timer/counters. you need to write the correct settings into the pic timer/gate registers to suit your requirement
    then clear the timer , trigger it when photo interrupter "fires" then read count when photo interrupter restores, clear and reset timer for next measure

    what kind of conditioning do you think necessary for the photo interrupter; simple pull-up (or down)?
    matters not to a timer gate on most chips , they can work either way eg16f1825

    Leave a comment:


  • Keith55555
    replied
    So two questions: Can a PIC using only PICBASIC PRO commands derive the frequency or is peeking and poking registers necessary?

    Also, what kind of conditioning do you think necessary for the photo interrupter; simple pull-up (or down)?

    Leave a comment:


  • richard
    replied
    virtually any pic chip @4mhz with a 16bit counter can measure those sorts times easily , a chip with a gated counter makes it even easier

    Leave a comment:


  • Keith55555
    replied
    Thanks Mike. I understand what you're saying but have never used the timers. I'll look into using them.

    Leave a comment:


  • mpgmike
    replied
    Have you chosen a PIC to work with? The Timer 2/4/6... is probably your first choice. Set up the Timer2 to interrupt at 5 ms. You can create a variable to count interrupts of 5 ms. In your Main routine, you can do a SELECT CASE to check the value of the Timer2. If there is a task that executes at 5 ms, then Counter = 1 will execute that sub routine. Counter = 2 equates to 10 ms and your SELECT CASE can call a sub routine to execute whatever you need to do then; and so forth.

    Leave a comment:


  • Keith55555
    started a topic Millisecond Counter

    Millisecond Counter

    I'd like to create a millisecond counter with a range from 5 milliseconds to 40 milliseconds. It does not have to be resolved to a single millisecond; e.g. need to know when it's hit each in a series of N+5 (5,10,15,20,25,30,35,40) milliseconds known. There'll be a photo interrupter reading an aperture in a disk and possibly be 8 LEDs each designating those 8 positions. Sort of like how an LED guitar tuner displays; 1st LED=5ms, 2nd=10ms, 3rd=15ms, etc.
    Will I need a subroutine to avoid inaccuracy? Can I get by with PULSIN?
Working...
X