avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] How to reserve registers


From: Jens Bauer
Subject: Re: [avr-gcc-list] How to reserve registers
Date: Sat, 17 Dec 2011 00:35:00 +0100

Hi Omar,

>> .global IncrementCounter
>> IncrementCounter:
>>     ldi r19, 1                          ; use to increment with carry
>>     clc
>>     adc counter_b0, r19
>>     adc counter_b1, r1
>>     adc counter_b2, r1
>>     adc counter_b3, r1
>>     ret

... or free r19 (this is an example of a pure interrupt only, which you could 
place directly in the interrupt vectors):

TIMER2_COMPA_vect:          ;[5]
    sec                     ;[1]
    adc     counter_b0,r1   ;[1]
    adc     counter_b1,r1   ;[1]
    adc     counter_b2,r1   ;[1]
    adc     counter_b3,r1   ;[1]
    reti                    ;[5]

                            ;[15]

Numbers in brackets are clock-cycles spent for the operation.
(eg. an interrupt takes 5 clock-cycles. If you don't place the code directly in 
the interrupt-vectors, you'd probably use rjmp to jump to the code; which means 
you'd need to add 2 extra clock cycles in that case.

If you need the counter to spend as little CPU-time as possible, you could 
alternatively do the following, but that means you'd get an unstable CPU-usage:

TIMER2_COMPA_vect:          ;[5]
    sec                     ;[1]
    adc     counter_b0,r1   ;[1]
    brcs    ad2             ;[2/1]
    reti                    ;[5]
                            ;(13)

ad2:                        ;[9]
    adc     counter_b1,r1   ;[1]
    brcs    ad3             ;[2/1]
    reti                    ;[5]
                            ;(16)

ad3:                        ;[12]
    adc     counter_b2,r1   ;[1]
    adc     counter_b3,r1   ;[1]
    reti                    ;[5]
                            ;(19)

-As I mentioned above; it would use slightly less CPU-time, but when you get a 
carry, the CPU-usage would generate a spike, which might not be what you want, 
so the above routine would spend between 13 and 19 clock cycles.


Love,
Jens



reply via email to

[Prev in Thread] Current Thread [Next in Thread]