AVR CTC Mode

Discussion in 'Electronics & Electrics' started by BeanerSA, Dec 31, 2010.

  1. BeanerSA

    BeanerSA Member

    Joined:
    Aug 26, 2009
    Messages:
    3,644
    Location:
    Gateway to the Barossa
    Can I also service an ISR whilst operating a timer in CTC mode, or is it just one or the other?

    I have already written my code to run an ISR as well as clear the counter, but it doesn't work, so I'm wondering if it is even possible.

    Anyone have any ideas?
     
  2. paulvk

    paulvk Member

    Joined:
    Mar 10, 2010
    Messages:
    1,030
    What is the code written in?
     
  3. dohzer

    dohzer Member

    Joined:
    Jan 5, 2002
    Messages:
    5,436
    Location:
    Melbourne
    Looks like you can, according to the ATmega8 datasheet:

    Which micro are you using?
     
  4. paulvk

    paulvk Member

    Joined:
    Mar 10, 2010
    Messages:
    1,030
    And here is something to keep in mind.

    When an interrupt is serviced no other interrupts can occur because the processor will disable all interrupts by clearing the master interrupt enable bit.
     
  5. OP
    OP
    BeanerSA

    BeanerSA Member

    Joined:
    Aug 26, 2009
    Messages:
    3,644
    Location:
    Gateway to the Barossa
    I'm currently using an old 8535, but am looking to migrating to an atmega48/88.

    As it works now, I have one timer producing a 2kHz output on it's output pin, and have fed this into, the input of the other timer, configured as a counter. The counter counts up to 999 (as I'm trying to time to 1/1000th of a sec), and then the CTC portion is working okay, the counter resets to zero. What isn't working is that I also have an ISR at OC1A, that should increment a variable, SECS.

    I haven't used the counter in overflow, as I just want to be able to read the value of the counter at a given time and determine how many 1/1000ths have elapsed. Preloading and using overflow, I felt, was going to involve to much math.
     
  6. SCwerTA

    SCwerTA Member

    Joined:
    Jan 13, 2005
    Messages:
    254
    Location:
    SE Melb
    Is your code written in asm or C?

    Firstly, if its C, and your SECS variable is shared with main and the ISR, check that the variable is volatile.

    Also does the 2KHz timer serve another purpose other than triggering the second timer? Just asking because its a roundabout way of setting up a 1ms counter essentially.
     
  7. dohzer

    dohzer Member

    Joined:
    Jan 5, 2002
    Messages:
    5,436
    Location:
    Melbourne
    Might want to post the code, or at least a minimal version of it.
     
  8. OP
    OP
    BeanerSA

    BeanerSA Member

    Joined:
    Aug 26, 2009
    Messages:
    3,644
    Location:
    Gateway to the Barossa
    Yep I was aware of that, but aren't interrupt bits set in the register and then serviced sequentially?

    Last bit first; no that's all the 2kHz timer does, but it does it with no software overhead, it's just set and forget. I got the idea from the appnote for Long Delay Generation.

    Secondly; sorry, only Bascom for me, already familiar with VB type languages as I'm not a real programmer :p

    Code:
    $regfile = "8535def.dat"
    $crystal = 8000000
    
    Config Lcd = 20 * 4
    Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , E = Portc.4 , Rs = Portc.5
    Cursor Off
    
    Dim Seconds As Byte
    Dim Minutes As Byte
    
    Config Porta = Input
    'Porta = &B11111111 'uncomment for internal pullups
    
    Config Timer2 = Timer , Async = Off , Prescale = 32 , Compare = Toggle , Clear Timer = 1
    Config Timer1 = Counter , Edge = Rising , Compare 1a = Disconnect , Clear Timer = 1
    
    Enable Interrupts
    Enable Oc2
    Enable Oc1a
    On Oc1a Addseconds:
    Compare1a = &HCE7
    Ocr2 = &H7C                                                 '0.5ms Pulse / 2000Hz
    
    Do
       Cls
       Lcd Timer2
       Lowerline
       Lcd Timer1
       Thirdline
       Lcd Seconds
       Waitms 20
    Loop
    
    Addseconds:
       Incr Seconds
       Return
    
    
    End                                                         'end program
     
  9. OP
    OP
    BeanerSA

    BeanerSA Member

    Joined:
    Aug 26, 2009
    Messages:
    3,644
    Location:
    Gateway to the Barossa
    Anyone? (bump)
     
  10. OP
    OP
    BeanerSA

    BeanerSA Member

    Joined:
    Aug 26, 2009
    Messages:
    3,644
    Location:
    Gateway to the Barossa
    I see your point, but this is mostly a hardware level question, and surely the Bascom code isn't that cryptic, is it?
     

Share This Page