Menu

Timer Problem with ATTiny85

Help
2020-09-14
2020-09-18
  • Philip Gansloser

    Hello,
    I'm new to microcontroller programming, but try to learn constantly :-)
    With the attached code I try to blink a LED on a ATTiny85 chip with 1 Hz frequency.
    This works fine, but the intended fine tuning of the frequency by changing the SetTimer value (6) of Timer 1 shows no effect.
    The blinking frequency always stays 1 Hz, even with a value of 255.

    What am I doing wrong?

    Kind regards
    Philip

     
  • Philip Gansloser

    Here is the code:

    #chip tiny85, 16
    #option Explicit
    #define LED_1 PORTB.1
    
    ;--- Set unused pins to input + pullup to avoid drifting ports:
    Dir PORTB IN       'set all pins to input
    Set PORTB = 0xFF  'activate all internal pullups
    
    Dir LED_1 OUT
    
    Dim intervall as Byte
    Dim warteseit, CounterValue_0, CounterValue_1, Pulse_Start, Pulse_Len as byte
    
    Start:
    
    'Enable portb.2 as the source of the interrupt. See the datasheet for more information.
    'Trigger on change of PB2
    
    On Interrupt Timer1Overflow Call IncCounter_1
    IntOn   'enable Interrupts
    
    ;Timer1 prescaler  1/64 => with 16 MHz, 8bit counter overflows every 1024µs;
    ;with preload 6 overflow every 1000 µs
    InitTimer1 Osc, PS_1_64
    SetTimer 1, 6
    StartTimer 1
    
    intervall = 0
    warteseit = 0
    
    Set LED_1 On
    
    Main:
      do forever
    
    
        ;Count Loops (0-10)
         intervall++
         if intervall = 50 then
           SET LED_1 Off
         end if
    
         if intervall >100 then
          intervall = 0
          Set LED_1 On
         End If
    
        ;Wait until next 10ms intervall starts
        do while (CounterValue_1 - warteSeit) <10
        ;emty loop
        loop
        warteseit+=10 ;next 10 ms Intervall
    
      loop
    
    Sub IncCounter_1
            SetTimer 1, 6
            CounterValue_1 ++
    End Sub
    
     
  • Anobium

    Anobium - 2020-09-15

    Morning,

    We need Boris. Boris Breuer bbreuer@users.sf.net

    He is our expert on the Tiny85.

    The timer code looks ok but the do while (CounterValue_1 - warteSeit) <10 loop is correct? Looks odd to me. CounterValue_1 has the range of 0 to 255 but warteseit will be the following sequence... 0, 10, 20, 30 .. 250, 4, 14, 24.... so, this test correct?

     
  • Philip Gansloser

    Hi Anobium,

    think I got it!
    Instead of

    SetTimer 1, 6 
    

    now I use

    TCNT1 = 6 ; 
    

    Writing the value directly into the Timer1 register TCNT1 works fine.
    Now it's possible to do a fine tuning of the time between the Interrup calls.

    The "SetTimer" command seems not working on ATTiny85.

    Thanks

     
    • Anobium

      Anobium - 2020-09-15

      Must be an issue in the Settimer method.

      Hopefully, one of the AVR guys can resolve for the future.

      Well done in resolving

       
  • Philip Gansloser

    Hi Anobium,

    thanks for the reply.
    The 'do while'-loop actually looks a bit weired, but it works fine.
    Here is a short sequence of the values and the calculated results:

    CounterValue_1 warteSeit (CounterValue_1 - warteSeit)
    . . .
    254 250 4
    255 250 5
    0 250 6
    1 250 7
    2 250 8
    3 250 9
    4 4 0
    5 4 1
    6 4 2
    . . .
    12 4 8
    13 4 9
    14 14 0
    15 14 1
    16 14 2
     
  • Frank Steinberg

    Frank Steinberg - 2020-09-17

    Hi,

    I found out that the handling for the TCNT1 register for the subroutine SetTimer is missing in the timer library (timer.h). I added that in the attached "lcd.h" - line 1191 - #ifdef Var (TCNT1) ...
    Please try it out.

    Frank

     

    Last edit: Frank Steinberg 2020-09-17
  • Anobium

    Anobium - 2020-09-17

    Let me know if this resolves the issue. And, I will copy to the release.

     
  • Philip Gansloser

    Hello Frank,

    thank you for your support and for providing the corrected timer library.
    I will test it soon and report back then.

    Philip

     
  • Frank Steinberg

    Frank Steinberg - 2020-09-18

    I've tested with this:

    ; ----- Chip configuration:
    #chip tiny85, 16.5
    
    ; ----- Include (software optimised) serial library:
    #include <SoftSerial.h>
    
    ; ----- Config serial UART for sending:
    #define SER1_BAUD 115200   ; 115200 baud
    #define SER1_TXPORT PORTB  ; PortB.3 = Pin2 on AtTiny85
    #define SER1_TXPIN 3       ;
    
    ; ----- Config Timer1:
    InitTimer1 OSC, PS_1_32
    
    ; ----- Test Timer1 methods:
    
    StartTimer 1      'start Timer1
    Ser1Send  13      'new line in Terminal
    Ser1Print Timer1  'send timervalue
    
    ClearTimer 1      'set timervalue to zero
    Ser1Send  13      'new line in Terminal
    Ser1Print Timer1  'send timervalue
    
    SetTimer 1, 100   'set timervalue to 100
    Ser1Send  13      'new line in Terminal
    Ser1Print Timer1  'send timervalue
    
    StopTimer 1       'stop Timer1
    SetTimer 1, 100   'set timervalue to 100
    Ser1Send  13      'new line in Terminal
    Ser1Print Timer1  'send timervalue
    

    Terminal output is:
    44
    45
    144
    100

    That's what to expect, so timer.h seems ok for me now. Please do another check anyway.

    Frank

     
  • Philip Gansloser

    Hello Frank,

    i have also tested the new timer library in my project with success.
    Works great!
    Thanks again for your efforts.

    Philip

     
  • Anobium

    Anobium - 2020-09-18

    Thank you. I have updated the distribution.

     

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.