STAR Firmware by JonnyC - Source Code and Explanation

When using the internal xtal I seriously doubt that _delay_ms / delay.h were accurate anyway.

Oh yeah…I like the turbo timeout, this way you can use very high power FETS or numerous 7135’s drive it hard (barn burner) for a few seconds and have it auto throttle back to not overheat, it’s a very good firmware :slight_smile:

You know the problem with doing loops like that, right? It keeps the CPU active and running at near-full power, so it eats up the battery faster. It might be better to set an interrupt for some number of cycles in the future and then go to sleep, waking whenever the interrupt goes off.

You’re probably right about there being a better way. It sounds like he’s just doing it to institute brief pauses (blinks, etc) - and the CPU draws single digit milliamps. So I think it should work fine for his purpose.

I’m still interested in what you described though, do you have a link to info on setting that up for AVR’s?

No, I simply skimmed through the attiny13a reference manual, and I’ve done similar things in the past in DOS and in Linux. Looks like it works in a very similar manner… assign an interrupt handler, set the parameters for a timer, start it, then execute the instruction to put the main CPU into standby mode. It’ll then idle at low power until something wakes it up, such as the timer popping and calling the interrupt you assigned.

I am into learning so I might look into it, but my intended purpose with this loop is only to replace the _delay_ms() function. To me it looks like _delay_ms() is just a more complicated and byte heavier way of doing exactly what my loop does, also without using interrupts. My loop should therefor not be eating up much more CPU power than the _delay_ms() function that it’s replacing.
Or have I missed something? Is _delay_ms() using interrupts?

And besides, in the main() function of the STAR 1.1 source the while(1){} loop runs voltage monitoring (unless disabled), so the CPU is constantly doing stuff anyway. If using interrupts is optimizing for CPU load, the entire voltage monitoring function should be activated by an interrupt instead of being run in a while(1){} loop.

If you look at the end of the while(1) loop, you'll see a "sleep_mode();" command which will go to sleep and wait for the WDT interrupt. I didn't put it in there for power consumption though, just so that the ADC low voltage detection doesn't trigger immediately (it has to detect low voltage 8 times over 4 seconds, if I read my code right).

Ahh, I see. I missed that one. I took a look at sleep_mode() and can’t make sense of what it does… at all.

But that takes me back to the _delay_ms() function. It doesn’t appear to use any interrupts. I can’t really say for sure but the code certainly has none of the mumbo jumbo that’s in sleep_mode(). To me _delay_ms() appears to be just a bunch of loops waiting for increments until they are done. Can anyone verify? Is there anything in _delay_ms() that suggests it would use less CPU load than a simple loop?

I have refined my loop a bit: (using “_” to space out the lines to make it more resemble C code)

uint8_t A = 0;
uint16_t B = 0;
uint8_t C = 0;
uint8_t X = 0;

while(A < X) {
_B = 0;
_while(B < 25850){
__C = 0;
__while(C == 0){} // Odd! Having nothing here makes total loop extremely fast. Adding C loop slows it down, but it does not matter how many times C loop is run, total time is same.
_}
}

X is about 0.1 seconds, so entering 10 into this argument gives about 1 second delay.

I compiled a hex file with a bunch of modes and timers that has five instances of my delay and compared it to the same code but with the _delay_ms() instead, and the difference was 249 bytes. Considering the limited space for my tests I think it’s quite useful, as it has been the difference of having a too large hex file and not.

I haven’t actually gotten started writing and flashing attiny firmware yet. I just figured, as long as the chip has dedicated hardware for offloading wait loops, it might be worth using. However, that would only work if that interrupt and counter aren’t already being used for something else, and it doesn’t guarantee the CPU won’t be awakened early by some other interrupt.

I’ll know more after I really have a chance to dive in and start mucking with things. I’m planning to do some stuff with variable-speed slow strobes and underclocking and clock dividers, so timing will get a bit tricky.

I agree, but it appears that the _delay_ms() routine that comes with the package doesn’t use the it, so by replacing it with my loop I will not make anything worse.

It could perhaps get better with what you suggest but re-writing the existing _ms_delay() function to incorporate interrupts is way over my head. I just needed a few more bytes and, as it appears to me, I didn’t cause any increased CPU load.

Edit: If you have a look at the delay.h (which I assume comes along with AVR Studio) I think you’ll see what I mean. I’m doing the same thing as that routine, just less byte intensive, that’s all.

Thanks for testing this. I’ll probably be hurting for spare bytes when I start making changes, so every little bit helps.

Anyone know what cap value/size to use for off time memory? I've been post hunting, but just can't find it - not on Jonny's web site, not mentioned in the source code for STAR_off_time, not on Richard's website, can't find it here, can't find a thread for it, just can't find it, period... Help? I know it must be somewhere?? Is Jonny's STAR_off_time the only driver source code that uses it?

I believe it goes between an IO pin and ground, from a comfy pic somewhere...

1uF works well and is the specified value. It’s in the OP waaaay at the bottom. Other values work too though, HarleyQuin investigates this on page 5 of this thread.

In terms of other firmwares that use a cap, Tido’s firmware (BLF-VLD) supports offtime but I don’t remember if it worked with just a cap or a cap and a resistor or what.

If my memory is correct, the cap is 1uF X7R 0805 size and is connected between pin2 (PB3) and ground.

I got the ones recommended by comfychair:

http://www.ebay.com/itm/181283883536

The easiest would be to solder it to the 4th star and grounding ring, but there are other options. Others should be able to help with that...

Thanks all! I ordered those caps off of the eBay link. I missed mentioning of the cap value in the OP. My links to posts don't see to work - very frustrating... The comfychair link I could not find

I put the cap between star 4 and the ground ring on the 105c. For the AK-47A I want to keep the bottom free of components so I put the cap on two pads marked R4. It connects pin 2 and ground just like using the 4th star.

The 101-AK-A1 is a little more difficult, there is no straight forward location for the cap. I haven’t done one yet, I just stack 7135s on an AK-47 if I want more than 1 amp with a flat bottom driver.

You are not the only one - it seems different people are using a different number of posts per page, so a link to a specific post that works for one person is broken for somebody else. Site really shouldn't require the page number in links to specific posts, or something. Links in notification emails don't work either for the same reason I think.

I can only get to the post in that link by changing the '?page=7' to '?page=4' - https://budgetlightforum.com/t/-/25032?page=4#comment-573278

Oh -- found out more... I had my setting at 150/page while locusto03 must be using 30/page (experimented til I found one that worked). I set mine to 50/page, thinking that's the default?? Not sure...

Mine is set to 50/page. Don’t know if that is default or if I made the change.