I have 2 Arduinos; an Uno which is sitting in my garden collecting data, and a Mega 2560 which I use for my development environment. I found this library called FreqCounter which will solve some of my problems with light readings with TSL235. When I first tried it, it didn't work on the Mega 2560, but after some digging around in the datasheets, and staring at the schematic I figured out that I have to use timer T5 on the Mega, because Timer T1 isn't connected. So my question is, what is the best way to make FreqCounter.cpp, which looks like:
void FreqCounter::start(int ms) {
//#if defined (AVR_ATmega168) || defined (AVR_ATmega48) || defined (AVR_ATmega88) || defined (AVR_ATmega328P) || (AVR_ATmega1280)
TIMSK0 &=~(1<<TOIE0); // disable Timer0 //disable millis and delay
delayMicroseconds(50); // wait if any ints are pending
f_period=ms;
if (f_comp ==0) f_comp=1; // 0 is not allowed in del us
// hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1)
TCCR1A=0; // reset timer/counter1 control register A
TCCR1B=0; // reset timer/counter1 control register A
TCNT1=0; // counter value = 0
// set timer/counter1 hardware as counter , counts events on pin T1 ( arduino pin 5)
// normal mode, wgm10 .. wgm13 = 0
TCCR1B |= (1<<CS10) ;// External clock source on T1 pin. Clock on rising edge.
TCCR1B |= (1<<CS11) ;
TCCR1B |= (1<<CS12) ;
...
look like
void FreqCounter::start(int ms) {
//#if defined (AVR_ATmega168) || defined (AVR_ATmega48) || defined (AVR_ATmega88) || defined (AVR_ATmega328P) || (AVR_ATmega1280)
TIMSK0 &=~(1<<TOIE0); // disable Timer0 //disable millis and delay
delayMicroseconds(50); // wait if any ints are pending
f_period=ms;
if (f_comp ==0) f_comp=1; // 0 is not allowed in del us
// hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1)
TCCR5A=0; // reset timer/counter1 control register A
TCCR5B=0; // reset timer/counter1 control register A
TCNT5=0; // counter value = 0
// set timer/counter1 hardware as counter , counts events on pin T1 ( arduino pin 5)
// normal mode, wgm10 .. wgm13 = 0
TCCR5B |= (1<<CS50) ;// External clock source on T1 pin. Clock on rising edge.
TCCR5B |= (1<<CS51) ;
TCCR5B |= (1<<CS52) ;
when I compile for the Mega?
Basically I want 1 FreqCounter.cpp that works for both the Uno and Mega. I thought about using #defines for the registers, but I don't know that the Arduino compiles the library every time, and it may just use a .o file compiled for the Uno and try to link it and upload it to the Mega. Maybe there is some run time value I can query that tells me the board type, and I can just put in a bunch of if statements, but that doesn't seem very elegant.
I hoping that someone with a good knowledge of the representations of the registers in C might be able to tell me a good way to do this.
Thanks.