Menu

InitTimer1 with AVR and external clock

maxwins
2017-07-25
2017-07-25
  • maxwins

    maxwins - 2017-07-25

    Hello all,

    I have an ATTiny4313 and have been experimenting with timers.

    My question is regarding the InitTimer1 command with "Ext" option. ie.:

    InitTimer1 Osc,PS_1 ' This works as expected when "OSC" parameter used for internal oscillator
    

    vs

    InitTimer1 Ext,PS_1 ' This doesn't appear to work... at least the scope output shows the pulsepin goes high and stays high, as if the timer is not running
    

    Is the "Ext"ernal clock option supposed to work with AVR's? The docs seem to suggest it should, but when using Ext the timer seems like it doesn't run.

    There is a #config osc=ext at the start of the code... Perhaps that is already forcing InitTimer1,osc,ps_1 to use the external clock source... Does anyone know?

    Anyway- I'd like to get the timer working with the external clock. That's the main point! At the moment, I don't know how to determine if it is using the external clock, as the timer only seems to work with this setup command: InitTimer1,Osc, PS_1

    Thank you, Max.

    Full working code sample attached for reference:

    ''' Source code written with GCB (Great Cow Basic)
    '''------------------------------------------------------------------------
    '''
    ''' Timer Testing
    '''
    '''
    '''************************************************************************
    
    ' ---------------------------------------------------------------------
    
    
    #define PulsePin PORTB.4 ' alias for development
    DIR PulsePin IN   ' Pin pulled down
    
    ' ---------------------------------------------------------------------
    
    #define USE_Timer0 FALSE
    #define USE_Timer1 TRUE
    #define USE_Timer2 FALSE
    #define USE_Timer3 FALSE
    #define USE_Timer4 FALSE
    #define USE_Timer5 FALSE
    #define USE_Timer6 FALSE
    #define USE_Timer7 FALSE
    #define USE_Timer8 FALSE
    #define USE_Timer9 FALSE
    #define USE_Timer10 FALSE
    #define USE_Timer11 FALSE
    #define USE_Timer12 FALSE
    
    
    ' ---------------------------------------------------------------------
    ' Configuration
    '
    ' NOTE: Must set 3-8MHz resonator option in ATMEL_ICE config
    ' ---------------------------------------------------------------------
    
     #chip tiny4313, 8 ' Using external 8MHz resonator 
     #config osc = ext
     #option explicit
    
    ' ---------------------------------------------------------------------
    
    
    ' ---------------------------------------------------------------------
    '
    ' MAIN program
    '
    ' ---------------------------------------------------------------------
    
    
    Dim tmpw as Word
    Dim tmp, Timer1_OVERFLOW_Flag as byte
    
    ' ---------------------------------------------------------------------
    
    
    'InitTimer1 Osc,PS_1 ' This works as expected when "OSC" parameter used for internal oscillator
    InitTimer1 Ext,PS_1 ' This doesn't appear to work... at least the scope output shows the pulsepin goes high and stays high, as if the timer is not running
    
    ' Set timer overflow interrupt
    On Interrupt Timer1Overflow Call Timer1_OVERFLOW
    
    ' Start the Timer
    StartTimer 1
    
    
    do Forever
    
    
        Dir PulsePin out
        Set PulsePin On ' Drive pin high
    
        ' Check ASM to adjust timing.
        ' Currently about 11uS of instructions between cleartimer and pinoff instruction,
        ' so adjust timer by 11x8MHz at tmpw
    
        ' delay required is 120uS
    
        ' 65535 - (120x8) + (11x8)
        tmpw = 64663 ' 120uS !!!
    
    
        ' Clear timer and set the TOP value (to force timelimit)
        ClearTimer 1
        TCNT1H = tmpw_H
        TCNT1L = tmpw
    
        ' Clear timer overflow flag
        Timer1_OVERFLOW_Flag = 0
    
        do
    
          loop until Timer1_OVERFLOW_Flag=1
    
        ' Clear pins
        Set PulsePin Off ' Drive pin low
        Dir PulsePin in ' Pin pulled down in idle state
    
    
        ' tmp=0 called 6 times for debug, to create delay of 3.2uS between pulses, for testing/debug
        tmp=0
        tmp=0
    
        tmp=0
        tmp=0
    
        tmp=0
        tmp=0
    
    loop
    
    
    
    ' ---------------------------------------------------------------------
    
    sub Timer1_OVERFLOW
    
      Timer1_OVERFLOW_Flag = 1
    
    end sub
    
    ' ---------------------------------------------------------------------
    
     
  • Anobium

    Anobium - 2017-07-25

    What version of the compiler etc? This important so we can help.

     
  • maxwins

    maxwins - 2017-07-25

    Thank you. The synWrite output window shows this:

    Compiler Version: 0.95.010 2016-10-28 Program Memory: 3734 bytes RAM: 105/256 bytes (41.02%) Chip: TINY4313

     
    • Anobium

      Anobium - 2017-07-25

      Could you update to the latest release? Timer.h has been updated since 0.95.010. We can then provide the very latest as Bill has resovled many issues in the timer library for the upcoming release.

       
  • maxwins

    maxwins - 2017-07-25

    Gulp! Your right!

    Downloaded update and tried again, but the same "issue" remains.

    2.3 Sec. Compiler Version: 0.97.01 2017-02-20 Program Memory: 342 bytes RAM: 11/256 bytes (4.3%) Chip: TINY4313

     
  • maxwins

    maxwins - 2017-07-25

    Here's the asm around the two InitTimer1 commands:

    ;InitTimer1 Osc,PS_1 ' This works as expected when "OSC" parameter used for internal oscillator
        ldi SysValueCopy,1
        sts TMRSOURCE,SysValueCopy
        ldi SysValueCopy,1
        sts TMRPRES,SysValueCopy
        rcall   INITTIMER1
    
    ;InitTimer1 Ext,PS_1 ' This doesn't appear to work... at least the scope output shows the pulsepin goes high and stays high, as if the timer is not running
        ldi SysValueCopy,2
        sts TMRSOURCE,SysValueCopy
        ldi SysValueCopy,1
        sts TMRPRES,SysValueCopy
        rcall   INITTIMER1
    

    edit: Removed previous incorrect comment.

    Looks like the 2 instructions generate the same asm code, except the first line "ldi SysValueCopy,2"

     

    Last edit: maxwins 2017-07-25
  • Anobium

    Anobium - 2017-07-25

    Now update the timer library to the attached version. Then, we have a good baseline to work from. This library is from v0.98.00.

     
  • maxwins

    maxwins - 2017-07-25

    Done. No change.

    The compiled code for the InitTimer1 lines look the same.

    I'm wondering if the On Interrupt or overflow code needs to vary when tied to an ext clock source.. maybe inittimer does work, but those events don't fire in the same way.

     
    • Anobium

      Anobium - 2017-07-25

      Looking at the ASM the External clock source on T1 pin/Clock on rising edge. TCCR1B = TCCR1B And 248 Or TMR1_TMP where TMR1_TMP = 7. So, this looks correct.

      Please set the T1 pin as an input please.

      Also, remove #config. Config serves no purpose as the fuses are set when the bootload firmware was installed.

       
  • maxwins

    maxwins - 2017-07-25

    Commented out the config line.
    By T1 pin, do you mean the clock-in (xtal1) pin?

    That's PA0 on the ATTINY4313, so I added this line:

    DIR PORTA.0 IN ' Set Xtal input pin
    

    No change to the results.

    Thinking of other details, the fuse values I have set are:
    Extended: 0xFF
    High: 0xDD
    Low: 0xEC

    Is it possible they might be involved in the problem?

     
    • Anobium

      Anobium - 2017-07-25

      T1 pin is on portd.5 on page 2 of the datasheet. Please try setting portd.5 to input.

      Can you share your programming method? Bootloader etc?

      Bill will take over for the next posting... :-)

       
  • maxwins

    maxwins - 2017-07-25

    T1 now understood. Thank you.

    D.5 set, no difference. That pin has nothing attached to it BTW.

    Programming method...
    1. Click HEX on the GCB toolbar,
    2. 2. Use Atmel Studio 7 "device programming" menu to set the fuses and load the HEX file to the ATTIny.

    Thank you very much for your help so far. I feel rather lost in what to look for, but I'm learning and very grateful.

     
    • Anobium

      Anobium - 2017-07-25

      Nothing attached to that pin. OK. Tell me about your oscillator... using an external one?

      The T1 is the external source for Timer1. So, the information on your oscillator and clock sources is needed to complete the picture.

       
  • maxwins

    maxwins - 2017-07-25

    I have an 8MHz resonator connected between XTAL1 and XTAL2 (PA0 and PA1).

    Until now, I thought that was the right place for it. But guessing by your remarks, perhaps I have that resonator in the wrong place? Or do I need an additional external resonator for the timer hardware?

    Hmm, that's doesn't quite make sense.

    Maybe this is how it goes...:

    InitTimer1 used with the OSC setting, will use whatever clock source is configured by the ATTINY fuses. In my case that will be an external resonator on XTAL1/2. In other cases, that could also be an internal oscillator in the ATTINY.

    InitTimer1 used with the EXT setting will use the clock source attached to T1.

    Is that correct?

    So when I specified "InitTimer1 OSC, PS_1" in my code sample, then I was using my external resonator as that was set by the fuses?

     
    • Anobium

      Anobium - 2017-07-25

      Yes. Your summary is what I am thinking. Others may tweak your summary but not me.

       
  • maxwins

    maxwins - 2017-07-25

    The docs for InitTimerX threw me, as the parameters Osc and Ext refer to either internal or external clock source, and I misunderstood that to mean literally a physically internal or external xtal.

    For anyone else searching here, that description might be extended to:

    Osc - Selects the clock source in use, as set by the ATTiny fuses. This might be an internal clock or an external clock source attached to the XTAL1 and XTAL2 pins.

    Ext - Selects the clock source attached to pin T1. This allows a different clock frequency than the main clock to be used, such as 32.768 kHz crystals commonly used for real time circuits.

    If anyone thinks this is incorrect, please reply and I will modify this text.

    Thank you for getting me through this Anobium.

     
    • Anobium

      Anobium - 2017-07-25

      Thank you. This will enhance the Help. I will wait a day or two then I will update the Help.

      Thank you for adding your insights to this community effort.

      :-)

       

Log in to post a comment.