Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <stdint.h>
- #define SSR_ON PORTD |= (1<<PD1); // Makro: SSR einschalten
- #define SSR_OFF PORTD &= ~(1<<PD1); // Makro: SSR ausschalten
- int32_t volatile timer = 0; // Timer
- uint32_t volatile dimm = 20; // Dimmer-Wert
- uint8_t i = 0; // Zähler
- uint8_t add = 0; // Flag
- // Zero-Cross-Detection
- ISR(INT0_vect) // Externer Interrupt
- {
- timer = 0; // Timer zurücksetzen
- }
- // Timer-Tick:
- ISR (TIMER0_OVF_vect) // Timer-Overflow (1MHz / 256 = 3906 Hz = 256µs)
- {
- timer++; // Timer-Tick weiterzählen
- if (timer >= 40) timer = 0; // Nächster Nulldurchgang
- if (timer > dimm) // Timer hat Wert erreicht (0-40)
- {
- SSR_ON // SSR einschalten
- }
- else // Wert noch nicht erreicht
- {
- SSR_OFF // SSR ausschalten
- }
- }
- int main (void) {
- DDRD |= (1 << PD1); // SSR zünden (Ausgang)
- DDRD &= ~ (1 << PD2); // ZC (Eingang)
- // Interrupt ZC-Detection:
- sei(); // Interrupts global aktivieren
- EICRA |= (1<<ISC01) | (1<<ISC00); // Steigende Flanke
- EIMSK |= (1<<INT0); // für INT0 (ZC)
- // Timer
- TCCR0B = (0 << CS02) | (0 << CS01) | (1 << CS00); // kein Vorteiler
- TIMSK0 = (1 << TOIE0); // Overflow Interrupt
- while (1)
- {
- if (dimm == 35) add = 0;
- if (dimm == 0) add = 1;
- if (add == 1)
- {
- dimm++;
- }
- else
- {
- dimm--;
- }
- _delay_ms(4200);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement