1 | Prozessor : ATmega8
|
2 | // Takt : 4000000Hz
|
3 | // Sprache : C
|
4 | ----------------------------
|
5 | //
|
6 | #define F_CPU 3686400
|
7 | #include <avr\io.h>
|
8 | #include <avr\interrupt.h>
|
9 | #include <avr\sleep.h>
|
10 | #include <util\delay.h>
|
11 |
|
12 | volatile uint8_t wakesleep; //Umschaltung wach/schlafmodus 0=schlaf 1= wach
|
13 | volatile uint8_t waitdark; //warten auf sichere Dunkelheit
|
14 | uint8_t msek; //zaehler für sek
|
15 | volatile uint16_t sek;
|
16 |
|
17 | volatile uint16_t eingabe;
|
18 | volatile uint8_t aufab;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | //--------------------------------------------------------------------
|
24 | // TIMER2_COMP_vect - Timer2 Interrupt bei Vergleichswert
|
25 | // aktuelle Einstellung: 125 Hz 8 ms
|
26 | //--------------------------------------------------------------------
|
27 | ISR(TIMER2_COMP_vect) // Interrupt für pwm
|
28 | {
|
29 | msek ++;
|
30 | if (msek>=125){sek++;msek=0;}
|
31 | if(aufab==0){eingabe++;}
|
32 | if(aufab==1){eingabe--;}
|
33 | }
|
34 | //------------------------------------------------------------------------
|
35 | //Interruptroutine für den AC(analog Comparator)
|
36 | //----------------------------------------------------------------
|
37 | ISR(ANA_COMP_vect)
|
38 | {
|
39 |
|
40 |
|
41 | }
|
42 | // Initialisierungen
|
43 | //------------------------------------------------------------------------
|
44 | void init() //PORTC 0,1 und D2 Reserve Ein u Ausgänge
|
45 | {
|
46 | // Ports initialisieren
|
47 | DDRB |= _BV(1); // PORTB1 auf Ausgang (ausgabe der pwm für faden der lichter
|
48 | DDRC |= 0xff; //PORTC auf Ausgänge f LEDs
|
49 | DDRD |=(1<<DDD0); //Ausgang für Motor
|
50 |
|
51 | DDRD &= ~_BV(6); // PORTD6 AIN0 auf Eingang ohne PullUp für AC (Referenzwert-poti)
|
52 | DDRD &= ~_BV(7); // PORTD7 AIN1 auf Eingang ohne PullUp für AC (ist-wert lichtempf Widerstand)
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | /* ACSR = 1<<ACIE | 0<<ACIS1; */ //comparator interrupt on falling edge (AIN1 > AIN0) geht
|
58 |
|
59 |
|
60 | TCCR1A=(1<<WGM11)|(1<<COM1A1); // 9 Bit Zähler, nicht invertierende PWM
|
61 | TCCR1B=(1<<CS11)|(1<<CS10) ; // Teiler 1/64
|
62 |
|
63 |
|
64 | //--- Timer 2 initialisieren --- 8ms - 125 Hz
|
65 | TCCR2=0x05; // Teiler 1/128
|
66 | TCCR2|=0x08; // Modus: Zählen bis Vergleichswert (WGM21=1)
|
67 | OCR2=250; // Vergleichswert speichern
|
68 | TIMSK|=0x80; // Interrupt bei Vergleichswert
|
69 | //--- Interrupts erlauben ---
|
70 | sei();
|
71 |
|
72 |
|
73 |
|
74 | }
|
75 |
|
76 | /////////////////////////////////////////////////////////////////////////////
|
77 | // Main-Funktion
|
78 | /////////////////////////////////////////////////////////////////////////////
|
79 | main()
|
80 | {
|
81 | init(); // Initialisierungen
|
82 | while (true) // Mainloop-Begin
|
83 | {
|
84 | //___________________________________________________________________________ einschaltung bei Dunkelheit
|
85 | /*if (sek==1) { // abfragen alle 1 sek
|
86 | if(ACSR & (1<<ACO)) {waitdark++;sek=0; } //{ PORTB|=(1<<PB1);} // (wenn ACO=1) geht!!!
|
87 | else {sek=0; waitdark=0;} }//{PORTB&=~(1<<PB1);} //(wenn ACO=0)
|
88 | if (waitdark==1) {PORTB|=(1<<PB1);} */ // nach 1 Abfragen einschalten
|
89 |
|
90 | //____________________________________________________________________________ sleep modus
|
91 |
|
92 | /* if(wakesleep==0){
|
93 | set_sleep_mode(SLEEP_MODE_IDLE);
|
94 | sleep_enable();
|
95 | sleep_cpu(); }
|
96 | if(wakesleep==1){sleep_disable(); PORTB |= (1<<PB1);} */
|
97 | //___________________________________________________________________________ ein u aus faden
|
98 | //geht!!
|
99 |
|
100 |
|
101 | if(eingabe>=499){aufab=1;}
|
102 | if(eingabe==0){aufab=3;_delay_ms (1000);aufab=0;eingabe++;}
|
103 | OCR1A=eingabe;
|
104 |
|
105 | if (PINB &(1<<PINB1)){ //Ports C2-5 mit PORTB1 synchronisieren
|
106 | PORTC|=(1<<PC5); PORTC|=(1<<PC4); PORTC|=(1<<PC3); PORTC|=(1<<PC2);}
|
107 | else{ PORTC &=~(1<<PC5); PORTC &=~(1<<PC4); PORTC &=~(1<<PC3); PORTC &=~(1<<PC2);}
|
108 |
|
109 |
|
110 |
|
111 | } // Mainloop-Ende
|
112 | }
|
113 | //---------------------------------------------------------------------------
|