Compatibility of Arduino timers. [SOLVED]

Hello.

I am having an issue with understanding Arduino timers.

I am using an Arduino mega running at 16mhz and an Arduino pro 5v running at 16mhz.

I have a library that produces 8 ticks a second. on the Arduino mega it uses timer five (timer one is busy doing other things) and on the Arduino pro the library uses timer 1.

The problem I'm having is that maybe the ticks are out of phase with on another or something because although both libraries provide the 8 ticks, the 8 ticks aren't lined up and so my library isn't working

Is there any insight I could get about how the timers correlate (differences and simularities)?

Any thots?

Are you comparing ticks on the Mega with ticks on the Pro? If so, they're not going to be in synch unless you provide some kind of synching signal between them. They run on seperate clocks with seperate tolerances.
--Michael

I understand. But, timer 1 on the Arduino mega and the Arduino pro seem to be "in sync". And by in sync I mean "the library works"

It's only when I use timer 5 (or not timer1) on the mega and timer 1 on the pro that it doesn't work..

Why would that be?

I need to know what you mean by out of phase or not lining up. What is this library and how are you measuring phase?

--Michael

Thank you for the response.

Here is the "full story

Hello,

Earlier I was having an issue which was that my TLC5940 and the Radiohead ASK library were incompatable (the TLC5940 library uses timers one and two, and the radiohead ASK library uses timer one OR two).

I solved that problem by making it so that the Radiohead ASK library uses timer 5 on my Mega.

Between two Mega's that worked fine.

However, I want to use a Arduino Pro 328 16mhz 5v for the TX. The issue is that it does not have a timer 5.

When I use timer 1 on the pro mini and timer 5 on the arduino mega, the mega does not receive the signals from the arduino pro. However, if I use timer 1 for both the message is received.

Is there something I can do to remedy this?

Well, I'm still unclear on your setup. Are you using the timers to generate a PWM output on one processor, which is connected to a receiving input on the other processor? Is that what you mean by signal?

But I'm starting to think I'm not going to be able to help anyway. I have only a little experience with using the timers and that only on the Uno. So the only suggestions I have are general ones: double check you're using the correct pins for each timer on each processor. The 328's timer 1 is a 16 bit timer. What is timer 5 on the Mega? Are prescalers and modes set compatibly?

Hope somebody else can come along with a better knowledge base. In general, here's a link I've found very helpful on the subject of timers:
www.gammon.com.au/timers

--Michael

Thank you for you help, I am using the timers to generate "ticks" according to the radio head library....

The data pin doesn't need to be hooked up to the timer pin so I'm assuming this is just used for counting

It needs to generate 8 ticks a second... But I'm not sure what the prescaler should be...

Timer 1 and timer 5 are both 16bit timers so I figured theyd be the same.

Thanks for the link but I've read that and still can't make great sense of it.

Could it be that there's some left-over reference to the wrong timer? Timer1 should substitute for Timer5 on the mega...

(also, you can do working 433mhz OOK/ASK with digitalWrite(), micros() and delayMicros()... - it's not as graceful as VW, but I've done it and it works fine. The fact that VW is so complicated makes debugging issues a lot harder - I'm sure it's got better range and faster data transmission and stuff, but if you can't figure out how to make it work, that does't help much. I tried it for a while and gave up. )

I appreciate all the comments!

Delta, I have attached several items:

Two pictures of my Serial output.

The first picture "MegaTimer5ProTimer1" shows the readout (and the signals not being received by the mega) when I use Timer 5 on the mega for the library, and Timer 1 on the pro for the library.

The second picture "MegaTimer1ProTimer1" shows the readout (and the signals being received by the mega) when I use timer one on both the mega and pro.

I also attached my Modified RH_ASK.cpp and the two sketches of the receiver and transmitter.

If you need anything else please let me know I really appreciate the help.

//

Dr. Azzy, could you please explain how I use micros to drive these? Is there a tutorial?

sketch_jul09a.ino (475 Bytes)

sketch_jul09b.ino (427 Bytes)

RH_ASK.cpp (20.5 KB)

I posted this overnight.. So it fell off the page. Thanks again for the help.

