Go Down

Topic: frequencyTimer2 problem (Read 3288 times) previous topic - next topic

jhonray

can someone help me..... i have a problem about timer2.. and this are the following problems.... so please help me i need it now... huhuhu.. how can i fix it..


C:\Program Files\arduino-0021\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static void FrequencyTimer2::enable()':
C:\Program Files\arduino-0021\libraries\FrequencyTimer2\FrequencyTimer2.cpp:150: error: 'TCCR2' was not declared in this scope
C:\Program Files\arduino-0021\libraries\FrequencyTimer2\FrequencyTimer2.cpp:150: error: 'COM20' was not declared in this scope
C:\Program Files\arduino-0021\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static void FrequencyTimer2::disable()':
C:\Program Files\arduino-0021\libraries\FrequencyTimer2\FrequencyTimer2.cpp:160: error: 'TCCR2' was not declared in this scope
C:\Program Files\arduino-0021\libraries\FrequencyTimer2\FrequencyTimer2.cpp:160: error: 'COM20' was not declared in this scope

Coding Badly


Which board is selected in the Arduino IDE?

jhonray

arduino duimelanove/ nano w/ atmega328..

Coding Badly


http://www.google.com/search?q=frequencytimer2+328+arduino
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239820770

Code: [Select]

/*
 FrequencyTimer2.h - A frequency generator and interrupt generator library
 Author: Jim Studt, jim@federated.com
 Copyright (c) 2007 David A. Mellis.  All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include <FrequencyTimer2.h>

#include <avr/interrupt.h>

void (*FrequencyTimer2::onOverflow)() = 0;
uint8_t FrequencyTimer2::enabled = 0;

#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
SIGNAL(SIG_OUTPUT_COMPARE2A)
#else
SIGNAL(SIG_OUTPUT_COMPARE2)
#endif
{
   static uint8_t inHandler = 0; // protect us from recursion if our handler enables interrupts

   if ( !inHandler && FrequencyTimer2::onOverflow) {
     inHandler = 1;
     (*FrequencyTimer2::onOverflow)();
     inHandler = 0;
   }
}

void FrequencyTimer2::setOnOverflow( void (*func)() )
{
   FrequencyTimer2::onOverflow = func;
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
   if ( func) TIMSK2 |= _BV(OCIE2A);
   else TIMSK2 &= ~_BV(OCIE2A);
#else
   if ( func) TIMSK |= _BV(OCIE2);
   else TIMSK &= ~_BV(OCIE2);
#endif
}

void FrequencyTimer2::setPeriod(unsigned long period)
{
   uint8_t pre, top;
 
   if ( period == 0) period = 1;
   period *= clockCyclesPerMicrosecond();

   period /= 2;            // we work with half-cycles before the toggle
   if ( period <= 256) {
     pre = 1;
     top = period-1;
   } else if ( period <= 256L*8) {
     pre = 2;
     top = period/8-1;
   } else if ( period <= 256L*32) {
     pre = 3;
     top = period/32-1;
   } else if ( period <= 256L*64) {
     pre = 4;
     top = period/64-1;
   } else if ( period <= 256L*128) {
     pre = 5;
     top = period/128-1;
   } else if ( period <= 256L*256) {
     pre = 6;
     top = period/256-1;
   } else if ( period <= 256L*1024) {
     pre = 7;
     top = period/1024-1;
   } else {
     pre = 7;
     top = 255;
   }

#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
   TCCR2B = 0;
   TCCR2A = 0;
   TCNT2 = 0;
   ASSR &= ~_BV(AS2);    // use clock, not T2 pin
   OCR2A = top;
   TCCR2A = (_BV(WGM21) | ( FrequencyTimer2::enabled ? _BV(COM2A0) : 0));
   TCCR2B = pre;
#else
   TCCR2 = 0;
   TCNT2 = 0;
   ASSR &= ~_BV(AS2);    // use clock, not T2 pin
   OCR2 = top;
   TCCR2 = (_BV(WGM21) | ( FrequencyTimer2::enabled ? _BV(COM20) : 0)  | pre);
#endif
}

unsigned long  FrequencyTimer2::getPeriod()
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
   uint8_t p = (TCCR2B & 7);
   unsigned long v = OCR2A;
#else
   uint8_t p = (TCCR2 & 7);
   unsigned long v = OCR2;
#endif
   uint8_t shift;
 
   switch(p) {
     case 0 ... 1:
     shift = 0;
     break;
     case 2:
     shift = 3;
     break;
     case 3:
     shift = 5;
     break;
     case 4:
     shift = 6;
     break;
     case 5:
     shift = 7;
     break;
     case 6:
     shift = 8;
     break;
     case 7:
     shift = 10;
     break;
   }
   return (((v+1) << (shift+1)) + 1) / clockCyclesPerMicrosecond();   // shift+1 converts from half-period to period
}

void FrequencyTimer2::enable()
{
   FrequencyTimer2::enabled = 1;
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
   TCCR2A |= _BV(COM2A0);
#else
   TCCR2 |= _BV(COM20);
#endif
}

void FrequencyTimer2::disable()
{
   FrequencyTimer2::enabled = 0;
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
   TCCR2A &= ~_BV(COM2A0);
#else
   TCCR2 &= ~_BV(COM20);
#endif
}

jhonray

another problem occur... like


In file included from C:\Program Files\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:6,
                from sketch_jan26a.cpp:70:
c:/program files/arduino-0021/hardware/tools/avr/lib/gcc/../../avr/include/math.h:439: error: expected unqualified-id before 'double'
c:/program files/arduino-0021/hardware/tools/avr/lib/gcc/../../avr/include/math.h:439: error: expected `)' before 'double'
c:/program files/arduino-0021/hardware/tools/avr/lib/gcc/../../avr/include/math.h:439: error: expected `)' before 'double'

Coding Badly

Code: [Select]

#ifndef FREQUENCYTIMER2_IS_IN
#define FREQUENCYTIMER2_IS_IN

/*
 FrequencyTimer2.h - A frequency generator and interrupt generator library
 Author: Jim Studt, jim@federated.com
 Copyright (c) 2007 David A. Mellis.  All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


[glow]//[/glow]#include <wiring.h>
[glow]#include <WProgram.h>[/glow]

class FrequencyTimer2
{
 private:
   static uint8_t enabled;
 public:
   static void (*onOverflow)(); // not really public, but I can't work out the 'friend' for the SIGNAL
   
 public:
   static void setPeriod(unsigned long);
   static unsigned long getPeriod();
   static void setOnOverflow( void (*)() );
   static void enable();
   static void disable();
};

#endif

jhonray

sir.. where do i put that code?... confusing..

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy