Main.c


1
#define F_CPU 4000000UL
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include <util/delay.h>
5
int transition = 0;
6
int divisorMax = 256;
7
8
ISR( TIMER0_OVF_vect ) //Overflow
9
{
10
  static uint8_t divisor = 0;
11
12
  divisor++;
13
  if( divisor >= 0 && divisor < transition ) {
14
15
      for(int i = 0; i < 16; i++)
16
      {
17
      SPDR = 0b00000000; // Schreibe Daten...
18
      while(!(SPSR & 0b10000000)); //...bis fertig.
19
      }
20
21
    PORTB = 0b00000100; // Schreibe Daten...
22
    PORTB = 0b00000000; // ...raus.
23
  }
24
  if( divisor >= transition && divisor <= divisorMax ) {
25
26
      for(int i = 0; i < 16; i++)
27
      {
28
      SPDR = 0b11111111; // Schreibe Daten...
29
      while(!(SPSR & 0b10000000)); //...bis fertig.
30
      }
31
32
    PORTB = 0b00000100; // Schreibe Daten...
33
    PORTB = 0b00000000; // ...raus.
34
  }
35
  if(divisor == divisorMax ) {
36
37
    divisor = 0;
38
   }
39
}
40
41
int main()
42
{
43
44
    DDRB = 0xFF;       // B-Pins als Ausgang definieren (Shift)
45
    SPCR = 0b01010000;   // Interrupt des SPI freigegeben (Shift)
46
47
    TIMSK = 0b00000001;   // Löse Interrupt aus (PWM)
48
    TCCR0 = 0b00000001;  // Vorteiler auf 1 setzten (PWM)
49
    sei();        // Schalte Interrupts ein (PWM)
50
51
    int direction = 1;
52
53
    while(1)
54
    {
55
       int delay = 1; //Pulsgeschwindigkeit
56
57
       if(direction == 1 && transition < (divisorMax-1) )
58
       {
59
        transition++;
60
        _delay_us(delay);
61
       }
62
63
       if(transition == (divisorMax-1))
64
       {
65
        direction = 0;
66
       }
67
68
       if(direction == 0 && transition > 0)
69
       {
70
        transition--;
71
        _delay_us(delay);
72
       }
73
       if(transition == 1)
74
       {
75
        direction = 1;
76
       }
77
78
    }
79
}