# Attiny25/45/85 FW Development Thread

1911 posts / 0 new

## Pages

DEL
Offline
Last seen: 2 years 3 months ago
Joined: 06/28/2015 - 08:35
Posts: 559
Location: Canada

Tom E, have a look at 17.8 in the datasheet (page 129 in my PDF copy).

The analog input signal has to charge up a small internal sample+hold capacitor (through an internal series resistor) during the ADC conversion process. If you go too high with the impedance of the analog signal, this capacitor does not charge up all the way and you will get a low result from the ADC.

Atmel recommends keeping the output impedance of the analog signal below 10 kohm. The impedance of our signal is, for all practical purposes, the parallel combination of R1 and R2, or 1/(1/R1+1/R2). This means keeping R2 around 12 k or less.

That said, we are normally only using 8-bit resolution anyway, do we need the maximum accuracy? MikeC is already using 10x R values, not sure if he verified ADC accuracy.

For E-switch drivers, I vote for the Vcc/1.1Vref trick without any resistors.

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

DEL wrote:
Atmel recommends keeping the output impedance of the analog signal below 10 kohm. The impedance of our signal is, for all practical purposes, the parallel combination of R1 and R2, or 1/(1/R1+1/R2). This means keeping R2 around 12 k or less.

That said, we are normally only using 8-bit resolution anyway, do we need the maximum accuracy? MikeC is already using 10x R values, not sure if he verified ADC accuracy.

I’m way out the specs. I’m using 1M2 as R1 and 300K as R2. I went this high because R2 acts as a bleed resistor for the off time cap on my drivers. With calibration I have been accurate down to 0.02 volts across the cell range (test 2.8V up to 4.23V) on several drivers, so I’m fine with these resistor values.

However, I have yet to build a zener version of my latest drivers (I waiting for new ordered resistors, ordered wrong ones before). I want to keep R2 the same so this means R1 is going to be much higher (about 2M7), but this might just be too high. I won’t know until I’ve tested on a few drivers but if so I’ll have to use a lower R2 value to keep R1 from being too high.

Interesting stuff about using VCC for voltage monitoring but nothing I will be looking into. I’m already doing LVP, off time and E-switch on the same pin so I wouldn’t be gaining any pins by doing this, and I don’t really need the space either. All my drivers are made zener compatible from the start anyway, so I need the voltage divider resistors to measure double cell voltage levels.

Just a note on calibration though, maybe you already know: The ATtinys voltage reading results vary and can be calibrated, I guess everyone knows that already, but just remember that it’s the internal reference voltage that is different on each MCU. The actual ADC readings are very precise and do not need calibrating, it’s the varying internal reference voltage that needs calibrating. I know Toykeeper wrote somewhere that mathematical calculations are not accurate but I’ve found that I can use all of the mathematical calculations with very accurate results as long as I have calibrated the internal reference voltage. When the voltage level and ADC value is known you can calculate what the internal reference voltage is, save this value and use it later to calculate the voltage in the LVP routine. It’s the method I use and I’ve found it to be the most accurate single point calibration method yet.

Halo...
Offline
Last seen: 3 years 9 months ago
Joined: 12/15/2011 - 02:39
Posts: 3304
Location: Halo island

Yea, I’ve read the 1.1v reference can be off by 10%. But manually calibrating each chip feels annoying. I think it’s possible to do automated calibration. Measure the voltage provided by your programmer, write that into your calibration program, flash calibration program, run while powered from your programmer, it stores the result, flash final program.

Would you be using any kind of automated calibration?

DEL
Offline
Last seen: 2 years 3 months ago
Joined: 06/28/2015 - 08:35
Posts: 559
Location: Canada
Mike C wrote:
I’m way out the specs. I’m using 1M2 as R1 and 300K as R2. I went this high because R2 acts as a bleed resistor for the off time cap on my drivers. With calibration I have been accurate down to 0.02 volts across the cell range (test 2.8V up to 4.23V) on several drivers, so I’m fine with these resistor values.

Oh wow…I am surprised this works. So 10x should be no concern for Tom E.
How close are you to calculated values before calibration? Did you try different ADC clock dividers? Possibly a slower ADC clock can help in this case.

