Announcement

Collapse
No announcement yet.

Millisecond Counter

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

  • 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?

  • #2
    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.
    We can crack this cotton PIC'n thang!

    Comment


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

      Comment


      • #4
        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

        Comment


        • #5
          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)?

          Comment


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

            Comment


            • #7
              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.
              We can crack this cotton PIC'n thang!

              Comment


              • #8
                Thanks Mike. I'll look into that.

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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.
                    We can crack this cotton PIC'n thang!

                    Comment


                    • #11
                      Well that's some sweet code Richard. Thank you.

                      Comment


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

                        Comment


                        • #13
                          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.

                          Comment

                          Working...
                          X