Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <stdint.h>
  5.  
  6. #define SSR_ON      PORTD |= (1<<PD1);                  // Makro: SSR einschalten
  7. #define SSR_OFF     PORTD &= ~(1<<PD1);                 // Makro: SSR ausschalten
  8.  
  9.  
  10. int32_t volatile timer = 0;                         // Timer
  11. uint32_t volatile dimm = 20;                            // Dimmer-Wert
  12. uint8_t i = 0;                                  // Zähler
  13. uint8_t add = 0;                                // Flag
  14.  
  15.  
  16.  
  17.     // Zero-Cross-Detection
  18.     ISR(INT0_vect)                              // Externer Interrupt
  19.     {
  20.  
  21.         timer = 0;                          // Timer zurücksetzen
  22.  
  23.     }
  24.  
  25.     // Timer-Tick:
  26.     ISR (TIMER0_OVF_vect)                           // Timer-Overflow (1MHz / 256 = 3906 Hz = 256µs)
  27.     {
  28.  
  29.         timer++;                            // Timer-Tick weiterzählen
  30.  
  31.         if (timer >= 40) timer = 0;                     // Nächster Nulldurchgang
  32.  
  33.             if (timer > dimm)                   // Timer hat Wert erreicht (0-40)
  34.             {
  35.                 SSR_ON                      // SSR einschalten
  36.             }
  37.             else                            // Wert noch nicht erreicht
  38.             {
  39.                 SSR_OFF                     // SSR ausschalten
  40.             }
  41.  
  42.     }
  43.  
  44. int main (void) {
  45.  
  46.     DDRD  |= (1 << PD1);                            // SSR zünden (Ausgang)
  47.     DDRD &= ~ (1 << PD2);                           // ZC (Eingang)
  48.  
  49.     // Interrupt ZC-Detection:
  50.     sei();                                  // Interrupts global aktivieren
  51.     EICRA |= (1<<ISC01) | (1<<ISC00);                   // Steigende Flanke
  52.     EIMSK |= (1<<INT0);                         // für INT0 (ZC)
  53.  
  54.     // Timer
  55.     TCCR0B = (0 << CS02) | (0 << CS01) | (1 << CS00);           // kein Vorteiler
  56.     TIMSK0 = (1 << TOIE0);                          // Overflow Interrupt
  57.  
  58.  
  59.     while (1)
  60.     {
  61.  
  62.         if (dimm == 35) add = 0;
  63.         if (dimm == 0) add = 1;
  64.  
  65.         if (add == 1)
  66.         {
  67.             dimm++;
  68.         }
  69.         else
  70.         {
  71.             dimm--;
  72.         }
  73.  
  74.         _delay_ms(4200);
  75.  
  76.     }
  77.  
  78. return 0;
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement