Clone wars

Cloning Arduino core ... using TIMER0 in Arduino

Sometimes you want to use TIMER0 in the Arduino IDE but default inclusion of Arduino libraries means that you can't have control over TIMER0 (it is used by millis() and delay() functions).  In a recent project, I wanted to use all the Atmega328 timers, and for various reasons, still use the Arduino IDE. The solution is simple.  Rather than hacking the core library, we'll clone the Arduino core library, define a board that uses the core library and then hack it to our specifications. 

Steps:


(1)  Clone the Arduino core
Copy the Arduino core (C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores) as "arduinohacked" - the new core directory mustn't have an underscore in it!

(2) Define a new board to use the cloned core library
Open "C:\Program Files (x86)\Arduino\hardware\arduino\avr\boards.txt" and put the following text at the bottom.  This will define a new board in the Arduino IDE.  I'm using a Arduino Nano, so I've copied the Arduino Nano text and changed the names and details to use the cloned core.  Of course, if you're not using a Nano and want to hack another board, simply copy its board entry and create a new one with a new name and necessary details (in this case 'nanohacked').  

##############################################################

nanohacked.name=Arduino Nano hacked core!

nanohacked.upload.tool=avrdude
nanohacked.upload.protocol=arduino

nanohacked.bootloader.tool=avrdude
nanohacked.bootloader.unlock_bits=0x3F
nanohacked.bootloader.lock_bits=0x0F

nanohacked.build.f_cpu=16000000L
nanohacked.build.board=AVR_NANO
nanohacked.build.core=arduino
nanohacked.build.variant=eightanaloginputs

## Arduino Nano w/ ATmega328
## -------------------------
nanohacked.menu.cpu.atmega328=ATmega328

nanohacked.menu.cpu.atmega328.upload.maximum_size=30720
nanohacked.menu.cpu.atmega328.upload.maximum_data_size=2048
nanohacked.menu.cpu.atmega328.upload.speed=57600

nanohacked.menu.cpu.atmega328.bootloader.low_fuses=0xFF
nanohacked.menu.cpu.atmega328.bootloader.high_fuses=0xDA
nanohacked.menu.cpu.atmega328.bootloader.extended_fuses=0x05
nanohacked.menu.cpu.atmega328.bootloader.file=atmega/ATmegaBOOT_168_atmega328.hex

nanohacked.menu.cpu.atmega328.build.mcu=atmega328p

##
Arduino Nano w/ ATmega168
## -------------------------
nanohacked.menu.cpu.atmega168=ATmega168

nanohacked.menu.cpu.atmega168.upload.maximum_size=14336
nanohacked.menu.cpu.atmega168.upload.maximum_data_size=1024
nanohacked.menu.cpu.atmega168.upload.speed=19200

nanohacked.menu.cpu.atmega168.bootloader.low_fuses=0xff
nanohacked.menu.cpu.atmega168.bootloader.high_fuses=0xdd
nanohacked.menu.cpu.atmega168.bootloader.extended_fuses=0x00
nanohacked.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_diecimila.hex

nanohacked.menu.cpu.atmega168.build.mcu=atmega168

##############################################################

(3)  Hack the cloned core
You now have control over the Arduino core libarary, without affecting any of your other projects - edit/hack as you wish!  In my case, it was a matter of editing the arduinohacked\wiring.c file to remove the ISR defintion for TIMER0 (TIMER0_OVF_vect).  This then allowed me to define one as I wished - of course now millis() and delay() will not work, but I didn't want those functions anyway.

(4)  Using your hacked core
Of course now simply specify the board "Arduino Nano hacked core!" in the Arduino IDE in order to use the new 'board'.  Have fun!

Comments

Popular posts from this blog

Getting started with the Pro Micro Arduino Board

Arduino and Raspberry Pi serial communciation