Forum: Mikrocontroller und Digitale Elektronik ATtiny 84 Timer unerklärlich langsam


von Ralf G. (sense)


Lesenswert?

Hallo Leute,

ich benötige Mal jemanden, der mein geistiges Dickicht lüftet. Ich will 
5 LEDs ansteuern, in einem synchronen Takt über den Timer1 eines ATtiny 
84.
Außerdem will ich über ein Poti(1kOhm) einstellen können, wie schnell 
der Timer1 läuft. Der Ausgang des Potis geht auf PA5 und die LEDs hängen 
an PA0-PA4. Bis jetzt ist ja nichts schweres dabei, aber irgend läuft 
der Timer1 total langsam. Eigentlich müsste er bei 1MHz Clock, einem 
Prescaler von 1024 und ein OCR1A von 1023, ca. jede 1,xx Sekunde ein 
Interrupt haben. Dem ist aber nicht so anscheinend. Also davon 
ausgegangen, dass Poti ist so eingestellt, dass praktisch VCC anliegt.
VCC ist 3,0V (2*AA-Batterien die voll sind) und Brownout-Detection ist 
bei 1,8 V gesetzt.
Der Clockoutput-Pin ist aktiviert und ich messe dort 1MHz.
Hier der Code:
1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <avr/pgmspace.h>
4
#define F_CPU 1000000
5
#include <util/delay.h>
6
7
#define ARRAY_SIZE 5
8
9
uint8_t step = 0;
10
const uint8_t figur_table[ARRAY_SIZE] PROGMEM = {(1<<PA0),(1<<PA1),(1<<PA2),(1<<PA3),(1<<PA4)};
11
int main()
12
{
13
  uint16_t adc_res = 0;
14
  DDRA = ((1<<PA0)|(1<<PA1)|(1<<PA2)|(1<<PA3)|(1<<PA4));
15
  ADCSRA = (1<<ADEN);
16
  ADMUX = 0b000101;
17
  ADCSRA = (1<<ADSC);
18
  while(ADCSRA & (1<<ADSC));
19
  adc_res = ADCW;
20
  adc_res = 0;
21
  ADCSRA |= (1<<ADSC);
22
  while(ADCSRA & (1<<ADSC));
23
  adc_res = ADCW;
24
  TCCR1B = (1<<CS12)|(1<<CS10)|(1<<WGM12);
25
  TIMSK1 = (1<<OCIE1A);
26
  OCR1A = adc_res;
27
  //OCR1A = 100;
28
  PORTA = ((1<<PA0)|(1<<PA1)|(1<<PA2)|(1<<PA3)|(1<<PA4));
29
  _delay_ms(100);
30
  //PORTA &= ~(1<<PA0)|~(1<<PA1)|~(1<<PA2)|~(1<<PA3)|~(1<<PA4);
31
  PORTA = 0x00;
32
  sei();
33
  while(1)
34
  {
35
    ADMUX = 0b000101;
36
    ADCSRA |= (1<<ADSC);
37
    while(ADCSRA & (1<<ADSC));
38
    adc_res = ADCW;
39
  }
40
41
}
42
ISR(TIM1_COMPA_vect)
43
{
44
  if(step > ARRAY_SIZE)
45
  {
46
    step = 0;
47
  }
48
  PORTA = 0x00;
49
  PORTA |= pgm_read_byte(&figur_table[step]);
50
  PORTA ^= (1<<PA4);
51
  step++;
52
}

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.