I just use the standard calculated values for the ADC and do not bother to calibrate. Most of my drivers read around 5-8 mV low high, but that is good enough for a 5-level flash out. I do have a ‘cell-impedance’ mode that flashes out cell+circuit impedance. Great as a sanity check on a new light, or to compare cells. But here I use differential voltage (no-load vs. 1×7135 load, 10-bit resolution) and absolute accuracy is less important. I also do a primitive over-sample/filter running on an interrupt to get consistent readings.

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Ok, did the test of adding turning off the AtoD during sleep mode, and first of all, it functions fine - can still do AtoD voltage measurements after sleep mode, so I know it restores it properly (seems to from testing), and it cuts the parasitic drain to about 1/2 -- Great News!

Using a modded SupFire M2-Z (from MtnE) running a wight 22mm FET+1 v013, on a good AWT IMR 2500 cell @4.13v:

Before the AtoD mod: 4.95 mA running the CPU, sleep mode: 0.314 mA

After the AtoD mod:    4.95 mA running the CPU, sleep mode: 0.154 mA

So thanks to Halo, a simple code mod helped big time.

At a drain rate of 0.314 mA, it would take 132.7 days (19 weeks - 4.4 months) to drain 1 amp-hour from a cell.

At a drain rate of 0.154 mA, it would take 270.6 days (38.7 weeks - 8.9 months) to drain 1 amp-hour from a cell.

The next easier thing to try would be to use the 10X factor on the resistors. Since I previously proved the voltage divider is responsible for about 1/2 the parasitic drain, then this mod would greatly reduce parasitic drain to a point where it should be.

New code (cleaned up comments/formatting):

```/**************************************************************************************
* sleep_until_switch_press - only called with the light OFF
* ========================
**************************************************************************************/
void sleep_until_switch_press()
{
// This routine takes up a lot of program memory :(

//  Turn the WDT off so it doesn't wake us from sleep. Will also ensure interrupts
// are on or we will never wake up.
WDT_off();

ADC_off();     // Save more power -- turn the AtoD OFF

// Need to reset press duration since a button release wasn't recorded
pressDuration = 0;

//  Enable a pin change interrupt to wake us up. However, we have to make sure the switch
// is released otherwise we will wake when the user releases the switch
while (is_pressed()) {
_delay_ms(16);
}
PCINT_on();

//-----------------------------------------
sleep_mode();  // Now go to sleep
//-----------------------------------------
// Alternate method? --> set_sleep_mode(SLEEP_MODE_PWR_DOWN);

// Hey, someone must have pressed the switch!!

PCINT_off();   // Disable pin change interrupt because it's only used to wake us up

ADC_on();      // Turn the AtoD back ON

WDT_on();      // Turn the WDT back on to check for switch presses

}  // Go back to main program
```
Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

I did find 47K and 220K 0805 resistors, thanks to FastTech's cheap assortments.

Using the same M2-Z light/FET+1 driver, swapped the R1 and R2 and it seems to work perfectly, exactly as predicted - 1/10 the parasitic drain -- now 0.016 mA, and I blink out the voltage, tried two cells one at 3.6v, and one at 4.1v and it blinked out the proper voltage.

To tell you the truth, I'm not that concerned about the accuracy of the voltage reading - if I'm under 0.2v, I'm happy with it, as long as it's within 0.1v.

So I think 7 years or so on parasitic drain ain't so bad. Probably sitting on a shelf, a battery would drain faster?

DEL
Offline
Last seen: 2 years 3 months ago
Joined: 06/28/2015 - 08:35
Posts: 559
Location: Canada

Nice going Tom

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

DEL wrote:
How close are you to calculated values before calibration? Did you try different ADC clock dividers? Possibly a slower ADC clock can help in this case.

Generally they are close enough and I was happy with that after flashing quite a few 85s… but then there was this one MCU that gave rather poor results. I just had to find out why. Turned out to be that the internal voltage reference was 1.0v which is within the tolerance according to the datasheet but gave rather poor voltage measurements. I guess they where still usable but it was a thorn in my side that I couldn’t let go, so I wrote a calibration routine to take care of it.
FmC
Offline
Last seen: 4 weeks 14 hours ago
Joined: 03/31/2013 - 05:23
Posts: 2197
Location: Brisbane, AU

That’s great news, Tom.

I’ll be putting a couple more drivers together soon (Fet+1), so will use the higher value resistors, & will re-compile the code with the ADC off/on values.

I’m glad I asked the question over in the other thread!

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Ohh, yeah, once again I'm all discombobulated. There was some overlapping goin on, but of course I forgot where/how/who this all originated .

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Interesting Sharpie. This is a just another couple of cases with us to miss some obvious, easy improvements, it seems like. Many of us, myself included, are reluctant to make changes to our basic Atmel driver design because of unknown risks, and proven history of what we have working.

For today, it seems like RMM drives the hardware, and TK drives the firmware directions. Rightfully so, really, because RMM is the only one true reseller (and major designer/builder, etc.) of these budget custom drivers en-mass, and TK has a proven track record of delivering stable, high performing feature rich firmware.

The e-switch support, along with the 25/45/85 usage has been not so readily adopted though and for good reasons – it’s limited to specialty lights, usually not so budget friendly, and the modding is more complicated. But I feel the two go hand in hand – you can do a lot more with an e-switch for the UI, therefore you want more memory/storage to take advantage of it. The two main gripes with e-switch lights has been:

- lockout ability, because the side switch’s can be activated easier than a tail switch

- parasitic drain concerns

I’ve solved the lockout with supporting a lockout sequence in the UI, while these mods really solve the parasitic drain issue, with calculated drain in the 7 year range for 1 amp-hour, and puts in right smack dab in the middle of name brand lights like the latest models of NiteCore and Sunwayman.

DavidEF
Offline
Last seen: 14 hours 33 min ago
Joined: 06/05/2014 - 06:00
Posts: 7699
Location: Salisbury, North Carolina, USA

Tom E wrote:
Interesting Sharpie. This is a just another couple of cases with us to miss some obvious, easy improvements, it seems like. Many of us, myself included, are reluctant to make changes to our basic Atmel driver design because of unknown risks, and proven history of what we have working.

For today, it seems like RMM drives the hardware, and TK drives the firmware directions. Rightfully so, really, because RMM is the only one true reseller (and major designer/builder, etc.) of these budget custom drivers en-mass, and TK has a proven track record of delivering stable, high performing feature rich firmware.

The e-switch support, along with the 25/45/85 usage has been not so readily adopted though and for good reasons – it’s limited to specialty lights, usually not so budget friendly, and the modding is more complicated. But I feel the two go hand in hand – you can do a lot more with an e-switch for the UI, therefore you want more memory/storage to take advantage of it. The two main gripes with e-switch lights has been:

- lockout ability, because the side switch’s can be activated easier than a tail switch

- parasitic drain concerns

I’ve solved the lockout with supporting a lockout sequence in the UI, while these mods really solve the parasitic drain issue, with calculated drain in the 7 year range for 1 amp-hour, and puts in right smack dab in the middle of name brand lights like the latest models of NiteCore and Sunwayman.

Yeah, so…

When is the newest model TomE light gonna hit the shelves?

The Cycle of Goodness: “No one prospers without rendering benefit to others”
- The YKK Philosophy

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Already has, hit my shelf that is...  I'm more afraid of breaking the shelf from all the weight. 1,000's of lumens sure is heavy.

Like many of us, my family thinks I'm nuts for some reason... Showed my office (flashlight shop) to my sister over the weekend ("this is where I reflow the boards", "these are the parts", etc.), and she was at first stunned, then of course comes the question on selling them, making money, etc... I still can't figure out how to make money on these - too much time. Better to sell \$6 lights for \$50, and know how to advertise with a catchy name, like G700?

DrJones
Offline
Last seen: 4 years 3 weeks ago
Joined: 01/05/2011 - 13:30
Posts: 1044
Location: Frankfurt, Germany

I use internal VCC measurement all the time with my ATtiny?5 builds. With my RGBW driver (ATtiny85) the standby current is about 0.3 µA, it survives a battery change running (well, sleeping that is) from the 10 µF 0805 buffer cap.

Halo: I only use my free time for firmware development; and (since 2 years with 2 small kids at home) mostly don’t have much of it. I only occasionally have enough time and leisure to do something new. H17F & Icarus took me about a year in the end. However I did spend a lot of time on firmware – especially on optimizing code. However the money I get from my hobby mostly stays in my hobby (e.g. LD50, SL2, TM16GT, soon a Meteor…)

