Timer.c


1
/*********************************************
2
Chip type           : AT90S8535
3
Clock frequency     : 8,000000 MHz
4
Memory model        : Small
5
External SRAM size  : 0
6
Data Stack size     : 128
7
*********************************************/
8
9
#include <90s8535.h>
10
11
//Variablendeklaration
12
int a=0,CompA=992, CompB=1000; //CompA-B geben die Impulslänge vor
13
14
//Timer 1 Overflow Interrupt Routine
15
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
16
{
17
TCNT1=710;  //Timer 1 Wert reinitialisieren  (Gibt die Frequenz vor)
18
19
}
20
21
// Timer 1 output compare A Interrupt Routine
22
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
23
{
24
a=a+1;                      //Zählervariable um 1 erhöhen
25
if (a==4){TCNT1=280; a=0;}   //Neuer Timerstartwert wenn 4 mal ein Comparematch aufgetreten ist
26
                             //Muss in beiden Interruptroutinen stehen, je nachdem welcher Interrupt
27
}                            // [A oder B] zuerst aufgetreten ist.
28
29
30
// Timer 1 output compare B Interrupt Routine
31
interrupt [TIM1_COMPB] void timer1_compb_isr(void)
32
{
33
a=a+1;                       //Zählervariable um 1 erhöhen
34
if (a==4){TCNT1=280; a=0;}   //Neuer Timerstartwert wenn 4 mal ein Comparematch aufgetreten ist
35
                             //Muss in beiden Interruptroutinen stehen, je nachdem welcher Interrupt
36
}                            // [A oder B] zuerst aufgetreten ist.
37
38
// Start Hauptprogramm
39
void main(void)
40
{
41
42
43
// Port D initialization
44
PORTD=0b00000000; //Port D Pin 4,5 als Ausgang
45
DDRD=0b000110000;
46
47
48
49
// Timer/Counter 1 initialization
50
// Clock source: System Clock
51
// Clock value: 31,250 kHz
52
// Mode: Ph. correct PWM top=03FFh (10 bit Modus)
53
// OC1A output: Inverted
54
// OC1B output: Inverted
55
// Noise Canceler: Off
56
// Input Capture on Falling Edge
57
TCCR1A=0b11110011;
58
TCCR1B=0b00000100;
59
60
TCNT1=900;
61
62
OCR1A=CompA;
63
64
OCR1B=CompB;
65
66
67
68
69
// External Interrupt(s) initialization
70
// INT0: Off
71
// INT1: Off
72
GIMSK=0x00;
73
MCUCR=0x00;
74
75
// Timer(s)/Counter(s) Interrupt(s) initialization
76
TIMSK=0b00011100;     //Interrupt bei Overflow und Comparematch A oder B
77
78
79
80
#asm("sei")  // Globale Interrupt freigeben 
81
82
while (1)  //Hauptschleife noch leer
83
      {
84
     
85
86
      };
87
}