Advertisement
nikonn

Lampensimulator 2.0b

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