And of course I spent quite some hours on the PID algorithm. I tried a simple stepdown first, but to which level depends on the host and environment, so I soon wanted to have the real thing, PID. I wrote a simplified thermal simulation (in PostScript ) with PID regulation and changing environment, and it was quite nice. Then I made an implementation with MCU limitations (integer math and limited accuracy), and it was quite bad – well, the thermal regulation was still fine (PID works under much worse conditions), but the light output was quite unsteady (PID is not designed to keep the ‘heating power’ steady, just the temperature). I spent several hours to think of alternative PID-like aproaches and finally made one I’m happy with. It also seems a bit more tolerant to a wide range of hosts (i.e. their thermal properties). So it’s actually not the normal PID algorithm, but one that combines P-, I- and D-terms in a different way.

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Ahh, I see – no voltage divider resistors, of course! That’s an extremely low draw in sleep, what it should be. Cool!

Halo...
Offline
Last seen: 3 years 9 months ago
Joined: 12/15/2011 - 02:39
Posts: 3304
Location: Halo island

Tom E wrote:

Before the AtoD mod: 4.95 mA running the CPU, sleep mode: 0.314 mA

After the AtoD mod:    4.95 mA running the CPU, sleep mode: 0.154 mA

So thanks to Halo, a simple code mod helped big time.

If at some point you switch over to the internal vcc measurement then, before sleep, you need to disable the Analog Comparator (or not leave 1.1v ref set as input). Otherwise the 1.1v ref will remain on and eat power during sleep.

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Narsil -> Tiny85's Only

Sorry, haven't been posting here much. Got a new release/update of Narsil posted (shared google drive folder), nothing major, but improvements for the Indicator LED support - documented in the manual. I now have this latest version installed in 6 lights, with the 47K and 220K resistors, from a SWM C20C to a SupFire M6 SRK style light. I still want to put it in smaller lights, and bigger lights. 5 of the 6 have an indicator LED and they all drain at 0.016 mA with the indicator/locator LED off.

Here's the new manual section that includes the new features. Now the Locator LED feature is turned OFF when in Lockout, and you can quickly turn it OFF and ON. I found this comes in handy.

# Indicator LED

When enabled, the Indicator LED can be used for the following:

• to locate the flashlight in the dark (Locator feature)
• to blink out the battery level
• as an secondary indicator of blinking when the main LED blinks

When the Locator feature in enabled, the LED can be temporarily turned OFF, by:

• with the main LED OFF, 1 quick click followed by a click&hold til the main LED goes off. To toggle it back on, repeat the same procedure (OFF -> quick click, click&hold)

Note the locator feature is turned OFF when the light is in Lock-Out.

Dutcheee
Offline
Last seen: 6 days 15 hours ago
Joined: 12/19/2015 - 21:40
Posts: 474
Location: Netherlands

Hi Tom E,

I just did my very first flashing attempt! It’s for an e-switch mod on the BLF A6 17DD driver.

All goes quite well, test in AVRdude is ok. So I guess I rewired the clip O.K. and the BLF driver is programmable and not locked.

The Attiny13A is now erased, and Narsil seems the nicest option for my DQG 4 mod. Nice work on the firmware!
I would like to enable beacon mode, do I need to change the .C file for that in wordpad?

When trying to flash Narsil.hex from your google drive I get the following error:

avrdude: ERROR: address 0×0410 out or range at line 65 of narsil.hex
avrdude: read from file ‘narsil.hex’ failed

Am I missing something, or might there be a flaw in the .hex file?

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

Narsil is written for ATtiny25/45/85. It won’t run on a 13A. The error message is because it won’t fit. Even if it did fit it wouldn’t work as registers are different.

Dutcheee
Offline
Last seen: 6 days 15 hours ago
Joined: 12/19/2015 - 21:40
Posts: 474
Location: Netherlands

OK, so I guess I missed something

Is there a momentary firmware that would fit the ATtiny13A? STAR momentary maybe?

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

Dutcheee wrote:
OK, so I guess I missed something

Yep, the title of this thread

Dutcheee wrote:
Is there a momentary firmware that would fit the ATtiny13A? STAR momentary maybe?

