Simple (?) math/flow problem

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd like to setup an ISR such that something happens every 100 cycles, but only for the first 1000 cycles. I'm sure this is simple but I'm not seeing it. Pseudo code:

ISR(TIMER0_COMPA_vect) {

	startupFlashCount++;
	if(startupFlashCount =< 1000) {
		if(startupFlashCount [IS A MULTIPLE OF 100]) {
			DO_SOMETHING;
		}
	}
}

Its that "IS A MULTIPLE OF 100" that I'm not getting right. Feel free to suggest a better method. Thanks.[/code]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

use two counters.

if (++count >= 100) {
    count = 0;
    if (actions < 10) {
        actions++;
        do_the_business;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks. Anything particularly inefficient about that?

Another thing that occured to me which would be "close enough" is that I could check the counter against a mask of 0x80 (128). Ie, any time the 1st 7 bits are clear, we have a match. In this case, the actual "100" and "1000" are arbitrary and not really important. I'm still not very good at bitwise stuff... any elegant way you can suggest to do that?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Its that "IS A MULTIPLE OF 100" that I'm not getting right.

(startupFlashCount % 100) == 0

(though not very efficient)

BTW:
Having the startupFlashCount++ in front of the if-block (instead of inside it) means that it will continue to count and will overflow and another "first 1000 cycles" periode begins. Is that intended?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hmm... no. Not intended.

Basically I just want to flash an LED approximately once every half second for the first 5 seconds (or whatever... its not that important) and then never do anything with that pin again (other code may affect that pin though) until the next time the AVR is power cycled.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Anything particularly inefficient about that?

Why would anyone ever worry too much about anything so trivial?

Compile and run in the Simulator. View the ASM code.

I would put serious money on any compiler being pretty good with translating that. Obey the standard rule of embedded systems: "use appropriate size variables" i.e. uint8_t

You might also simulate the % operator method. Just count the cycles and compare.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What's "the Simulator"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Avr studio has a simulator - a software implementation of the avr running on your PC. You can single step, set breakpoints and measure execution time. So if you're not sure what is hapoening in your code or you want to find out execution time, then the simulator might assist.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I just want to flash an LED approximately once every half second for the first 5 seconds

Quote:

setup an ISR such that something happens every 100 cycles, but only for the first 1000 cycles.

100 cycles == 1/2 second; giving an AVR clock speed of 200Hz.

Does this ultra-low frequency give best power consumption numbers? I'd think not much could be done, even simple timekeeping, at 200Hz. I'd think you would get better total power consumption by running faster and then getting back to deep sleep sooner.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You're making an assumption that no scaling has been done, aren't you?

I'm actually not sure. Its not my code... I'm just trying to put in the ISR. The comments state that it runs ever 5ms. 100 * 5ms = half a second. The clock is 8Mhz

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

You're making an assumption that no scaling has been done, aren't you?

??? No, I don't think I'm making any "assumptions". I re-examined the thread, extracted two pertinent snippets relating to timing (the initial post's "100 cycles" caught my eye), and then asked my question.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I didn't mean that to sound accusatory. You just sounded certain about the timing and I was just asking if you were making an assumption or if there's some absoluteness to the timing that I'm not understanding.

I'm not very well versed at all in AVR code or methods.

Assuming the comments are correct, this runs at 8Mhz with scaling that provides a 5ms timer. So my intention with the 100/1000 is that it would "fire" every half second for 5 seconds.

Anyway, David's method is fine. Problem has been solved.

Thanks Kartman, for the Simulator explanation. I've never been able to get anywhere at all with AVR Studio so I've stuck with WinAVR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Thanks Kartman, for the Simulator explanation. I've never been able to get anywhere at all with AVR Studio so I've stuck with WinAVR.

It is well worth your while to gain experience with the Simulator. It clears up many simple timing questions. And reassures you that avr-gcc is sh*t-hot at translating C code.
It can also confirm that some C sequences are better than others.

Mind you, it is seldom critical. i.e. just write straightforward code. it will be best for 95% of the time. For the other 5% it really won't be noticeable.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers. Thanks David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
ISR(TIMER0_COMPA_vect)
{
    // two bytes would be required to count to 1000.
    static uint8_t ls_count=100;
    static uint8_t ms_count=10;

    --ls_count;
    if(!ls_count) {
        ls_count=100;
        DO_SOMETHING;
        --ms_count;
        if(!ms_count) {
            DISABLE_INTERUPT
        }
    }
}

By "cycles", I think OP meant ISR executions.

"SCSI is NOT magic. There are *fundamental technical
reasons* why it is necessary to sacrifice a young
goat to your SCSI chain now and then." -- John Woods

Last Edited: Mon. Oct 22, 2012 - 12:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Great, thanks! And yes, that's what I meant.