STAR Firmware by JonnyC - Source Code and Explanation

It's a modified C, so the basics are there but there are some differences. There are some good guides to programming Atmel MCUs online. If you're not already proficient, and your goal is specifically to program Atmel, then I would focus on Atmel programming tutorials and not generic C or C++ tutorials.

Can I use C++ on the AVR?

It uses a limited version of C with some assembly-like conveniences thrown in for direct access to hardware features. An atmel-specific guide would be far more helpful than a generic C guide, and either one would be light years ahead of a C guide. Even a simple C program probably wouldn’t fit on an attiny.

The main ways that its version of C is limited are that the MCU lacks some basic operations — so if you use those it must emulate the process, and that takes up a lot of extra room. Specifically, floating point math is probably a bad idea (use integers instead), and it uses a lot of room for each multiply or divide instruction… so try to use addition, subtraction, and bitwise shifting instead. Also try to use 8-bit integers when possible, since that uses significantly less room than 16-bit or 32-bit.

Oh, and most standard C library functions will also use a lot of extra room… so you may want to write your own minimal versions instead, if you need anything from the standard libraries. The atmel-specific libraries are pretty decent and compact though.

Other space-saving tricks are to use data tables instead of logic (when possible), and try to avoid accessing ‘volatile’ variables or variables which were defined outside the local scope. Don’t use interrupts unless absolutely necessary. And always compile with -Os to optimize for size.

First off, thank you for all your help. Glad I didn’t go barking up the C tree.

So, my first attempt at testing/programming went without a hitch. That was on a pre-made board.

With that success in my pocket, I opened my Digikey box and pulled out one of the Attiny13As from the tube. First off, finding pin 1 was hell with these 50 something eyes. Had to use my 30X stereo microscope to see a slight notch above the pin (which didn’t match the datasheet, BTW). I’ve worked with much smaller chips and finding pin 1 was never that hard. Pin one found, I attached the Pomona clip to the bare chip and did a test. “Target not found” was the error message. Tried it a few times on two different chips—same error message. Dumbfounded, I remembered that I had the programmer set to 3.3 volts (which worked before on the star board), so I set it to 5 volts and tried it again. Success! Reads and writes every time at 5 volts. I don’t ask why anymore (especially since I’m using a $4 programmer with questionable components). I just declare victory and move on.

Hope this helps someone…

Anyone?

I'm not sure which would be the better replacement between the 0.22uF and 2.2uF. Either one of those is going to require some tuning and tweaking (not worth the hassle, in my opinion). My opinion: don't cheap out. Pay the extra $1, get the proper parts, and save yourself the headache.

Same goes for the Y5V. They aren't as temperature stable and may give you switching issues over time and with heat exposure. With off-time memory capacitor stability affects the timing, so you don't want it to drift too much.

If you’re only going to have one type of press and can do simple coding, don’t even bother with an off time cap. Using brown out detection method works very good.

For those that don’t know how to do it, here are the details:

For this to work you have to change a fuse settings in the AVRDUDE command. “Brownout detection” must be enabled. Snipped from Alex site:
“Example of working fuse bit configuration avrdude arguments: -U lfuse:w:0x79:m -U hfuse:w:0xed:m”

The code:
First you need a byte in the “no initialise” area. Define it like this:

http://pastebin.com/embed_iframe.php?i=W6F8DCRr

When the light is on normally (after all start up code) you set it to 0 with “OffCheck = 0”.

On startup when detecting a short press, just check if it is still 0 with “if (!OffCheck)”.

With the brownout fuse set the OffTime integer value survives short off presses because of the input capacitor. It decays away after about half a second. Decayed non initialised bytes are 0xFF (all bits 1), the exact opposite to 0 (all bits 0 ). Meaning if the OffCheck value is still 0 it was a short press. If it’s not 0, it has decayed to 0xFF meaning the light was off for longer.

Brownout detection works pretty well for distinguishing between short (< 0.5s) and long (> 0.5s) presses.

I haven’t made a STAR clone with it yet, but I have a non-STAR firmware published and working if you want a reference. It’s ToyKeeper/s7/s7.c in my repo linked below. It may be a bit on the fancy side though, with 15 different modes… but at least you don’t have to cycle through them to use just the basic “normal” modes.

ToyKeeper, I'm having no luck with the Offtime-Cap firmware. I'm not seeing any blinking when turning on-off-on etc. Maybe I don't understand how to use it, can you offer any advice?