I have no idea what flavors of 13A firmware there are now days, I’ve been done with that little thing since ages ago. You should be able to find a flavor for your needs here somewhere: http://budgetlightforum.com/node/38364
Dutcheee
Offline
Last seen: 6 days 15 hours ago
Joined: 12/19/2015 - 21:40
Posts: 474
Location: Netherlands

Mike C wrote:
Dutcheee wrote:
OK, so I guess I missed something

Yep, the title of this thread
Dutcheee wrote:
Is there a momentary firmware that would fit the ATtiny13A? STAR momentary maybe?
I have no idea what flavors of 13A firmware there are now days, I’ve been done with that little thing since ages ago. You should be able to find a flavor for your needs here somewhere: http://budgetlightforum.com/node/38364
Thanks, I’ll look for a FW with beacon mode on your provided link! And will try post in the right thread next time
Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Think it's called STAR momentary, or Momentary STAR, probably under JonnyC in TK's repository -- this should be the e-swtich version for 13A's. There may be others as well. I should make it cleared that Narsil is only for 85's. Even 45's don't have enough memory.

Yes, 13A's are obsolete in this thread . Talk bout tiny...

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

I’ve found that 1M3 as R1 and 300K as R2 are fine for voltage divider resistors and off time cap charging on single cell voltage drivers. However, I’ve just tested with 2M7 as R1 for double voltage drivers with zeners and found the off time cap charging to take a little too longer than I would like. My intended solution is to stick with the single cell resistors but use the 2.56V internal reference voltage instead of 1.1V. The variation on the 2,56V reference voltage is much higher but I already do calibration on the internal reference voltage.

Anyone tried using same resistors as single cell drivers, but switching to 2,56V for internal reference?

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

Mike C wrote:
Anyone tried using same resistors as single cell drivers, but switching to 2,56V for internal reference?

I have!

I sucked it up and replaced the resistor on the driver. It works just fine, now my off time cap measurements are all good, slow charging issues are gone. So for those interested, with the ATtiny25/45/85 the same voltage divider resistors can be used for both single and double cell drivers as long as you switch to the 2,56V for internal reference for double cell readings. I used the option without the external capacitor (page 134, table 17-3 in datasheet). You might have to do some sort of calibration because the datasheet specifies that the 2.56 vRef varies from MCU to MCU even more than the 1.1 vRef does.

I’ll test weather I can stick to 2,56V vRef for single cell drivers too, because if the readings are good enough after calibrating I will be able to clean up my code a little by removing a few #ifdef definitions.

Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Interesting, that’s all good to know, but not sure I understand how this helps or what problem it’s solving. A 1 cell driver and 2 cell driver have to be different anyway in hardware? You have to use a zener (tail switch) or LDO (e-switch) for 2 cells anyway, so not a big deal to use different resistor values for the 1.1v ref? Plus the complication of extra fine tuning for the 2.56v ref?

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

Tom E wrote:
Interesting, that’s all good to know, but not sure I understand how this helps or what problem it’s solving. A 1 cell driver and 2 cell driver have to be different anyway in hardware? You have to use a zener (tail switch) or LDO (e-switch) for 2 cells anyway, so not a big deal to use different resistor values for the 1.1v ref? Plus the complication of extra fine tuning for the 2.56v ref?

It solves my issue of slow cap charging times because of too high R1 voltage divider resistor. I was having issues, I asked a question, then tested and posted the answer. But lesson learned, I’ll just delete the question the next time.
Tom E
Offline
Last seen: 58 min 12 sec ago
Joined: 08/19/2012 - 08:23
Posts: 12873
Location: LI NY

Ahh, no problem. Sorry, didn’t understand – what was the impact/effect of the slow cap charging?

Mike C
Offline
Last seen: 3 weeks 4 days ago
Joined: 01/22/2014 - 08:03
Posts: 2486
Location: Sweden

Quick short presses just after turn on where measured as long off presses, and my normal long off presses where being measured as cold starts. I had to wait a short moment before doing mode changing. It was annoying enough for me to attempt solving.

fixed it
Offline
Last seen: 4 months 3 weeks ago
Joined: 12/08/2015 - 14:27
Posts: 396
Location: Canada

Mike C wrote:
It solves my issue of slow cap charging times because of too high R1 voltage divider resistor.
Maybe I’m being slow this morning but with both higher voltage and higher R1, shouldn’t you end up with roughly the same current and charge time?