Forum: Mikrocontroller und Digitale Elektronik Timer Bibliothek umschreiben für anderen Timer


von tesseract (Gast)


Lesenswert?

hallo

ich will auf einem arduino mega mit einem atmega2560 3 timer ansteuern.

da ich leider zu doof bin um das selber zu machen, benutze ich dazu 
Bibliotheken.

leider finde ich nur eine für timer3.

ich will aber 3 timer benutzen. z.b. 2, 3 und 4

kann ich die Bibliotheken einfach auf diese timer umschreiben? welche 
werte muss ich verändern?

1
#include "TimerThree.h"
2
3
TimerThree Timer3;              // preinstatiate
4
5
ISR(TIMER3_OVF_vect)          // interrupt service routine that wraps a user defined function supplied by attachInterrupt
6
{
7
  Timer3.isrCallback();
8
}
9
10
void TimerThree::initialize(long microseconds)
11
{
12
  TCCR3A = 0;                 // clear control register A 
13
  TCCR3B = _BV(WGM13);        // set mode as phase and frequency correct pwm, stop the timer
14
  setPeriod(microseconds);
15
}
16
17
void TimerThree::setPeriod(long microseconds)
18
{
19
  long cycles = (F_CPU * microseconds) / 2000000;                                // the counter runs backwards after TOP, interrupt is at BOTTOM so divide microseconds by 2
20
  if(cycles < RESOLUTION)              clockSelectBits = _BV(CS10);              // no prescale, full xtal
21
  else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11);              // prescale by /8
22
  else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11) | _BV(CS10);  // prescale by /64
23
  else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12);              // prescale by /256
24
  else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12) | _BV(CS10);  // prescale by /1024
25
  else        cycles = RESOLUTION - 1, clockSelectBits = _BV(CS12) | _BV(CS10);  // request was out of bounds, set as maximum
26
  ICR3 = pwmPeriod = cycles;                                                     // ICR1 is TOP in p & f correct pwm mode
27
  TCCR3B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
28
  TCCR3B |= clockSelectBits;                                                     // reset clock select register
29
}
30
31
void TimerThree::setPwmDuty(char pin, int duty)
32
{
33
  unsigned long dutyCycle = pwmPeriod;
34
  dutyCycle *= duty;
35
  dutyCycle >>= 10;
36
  if(pin == 5) OCR3A = dutyCycle;
37
  if(pin == 2) OCR3B = dutyCycle;
38
  if(pin == 3) OCR3C = dutyCycle;
39
}
40
41
void TimerThree::pwm(char pin, int duty, long microseconds)  // expects duty cycle to be 10 bit (1024)
42
{
43
  if(microseconds > 0) setPeriod(microseconds);
44
  
45
  // sets data direction register for pwm output pin
46
  // activates the output pin
47
  if(pin == 5) { DDRE |= _BV(PORTE3); TCCR3A |= _BV(COM3A1); }
48
  if(pin == 2) { DDRE |= _BV(PORTE4); TCCR3A |= _BV(COM3B1); }
49
  if(pin == 3) { DDRE |= _BV(PORTE5); TCCR3A |= _BV(COM3C1); }
50
  setPwmDuty(pin, duty);
51
  start();
52
}
53
54
void TimerThree::disablePwm(char pin)
55
{
56
  if(pin == 5) TCCR3A &= ~_BV(COM3A1);   // clear the bit that enables pwm on PE3
57
  if(pin == 2) TCCR3A &= ~_BV(COM3B1);   // clear the bit that enables pwm on PE4
58
  if(pin == 3) TCCR3A &= ~_BV(COM3C1);   // clear the bit that enables pwm on PE5
59
}
60
61
void TimerThree::attachInterrupt(void (*isr)(), long microseconds)
62
{
63
  if(microseconds > 0) setPeriod(microseconds);
64
  isrCallback = isr;                                       // register the user's callback with the real ISR
65
  TIMSK3 = _BV(TOIE1);                                     // sets the timer overflow interrupt enable bit
66
  sei();                                                   // ensures that interrupts are globally enabled
67
  start();
68
}
69
70
void TimerThree::detachInterrupt()
71
{
72
  TIMSK3 &= ~_BV(TOIE1);                                   // clears the timer overflow interrupt enable bit 
73
}
74
75
void TimerThree::start()
76
{
77
  TCCR3B |= clockSelectBits;
78
}
79
80
void TimerThree::stop()
81
{
82
  TCCR3B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));          // clears all clock selects bits
83
}
84
85
void TimerThree::restart()
86
{
87
  TCNT3 = 0;
88
}

und
1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
4
#define RESOLUTION 65536    // Timer3 is 16 bit
5
6
class TimerThree
7
{
8
  public:
9
  
10
    // properties
11
    unsigned int pwmPeriod;
12
    unsigned char clockSelectBits;
13
14
    // methods
15
    void initialize(long microseconds=1000000);
16
    void start();
17
    void stop();
18
    void restart();
19
    void pwm(char pin, int duty, long microseconds=-1);
20
    void disablePwm(char pin);
21
    void attachInterrupt(void (*isr)(), long microseconds=-1);
22
    void detachInterrupt();
23
    void setPeriod(long microseconds);
24
    void setPwmDuty(char pin, int duty);
25
    void (*isrCallback)();
26
};
27
28
extern TimerThree Timer3;


hier kommen die bibliotheken her:
http://www.arduino.cc/playground/Code/Timer1

von Karl H. (kbuchegg)


Lesenswert?

tesseract schrieb:

> da ich leider zu doof bin um das selber zu machen, benutze ich dazu
> Bibliotheken.

Zu doof bist du mit Sicherheit nicht. Aber es freut dich einfach nicht, 
dir das Datenblatt zu nehmen und dir anzusehen, was da alles beim Timer 
3 eingestellt wurde und zu analysieren wie das funktioniert, und dann 
bei einem anderen Timer dieselbe Funktionalität im Datenblatt zu suchen 
und entsprechende Anpassungen vorzunehmen.

Nennen wir das Kind doch beim Namen. OK?

von tesseract (Gast)


Lesenswert?

ok, ich bin nicht zu doof, aber habe noh nie ein datenblatt von einem 
atmega angeschaut und war entwas aengstlich.

ein freund hat mir jetzt geholfen und es leuft.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.