Also, I didn't post the pics, but I have another Arduino mega.. Using timer five it can transmit to the receiver mega which is using timer 5. But not if the transmitting mega is using timer 1....

Hum - uh

The comments in the code suggest that it originally had that option to use timer 2 and you changed that to 5. Why not leave it like it was originally, and tell it to use timer2? Do you have non-working code that tries to use timer2? Non-working code can be fixed. Working code, there's nothing to do on.

I think

   #ifdef TIMSK2
    // atmega168
    TIMSK5 |= _BV(OCIE5A);
   #else
    // others
    TIMSK |= _BV(OCIE5A);
   #endif // TIMSK2

is wrong in the general case. I think you should be checking for the register that goes with the timer you're using.

The code I mentioned is this:
https://github.com/SpenceKonde/AzzyProjects/blob/master/433mhz/txrxtoserial21/txrxtoserial21.ino

the folder that sketch is in has a crapload of related sketches with various versions of the tx/rx code. The one i linked above has the newest code, and that's where I pull code from to make new projects based on it.
https://github.com/SpenceKonde/AzzyProjects/tree/master/433mhz/Docs has poorly written documentation, mostly dedicated to commands that I've created and used in various projects (you'll have to dig around to find code that uses them, where it exists; I think a few commands were spec'ed but never written)

DrAzzy:
Hum - uh

The comments in the code suggest that it originally had that option to use timer 2 and you changed that to 5. Why not leave it like it was originally, and tell it to use timer2? Do you have non-working code that tries to use timer2? Non-working code can be fixed. Working code, there's nothing to do on.

I think

   #ifdef TIMSK2

// atmega168
    TIMSK5 |= _BV(OCIE5A);
  #else
    // others
    TIMSK |= _BV(OCIE5A);
  #endif // TIMSK2




is wrong in the general case. 


The code I mentioned is this:
https://github.com/SpenceKonde/AzzyProjects/blob/master/433mhz/txrxtoserial21/txrxtoserial21.ino

the folder that sketch is in has a crapload of related sketches with various versions of the tx/rx code. The one i linked above has the newest code, and that's where I pull code from to make new projects based on it. 
https://github.com/SpenceKonde/AzzyProjects/tree/master/433mhz/Docs has poorly written documentation, mostly dedicated to commands that I've created and used in various projects (you'll have to dig around to find code that uses them, where it exists; I think a few commands were spec'ed but never written)

Thanks for the reply. I did a hackjob on the timers because I don't know what i'm doing. Can you please explain "wrong in the general case" So that mean, the else statement is wrong?

The reason I cannot use timer two either is because the tlc5940 chip uses timers one and two already. I am using that as well.

I just wanted to update this and say I solved the problem. Thanks for everyone's contributions.

The issue was in way I modified the RH_ASK.cpp file..

#if defined(RH_ASK_ARDUINO_USE_TIMER2)

prescaler = timerCalc(_speed, (uint16_t)-1, &nticks);
    if (!prescaler)
        return; // fault
    // Use timer 2
    TCCR5A = _BV(WGM51); // Turn on CTC mode
    // convert prescaler index to TCCRnB prescaler bits CS10, CS11, CS12
    TCCR5B = prescaler;

    // Caution: special procedures for setting 16 bit regs
    // is handled by the compiler
    OCR5A = nticks;
    // Enable interrupt
   #ifdef TIMSK2
    // atmega168
    TIMSK5 |= _BV(OCIE5A);
   #else
    // others
    TIMSK |= _BV(OCIE5A);
   #endif // TIMSK2

should have been:

#if defined(RH_ASK_ARDUINO_USE_TIMER2)
    // Use timer 5
    prescaler = timerCalc(_speed, (uint16_t)-1, &nticks);    
    if (!prescaler)
        return; // fault
    TCCR5A = 0; // Output Compare pins disconnected
    TCCR5B = _BV(WGM52); // Turn on CTC mode

    // convert prescaler index to TCCRnB prescaler bits CS10, CS11, CS12
    TCCR5B |= prescaler;

    // Caution: special procedures for setting 16 bit regs
    // is handled by the compiler
    OCR5A = nticks;
	TIMSK5 |= _BV(OCIE5A);