Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
ISR questions

 
Post new topic   Reply to topic    Forum Index -> ZBasic Native Mode
Author Message
sturgessb



Joined: 25 Apr 2008
Posts: 252
Location: Norwich, UK

Posted: 08 January 2009, 13:02 PM    Post subject: ISR questions Reply with quote

Just a few (probably silly) questions regarding Interrupt Routines.

Q1:
I know that Interrupts have different priorities. Does that mean that if you have ISRa running and ISRb (with a higher priority) gets triggered mid way though, does ISRa continue running once ISRb is finished or does it get forgotten about?

Q2:
Does 'Lock Task' work inside an ISR so that it is guaranteed to run in full? if ISRb is triggered while ISRa is mid way though but has 'Lock Task' around its contents, what happens? does ISRb get run when ISRa is finished?

Q3:
Is it possible to get and ISR to run on each TIMER Clock Period? Or would i just have to have a loop that checks when timervalue = 1 and then reset to 0 each time?

Thanks

Ben
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2681
Location: Portland, OR

Posted: 08 January 2009, 15:13 PM    Post subject: Re: ISR questions Reply with quote

sturgessb wrote:
Does that mean that if you have ISRa running and ISRb (with a higher priority) gets triggered mid way though, does ISRa continue running once ISRb is finished or does it get forgotten about?
A pending interrupt is never ignored. Multiple, near simultaneous, interrupts are handled in one of two ways, sequentially or nested, with the former being recommended.

In the case of two or more simultaneously triggered interrupts, the ISRs will be handled based on the priority of the interrupt. The interrupt priority is indicated by the position of the interrupt vector in the interrupt vector table; the lower the address, the higher the priority. The layout of the interrupt vector table can be found in the datasheet for the corresponding AVR.

Under sequential handling, when an ISR begins executing, interrupts are globally disabled and when it completes execution, it re-enables interrupts. If another interrupt fired during that time, its ISR will then execute very shortly after interrupts are globally enabled.

For nested handling, interrupts are not globally disabled in the ISR. If another interrupt triggers, its ISR will begin execution immediately and when that ISR completes the interrupted ISR will resume execution. The reason that this method is highly discouraged is that stack use is potentially much greater than in the sequential case.

If you define an ISR in ZBasic, it is configured for sequential processing unless you define it with the Naked attribute in which case you have complete control and can configure it for either sequential or nested handling.

sturgessb wrote:
Does 'Lock Task' work inside an ISR so that it is guaranteed to run in full?
As described above, a typical ISR will always run to completion without interruption. Invoking LockTask() in an ISR has no effect on ISR execution. Rather, it will lock the task that was running at the time the ISR was invoked.

sturgessb wrote:
Is it possible to get and ISR to run on each TIMER Clock Period?
If you mean each time the TCNT increments, the answer is no. There are two basic types of interrupt handlers related to timers: overflow and compare match. The overflow interrupt will be triggered when the timer reaches its maximum count (and also when the timer counting changes direction at zero in PWM modes). The compare match interrupt will be triggered when the TCNT register matches the corresponding OCR register, of which timers may have one, two or three.

It is considered "best practice" to keep ISR execution time as short as possible, performing only the absolute minimum operations necessary. Often, this means just setting a global flag that is later recognized by the Main() task or an auxillary task. In other cases, the minimum processing is necessarily more involved but an effort should be made to keep the processing as short as possible.
Back to top
spamiam



Joined: 13 Nov 2005
Posts: 693

Posted: 08 January 2009, 17:58 PM    Post subject: Re: ISR questions Reply with quote

[quote="dkinzer"]
sturgessb wrote:
sturgessb wrote:
Is it possible to get and ISR to run on each TIMER Clock Period?
If you mean each time the TCNT increments, the answer is no. There are two basic types of interrupt handlers related to timers: overflow and compare match....


Using the "compare match", you can set the matching value to zero and then the compare match interrupt will fire on every other clock cycle of the timer.

Needless to say, it is not reasonable to have very small prescaler settings on the timer because the timer will fire again even before the last interrupt is processed. In general, there are 10-20 clock cycles of overhead in processing an interrupt, plus whatever code is in the interrupt. So, a prescaler of /32 or more might be reasonable with the interrupt firing every 64 main clock cycles.

Setting a prescaler as above would create interrupts about as frequently as using a prescaler of /1 and then setting the compare match value to 63. [I believe this is accurate, but I always need to refer to the datasheet and figure it out again every time I try something like that.]

Using the "naked" attribute will probably will reduce the ISR overhead, but I believe then you must manually handle all the pushing and popping as well as controlling interrupts.