? Blinking ?

The Offtime-Cap.c (.hex) firmware blinks out the ADC value of the voltage on the Offtime cap. This helps tweak the short press and medium press values for mode switching in other firmwares.

I think...!?!

Crux, does it turn on at all?

Maybe the first level is too low to light up the emitter.

And if it won’t change modes, the OTC values are probably way off.

I’m just guessing though.

Oh, wait. You mean offtime-cap.c? You might want to set BLINK_PWM to a higher number, perhaps. If it’s set high enough, it should at least blink once briefly after it turns on, since a zero will still register. But it’s half as bright as the regular blinks, and much shorter, so it might not light up at all if the #define is too low.

For a value of 203, for example, it should blink out “_ , ., _ _”. Or for a value of 0, it should blink out a single “.”.

But, of course, if it gives you a zero, the OTC is either not working or has been off for a long time.

Change the shift register code to this:

    buffer = (buffer << 1) | ((~PINB & (1 << SWITCH_PIN)) == 0);

This inverts the logical value of the input pin.

I must have a compile issue. The .hex from your repository runs, but the blinks are too short for my driver. When I change BLINK_PWM and rebuild (Studio 6.2) the PWM pin just goes to 1 volt (?!) with no pwm. The .hex file I made is 132 bytes yours is 688 bytes.

Let me redo this from scratch, I have something wrong here.

EDIT: Working fine now (deleted all and recreated project) - operator error. Thanks ToyKeeper!

Ah, now I see what you're talking about. (I obviously don't have any idea what I'm talking about!)

Okay, was BLINK_PWM the only thing you needed to change?

To help make it work on more drivers, I set it to 20 and re-published a new build. That way, even the “zero” blink will be at a level of 10, which should show up on almost anything.

Yes, all I changed was BLINK_PWM (to 40, because of my PWM-to-analog driver). 10 is probably fine for the bulk of the drivers. Of course after that success I also changed the Vref to Vcc just to see where the OffTime cap voltage range is. My driver runs on either two or three cells, so Vcc is stable at 5V. With 1.1V vref I was reading 255 on short clicks. With Vcc vref the counts were at 60 to 45 range. I'll need to do some more testing to find good values for medium and short presses.

So now I've been working with Battcheck.c to figure out where that count is. I plan on adding a second low battery step-down mode, one at 9V and one at 6V. Somehow I need to differentiate between three dead cells (9V), and two full cells (8.4V). Sounds like disaster waitin' to strike. :)

I not asking for help (yet) but I'm open to any advice.

THANK YOU for posting and hosting these programs as well as the others in the repository.

hey, im looking for a “step by step” tutorial for programing the Attiny13A useing an arduino,
just like drdanke did here in post #29, but if possible with more details and maybe even a video,

i like the idea of not need to buy the programer and the clips, and i do have the arduino alrady.

thanks.

Just so happens I have another USBasp on the way. I found the latest firmware and will update one of the units when it gets here. BTW: I’m not used to handling things like this with open chip leads w/o being grounded with a wrist strap. Has anyone had an ESD failure? I think when I’m done flashing the new firmware, I’ll slip it into some 3/4” clear heatshrink tubing and shrink the two ends.

I took the next step and loaded Atmel Studio 6.2 (along with some serious bloatware from Microsoft). So far, all the c files I’ve looked at compiled fine. Finding the optimization settings was a hoot. Looking at the code, quite a bit of it makes sense.

This is all coming together just in time, because the 22mm 16 X 7135 boards arrived from OSH today, and the F13 light is in transit. I’ve got two or three other projects underway as well plus a test bench.

Thanks to all of you for your help and support.

No ESD problems yet, and I've programmed a few.

If I want to change the switch pin to Star 3 and the temperature sensor to Star 4 of MTN_momentary_temp, should I simply modify these lines:

#define SWITCH_PIN PB3 to #define SWITCH_PIN PB4

#ifdef VOLTAGE_MON
volatile uint8_t adc_channel = 1;
#else
volatile uint8_t adc_channel = 2;
#endif

to

#ifdef VOLTAGE_MON
volatile uint8_t adc_channel = 1;
#else
volatile uint8_t adc_channel = 3;
#endif

also modify the adc_channel to 0x03 (PB3)

// Switch ADC to temp monitoring
adc_channel = 0x03;
ADMUX = ((ADMUX & 0b11111100) | adc_channel);
} else if (adc_channel == 0x03) {

Thanks for your comments.