Advertisement
nikonn

Lampensimulator 2.0

Apr 4th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.08 KB | None | 0 0
  1. // Software für Lampensimulator mit ATtiny2313
  2.  
  3. #include <avr/io.h>
  4. #include <stdlib.h>
  5. #include <util/delay.h>
  6. #include <avr/interrupt.h>
  7.  
  8. #define TRUE 1
  9. #define FALSE 0
  10.  
  11. // Fuse-Bits: --- für internen Clock ---
  12. // efuse = FF
  13. // hfuse = DF    
  14. // lfuse = E2 (4 MHz Grundtaktakt ohne Teilung)                                
  15. // -> Frequenz : 4.000.000 Hz
  16.  
  17. /* Erläuterung zu Bitmanipulation: PORTB &= ~(1 << PIN0) (Bit löschen)
  18. (1 << n) : Zuerst wird durch die '<<'-Ausdrücke eine "1" n-mal nach links geschoben.
  19. Dies ergibt somit 0b00000001 für (1 << PIN0) und 0b00000100 für (1 << PIN2).
  20. Das Ergebnis wird bitweise ODER-verknüpft also 0b00000001 or 0b00000100 wird zu 0b00000101.
  21. ~ : Der Wert in der Klammer wird bitweise invertiert, aus 0b00000101 wird 0b11111010.
  22. &= : PORTB wird mit der berechneten Maske UND-verknüpft und das Ergebnis wieder PORTB zugewiesen.
  23. Quelle: http://www.mikrocontroller.net/articles/Bitmanipulation#AVR-Assembler
  24. */
  25.  
  26. // Die Lampen werden durch das Auslesen entsprechender Arrays gesteuert:
  27. // Der Teiler gibt an, wie viele Zyklen (je 130 ms) von TIMER1_OVF_vect
  28. // für eine Hell- bzw. Dunkelphase gebraucht werden:
  29. #define BLINKARRAY1A_LAENGE 2
  30. #define BLINKARRAY1A_TEILER 8 // Takt 0,5 Hz.
  31. #define BLINKARRAY2A_LAENGE 2
  32. #define BLINKARRAY2A_TEILER 4 // Takt 1 Hz.
  33. #define BLINKARRAY3A_LAENGE 2
  34. #define BLINKARRAY3A_TEILER 2 // Takt 2 Hz.
  35.  
  36. #define BLINKARRAY1B_LAENGE 6
  37. #define BLINKARRAY1B_TEILER 4 // Takt 1 Hz.
  38. #define BLINKARRAY2B_LAENGE 8
  39. #define BLINKARRAY2B_TEILER 4 // Takt 1 Hz.
  40. #define BLINKARRAY3B_LAENGE 10
  41. #define BLINKARRAY3B_TEILER 4 // Takt 1 Hz.
  42.  
  43.  
  44. volatile uint8_t BlinkArray1AZaehler = 0; // Normales Wechselblinken LED 1
  45. volatile uint8_t BlinkArray1A[BLINKARRAY1A_LAENGE] = {1,0};
  46.  
  47. volatile uint8_t BlinkArray2AZaehler = 0; // Normales Wechselblinken LED 2
  48. volatile uint8_t BlinkArray2A[BLINKARRAY2A_LAENGE] = {1,0};
  49.  
  50. volatile uint8_t BlinkArray3AZaehler = 0; // Normales Wechselblinken LED 3
  51. volatile uint8_t BlinkArray3A[BLINKARRAY3A_LAENGE] = {1,0};
  52.  
  53. volatile uint8_t BlinkArray1BZaehler = 0; // Position blinken - 2 s Pause und 1 x Puls 0,5 s
  54. volatile uint8_t BlinkArray1B[BLINKARRAY1B_LAENGE] = {0,0,0,0,1,0};
  55.  
  56. volatile uint8_t BlinkArray2BZaehler = 0; // Position blinken - 2 s Pause und 2 x Puls 0,5 s
  57. volatile uint8_t BlinkArray2B[BLINKARRAY2B_LAENGE] = {0,0,0,0,1,0,1,0};
  58.  
  59. volatile uint8_t BlinkArray3BZaehler = 0; // Position blinken - 2 s Pause und 3 x Puls 0,5 s
  60. volatile uint8_t BlinkArray3B[BLINKARRAY3B_LAENGE] = {0,0,0,0,1,0,1,0,1,0};
  61.  
  62. // Speicher-Variablen für die Zustände Wechselblinken oder Positionierungssignal:
  63. // FALSE = Normales Wechselblinken
  64. // TRUE = Positionierungssignal ausgeben
  65. volatile uint8_t taster1toggle = FALSE; // Toggle-Zustand  Taster 1
  66. volatile uint8_t taster2toggle = FALSE; // Toggle-Zustand  Taster 2
  67. volatile uint8_t taster3toggle = FALSE; // Toggle-Zustand  Taster 3
  68.  
  69. /* -------------------------------------------------------------------------------------
  70.    Timer1-Interrupt-Funktion:
  71. --------------------------------------------------------------------------------------*/
  72. ISR(TIMER1_OVF_vect)
  73. {
  74.     if (taster1toggle == FALSE)
  75.         {
  76.         // Normales Blinken
  77.         if (BlinkArray1A[BlinkArray1AZaehler/BLINKARRAY1A_TEILER]==TRUE)
  78.             {
  79.                 PORTB |= (1<<PIN0); // Bit setzen
  80.             }
  81.             else
  82.             {
  83.                 PORTB &= ~(1 << PIN0); // Bit löschen
  84.             }
  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.        
  104.         BlinkArray1BZaehler++;
  105.         if (BlinkArray1BZaehler>(BLINKARRAY1B_LAENGE*BLINKARRAY1B_TEILER))
  106.             {
  107.                 BlinkArray1BZaehler=0;
  108.             }
  109.         }
  110.        
  111.     if (taster2toggle == FALSE)
  112.         {
  113.         // Normales Blinken
  114.         if (BlinkArray2A[BlinkArray2AZaehler/BLINKARRAY2A_TEILER]==TRUE)
  115.             {
  116.                 PORTB |= (1<<PIN1); // Bit setzen
  117.             }
  118.             else
  119.             {
  120.                 PORTB &= ~(1 << PIN1); // Bit löschen
  121.             }
  122.        
  123.         BlinkArray2AZaehler++;
  124.         if (BlinkArray2AZaehler>(BLINKARRAY2A_LAENGE*BLINKARRAY2A_TEILER))
  125.             {
  126.                 BlinkArray2AZaehler=0;
  127.             }
  128.         }
  129.         else
  130.         {
  131.         // Positionierungssignal ausgeben
  132.         if (BlinkArray2B[BlinkArray2BZaehler/BLINKARRAY2B_TEILER]==TRUE)
  133.             {
  134.                 PORTB |= (1<<PIN1); // Bit setzen
  135.             }
  136.             else
  137.             {
  138.                 PORTB &= ~(1 << PIN1); // Bit löschen
  139.             }
  140.        
  141.         BlinkArray2BZaehler++;
  142.         if (BlinkArray2BZaehler>(BLINKARRAY2B_LAENGE*BLINKARRAY2B_TEILER))
  143.             {
  144.                 BlinkArray2BZaehler=0;
  145.             }
  146.         }
  147.     if (taster3toggle == FALSE)
  148.         {
  149.         // Normales Blinken
  150.         if (BlinkArray3A[BlinkArray3AZaehler/BLINKARRAY3A_TEILER]==TRUE)
  151.             {
  152.                 PORTB |= (1<<PIN2); // Bit setzen
  153.             }
  154.             else
  155.             {
  156.                 PORTB &= ~(1 << PIN2); // Bit löschen
  157.             }
  158.        
  159.         BlinkArray3AZaehler++;
  160.         if (BlinkArray3AZaehler>(BLINKARRAY3A_LAENGE*BLINKARRAY3A_TEILER))
  161.             {
  162.                 BlinkArray3AZaehler=0;
  163.             }
  164.         }
  165.         else
  166.         {
  167.         // Positionierungssignal ausgeben
  168.         if (BlinkArray3B[BlinkArray3BZaehler/BLINKARRAY3B_TEILER]==TRUE)
  169.             {
  170.                 PORTB |= (1<<PIN2); // Bit setzen
  171.             }
  172.             else
  173.             {
  174.                 PORTB &= ~(1 << PIN2); // Bit löschen
  175.             }
  176.        
  177.         BlinkArray3BZaehler++;
  178.         if (BlinkArray3BZaehler>(BLINKARRAY3B_LAENGE*BLINKARRAY3B_TEILER))
  179.             {
  180.                 BlinkArray3BZaehler=0;
  181.             }
  182.         }
  183.        
  184. }
  185.  
  186. int Init_Timer1(void)
  187. {
  188.     // Timer 1 ist 16-Bit-Timer
  189.     // CS12=0, CS11=1, CS10=0 => Vorteiler 8;  -> 4.000.000 Hz : 8  = 500 000  
  190.     // Der Overflow-Interrupt wird aber erst ausgelöst, wenn der Zähler die Zahl 65 536 (16 Bit) erreicht
  191.     // Zeitintervall = 500 000 : 65 536 = 7,63 Hz (ca. alle 130 ms)
  192.     // Damit lassen sich Frequenzen von 1, 2 und 0,5 Hz. "einigermaßen" genau ableiten
  193.     TCCR1B |= (0<<CS12) | (1<<CS11) | (0<<CS10); // Vorteiler 8
  194.     TIMSK |= (1<<TOIE1); // Enable timer 1 interrupt -> ohne die anderen Bits zu verändern
  195.     sei(); //enable all interrupts
  196.     return 0;
  197. }
  198.  
  199. //Funktion zum abfragen des Zustands des negierenden Tasters1
  200. int taster1(void)
  201. {
  202.  
  203.  
  204.     if (PIND & (1<<PIND0))
  205.         {
  206.             return FALSE;
  207.         }
  208.         else
  209.         {
  210.             return TRUE;
  211.         }
  212.    
  213. }
  214.  
  215. //Funktion zum abfragen des Zustands des negierenden Tasters2
  216. int taster2(void)
  217. {
  218.  
  219.  
  220.     if (PIND & (1<<PIND1))
  221.         {
  222.             return FALSE;
  223.         }
  224.         else
  225.         {
  226.             return TRUE;
  227.         }
  228.    
  229. }
  230.  
  231. //Funktion zum abfragen des Zustands des negierenden Tasters3
  232. int taster3(void)
  233. {
  234.  
  235.  
  236.     if (PIND & (1<<PIND2))
  237.         {
  238.             return FALSE;
  239.         }
  240.         else
  241.         {
  242.             return TRUE;
  243.         }
  244.    
  245. }
  246.  
  247. int main (void) {
  248.  
  249.     // Port B Pin0, Pin1, Pin2 auf Ausgang setzen
  250.     DDRB |= (1 << PIN0) | (1 << PIN1) | (1 << PIN2);
  251.     // Port D Pin0, Pin1 und Pin2 (Taster 1, 2 3) als Eingänge setzen
  252.     DDRD &= ~((1 << PIN0) | (1 << PIN1) | (1 << PIN2));
  253.  
  254.     Init_Timer1();
  255.    
  256.     uint8_t taster1_alt = FALSE;
  257.     uint8_t taster2_alt = FALSE;
  258.     uint8_t taster3_alt = FALSE;
  259.    
  260.     while (1)
  261.         {                  
  262.             if ((taster1()==TRUE) && (taster1_alt == FALSE))
  263.                 {
  264.                 if (taster1toggle==FALSE)
  265.                     {
  266.                         taster1toggle=TRUE;
  267.                 // Sorgt dafür, dass Positionierungs-Ausgabe mit 2 s Pause startet:
  268.                         BlinkArray1BZaehler = 0;
  269.                     }
  270.                     else
  271.                     {
  272.                         taster1toggle=FALSE;
  273.                     }
  274.                    
  275.                 }
  276.             taster1_alt=taster1();
  277.  
  278.             if ((taster2()==TRUE) && (taster2_alt == FALSE))
  279.                 {
  280.                 if (taster2toggle==FALSE)
  281.                     {
  282.                         taster2toggle=TRUE;
  283.                 // Sorgt dafür, dass Positionierungs-Ausgabe mit 2 s Pause startet:
  284.                         BlinkArray2BZaehler = 0;
  285.                     }
  286.                     else
  287.                     {
  288.                         taster2toggle=FALSE;
  289.                     }
  290.                    
  291.                 }
  292.             taster2_alt=taster2();
  293.                
  294.             if ((taster3()==TRUE) && (taster3_alt == FALSE))
  295.                 {
  296.                 if (taster3toggle==FALSE)
  297.                     {
  298.                         taster3toggle=TRUE;
  299.                 // Sorgt dafür, dass Positionierungs-Ausgabe mit 2 s Pause startet:
  300.                         BlinkArray3BZaehler = 0;
  301.                     }
  302.                     else
  303.                     {
  304.                         taster3toggle=FALSE;
  305.                     }
  306.                 }
  307.             taster3_alt=taster3();
  308.                    
  309.             _delay_ms(10);
  310.         }
  311.     return 0;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement