Timer T5 on Mega 2560

Hi all

I’m having Problems to convert the following code from an Arduino Uno (ATmega168/328) into a running Version on the Arduino Mega 2560 (ATmega2560). Problem is that Timer1 is not wird to Pin5 at Arduino Mega 2560.

#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+

unsigned long t0=0; //-Last time
int t=0; //-time between ints
unsigned char tflag=0; //-Measurement ready flag

float SENSITIVITY= 1000.0; //-Guess what

//-Generate interrupt every 1000 oscillations of the search coil
SIGNAL(TIMER1_COMPA_vect)
{
OCR1A+=1000;
t=micros()-t0;
t0+=t;
tflag=1;
}

void setup()
{
pinMode(13,OUTPUT); //-piezo pin
digitalWrite(12,HIGH); //-NULL SW. pull up
//-Set up counter1 to count at pin 5
TCCR1A=0;
TCCR1B=0x07;
SET(TIMSK1,OCF1A);
}

I found several Information that T5 (Pin47) can be used the same wise but I’m not able to get it run.
Does anyone of you know how to handel the code that T5 (Pin47) on Arduino Mega is controlled the same wise as T1 (Pin5) on Arduino Uno. Project ist the metal detector found here…

Does anyone of you know how to handel the code that T5 (Pin47) on Arduino Mega is controlled the same wise as T1 (Pin5) on Arduino Uno.

Use pin 47 and change all references to Timer1 to Timer5. Every capitalized timer reference needs to be changed. For example TCCR1A = 0 should be changed to TCCR5A = 0;

but I'm not able to get it run

What did you try?

Hi all

I already tried several combinations using timer 5 instance but was not successful. Here is the original code

https://dl.dropboxusercontent.com/u/2248531/blog/metal_detector/metal_detector.ino

For Excample I tried the follwing and several combinations of it:
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+

unsigned long t0=0; //-Last time
int t=0; //-time between ints
unsigned char tflag=0; //-Measurement ready flag

float SENSITIVITY= 1000.0; //-Guess what

//-Generate interrupt every 1000 oscillations of the search coil
SIGNAL(TIMER1_COMPA_vect)
{
OCR5A+=1000;
t=micros()-t0;
t0+=t;
tflag=1;
}

void setup()
{
pinMode(2,OUTPUT); //-piezo pin
digitalWrite(3,HIGH); //-NULL SW. pull up
//-Set up counter1 to count at pin 5
TCCR5A=0;
TCCR5B=0x07;
SET(TIMSK5,OCF5B);
}
//-Float ABS
float absf(float f)
{
if(f<0.0)
return -f;
else
return f;
}

int v0=0; //-NULL value
float f=0; //-Measurement value
unsigned int FTW=0; //-Click generator rate
unsigned int PCW=0; //-Click generator phase
unsigned long timer=0; //-Click timer
void loop()
{
if(tflag)
{
if(digitalRead(2)==LOW) //-Check NULL SW.
v0=t; //-Sample new null value
f=f*0.9+absf(t-v0)*0.1; //-Running average over 10 samples
tflag=0; //-Reset flag

float clf=f*SENSITIVITY; //-Convert measurement to click frequency
if(clf>10000)
clf=10000;
FTW=clf;
}

//-Click generator
if(millis()>timer)
{
timer+=10;
PCW+=FTW;
if(PCW&0x8000)
{
digitalWrite(3,HIGH);
PCW&=0x7fff;
}
else
digitalWrite(3,LOW);
}
}

No solution did work. Then I read the following "
Note that timer1 can be used on a Mega but does not support all three output pins OCR1A, OCR1B & OCR1C. Only A & B are supported. OCR1A is connected to pin 11 of the Mega and OCR1B to pin 12. Using one of the three calls that specify a pin, 1 will map to pin 11 on the Mega and 2 will map to pin 12. Timer3 has only been tested on the Mega"

But as well Timer3 (A) on Pin11 or (B) Pin12 did not work :-(.

Does anyone of you guys know how to handel it?

SIGNAL(TIMER1_COMPA_vect)

This has not been changed to timer 5.

#define SET(x,y) (x |=(1<<y))            //-Bit set/clear macros
  SET(TIMSK5,OCF5B);

I see that syntax in the original code, but it does not look correct. The code is using the compare A interrupt. The Compare Interrupt need to be enabled. The value of OCF5B is no where determined, and it’s not clear that it’s value is setting the correct bit in TIMSK5. I would use

TIMSK5 |= 1<< OCIE5A;

//compare A interrupt enable

No solution did work. Then I read the following "
Note that timer1 can be used on a Mega but does not support all three output pins OCR1A, OCR1B & OCR1C. Only A & B are supported. OCR1A is connected to pin 11 of the Mega and OCR1B to pin 12. Using one of the three calls that specify a pin, 1 will map to pin 11 on the Mega and 2 will map to pin 12. Timer3 has only been tested on the Mega"

But as well Timer3 (A) on Pin11 or (B) Pin12 did not work :-(.

This makes no sense to me. The code uses the timer in input capture mode which does not have an input pin broken out on the Mega. Your first post correctly identified this problem. I see nowhere in the code which is using the timer out put pins A or B. The OCR5A value is used internally, so that when the count of externally applied pulses is 1000, it executes the ISR.

Hi Cattledog

Thanks for your Response. It works now perfectely.

Regards, Mark