Difference between revisions of "AVR Drivers"

From Flashlight Wiki
Jump to navigation Jump to search
Line 1: Line 1:
Some drivers have an ATMEL microprocessor on board to control different modes. ATMEL processors are programmable therefore the same driver can be loaded with different settings to give different numbers of modes, different output levels, as well as flashing modes like strobe or SOS, along with features like mode memory. This page will summarize some of the things you will need if you want to program your own drivers. This is based on a [http://budgetlightforum.cz.cc/node/799 BLF discussion thread] where you can go for help or to get answers to your questions.
Some drivers have an Atmel microprocessor on board to control different modes. Atmel processors are programmable therefore the same driver can be loaded with different settings to give different numbers of modes, different output levels, as well as flashing modes like strobe or SOS, along with features like mode memory. This page will summarize some of the things you will need if you want to program your own drivers. This is based on a [http://budgetlightforum.cz.cc/node/799 Budget Light Forum discussion thread] where you can go for help or to get answers to your questions. This whole thing was started by a BLF member Tido.


All of this is based on AVR tools developed for Atmel chips, which is mostly open source (free). [http://en.wikipedia.org/wiki/Atmel_AVR Wikipedia article].
All of this is based on AVR tools developed for Atmel chips, which is mostly open source (free). [http://en.wikipedia.org/wiki/Atmel_AVR Wikipedia article]. ISP means "In Sytem Programming," in other words, the chip can be reprogrammed while still soldered in place.


==Things You Will Need==
==Things You Will Need==
Line 7: Line 7:
===Drivers===
===Drivers===


Of course you will need a driver first. There are several NANJG drivers available that include an ATMEL TINY13A chip that can be programmed. These chips can also be locked down so they can't be changed, but so far none seem to be. However, some variants of NANJG drivers use a PIC chip that can not be programmed with AVR tools. See [[Popular drivers]] to see some of the drivers people have been using. Most of these use current regulators to limit the current delivered to the LED while the Atmel chip itself turns the power on and off very quickly, leaving it on for full power, or flashing on and off quickly to simulate a lower mode. By slowing the flashing more, you get strobe modes or SOS modes.
Of course you will need a driver first. There are several NANJG drivers available that include an Atmel TINY13A chip that can be programmed. These chips can also be locked down so they can't be changed, but so far none seem to be. However, some variants of NANJG drivers use a PIC chip that can not be programmed with AVR tools. See [[Popular drivers]] to see some of the drivers people have been using. Most of these use current regulators to limit the current delivered to the LED while the Atmel chip itself turns the power on and off very quickly, leaving it on for full power, or flashing on and off quickly to simulate a lower mode. This is called Pulse Width Modulation (PWM). By slowing the flashing more, you get strobe modes or SOS modes. So essentially the Atmel chip is just a computer-controlled on-off switch.


The Atmel ATTiny13 has an 8-bit processor with 1 kb of flash memory, 64 bits of RAM, and 64 bits of EEPROM that can be written several thousand times. There are also settings called "fuses" which can be set or cleared.  
The Atmel ATTiny13A has an 8-bit processor with 1 kb of flash memory, 64 bits of RAM, and 64 bits of EEPROM that can be written several thousand times. There are also settings called "fuses" which can be set or cleared.  


===Hardware===
===Hardware===


You will need two pieces of hardware: a programmer and a clip to attach to the Atmel chip which is already soldered in place. There are a number of programmers available, including one called USBTinyISP that you can build yourself from a kit. Ironically, it uses an Atmel chip as well which must be programmed before it can be used as a programmer. There are also a number of cheap drivers available on eBay if you search for USBASP. These all look similar with an exposed blue circuit board and cost about $12, usually including a ribbon cable, which you probably won't need.
You will need two pieces of hardware: an AVR ISP programmer that attaches to your computer's USB port and a clip to attach to the Atmel chip which is already soldered in place.  


You will also need a SOIC 8 clip to attach to the pins of the Atmel chip on the flashlight driver. There are cheap ones on eBay that have been problematic, but will generally work. They cost about $12 as well. It is best to get one with a ribbon cable that attaches to pins coming out of the clip. The individual wires of the ribbon cable can be disconnected from pins and then reattached so that the correct pins from the USB programmer are connecting to the correct pins on the Atmel chip via the clip.
There are a number of ISP programmers available, including one called USBTinyISP that you can build yourself from a kit. Ironically, it uses an Atmel chip as well which must be programmed before it can be used as a programmer. There are also a number of cheap drivers available on eBay if you search for USBASP. These all look similar with an exposed blue circuit board and cost about $12, usually including a ribbon cable, which you probably won't need.
 
The other piece of hardware is a SOIC 8 clip to attach to the pins of the Atmel chip on the flashlight driver. There are cheap ones on eBay that have been problematic, but will generally work. They cost about $12 as well. It is best to get one with a ribbon cable that attaches to pins coming out of the clip. The individual wires of the ribbon cable can be moved to the correct pins for the ISP programmer. See below for the proper configuration.


===Software===
===Software===
Line 21: Line 23:
You need some kind of software to interface with the USB programmer and read or load programs into the Atmel chip.
You need some kind of software to interface with the USB programmer and read or load programs into the Atmel chip.


You will at least need AVRDUDE to upload and download files to the Atmel chip. It is a command line interface (DOS). AVR Studio is available from Atmel to allow you to write and compile programs and load them. [http://winavr.sourceforge.net/ WinAVR] is another package that includes the tools you will need if you want to write and compile your own programs. Eclipse is another package that can be used to develop programming and is what Tido uses.
You will at least need AVRDUDE (AVR Downloader UploaDEr) to upload and download files to the Atmel chip. It is a command line interface (DOS). If you use Tido's programs then you don't need to compile anything. Those programs are ready to be loaded.
 
If you want to customize the programs or write your own, you will need a compiler. The programs can be written in assembly language or C. Assembly can give more compact programs, but C is easier to write and understand, so that is what is being used so far. AVR Studio is available from Atmel to allow you to write and compile programs and load them. [http://winavr.sourceforge.net/ WinAVR] is a collection of all the software you need as well as a graphical user interface (GUI) to edit, compile, and load programs. Eclipse is another package that can be used to develop programming and is what Tido uses.


===BLF-VLD===
===BLF-VLD===


Budget Light Forum user Tido created the original files that can be used, called the BLF Versatile LED Driver. If you go to the first entry of the thread [http://budgetlightforum.cz.cc/node/799 How To Build a Flashlight With Perfect Modes], you will find a link to the latest zip archive of the files you need. This zip archive includes a helpful readme file.
Tido created the original files that can be used, called the BLF-VLD (Budget Light Forum Versatile LED Driver). If you go to the first entry of the thread [http://budgetlightforum.cz.cc/node/799 How To Build a Flashlight With Perfect Modes], you will find a link to the latest zip archive of the files you need. This zip archive includes a helpful readme file.


Without doing any programming, you can choose from three setups that Tido has written:
Without writing any code, you can choose from three setups that Tido has already written:


'''Simple''' is a 3-mode setup with Low, Medium, and High. It includes mode memory (if light is on for 2 second or more, the mode is memorized when the light is turned off) and a low battery warning.
'''Simple''' is a 3-mode setup with Low, Medium, and High. It includes mode memory (if light is on for 2 seconds or more, the mode is memorized when the light is turned off) and a low battery warning.


'''Extended''' has hidden modes that can be accessed by running through all of the modes quickly. By default you run 3 modes, but can reach other modes if you want.
'''Extended''' has hidden modes that can be accessed by running through all of the modes quickly. By default you run 3 modes, but can reach other modes if you want.
Line 40: Line 44:
Installing software on your PC and setting it up to get the USB programmer working. Put files in the correct locations so you can access them.
Installing software on your PC and setting it up to get the USB programmer working. Put files in the correct locations so you can access them.


Follow instructions [http://interactive-matter.eu/how-to/developing-software-for-the-atmel-avr-with-avr-eclipse-avr-gcc-avrdude/ here] for installing WinAVR and Eclipse. Then configure Eclipse for AVR. There is also a [http://avr-eclipse.sourceforge.net/wiki/index.php/The_AVR_Eclipse_Plugin Wiki] about Eclipse AVR.
Follow instructions [http://interactive-matter.eu/how-to/developing-software-for-the-atmel-avr-with-avr-eclipse-avr-gcc-avrdude/ here] for installing WinAVR and Eclipse. Eclipse is optional. Then configure Eclipse for AVR. There is also a [http://avr-eclipse.sourceforge.net/wiki/index.php/The_AVR_Eclipse_Plugin Wiki] about Eclipse AVR.


===Set up your clip===
===Set up your clip===
Line 85: Line 89:
<CODE>
<CODE>
avrdude -pt13 -c usbasp -P com2 -u -Uflash:w:BLF-VLD.hex:a -Ueeprom:w:BLF-VLD.eep:a -Ulfuse:w:0x79:m -Uhfuse:w:0xef:m </CODE>
avrdude -pt13 -c usbasp -P com2 -u -Uflash:w:BLF-VLD.hex:a -Ueeprom:w:BLF-VLD.eep:a -Ulfuse:w:0x79:m -Uhfuse:w:0xef:m </CODE>
==Other Issues==
'''Mode Memory''' Some flashlights will remember the last mode they were in. Often the mode will be remembered after the light is off for a couple of seconds (if it was too short an interval you couldn't change modes by half-pressing the tail clicky to off briefly). But NANJG drivers usually have memory that sets after the light is on for a couple of seconds. When you turn the light off, it will come back on in the last mode, but if the light was on for less than 2 seconds, it will come back on in the next mode. This works pretty well except that if the light is on for a couple of seconds and you want to change modes, you will have to half-press the clicky once and end up in the same memorized mode, and then again to change the mode. The reason for memory to be this way is that when power is cut to the driver, it can't write to its memory. But it is easy for the memory to be written while the light is on and a timer can be used to write to the memory after the light is on for a couple of seconds. In order to get conventional memory, a capacitor would have to be added to hold on to power long enough after the power is turned off for the driver to write to its memory.
'''Low Battery''' The Atmel chip can monitor voltage through its VCC pin, however, the voltage can't be more than 1V. Therefore the battery voltage being directed to the VCC pin must go through a couple of resistors to come down to a readable level. Then the software can have some calibrated values so that the user is warned of a low battery by the brightness dropping or the light flashing periodically.

Revision as of 20:07, 8 April 2011

Some drivers have an Atmel microprocessor on board to control different modes. Atmel processors are programmable therefore the same driver can be loaded with different settings to give different numbers of modes, different output levels, as well as flashing modes like strobe or SOS, along with features like mode memory. This page will summarize some of the things you will need if you want to program your own drivers. This is based on a Budget Light Forum discussion thread where you can go for help or to get answers to your questions. This whole thing was started by a BLF member Tido.

All of this is based on AVR tools developed for Atmel chips, which is mostly open source (free). Wikipedia article. ISP means "In Sytem Programming," in other words, the chip can be reprogrammed while still soldered in place.

Things You Will Need

Drivers

Of course you will need a driver first. There are several NANJG drivers available that include an Atmel TINY13A chip that can be programmed. These chips can also be locked down so they can't be changed, but so far none seem to be. However, some variants of NANJG drivers use a PIC chip that can not be programmed with AVR tools. See Popular drivers to see some of the drivers people have been using. Most of these use current regulators to limit the current delivered to the LED while the Atmel chip itself turns the power on and off very quickly, leaving it on for full power, or flashing on and off quickly to simulate a lower mode. This is called Pulse Width Modulation (PWM). By slowing the flashing more, you get strobe modes or SOS modes. So essentially the Atmel chip is just a computer-controlled on-off switch.

The Atmel ATTiny13A has an 8-bit processor with 1 kb of flash memory, 64 bits of RAM, and 64 bits of EEPROM that can be written several thousand times. There are also settings called "fuses" which can be set or cleared.

Hardware

You will need two pieces of hardware: an AVR ISP programmer that attaches to your computer's USB port and a clip to attach to the Atmel chip which is already soldered in place.

There are a number of ISP programmers available, including one called USBTinyISP that you can build yourself from a kit. Ironically, it uses an Atmel chip as well which must be programmed before it can be used as a programmer. There are also a number of cheap drivers available on eBay if you search for USBASP. These all look similar with an exposed blue circuit board and cost about $12, usually including a ribbon cable, which you probably won't need.

The other piece of hardware is a SOIC 8 clip to attach to the pins of the Atmel chip on the flashlight driver. There are cheap ones on eBay that have been problematic, but will generally work. They cost about $12 as well. It is best to get one with a ribbon cable that attaches to pins coming out of the clip. The individual wires of the ribbon cable can be moved to the correct pins for the ISP programmer. See below for the proper configuration.

Software

You need some kind of software to interface with the USB programmer and read or load programs into the Atmel chip.

You will at least need AVRDUDE (AVR Downloader UploaDEr) to upload and download files to the Atmel chip. It is a command line interface (DOS). If you use Tido's programs then you don't need to compile anything. Those programs are ready to be loaded.

If you want to customize the programs or write your own, you will need a compiler. The programs can be written in assembly language or C. Assembly can give more compact programs, but C is easier to write and understand, so that is what is being used so far. AVR Studio is available from Atmel to allow you to write and compile programs and load them. WinAVR is a collection of all the software you need as well as a graphical user interface (GUI) to edit, compile, and load programs. Eclipse is another package that can be used to develop programming and is what Tido uses.

BLF-VLD

Tido created the original files that can be used, called the BLF-VLD (Budget Light Forum Versatile LED Driver). If you go to the first entry of the thread How To Build a Flashlight With Perfect Modes, you will find a link to the latest zip archive of the files you need. This zip archive includes a helpful readme file.

Without writing any code, you can choose from three setups that Tido has already written:

Simple is a 3-mode setup with Low, Medium, and High. It includes mode memory (if light is on for 2 seconds or more, the mode is memorized when the light is turned off) and a low battery warning.

Extended has hidden modes that can be accessed by running through all of the modes quickly. By default you run 3 modes, but can reach other modes if you want.

Programmable allows you to assign any of the extended modes to your 3 simple modes.

Getting Ready

Installing software

Installing software on your PC and setting it up to get the USB programmer working. Put files in the correct locations so you can access them.

Follow instructions here for installing WinAVR and Eclipse. Eclipse is optional. Then configure Eclipse for AVR. There is also a Wiki about Eclipse AVR.

Set up your clip

Match the pins of the Atmel chip with the pins of the USB programmer. Only pins 1, 5, 6, and 7 of the Atmel are used by the ISP programmer.

The pins of the ATMEL chip are

Pin numbers of Atmel ATTiny13. Pin 1 has an indented circle and is marked with a triangle.


ATMEL ATTiny13A pins
8 VCC 7 SCK 6 MISO 5 MOSI
1 RST 2 INP 3 INP 4 GND

The programmer is 10 pins however which are set up this way:

AVR Programmer ribbon wires
Pin Function Function Pin
1 MOSI VCC 2
3 NC GND 4
5 RST GND 6
7 SCK GND 8
9 MISO GND 10

So Wire 1 (MOSI) of the ribbon cable from the programmer must attach to Pin 5 of the Atmel. Make sure you mark which end of the clip attaches to the top of the Atmel or everything will be misaligned next time you use it. Line up RST and SCK with their appropriate wires. But notice that MISO on the programmer is Wire number 9 and the ribbon only has 8 wires. You will need to disassemble the end of the ribbon cable at the controller end and move wire 8 over to the 9 position so that MISO lines up correctly.

Download settings

Use the software and hardware to download the existing program from the Atmel chip on the flashlight driver. You will need this if your programming doesn't work and you want to reset the driver to how it was originally. Get fuse settings too.

Upload new settings

Open a command window in one of the BLF-VLD subdirectories: "Fixed Modes", "Programmable", or "Simple". With everything hooked up, enter this command:

avrdude -pt13 -c usbasp -P com2 -u -Uflash:w:BLF-VLD.hex:a -Ueeprom:w:BLF-VLD.eep:a -Ulfuse:w:0x79:m -Uhfuse:w:0xef:m

Other Issues

Mode Memory Some flashlights will remember the last mode they were in. Often the mode will be remembered after the light is off for a couple of seconds (if it was too short an interval you couldn't change modes by half-pressing the tail clicky to off briefly). But NANJG drivers usually have memory that sets after the light is on for a couple of seconds. When you turn the light off, it will come back on in the last mode, but if the light was on for less than 2 seconds, it will come back on in the next mode. This works pretty well except that if the light is on for a couple of seconds and you want to change modes, you will have to half-press the clicky once and end up in the same memorized mode, and then again to change the mode. The reason for memory to be this way is that when power is cut to the driver, it can't write to its memory. But it is easy for the memory to be written while the light is on and a timer can be used to write to the memory after the light is on for a couple of seconds. In order to get conventional memory, a capacitor would have to be added to hold on to power long enough after the power is turned off for the driver to write to its memory.

Low Battery The Atmel chip can monitor voltage through its VCC pin, however, the voltage can't be more than 1V. Therefore the battery voltage being directed to the VCC pin must go through a couple of resistors to come down to a readable level. Then the software can have some calibrated values so that the user is warned of a low battery by the brightness dropping or the light flashing periodically.