Go Down

Topic: Help Change timer 2 to timer 0 (Read 5009 times) previous topic - next topic

williamanos

I am attempting to change this code from using timer 2 to using timer 0 so I can use it with an attiny85 that does not have timer 2.

Here is a link to the zip file containing the code: http://arcfn.com/files/IRremote.zip

I am modifying the .cpp file

When I first tried to upload this without having changed the timer I got this error:
Code: [Select]
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRsend::mark(int)':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:172:3: error: 'TCCR2A' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:172:13: error: 'COM2B1' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRsend::space(int)':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:180:3: error: 'TCCR2A' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:180:15: error: 'COM2B1' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRsend::enableIROut(int)':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:198:3: error: 'TIMSK2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:198:14: error: 'TOIE2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:207:3: error: 'TCCR2A' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:207:12: error: 'WGM20' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:208:3: error: 'TCCR2B' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:208:12: error: 'WGM22' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:208:25: error: 'CS20' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:211:3: error: 'OCR2A' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:212:3: error: 'OCR2B' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRrecv::enableIRIn()':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:224:3: error: 'TCCR2A' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:229:3: error: 'TCCR2B' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:229:3: error: 'CS22' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:230:3: error: 'CS21' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:231:3: error: 'CS20' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:234:3: error: 'TIMSK2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:234:3: error: 'TOIE2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:236:3: error: 'TCNT2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In function 'void TIMER2_OVF_vect()':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:266:3: error: 'TCNT2' was not declared in this scope


I went through the .cpp file and changed each one of the parts mentioned so that the 2s became 0s. Having done that I am still getting this error message:
Code: [Select]


/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRsend::enableIROut(int)':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:198:3: error: 'TIMSK0' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRrecv::enableIRIn()':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:234:3: error: 'TIMSK0' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:236:3: error: 'TCNT2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In function 'void TIMER2_OVF_vect()':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:266:3: error: 'TCNT2' was not declared in this scope


I don't understand! Please help.

Thanks,
Will

Nick Gammon

Have a look at the datasheet. Since there is only one timer it is TIMSK not TIMSK0. And so on for the other things.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

williamanos

Thanks Nick! I have changed the TIMSK0's to TIMSK but i am still getting this error message:

Code: [Select]

/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In member function 'void IRrecv::enableIRIn()':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:236:3: error: 'TCNT2' was not declared in this scope
/home/will/sketchbook/libraries/IRremote/IRremote.cpp: In function 'void TIMER2_OVF_vect()':
/home/will/sketchbook/libraries/IRremote/IRremote.cpp:266:3: error: 'TCNT2' was not declared in this scope


and yet when I search the .cpp file there is no 'TCNT2'

I had changed these to TCNT0. Need I go back and delete all of the zeros inserted at the beginning?

Thank you so much again!

williamanos

correction: there is no TCNT at all in the .cpp file

Nick Gammon

Look in IRremoteInt.h which is included by the main library:

Code: [Select]
// clock timer reset value
#define INIT_TIMER_COUNT2 (CLK - USECPERTICK*CLKSPERUSEC + CLKFUDGE)
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT2
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

williamanos

Thank you so much Nick! It compiled! I will test the circuit and report back!

williamanos

Ok it now compiles! However, I now get this error when attempting to load the sketch onto the attiny85:

Code: [Select]
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: verification error, first mismatch at byte 0x03d3
         0xd1 != 0xff
avrdude: verification error; content mismatch

williamanos

If I keep the output labeled status on pin 13 and change the recieve input to 0 and the button input to 1 I get this error:

Code: [Select]
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85

williamanos

For reference, this is the project I am trying to load to the attiny85: http://www.arcfn.com/2009/09/arduino-universal-remote-record-and.html
I have seen elsewhere that this last error is to be expected when loading sketches to the attiny85 but should not interfere with anything. In any case, I have changed the status pin to pin 4 with a red LED. I have changed the receive pin to pin 1. I have changed the button pin to pin 0. The output signal is sent to the IR LED through pin 3 in the original code so I have attached the IR led here to pin 3 on the attiny85.

I am using an arduino board as and ISP programmer connected to my attiny85 on a breadboard.
When I load the sketch, the tx and rx leds on the arduino board keep blinking long after the sketch is loaded. The led on the arduino board attached to pin 13 also flashes along with the tx and rx leds. After a few seconds of this they all stop and do not come back on. Neither the IR led nor the status led on the breadboard connected to the attiny light up at all. The circuit also does not appear to receive any IR signals directed at the IR receiver.

williamanos

Code now loads fine after a few seconds. The circuit is totally unresponsive though. It doesn't register any IR signals, status or IR output. I have tested each pin with an LED using the example Blink sketch and they all work fine.

Nick Gammon

Before I take a look at that, hunt down the source for the TV B Gone gadget. That uses an Attiny and it does IR sending. You might get some clues from that.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

williamanos

The most obvious difference for the tv b gone is that it uses an external 8mhz resonator to get accurate timing. If that were the problem though, wouldn't I be getting at least inaccurate readings and outputs? I'm still learning my way around comparatively simple sketches, but the tv b gone seems to go about sending IR signals differently than the project I am trying to adapt.

Nick Gammon

I sort-of have it going, here's proof:



Channel 1 is measuring the IR receiver connected to pin 5 (D0) of the Attiny85.

Channel 2 is measuring an LED I have  on pin 3 (D4) of the Attiny.

That blip is bringing D4 high for a moment when it recognizes a code on my remote.

Sketch:

Code: [Select]
#include <IRremote.h>
const int RECV_PIN = 0;
const int LED = 4;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  DDRB |= _BV(4);  // pinMode (4, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    PORTB |= _BV (4);  // digitalWrite (4, HIGH);
    irrecv.resume(); // Receive the next value
    PORTB &= ~_BV (4);  // digitalWrite (4, LOW);
  }
}


I seem to be very short of memory. Even adding a delay(100) gave a compiler error.

The library I compiled against is here:

http://gammon.com.au/Arduino/IRremote_Attiny.zip

I had to change a couple more things, in particular:

Code: [Select]
#define SYSCLOCK 16000000  // main Arduino clock

to:

Code: [Select]
#define SYSCLOCK 8000000  // main Arduino clock

After all, it is an 8 MHz clock.

And the ISR, from:

Code: [Select]
ISR(TIMER2_OVF_vect)

to:

Code: [Select]
ISR(TIMER0_OVF_vect)


Look for blocks of code like this to see the changes I made:

Code: [Select]
#if defined (__AVR_ATtinyX5__)
  // code for Attiny
#else
  // code for Atmega328
#endif


I'm not sure quite how useful it is, you'll need to work out the memory issues before you can get too much further.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

williamanos

Wow amazing job Nick! That's wonderful. You're right about the memory being a problem, but at least we know it is possible for an attiny85 to recognize an IR signal.

Go Up