Menu

Timer Problems

Help
H2TECH
2011-07-09
2013-05-30
  • H2TECH

    H2TECH - 2011-07-09

    After reading almost every post about pic timers and then making numerous programs incorporating timers, I still cannot figure what's wrong with my programs. Basically, I want to learn how to use both timers to eventually read a frequency, count a series of pulses, or measure a length of time. Currently I'm using the pic 16f688 microcontroller for these programs, but I can also use pic 12f629's. I suppose a good place to start is providing one of the simple counting programs. Nothing spectacular, but the program counts how many times I press a switch in a short period and then blinks an led that amount of times. If anyone can help on what's missing or provide essential information on these timers, I would greatly appreciate it.

    ;Chip Settings
    #chip 16f688,8
    #config OSC = INT

    ;Defines (Constants)
    #define led portc.1 'led to indicate events or number of pulses
    #define sample porta.5

    dir led out
    dir sample in

    initTimer1 ext, PS1_1/1

    MAIN:
    ClearTimer 1
    Wait until sample = on
    set led on
    StartTimer 1
    wait 1 s
    StopTimer 1
    new = TMR1H
    set led off
    wait 2 s
    Repeat new
    set led on
    wait 400 ms
    set led off
    wait 400 ms
    end Repeat
    wait 2 s
    goto MAIN

    Also to note: Typically when this is ran, when I press the button once, the led flashes upto 50 times, which is definitely a problem.

     
  • H2TECH

    H2TECH - 2011-07-09

    Sorry, I didn't know my program inserted as a paragraph of clutter. I'm not sure how exactly to fix this issue, but if anyone can decipher the paragraph or suggest how to fix this problem, I would further appreciate the help. If not an example program demonstrating timer use would also be handy.

     
  • H2TECH

    H2TECH - 2011-07-09

    Please ignore last reply, my internet browser oddly scrampled my first post as one paragraph. After a restart, it properly displayed my program example.

     
  • Nobody/Anonymous

    Switches can suffer from switch bounce when they are pressed or released where the contacts will chatter on and off a number of times. Still if you are getting 50 in TMR1H that means its counting over 12000 pulses which seems a bit much even for switch bounce. How is the switch connected to the timer1 input? Are you using a pull up or pull down resistor to stop the input from floating when the switch isn't pressed?

    An alternative would be to use one of the 12F629's to generate a range of frequencies on the five output pins and connect one of those outputs as the timer1 input to the 16F688 - you could move the switch to another input on the 688 and just use it to start the count period. The program for the 12F629 could be something like this -

    #chip 12F629, 4
    #config OSC = INTRC_OSC_NOCLKOUT, PWRTE = ON, MCLRE = OFF, WDT = OFF
    dir GPIO out
    count = 0
    do
        GPIO = count
        count ++
        wait 2 ms
    loop
    

    GP0 should pulse at 250Hz, GP1 at 125Hz,  GP2 at 62.5, GP4 at 15.6Hz and GP5 at 7.8Hz although I haven't tried the program on chip.

    Your program in post 1 looks as if it should work with a repeatable input - if you use the 12F629 idea you would need to change the line that loads new to "new =TMR1L" as the count should be low.

     
  • H2TECH

    H2TECH - 2011-07-09

    Alright, I tested your low frequency technique and the led flashes the correct number of times. That's one part down, but how about measuring time. I also have a quickly made program that should count how long one interval of a certain frequency is on. I used a 12f629 to accurately produce a 5 kHz pulse which is applied to the timer 1 pin. If my math is correct and the timer 1 prescalar is 1/8, each instruction cycle should take 4 microseconds. Is this math correct? If so, led1 should stay on at 5 kHz, but led2 should turn on at 100 Hz. However, led2 does not turn on at 100 Hz.

    Also as a side question, if I obtain a frequency and save it as a variable, what is the most accurate method to reproduce that frequency on a random pin? Any additional help would once again be appreciated.

    ;Chip Settings
    #chip 16F688,8
    #config osc = int

    #define led1 porta.4
    #define led2 porta.3
    #define led3 portc.1
    #define sample porta.5

    dir led1 out
    dir led2 out
    dir led3 out
    dir sample in
    dim new as word

    InitTimer1 osc, PS1_1/8 '
    wait 1 s

    MAIN:
    cleartimer 1
    wait until sample on 'perhaps not the best method of measuring
    startTimer 1
    wait until sample off
    wait until sample on
    stoptimer 1
    new = TMR1L

    if new<200 then 'if input is 5kHz, this should light
    set led1 on
    wait 5 s
    set led1 off
    goto MAIN
    end if

    if new < 10000 then
    set led2 on
    wait 5 s
    set led2 off
    goto MAIN
    end if

    if new < 70000 then '70000 just to be safe
    set led3 on
    wait 5 s
    set led3 off
    goto MAIN
    end if

     
  • kent_twt4

    kent_twt4 - 2011-07-10

    Word variables go from 0-65535.  Using the code tags help preserve tabs and spaces, and makes the code stand out.  Timer1 is 16 bits, so one way of dealing with that would be:

    dim new as word alias TMR1H,TMR1L
    

    When sorting variables into groups, consider a top down approach like

    If new > 1024
    ...
    ...
    End If
    If new < 200
    ...
    ...
    End If
    

    An easy way of dealing with frequencies is to use the CCPx module in the PWM mode, and change the PR2 register on the fly.  This means that you have to use a PIC with CCPx modules, and the CCPx pin as output.  For multiple strategies using Timers and CCPx, look up "Compiled Tips'n Tricks" from microchip, lots of good stuff.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.