pwm.c


1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <avr/signal.h>
4
5
#define  DDRXLED      DDRD     
6
#define  PORTLED      PORTD
7
#define  LED1      PD0
8
#define  LED2      PD1
9
#define  LED3      PD2
10
11
volatile unsigned char z;
12
volatile unsigned char wert1help;
13
volatile unsigned char wert2help;  
14
volatile unsigned char wert3help;
15
volatile unsigned char wert1;
16
volatile unsigned char wert2;  
17
volatile unsigned char wert3;
18
19
20
21
SIGNAL(SIG_OVERFLOW0)
22
{
23
  
24
    if(z++ == 0)                  // OVERFLOW COUNTER man koennte hier selber auch das Register abfragen    
25
    {
26
    PORTLED &= ~(1<<LED1);              // ALLE LED's aus bei 0
27
    PORTLED &= ~(1<<LED2);
28
    PORTLED &= ~(1<<LED3);
29
    }
30
    
31
32
    if(wert1help++ == wert1)            // LED1 an wenn Hilfregister = wert 
33
    {  
34
      PORTLED |= (1<<LED1);
35
    }
36
      if(wert2help++ == wert2)            // LED2 an
37
    {  
38
      PORTLED |= (1<<LED2);            
39
    }
40
    if(wert3help++ == wert3)            // LED3 an
41
    {  
42
      PORTLED |= (1<<LED3);
43
    }
44
  
45
  
46
}
47
48
void io_init(void)
49
{
50
  DDRXLED |= (1<<LED1) | (1<<LED2) | (1<<LED3);    // Als Ausgang
51
  PORTLED &= ~(1<<LED1) & ~(1<<LED2) & ~(1<<LED3);  // Ausgaenge "0"
52
}
53
54
55
56
void timer_init(void)
57
{
58
  TCCR0 = (1<<CS00);      // PRESCALER
59
  TIMSK = (1<<TOIE0);      // TIMER 0 ENABLE
60
}
61
62
63
64
65
int main(void)
66
{
67
  io_init();          // io init
68
  timer_init();        // TIMER 0 OVERFLOW BETRIEB  PRESCALER: 8 
69
  sei();            // globale int's zulassen
70
  wert1 = 100;        // led 1 = 100
71
  wert2 = 10;          // led 2 = 10
72
  wert3 = 200;        // led 3 = 200    
73
  for(;;){}          // Endlosschleife
74
}