Menu

Trouble with Interrupt and Timer. Anyone see the problem? Here is the code.

Help
lhatch
2014-01-23
2014-01-24
  • lhatch

    lhatch - 2014-01-23

    Not sure if the PS is the problem, I pulled the commands from the GCBasic pdf file.

    #chip 12F675, 4
    #config osc = int
    
    #define LED GPIO.4
    Dir LED Out
    Dim count as byte
    
      InitTimer1 Osc, PS1_1/8
      On Interrupt Timer1Overflow Call IncCounter
      IntON
    
        count = 0
    do
    
        wait 1 h
    
    loop
    
    end
    
    '#########################################
    '# Sub routines and functions
    '#########################################
    
    Sub IncCounter
        count ++
        if count < 100 then
           Set LED ON
        else
            if count <200 then
               set LED OFF
            else
               count = 0
            end if
        end if
    End Sub
    
     

    Last edit: lhatch 2014-01-24
  • kent_twt4

    kent_twt4 - 2014-01-23

    Hard to tell with the formatting, but need hashtags for #chip, #config, #define. Try using four "~~~~" before and after code, with a blank line before and after the ~'s.

    Not tested, but you could try a different way of toggling the led in the sub incCounter:

    sub incCounter
       if counter = 255 then led = ! led
    end sub
    
     
  • Chuck Hellebuyck

    What is it doing or not doing?
    Seems like the LED should flash at a roughly 1 minute rate (52 seconds).

    4 MHz/4 = 1 MHz instruction clock that feeds timer1 prescaler
    1:8 prescaler makes timer1 clock 125 kHz
    (1/125 kHz) * 65535 16 bit timer1 = 0.524 seconds per overflow
    0.524 * 100 count variable increments = 52 seconds to change LED state.

    Is this not happening?

     
  • Marcoos

    Marcoos - 2014-01-23

    if I understand what you want to do there are logical errors in the interrupt routine.
    see if this can help you
    Ciao

    '# Sub routines and functions
    '#########################################

    Sub IncCounter
    count ++
    if count < 100 then
    Set LED ON
    else
    if count >100 & <200 then
    set LED OFF
    else
    count = 0
    end if
    end if
    End Sub
    [/code]

     
  • lhatch

    lhatch - 2014-01-24

    @Chuck. I get no LED on at all. I I do a simple wait 1 s, set LED on, wait 1 s, set LED off, works fine. So not the hardware.

    I don't see a logic problem with the LED toggle code, looks logical me too. Count is set to 0, interrupt fires and incs it to 1, that is less than 100 (so is 0), set LED ON should run. So I would think it would pop right on.

    @Chuck, thanks for the info, was not 100% on the PS, now I am. I had thought maybe it was so fast I could not see it blink. Not the case. I also tried <10 and <20 to see if that was it before posting.

    Anyway. Still no LED blinking, I want to make the a part of all my new projects, and I could make a state machine as well.

    I will try Kent's toggle for grins, but my logic is fine far as I can see. Maybe I will look at the ASM too.

    Thanks for any other ideas in advance.

     
  • lhatch

    lhatch - 2014-01-24

    @kent_twt4 The toggle threw an error about GPIO and being defined.

     
    • kent_twt4

      kent_twt4 - 2014-01-24

      Oh well, worth a try. This works on a 12f683, not a whole lot in code savings though:

      toggle = 1
      ...
      ...
      ...
      sub IncCounter
      count += 1
      If count = 5 Then
          count = 0
          LED = toggle.0
          comf toggle,f    'invert toggle and save to register
      End If
      end sub
      
       
  • Chuck Hellebuyck

    I built the program and ran the assembly file in MPLAB to simulate it.
    The timer wasn't runnning.

    Then I realized you never started it in the code.
    You need to add the line:

    StartTimer 1

    I put it right after the IntON instruction and it ran fine in the simulator.

    InitTimer1 Osc, PS1_1/8
    On Interrupt Timer1Overflow Call IncCounter
    IntON
    StartTimer 1

     
  • lhatch

    lhatch - 2014-01-24

    Thanks Chuck, original code and your starttimer and it's blinking now.

    With 4 and 8 for the counts, not 100 and 200.

    I saw the sample in the help file (or PDF) and did not see the starttimer 1. Once again, thank you very much.

    How did you get MPASM to simulate it? I could not compile the ASM (wrong assembler I guess) and it would not load the .HEX file. Whats the secret or a link to it? I looked to see if I could change the assembler and did not see it.

     

    Last edit: lhatch 2014-01-24

Log in to post a comment.