STAR Firmware by JonnyC - Source Code and Explanation

1335 posts / 0 new
Last post
ToyKeeper
ToyKeeper's picture
Offline
Last seen: 4 hours 31 min ago
Joined: 01/12/2013 - 14:40
Posts: 10786
Location: (469219) 2016 HO3

dkuku wrote:
How to change the is_pressed() function to positive logic in momentary firmware?
I disabled the pullup resistor and soldered a pulldown resistor

If I understand your question correctly, you can do this by simply changing the “return (buffer” part to “return !(buffer” to logically invert the value.
dkuku
Offline
Last seen: 6 years 9 months ago
Joined: 02/24/2015 - 00:18
Posts: 3

Thanks, I will try it at home.
I’he tried it with ~ but it didn’t work. Smile

Crux
Crux's picture
Offline
Last seen: 2 months 3 days ago
Joined: 05/03/2011 - 16:27
Posts: 227
Location: Northcoast, Ohio, USA

Mike C wrote:
Crux wrote:

Because I'll be running an ATtiny13A from a regulated voltage, would it be better to switch the ADC Vref to Vcc rather than the Internal reference?

It seems like this would allow finer control of battery voltage monitoring. Of course the resistors will still be across the battery and the values will need to be tweaked.

Do I only need to change the REFS0 bit in the ADMUX register and adjust the low voltage stepdown values? Or is it even worth the bother?

-Crux

Looks like changing that bit is all you need to do to change Vref, but I haven't tried it myself. If you're using an off time cap you'll have to make a few more changes though.

So... how feasible would it be to switch the Vref such that the Int 1V would be used for the OTC measurements and Vdd is used for low battery? I'd like to be able to use this with two and/or three cells, and I figure 5V Vdd as Vref will make the ADC count a bit more accurate. But I now realize that a 1V Vref is much more ideal for the OTC measurement. EDIT: or I may just use 20 and 38 for the Med and Short values...

Anyone have any ideas?

vex_zg
Offline
Last seen: 3 years 5 months ago
Joined: 08/10/2014 - 11:08
Posts: 420
Location: Sweden
Crux wrote:

Mike C wrote:
Crux wrote:

Because I’ll be running an ATtiny13A from a regulated voltage, would it be better to switch the ADC Vref to Vcc rather than the Internal reference?

It seems like this would allow finer control of battery voltage monitoring. Of course the resistors will still be across the battery and the values will need to be tweaked.

Do I only need to change the REFS0 bit in the ADMUX register and adjust the low voltage stepdown values? Or is it even worth the bother?

-Crux

Looks like changing that bit is all you need to do to change Vref, but I haven’t tried it myself. If you’re using an off time cap you’ll have to make a few more changes though.

So… how feasible would it be to switch the Vref such that the Int 1V would be used for the OTC measurements and Vdd is used for low battery? I’d like to be able to use this with two and/or three cells, and I figure 5V Vdd as Vref will make the ADC count a bit more accurate. But I now realize that a 1V Vref is much more ideal for the OTC measurement. EDIT: or I may just use 20 and 38 for the Med and Short values…

Anyone have any ideas?

you can increase accuracy by calibrating the specfic PCB+voltage divider+MCU combo. Also you can get 10 bits out of ADC, as compared to 8 which are normally fetched in voltage monitoring. Just don’t do right shift of ADCL, but read both ADCL and ADCH. It’s a matter of do you trust more your regulated vref or internal vref, what’s more stable. For even more accuracy I’d check how voltage divider changes with temperature, ideally both resisotrs should increase R in the same percentage thus negating any temperature drift.

what is OTC?

edit: by calibrating I mean this: Get a calibrated multimeter, and for a number of ADC values throughout the range, note the value that DMM is showing. Then make a “adjustVoltageADC(ADCH,ADCL)” function that would actually have hardcoded values of mapping between ADC and calibrated DMM, and for each ADCH, ADCL pair it would interpolate what the actual voltage is. That’s how I plan to do it anyway.

Mike C
Mike C's picture
Offline
Last seen: 4 days 10 hours ago
Joined: 01/22/2014 - 08:03
Posts: 2585
Location: Sweden

Crux wrote:
So… how feasible would it be to switch the Vref such that the Int 1V would be used for the OTC measurements and Vdd is used for low battery? I’d like to be able to use this with two and/or three cells, and I figure 5V Vdd as Vref will make the ADC count a bit more accurate. But I now realize that a 1V Vref is much more ideal for the OTC measurement. EDIT: or I may just use 20 and 38 for the Med and Short values…
That should be no problem if you have the space. OTC measurement is only done once (during start-up sequence), once that’s done you should be able to switch Vref before starting up the ADC routine for voltage monitoring. But, just to try cover all bases, I am assuming that changing Vref does not have any affect on the output voltage when cap pin is set high to charge the capacitor.

Crux wrote:
Anyone have any ideas?
If you can live with checking for a short press only you could switch to the brown out detection method explained here: http://budgetlightforum.com/node/36932. As mentioned, this method can’t detect different off time lengths, but for simple short press checking this method is reliable, easier to code and saves you space.

vex_zg wrote:
what is OTC?
Off time capacitor.
ADV
ADV's picture
Offline
Last seen: 4 months 3 days ago
Joined: 05/20/2014 - 12:03
Posts: 47
Location: Michigan, USA
dkuku wrote:
Thanks, I will try it at home. I’he tried it with ~ but it didn’t work. Smile

I have a circuit I have been playing around with with a similar situation where the switch is coming from the positive supply (about 4V). With a pull-down resistor, removing the call out to enable the pull-up on the pin, and ToyKeepers mod above, I have the levels changing without an issue.

ADV

dkuku
Offline
Last seen: 6 years 9 months ago
Joined: 02/24/2015 - 00:18
Posts: 3

works also for me also.thanks.

Jaidmaster
Jaidmaster's picture
Offline
Last seen: 9 months 1 week ago
Joined: 09/05/2013 - 15:02
Posts: 101
Location: Colorado

Just got my USBASP in yesterday. I had been building some hex files while I waited for it to arrive.
I just finished up my Convoy C8 with off-time, no memory, MN-L-M-T. No stars on the 105D complicated it a bit, but once I got the levels I wanted (using on-time STAR) I flashed my off-time and then soldered the 1uF directly onto the attiny pins between 2 and 4. PWM level 4 is the lowest the LED would turn on (2800mA 105D w/ XM-L2).
My SRK clone is next on the list.

Thanks!

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 4 hours 31 min ago
Joined: 01/12/2013 - 14:40
Posts: 10786
Location: (469219) 2016 HO3

Jaidmaster wrote:
… soldered the 1uF directly onto the attiny pins between 2 and 4.

My SRK clone is next on the list.

Ooh, I hadn’t thought about soldering one there… that’s a clever solution. Smile

For the SRK, be sure to take a look at ToyKeeper/Ferrero_Rocher/* in my code repo. Everything in there should run on a SRK (with attiny13a) and it’s all enhanced compared to STAR_momentary.

Jaidmaster
Jaidmaster's picture
Offline
Last seen: 9 months 1 week ago
Joined: 09/05/2013 - 15:02
Posts: 101
Location: Colorado

Thanks – will take a look. Need to get it disassembled first.
I thought about adhering the cap on top of the attiny and just run some small wire down to the pins – thought this would make it easier to reflash (out of the way of the clip), but figured I’d do that next time I need to flash it. Looking forward to trying it out tonight.

ToyKeeper wrote:
Jaidmaster wrote:
… soldered the 1uF directly onto the attiny pins between 2 and 4.

My SRK clone is next on the list.

Ooh, I hadn’t thought about soldering one there… that’s a clever solution. Smile

For the SRK, be sure to take a look at ToyKeeper/Ferrero_Rocher/* in my code repo. Everything in there should run on a SRK (with attiny13a) and it’s all enhanced compared to STAR_momentary.

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 4 hours 31 min ago
Joined: 01/12/2013 - 14:40
Posts: 10786
Location: (469219) 2016 HO3

If you use anything with a battery check mode or low-voltage protection, you might want to check the calibration first. You can get raw voltage ADC values with the battcheck firmware; just connect a battery or power source at the voltage where you want to set a threshold, and it’ll blink out the number to plug into other firmwares.

ZOTZ
ZOTZ's picture
Offline
Last seen: 4 years 8 months ago
Joined: 02/03/2015 - 15:13
Posts: 7
Location: montreal, québec

Hi everyone, after several try to resolve a strange problem with output i have to come back to get a good answer! I installed the firmware STAR mom , with e switch on pin number 2. I reprogram almost 30 drivers , may be 20 working good but some work strangly. If i put on turbo mode , even if a reprogram the timer to 120 sec or more or less… it will turn to lower mode after 5 sec. I changed a bunch of values, remove turbo mode, reprogram and get the same result.

I resolded every 7135 to check if it was a bad ground but i got the same result.

Is there a line of code that i could remove that could influence this situation???

Any idea what could go wrong with those driver? a bad 7135???

tks for your help

_the_
_the_'s picture
Offline
Last seen: 1 day 13 hours ago
Joined: 07/08/2011 - 06:22
Posts: 3647
Location: Finland

Stepping down in 5s sounds like a low voltage protection. Have you checked what voltage that driver thinks it's getting? See TK's calibration tip above.

=the=

 

ImA4Wheelr
Offline
Last seen: 2 months 2 weeks ago
Joined: 02/03/2013 - 14:51
Posts: 7935
Location: SC

^ Yeah, get a DMM voltage reading at the MCU pin connected to your voltage divider used for LVP.  If low, check out the 2 resisters in your voltage divider.

ZOTZ
ZOTZ's picture
Offline
Last seen: 4 years 8 months ago
Joined: 02/03/2015 - 15:13
Posts: 7
Location: montreal, québec

Ill Check tonight with the TK battcheck program … I think your Right on that one.

Easy think to do is to remove battery voltage monitoring and see how it work.

That will point out what is the exact problem.

Tks for your answer!

ZOTZ
ZOTZ's picture
Offline
Last seen: 4 years 8 months ago
Joined: 02/03/2015 - 15:13
Posts: 7
Location: montreal, québec

Tks for all your answer. I check the voltage with the program of TK and i got 181 for result. Resistor have been check and value look ok. i Play with the code and i changed value and removed some

#ifdef VOLTAGE_MON if (adc_ticks > 0) { —adc_ticks; } if (adc_ticks 0) { // See if conversion is done if (ADCSRA & (1 ADC_CRIT : ADC_LOW)) { ++lowbatt_cnt; } else { lowbatt_cnt = 0; } }

// See if it’s been low for a while if (lowbatt_cnt >= 4) {CHANGE VALUE TO 10 , SHOULD I PUT IT TO 3750 (1MIN)???

prev_mode(); REMOVED THAT LINE AND EVERYTHING WORK FINE!!!

lowbatt_cnt = 0; // If we reach 0 here, main loop will go into sleep mode // Restart the counter to when we step down again adc_ticks = ADC_DELAY; }

// Make sure conversion is running for next time through ADCSRA |= (1 << ADSC); } #endif } press_duration = 0;

Any idea how to change the value of battery monitoring (ADCH) in the code??? Dont want to compromise the battery monitoring and automatic previous mode…

TKs for your help!

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 4 hours 31 min ago
Joined: 01/12/2013 - 14:40
Posts: 10786
Location: (469219) 2016 HO3

To make LVP work correctly, you kinda need to measure the voltage with both a full battery and a nearly-empty battery, to find out how much it drops. Just one value won’t help much. Smile

laythaws
Offline
Last seen: 1 year 12 months ago
Joined: 01/15/2015 - 00:57
Posts: 107
Location: Location::Location::

Which of these will be a best replacement for off time cap, 0.1uf, 0.22uf, or 2.2uf? I am planning to buy the otc and c1 10uf x7r from one ebay seller but he doesnt have 1uf x7r in stock, only those caps i mentioned are in stock.
Also what would be the effect if i use y5v capacitor on c1?

ZOTZ
ZOTZ's picture
Offline
Last seen: 4 years 8 months ago
Joined: 02/03/2015 - 15:13
Posts: 7
Location: montreal, québec

Hi to all, tks for all your answer, youre the best! i finally found whats was the problem. My power supply couln’t deliver the proper current… well it work for 20 drivers but the other batch that i received was consumming more current??? I installed a freshly charge 18650 battery pack and and… IT WORK!

I spend some time figured it out what it was in the code to change setting in voltage monitor. It pin point the problem , but i felt a little disapointed to change some safety feature.Everything was fine from the begining! Thumb up for JohnyC for its code… its really well design! Almost bulletproof!

Tks for all your reply

LED Boatguy
Offline
Last seen: 1 year 4 months ago
Joined: 02/10/2015 - 17:28
Posts: 73
Location: Kollyforneah

Well, I bought the hardware (USBasp, Pomona clip, color ribbon, USB extender) and installed AVR Tools, the driver and RMM’s batch file. The only hard part so far was finding the right driver (Libsub0 v1.2.4.0 ) and installing an unsigned driver in Win 8.1. Is that the right driver?

Hooked the Pomona to a MCU and downloaded the BLF-VLD.hex, and got this:

C:\Hex and Flash Files>flash BLF-VLD

C:\Hex and Flash Files>avrdude -p t13 -c usbasp -u -Uflash:w:BLF-VLD.hex:a -
se:w:0×75:m -Uhfuse:w:0xFF:m

avrdude: warning: cannot set sck period. please check for usbasp firmware up
.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0×1e9007
avrdude: NOTE: “flash” memory has been specified, an erase cycle will be per
ed To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware up
.
avrdude: reading input file “BLF-VLD.hex”
avrdude: input file BLF-VLD.hex auto detected as Intel Hex
avrdude: writing flash (882 bytes):

Writing | ################################################## | 100% 1.02s

avrdude: 882 bytes of flash written
avrdude: verifying flash memory against BLF-VLD.hex:
avrdude: load data flash data from input file BLF-VLD.hex:
avrdude: input file BLF-VLD.hex auto detected as Intel Hex
avrdude: input file BLF-VLD.hex contains 882 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.69s

avrdude: verifying …
avrdude: 882 bytes of flash verified
avrdude: reading input file “0×75”
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0×75:
avrdude: load data lfuse data from input file 0×75:
avrdude: input file 0×75 contains 1 bytes
avrdude: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying …
avrdude: 1 bytes of lfuse verified
avrdude: reading input file “0xFF”
avrdude: writing hfuse (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xFF:
avrdude: load data hfuse data from input file 0xFF:
avrdude: input file 0xFF contains 1 bytes
avrdude: reading on-chip hfuse data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying …
avrdude: 1 bytes of hfuse verified

avrdude done. Thank you.

C:\Hex and Flash Files>

So, did it work? I don’t have a light to test it with, and my test bench isn’t up yet—but a 5-pound, finned, heat sink came in today, so I’m a little closer.

This is the only warning I get:

avrdude: warning: cannot set sck period. please check for usbasp firmware up

I get this warning using the avrdude test string also. Can I just ignore this warning? Is there a way to get rid of it?

Thanks in advance.

pilotdog68
pilotdog68's picture
Offline
Last seen: 7 months 1 week ago
Joined: 05/30/2013 - 23:31
Posts: 6422
Location: Held against my will in IOWA, USA

looks like it worked to me

My Favorite Modded Lights: X6R, S8 , X2R , M6, SP03

Major Projects:  Illuminated Tailcap, TripleDown/TripleStack Driver

RMM
RMM's picture
Offline
Last seen: 1 year 9 months ago
Joined: 07/23/2013 - 13:47
Posts: 4006
Location: USA

Looks good.  Don't worry about that warning.

Mountain Electronics : batteries, Noctigon, and much more! What's new? 

LED Boatguy
Offline
Last seen: 1 year 4 months ago
Joined: 02/10/2015 - 17:28
Posts: 73
Location: Kollyforneah
pilotdog68 wrote:
looks like it worked to me

Well that was painless. Do you know what the “cannot set sck period” warning is? I’ve seen it before in these threads.

Also, is the source code C++? I want to study the correct version of C. Some computer guy I met snow camping this weekend recommended Lynda.com to learn C++.

Thanks again.

Oops RMM, looks like I typed over you.

RMM
RMM's picture
Offline
Last seen: 1 year 9 months ago
Joined: 07/23/2013 - 13:47
Posts: 4006
Location: USA

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?

Mountain Electronics : batteries, Noctigon, and much more! What's new? 

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 4 hours 31 min ago
Joined: 01/12/2013 - 14:40
Posts: 10786
Location: (469219) 2016 HO3

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.

LED Boatguy
Offline
Last seen: 1 year 4 months ago
Joined: 02/10/2015 - 17:28
Posts: 73
Location: Kollyforneah

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…

laythaws
Offline
Last seen: 1 year 12 months ago
Joined: 01/15/2015 - 00:57
Posts: 107
Location: Location::Location::
laythaws wrote:
Which of these will be a best replacement for off time cap, 0.1uf, 0.22uf, or 2.2uf? I am planning to buy the otc and c1 10uf x7r from one ebay seller but he doesnt have 1uf x7r in stock, only those caps i mentioned are in stock. Also what would be the effect if i use y5v capacitor on c1?

Anyone?

RMM
RMM's picture
Offline
Last seen: 1 year 9 months ago
Joined: 07/23/2013 - 13:47
Posts: 4006
Location: USA

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. 

Mountain Electronics : batteries, Noctigon, and much more! What's new? 

Mike C
Mike C's picture
Offline
Last seen: 4 days 10 hours ago
Joined: 01/22/2014 - 08:03
Posts: 2585
Location: Sweden
laythaws wrote:
laythaws wrote:
Which of these will be a best replacement for off time cap, 0.1uf, 0.22uf, or 2.2uf? I am planning to buy the otc and c1 10uf x7r from one ebay seller but he doesnt have 1uf x7r in stock, only those caps i mentioned are in stock. Also what would be the effect if i use y5v capacitor on c1?

Anyone?


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:0×79:m -U hfuse:w:0xed:m”

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


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.

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 4 hours 31 min ago
Joined: 01/12/2013 - 14:40
Posts: 10786
Location: (469219) 2016 HO3

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.

Pages