document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // Software für Lampensimulator mit ATtiny2313
  2. #include <avr/io.h>
  3. #include <stdlib.h>
  4. #include <util/delay.h>
  5. #include <avr/interrupt.h>
  6. #define TRUE 1
  7. #define FALSE 0
  8. // Fuse-Bits: --- für internen Clock ---
  9. // efuse = FF
  10. // hfuse = DF    
  11. // lfuse = E2 (4 MHz Grundtaktakt ohne Teilung)                                
  12. // -> Frequenz : 4.000.000 Hz
  13. //
  14. // Die Lampen werden durch das Auslesen entsprechender Arrays gesteuert:
  15. // Der Teiler gibt an, wie viele Zyklen (je 130 ms) von TIMER1_OVF_vect
  16. // für eine Hell- bzw. Dunkelphase gebraucht werden:
  17. #define BLINKARRAY1A_LAENGE 2
  18. #define BLINKARRAY1A_TEILER 8 // Takt 0,5 Hz.
  19. #define BLINKARRAY2A_LAENGE 2
  20. #define BLINKARRAY2A_TEILER 4 // Takt 1 Hz.
  21. #define BLINKARRAY3A_LAENGE 2
  22. #define BLINKARRAY3A_TEILER 2 // Takt 2 Hz.
  23. #define BLINKARRAY1B_LAENGE 6
  24. #define BLINKARRAY1B_TEILER 4 // Takt 1 Hz.
  25. #define BLINKARRAY2B_LAENGE 8
  26. #define BLINKARRAY2B_TEILER 4 // Takt 1 Hz.
  27. #define BLINKARRAY3B_LAENGE 10
  28. #define BLINKARRAY3B_TEILER 4 // Takt 1 Hz.
  29.  
  30. volatile uint8_t BlinkArray1AZaehler = 0; // Normales Wechselblinken LED 1
  31. volatile uint8_t BlinkArray1A[BLINKARRAY1A_LAENGE] =
  32. {
  33.   1, 0
  34. };
  35. volatile uint8_t BlinkArray2AZaehler = 0; // Normales Wechselblinken LED 2
  36. volatile uint8_t BlinkArray2A[BLINKARRAY2A_LAENGE] =
  37. {
  38.   1, 0
  39. };
  40. volatile uint8_t BlinkArray3AZaehler = 0; // Normales Wechselblinken LED 3
  41. volatile uint8_t BlinkArray3A[BLINKARRAY3A_LAENGE] =
  42. {
  43.   1, 0
  44. };
  45. volatile uint8_t BlinkArray1BZaehler = 0;
  46.   // Position blinken - 2 s Pause und 1 x Puls 0,5 s
  47. volatile uint8_t BlinkArray1B[BLINKARRAY1B_LAENGE] =
  48. {
  49.   0, 0, 0, 0, 1, 0
  50. };
  51. volatile uint8_t BlinkArray2BZaehler = 0;
  52.   // Position blinken - 2 s Pause und 2 x Puls 0,5 s
  53. volatile uint8_t BlinkArray2B[BLINKARRAY2B_LAENGE] =
  54. {
  55.   0, 0, 0, 0, 1, 0, 1, 0
  56. };
  57. volatile uint8_t BlinkArray3BZaehler = 0;
  58.   // Position blinken - 2 s Pause und 3 x Puls 0,5 s
  59. volatile uint8_t BlinkArray3B[BLINKARRAY3B_LAENGE] =
  60. {
  61.   0, 0, 0, 0, 1, 0, 1, 0, 1, 0
  62. };
  63. // Variablen für die Zustände Wechselblinken oder Positionierungssignal:
  64. // FALSE = Normales Wechselblinken
  65. // TRUE = Positionierungssignal ausgeben
  66. volatile uint8_t taster1toggle = FALSE; // Toggle-Zustand  Taster 1
  67. volatile uint8_t taster2toggle = FALSE; // Toggle-Zustand  Taster 2
  68. volatile uint8_t taster3toggle = FALSE; // Toggle-Zustand  Taster 3
  69.  
  70. /* --------------------------------------------------------------------
  71. Timer1-Interrupt-Funktion:
  72. ---------------------------------------------------------------------*/
  73. ISR(TIMER1_OVF_vect)
  74. {
  75.   if (taster1toggle == FALSE)
  76.   {
  77.     // Normales Blinken
  78.     if (BlinkArray1A[BlinkArray1AZaehler / BLINKARRAY1A_TEILER] == TRUE)
  79.     {
  80.       PORTB |= (1 << PIN0); // Bit setzen
  81.     }
  82.     else
  83.     {
  84.       PORTB &= ~(1 << PIN0); // Bit löschen
  85.     }
  86.     BlinkArray1AZaehler++;
  87.     if (BlinkArray1AZaehler > (BLINKARRAY1A_LAENGE *BLINKARRAY1A_TEILER))
  88.     {
  89.       BlinkArray1AZaehler = 0;
  90.     }
  91.   }
  92.   else
  93.   {
  94.     // Positionierungssignal ausgeben
  95.     if (BlinkArray1B[BlinkArray1BZaehler / BLINKARRAY1B_TEILER] == TRUE)
  96.     {
  97.       PORTB |= (1 << PIN0); // Bit setzen
  98.     }
  99.     else
  100.     {
  101.       PORTB &= ~(1 << PIN0); // Bit löschen
  102.     }
  103.     BlinkArray1BZaehler++;
  104.     if (BlinkArray1BZaehler > (BLINKARRAY1B_LAENGE *BLINKARRAY1B_TEILER))
  105.     {
  106.       BlinkArray1BZaehler = 0;
  107.     }
  108.   }
  109.   if (taster2toggle == FALSE)
  110.   {
  111.     // Normales Blinken
  112.     if (BlinkArray2A[BlinkArray2AZaehler / BLINKARRAY2A_TEILER] == TRUE)
  113.     {
  114.       PORTB |= (1 << PIN1); // Bit setzen
  115.     }
  116.     else
  117.     {
  118.       PORTB &= ~(1 << PIN1); // Bit löschen
  119.     }
  120.     BlinkArray2AZaehler++;
  121.     if (BlinkArray2AZaehler > (BLINKARRAY2A_LAENGE *BLINKARRAY2A_TEILER))
  122.     {
  123.       BlinkArray2AZaehler = 0;
  124.     }
  125.   }
  126.   else
  127.   {
  128.     // Positionierungssignal ausgeben
  129.     if (BlinkArray2B[BlinkArray2BZaehler / BLINKARRAY2B_TEILER] == TRUE)
  130.     {
  131.       PORTB |= (1 << PIN1); // Bit setzen
  132.     }
  133.     else
  134.     {
  135.       PORTB &= ~(1 << PIN1); // Bit löschen
  136.     }
  137.     BlinkArray2BZaehler++;
  138.     if (BlinkArray2BZaehler > (BLINKARRAY2B_LAENGE *BLINKARRAY2B_TEILER))
  139.     {
  140.       BlinkArray2BZaehler = 0;
  141.     }
  142.   }
  143.   if (taster3toggle == FALSE)
  144.   {
  145.     // Normales Blinken
  146.     if (BlinkArray3A[BlinkArray3AZaehler / BLINKARRAY3A_TEILER] == TRUE)
  147.     {
  148.       PORTB |= (1 << PIN2); // Bit setzen
  149.     }
  150.     else
  151.     {
  152.       PORTB &= ~(1 << PIN2); // Bit löschen
  153.     }
  154.     BlinkArray3AZaehler++;
  155.     if (BlinkArray3AZaehler > (BLINKARRAY3A_LAENGE *BLINKARRAY3A_TEILER))
  156.     {
  157.       BlinkArray3AZaehler = 0;
  158.     }
  159.   }
  160.   else
  161.   {
  162.     // Positionierungssignal ausgeben
  163.     if (BlinkArray3B[BlinkArray3BZaehler / BLINKARRAY3B_TEILER] == TRUE)
  164.     {
  165.       PORTB |= (1 << PIN2); // Bit setzen
  166.     }
  167.     else
  168.     {
  169.       PORTB &= ~(1 << PIN2); // Bit löschen
  170.     }
  171.     BlinkArray3BZaehler++;
  172.     if (BlinkArray3BZaehler > (BLINKARRAY3B_LAENGE *BLINKARRAY3B_TEILER))
  173.     {
  174.       BlinkArray3BZaehler = 0;
  175.     }
  176.   }
  177. }
  178.  
  179. int Init_Timer1(void)
  180. {
  181.   // Timer 1 ist 16-Bit-Timer
  182.    // CS12=0, CS11=1, CS10=0 => Vorteiler 8;  -> 4.000.000 Hz : 8  = 500 000  
  183.   // Der Overflow-Interrupt wird aber erst ausgelöst,
  184.   // wenn der Zähler die Zahl 65 536 (16 Bit) erreicht
  185.   // Zeitintervall = 500 000 : 65 536 = 7,63 Hz (ca. alle 130 ms)
  186.   // Damit lassen sich Frequenzen von ca. 1, 2 und 0,5 Hz. ableiten
  187.   TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10); // Vorteiler 8
  188.   // Enable timer 1 interrupt -> ohne die anderen Bits zu verändern
  189.   TIMSK |= (1 << TOIE1);
  190.   sei(); //enable all interrupts
  191.   return 0;
  192. }
  193.  
  194. //Funktion zum Abfragen des Zustands des negierenden Tasters1
  195. int taster1(void)
  196. {
  197.   if (PIND &(1 << PIND0))
  198.   {
  199.     return FALSE;
  200.   }
  201.   else
  202.   {
  203.     return TRUE;
  204.   }
  205. }
  206.  
  207. //Funktion zum Abfragen des Zustands des negierenden Tasters2
  208. int taster2(void)
  209. {
  210.   if (PIND &(1 << PIND1))
  211.   {
  212.     return FALSE;
  213.   }
  214.   else
  215.   {
  216.     return TRUE;
  217.   }
  218. }
  219.  
  220. //Funktion zum Abfragen des Zustands des negierenden Tasters3
  221. int taster3(void)
  222. {
  223.   if (PIND &(1 << PIND2))
  224.   {
  225.     return FALSE;
  226.   }
  227.   else
  228.   {
  229.     return TRUE;
  230.   }
  231. }
  232.  
  233. int main(void)
  234. {
  235.   // Port B Pin0, Pin1, Pin2 auf Ausgang setzen
  236.   DDRB |= (1 << PIN0) | (1 << PIN1) | (1 << PIN2);
  237.   // Port D Pin0, Pin1 und Pin2 (Taster 1, 2 3) als Eingänge setzen
  238.   DDRD &= ~((1 << PIN0) | (1 << PIN1) | (1 << PIN2));
  239.   Init_Timer1();
  240.   uint8_t taster1_alt = FALSE;
  241.   uint8_t taster2_alt = FALSE;
  242.   uint8_t taster3_alt = FALSE;
  243.   while (1)
  244.   {
  245.     if ((taster1() == TRUE) && (taster1_alt == FALSE))
  246.     {
  247.       if (taster1toggle == FALSE)
  248.       {
  249.         taster1toggle = TRUE;
  250.         // Sorgt dafür, dass Positionierungs-Ausgabe mit 2 s Pause startet:
  251.         BlinkArray1BZaehler = 0;
  252.       }
  253.       else
  254.       {
  255.         taster1toggle = FALSE;
  256.       }
  257.     }
  258.     taster1_alt = taster1();
  259.     if ((taster2() == TRUE) && (taster2_alt == FALSE))
  260.     {
  261.       if (taster2toggle == FALSE)
  262.       {
  263.         taster2toggle = TRUE;
  264.         // Sorgt dafür, dass Positionierungs-Ausgabe mit 2 s Pause startet:
  265.         BlinkArray2BZaehler = 0;
  266.       }
  267.       else
  268.       {
  269.         taster2toggle = FALSE;
  270.       }
  271.     }
  272.     taster2_alt = taster2();
  273.     if ((taster3() == TRUE) && (taster3_alt == FALSE))
  274.     {
  275.       if (taster3toggle == FALSE)
  276.       {
  277.         taster3toggle = TRUE;
  278.         // Sorgt dafür, dass Positionierungs-Ausgabe mit 2 s Pause startet:
  279.         BlinkArray3BZaehler = 0;
  280.       }
  281.       else
  282.       {
  283.         taster3toggle = FALSE;
  284.       }
  285.     }
  286.     taster3_alt = taster3();
  287.     _delay_ms(10);
  288.   }
  289.   return 0;
  290. }
');