Hallo,
bei folgendem Problem komme ich nicht weiter und wollte deshalb euch um
Hilfe bitten.
Dieser Code läuft auf einem Arduino ohne Probleme und ich erhalte das
gewünschte Ergebnis (PWM an zwei Pins mit leicht unterschiedlichen
Frequenzen):
1 | void setup() {
|
2 | // put your setup code here, to run once:
|
3 | pinMode(3, OUTPUT);
|
4 | pinMode(10, OUTPUT);
|
5 | mag_on();
|
6 | OCR2A = round(time_mag);
|
7 | OCR2B = round(duty_mag*time_mag/100L);
|
8 |
|
9 | led_on();
|
10 | OCR1A = round(time_led);
|
11 | OCR1B = round(duty_led*time_led/100L);
|
12 |
|
13 | sei();
|
14 | }
|
15 |
|
16 | void mag_on() {
|
17 | TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
|
18 | TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21) | _BV(CS20);
|
19 | }
|
20 |
|
21 | void led_on() {
|
22 | TCCR1A = _BV(COM1A0) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
|
23 | TCCR1B = _BV(WGM12) | _BV(WGM13) | _BV(CS11);
|
24 | }
|
Da ich aber keinen Arduino in mein Projekt einbauen möchte, sondern
lieber einen Attiny85 (habe schon öfters mit dem gearbeitet), habe ich
versucht, dieses Code auf den Attiny zu portieren:
1 | void initTimer0() { // for magnet (pin 3) && PB4
|
2 | TCCR0A = (1 << WGM01); // set ctc mode
|
3 | TCCR0B = (1 << CS02) | (1 << CS00); // prescaler to 1024
|
4 | OCR0A = 98; // top border of counter used for turn magnet on // every 0.012544 sec
|
5 | OCR0B = 14; // interrupt used for turn magnet off // after 0.0018 sec
|
6 | TIMSK = (1 << OCIE0A) | (1 << OCIE0B); // enables interrupts for A and B
|
7 | }
|
8 |
|
9 | ISR(TIMER0_COMPA_vect) { // turn magnet on
|
10 | PORTB |= (1 << PB4);
|
11 | }
|
12 |
|
13 | ISR(TIMER0_COMPB_vect) { // turn magnet off
|
14 | PORTB &= ~(1 << PB4);
|
15 | }
|
16 |
|
17 | void initTimer1() { // for led
|
18 | TCCR1 = (1 << CTC1) | (1 << PWM1A) | (1 << CS13) | (1 << CS11) | (1 << CS10); // set ctc mode and prescaler to 1024
|
19 | GTCCR = 0;
|
20 | OCR1C = 97; // top border of counter used for turn led on // every 0.012544 sec
|
21 | OCR1B = 14; // interrupt used for turn led off // after 0.0018 sec
|
22 | TIMSK = (1 << OCIE1B); // enables interrupts for A and B
|
23 | PLLCSR &= ~(1 << PLLE); // set synchronous clock
|
24 | }
|
25 |
|
26 | ISR(TIMER1_COMPB_vect) { // turn led off
|
27 | PORTB &= ~(1 << PB3);
|
28 | }
|
29 |
|
30 | ISR( TIMER1_OVF_vect) { // turn led on
|
31 | PORTB |= (1 << PB3);
|
32 | }
|
33 |
|
34 | void setup() {
|
35 | // put your setup code here, to run once:
|
36 | pinMode(4, OUTPUT);
|
37 | pinMode(3, OUTPUT);
|
38 |
|
39 | initTimer0();
|
40 | initTimer1();
|
41 |
|
42 | sei();
|
43 | }
|
Leider erhalte ich gar keinen Output an den gewünschten Pins (2 und 3).
Wäre für jede Hilfe sehr dankbar!
Mit freundlichen Grüßen
Alex