-Tony
Back to top
sturgessb



Joined: 25 Apr 2008
Posts: 252
Location: Norwich, UK

Posted: 08 January 2009, 23:46 PM    Post subject: Reply with quote

Thanks Guys

Last edited by sturgessb on 09 January 2009, 10:49 AM; edited 1 time in total
Back to top
sturgessb



Joined: 25 Apr 2008
Posts: 252
Location: Norwich, UK

Posted: 09 January 2009, 1:04 AM    Post subject: Reply with quote

Am i missing something, I have the folowing

Code:

Register.TCCR2B = &H03
Register.OCR2A = 100
Register.TIMSK2 = Bx0000_0010


with...

Code:

ISR Timer2_CompA()
   tempuint = tempuint + 1
END ISR


I would expect this to increment tempuint every time that timer2 TCNT2 value reached 100. but this does not seem to be true, the ISR is running but not to that expected pattern and changing the ocr2a value seems to not make any difference. i must be not following 100%

cheers

ben
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2681
Location: Portland, OR

Posted: 09 January 2009, 3:11 AM    Post subject: Reply with quote

sturgessb wrote:
Am i missing something
You really should set the other timer control registers, too. In the case of Timer2, that means Register.TCCR2A. Timer1 has one additional control register. Also, you should clear the TIFR bits (see my earlier posts) just to be certain that you won't get an interrupt immediately when you set TIMSK.

Assuming that TCCR2A is zero, I would guess that Timer2 counts from zero to 100 at which time you should get a compare match interrupt. The timer will continue to count upward to 255 and then wrap to zero and repeat the process.

The timer can be programmed in a mode so that it will reset to zero when it gets to the OCRA value. Perhaps that's what you expected but it isn't configured that way. Consult the "waveform generation mode" table and look for CTC mode if that's what you want.
Back to top
sturgessb



Joined: 25 Apr 2008
Posts: 252
Location: Norwich, UK

Posted: 09 January 2009, 9:51 AM    Post subject: Reply with quote

Ah yes that will be it, as its wrapping the trigger point will always be the same whether i set it to 1 or 255. Register.TCCR2A = Bx0000_0010 does the job.

Thanks Don
Back to top
sturgessb



Joined: 25 Apr 2008
Posts: 252
Location: Norwich, UK

Posted: 09 January 2009, 12:05 PM    Post subject: Reply with quote

Is it possible on the 24n to use timer0? i know its used for the RTC, but seeing as i dont want to use any RTC functions, could i take control of it and use for my own purpose?
Back to top
sturgessb



Joined: 25 Apr 2008
Posts: 252
Location: Norwich, UK

Posted: 11 January 2009, 16:46 PM    Post subject: Re: ISR questions Reply with quote

spamiam wrote:

Using the "naked" attribute will probably will reduce the ISR overhead, but I believe then you must manually handle all the pushing and popping as well as controlling interrupts.


If this a fact, if so what kindof speed improvement could be acheived?

Ben
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2681
Location: Portland, OR

Posted: 12 January 2009, 3:50 AM    Post subject: Re: ISR questions Reply with quote

sturgessb wrote:
If this a fact, if so what kindof speed improvement could be acheived?
Sometimes not much, sometimes a lot. Much depends on the ZBasic code that you write, the C code that the ZBasic compiler generates from it and the assembly language code the avr-gcc compiler generates from the C code.

Generally speaking, it is recommended to pursue hand optimization of ISRs (or any other code) if performance measurements show that it is necessary. If and when performance needs to be improved, you are well advised to ponder the design to see if you can change the algorithm or some of the design criteria to fundamentally improve the performance.

After you've gotten the best performance that you can get with the best design/algorithm that you can envision and the performance still falls short of the goal, that is the time to look at more esoteric optimization techniques like using C or assembly language code.

In any event, if you're interested in looking at the AVR assembly language code that gets generated for your application, add the --list and --keep-files options to your project. After compilation, you should find a file named zxTempDir/<proj>/<proj>.lss below your project directory, where <proj> is the name of your project. The .lss file contains assembly language code representing the actual instructions that the AVR will be executing. To understand the meaning of the instructions in this file, you need to understand AVR assembly language instructions and know something about the interrupt vector table. You also need to be able to relate the generated assembly language back to the original code that you wrote. If you've not studied compiler construction and understand common code generation idioms, this may be a daunting prospect.

If you already understand the fundamentals of assembly language code, you can probably dive right into the Atmel documentation for the AVR Instruction Set. If you don't yet understand the fundamentals, you can probably find some sites on the Internet or find some books on the subject at a technical bookstore.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Native Mode Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2013 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group