Make RadioHead RH_ASK use timer 5 and not timer 1 and not timer 2 [solved]

OK, so i found out that the Radiohead library uses timer1 (or timer2) and that the 5940 library uses both timer 1 and timer 2...

so... i need to figure out how to change which timer that the Radiohead library uses...

i found this code

// Use timer 1
    prescaler = timerCalc(_speed, (uint16_t)-1, &nticks);    
    if (!prescaler)
        return; // fault
    TCCR1A = 0; // Output Compare pins disconnected
    TCCR1B = _BV(WGM12); // Turn on CTC mode

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

    // Caution: special procedures for setting 16 bit regs
    // is handled by the compiler
    OCR1A = nticks;
    // Enable interrupt
   #ifdef TIMSK1
    // atmega168
    TIMSK1 |= _BV(OCIE1A);
   #else
    // others
    TIMSK |= _BV(OCIE1A);

which i believe tells the radiohead library which timer to use, and i tried (crudely) to modify it to use timer 4 the best way I know how...

// Use timer 1
    prescaler = timerCalc(_speed, (uint16_t)-1, &nticks);    
    if (!prescaler)
        return; // fault
    TCCR4A = 0; // Output Compare pins disconnected
    TCCR4B = _BV(WGM12); // Turn on CTC mode

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

    // Caution: special procedures for setting 16 bit regs
    // is handled by the compiler
    OCR4A = nticks;
    // Enable interrupt
   #ifdef TIMSK1
    // atmega168
    TIMSK4 |= _BV(OCIE4A);
   #else
    // others
    TIMSK |= _BV(OCIE4A);

But, that just caused my arduino to lock up... I think its because i don't know what to do about

TCCR4B = _BV(WGM12); // Turn on CTC mode

that line, how do you turn on CTC mode for timer 4? I tried looking at google, but this so so far over my head i don't even know what to search for.

////// previous post

I am working on a rather large program, and I have ran into a problem which I didn't experience with my prototype, but I am having with a (mostly) finished project.

I am using radiohead ASK library, and when it the program transmits, and driver.waitPacketSent(); the program becomes unresponsive, the message isn't sent, and I have to reset the arduino.

I checked how much ram I am using right before calling driver.waitPacketSent(); and it says i have 992 bytes (though I'm not sure that is enough, is it?)

I will post my code in the attachment, so you can see it but its a large LARGE program so really what I am looking for is some kind of options, or most likely causes. Would a timer conflict cause this? I tried telling radio head to use timer 2, but that made it so my LCD screen didn't even get initialized.

I am using an character lcd (liquid crystal library), tlc5940, a 1307RTC, and a TX and RX 433 modules. The TLC5940 wasn't in my prototype. I changed to it from PCA9685 to the 5940 because i couldn't accomplish the SMD soldering.

I did a test just of the RX and TX module (just uploaded a sketch to my arduino to transmit) and that works fine.... so it's gotta be a conflict or a code error, I think?

RFledBoxTs (3).zip (14.4 KB)

updated

could someone please point me to the right direction? is changing timers extremely difficult? Thanks!

Ok, so or anyone who has a similar problem, I was able to figure it out...

I ended up changing the #define RH_ASK_ARDUINO_USE_TIMER2 to timer 5, so I could change back easily...

you can edit the following codes to get RH_ASK library to use timer 5 instead of timers one or two:

uncomment #define RH_ASK_ARDUINO_USE_TIMER2 on line 17

then change line 84 to:

PROGMEM static const uint16_t prescalers[] = {0, 1, 8, 64, 256, 1024, 3333};

then change lines 234 - 253 to:

#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

then change line 484 to

#define RH_ASK_TIMER_VECTOR TIMER5_COMPA_vect

then your done! Its that easy...

The lines might vary a bit.. look for lines that a similar but just a bit different.

1 Like

Hi ... I do need to change RadioHead timer to 5 and I tried to do the steps but I failed ..

line numbers are really different and some lines are very similar so I am confused which to replace ..
can you explain it in more details please or do it on updated radiohead library file
thanks

We've helped @namer in their other thread on this topic:
http://forum.arduino.cc/index.php?topic=501705