Full Version : VERY SIMPLE TIMER
avr >>BEGINNERS & BUTTERFLIES >>VERY SIMPLE TIMER


AVR_Admin- 04-13-2006
VERY SIMPLE TIMER in ASM by RETRODAN


QUOTE
I'm sorry I'm a slow learner... I don't know which ports and registers to properly setup parameters for my RTC


PRELUDE:

In my latest Butterfly Bootloader (Cricket) I wanted to totally change the upload logic. I got tired of holding down the joystick in the middle postion and I think it's wearing out. I designed it mostly for my own personal use and find it very difficult to use anything else now that I am used to how much easier it is to use.

What I wanted was when the Cricket was RESET it would "chirp" once then calibrate the OSciallator then "chirp" again and automatically wait for you to either upload a new program or to move the joystick to launch a program. After a reasonable amount of time it would "chirp" one last time to let you know it was going to sleep to conserve battery.

I used a timer to do this, and perhaps this listing will help you to get started with your RTC in ASM. The only lines you really need to worry about are the first three and four near the end that read the clock and exit when high byte gets to 255.

THE MATH:

Here is how the Math works out:

The clock speed I chose to use for this part of program is 8Mhz and writing a 5 to TCCRB1 set the "prescaler" to 1024.

That mean that the timer will only "tick" after 1024 clock cycles.

CODE

Tick = PreScale / 8Mhz = 1,024/8,000,000 = 0.000128 Sec


Since I am using 2 bytes for the clock/timer the lower-byte is going to "roll-over" and increment the high-byte when it hits 256:

CODE

HighByteTick = 256 x 0.000128 = 0.032768 Sec


Since I Exit when HighByte gets to 255:

CODE

Exit=255 x 0.032768 = 8.35 Seconds


When I initially worked this out, I figured the 8 seconds was way too short to start uploads, and was going to extend it, but it turns out that 8 seconds is ideal.

THE PROGRAM SEGMENT:

CODE

;----------------------------------;
; BOOT SENSORY LOOP              ;
; CHECK JOYSTICK & UART THEN SLEEP;
;----------------------------------;
           STS     TCCR1B,FIVE;PRESCALE /1024
           STS     TCNT1L,ZERO;START AT ZERO
           STS     TCNT1H,ZERO

BOOT_LOOP:  CLR     TMP
           SBIS    PINB,6      ;JOYSTICK UP
            RJMP   START_8MHZ    
           SBIS    PINB,7      ;JOYSTICK DOWN
            RJMP   START_2MHZ            
           SBIS    PINE,2      ;JOYSTICK LEFT
            RJMP   START_4MHZ
           SBIS    PINE,3      ;JOYSTICK RIGHT
            RJMP   START_1MHZ

           LDS     TMP,TCNT1L  ;CHECK CLOCK
           LDS     TMP,TCNT1H
           CPI     TMP,255
            BREQ   GO_SLEEP

      LDS     TMP1,UCSR0A;CHECK UART
           SBRS    TMP1,RXC
            RJMP   BOOT_LOOP  ;NO UART THEN LOOP


IN CONCLUSION:

So I don't profess to be an expert on using timers in ASM, but I hope that you can work-out your own MATH using this as an example, and get you on-your-way to designing your own RTC.



ADDENDUM:

Another example of the Math using the same clock and prescaler:

CODE

1 Sec = Clock/PreScaler = 8,000,000/1024 = 7812.5 Ticks
1 Sec= 7812.5 / 256 = 30.5 HighByteTicks


So if you waited for HighByte of clock to reach 30 or 31 you would have approx. 1 second but this not going to be very accurate.

2 Seconds for 71 HighByteTicks in not too far off.

If you set the prescaller to 256 instead of 1024 by writing a 4 (instead of 5) to TCCR1B then:

CODE

1Sec= 8000000/256 = 31250 Ticks
1Sec= 31250 / 256 = 122.07 HighByteTicks


So by waiting for high byte to reach 122 in this example you are getting about a second also.


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.