Advertisement
Guest User

Untitled

a guest
Apr 9th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 KB | Source Code | 0 0
  1.  
  2. /***INCLUDE********************************************************************************************************************************************/
  3.  
  4. #define F_CPU 8000000UL
  5.  
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include <util/delay.h>
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11.  
  12. /***GLOBALE VARIABELN*/
  13.  
  14. uint8_t modul = 0;
  15. uint8_t posizion_LED_MODUL = 0;
  16. uint8_t posizion_TASTER_MODUL = 0;
  17. uint8_t posizion_SCHALTER_MODUL = 0;
  18. uint8_t posizion_SIEBEN_SEGMENT_MODUL = 0;
  19. uint8_t posizion_ANALOG_MODUL = 0;
  20. uint8_t posizion_TASCHENRECHNER_MODUL = 0;
  21.  
  22. //Port init auswahl // LSB -> port geschriben // Bit 1 - 3 -> auswahl
  23. const uint8_t Port_A                = 0b00000001;
  24. const uint8_t Port_B                = 0b00000011;
  25. const uint8_t Port_C                = 0b00000101;
  26. const uint8_t Port_D                = 0b00000111;
  27. const uint8_t Port_A_B              = 0b00001001;
  28. const uint8_t Port_B_C              = 0b00001011;
  29. const uint8_t Port_C_D              = 0b00001101;
  30.  
  31. //Modul init auswahl // MSB -> port geschriben // Bit 4 -
  32. const uint8_t LED_MODUL             = 0b10000000;
  33. const uint8_t TASTER_MODUL          = 0b10010000;
  34. const uint8_t SCHALTER_MODUL        = 0b10100000;
  35. const uint8_t SIEBEN_SEGMENT_MODUL  = 0b10110000;
  36. const uint8_t ANALOG_MODUL          = 0b11000000;
  37. const uint8_t TASCHENRECHNER_MODUL  = 0b11010000;
  38.  
  39.  
  40. uint8_t ddr_0 = 0;
  41. uint8_t port_0 = 0;
  42.  
  43. uint8_t ddr_1 = 0;
  44. uint8_t port_1 = 0;
  45.  
  46. uint16_t time_counter = 0;
  47.  
  48. int Modulbestimmung(uint8_t posizion);
  49. int Init_Interupt(void);
  50. int Init(uint8_t befehl);
  51. int LED_MODUL_schreiben(uint8_t bin_wert);
  52. int LED_MODUL_An(uint8_t led);
  53. int LED_MODUL_Aus(uint8_t led);
  54. int LED_MODUL_Wechseln(uint8_t led);
  55. uint8_t TASTER_MODUL_Port_lesen(void);
  56. bool TASTER_MODUL_Pin_lesen(uint8_t taster);
  57. uint8_t SCHALTER_MODUL_Port_lesen(void);
  58. bool SCHALTER_MODUL_Pin_lesen(uint8_t schalter);
  59.  
  60. /***HAUPTPROGRAMM**************************************************************************************************************************************/
  61. int main()
  62. {
  63.     Init_Interupt();
  64.     //
  65.     Init(Port_C + LED_MODUL);
  66.     Init(Port_A + SCHALTER_MODUL);
  67.     //DDRA = 0x00;
  68.    
  69.     while(1)
  70.     {
  71.         if(SCHALTER_MODUL_Pin_lesen(0) && SCHALTER_MODUL_Pin_lesen(1))
  72.         {
  73.             LED_MODUL_An(0);
  74.         }else
  75.         {
  76.             LED_MODUL_Aus(0);
  77.         }
  78.  
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. /***PROTOTYPEN UNTERFUNKTIONEN DEFINIERET**************************************************************************************************************/
  85. int Init_Interupt(void)
  86. {
  87.     TCCR2 |= ((1<<CS22) | (1<<CS20) | (1<<CS21));
  88.     //OCR2 = (125-1);
  89.     TIMSK |= (1<<TOIE2);
  90.     sei();/*
  91.     GICR |= (1<<INT0);
  92.     sei();*/
  93.     return 0;
  94. }
  95.  
  96. int Modulbestimmung(uint8_t posizion)
  97. {
  98.     if(modul == 1){
  99.         posizion_LED_MODUL = posizion;
  100.     }
  101.     if(modul == 2){
  102.         posizion_TASTER_MODUL = posizion;
  103.     }
  104.     if(modul == 3){
  105.         posizion_SCHALTER_MODUL = posizion;
  106.     }
  107.     if(modul == 4){
  108.         posizion_SIEBEN_SEGMENT_MODUL = posizion;
  109.     }
  110.     if(modul == 5){
  111.         posizion_ANALOG_MODUL = posizion;
  112.     }
  113.     if(modul == 6){
  114.         posizion_TASCHENRECHNER_MODUL = posizion;
  115.     }
  116.     return 0;
  117. }
  118.  
  119. int Init(uint8_t befehl)
  120. {
  121.     modul = 0;
  122.     if(((befehl & 0b10000000) != 0) && ((befehl & 0b00000001) != 0))
  123.     {
  124.         if((befehl & 0xF0) == LED_MODUL)
  125.         {
  126.             ddr_0 = 0xFF;
  127.             port_0 = 0x00;
  128.             modul = 1;
  129.         }
  130.         if((befehl & 0xF0) == TASTER_MODUL)
  131.         {
  132.             ddr_0 = 0x00;
  133.             modul = 2;
  134.         }
  135.         if((befehl & 0xF0) == SCHALTER_MODUL)
  136.         {
  137.             ddr_0 = 0x00;
  138.             modul = 3;
  139.         }
  140.         if((befehl & 0xF0) == SIEBEN_SEGMENT_MODUL)
  141.         {
  142.             ddr_0 = 0xFF;
  143.             port_0 = 0x00;
  144.             modul = 4;
  145.         }
  146.         if((befehl & 0xF0) == ANALOG_MODUL)
  147.         {
  148.             ddr_0 = 0x00;
  149.             modul = 5;
  150.         }
  151.         if((befehl & 0xF0) == TASCHENRECHNER_MODUL)
  152.         {
  153.             ddr_0 = 0x00;
  154.             modul = 6;
  155.         }
  156.        
  157.         //Schreiben
  158.         if((befehl & 0x0F) == Port_A)
  159.         {
  160.             DDRA = ddr_0;
  161.             PORTA = port_0;
  162.             Modulbestimmung(1);
  163.         }
  164.         if((befehl & 0x0F) == Port_B)
  165.         {
  166.             DDRB = ddr_0;
  167.             PORTB = port_0;
  168.             Modulbestimmung(2);
  169.         }
  170.         if((befehl & 0x0F) == Port_C)
  171.         {
  172.             DDRC = ddr_0;
  173.             PORTC = port_0;
  174.             Modulbestimmung(3);
  175.         }
  176.         if((befehl & 0x0F) == Port_D)
  177.         {
  178.             DDRD = ddr_0;
  179.             PORTD = port_0;
  180.             Modulbestimmung(4);
  181.         }
  182.         if((befehl & 0x0F) == Port_A_B)
  183.         {
  184.             DDRA = ddr_0;
  185.             PORTA = port_0;
  186.             DDRB = ddr_1;
  187.             PORTB = port_1;
  188.             Modulbestimmung(5);
  189.         }
  190.         if((befehl & 0x0F) == Port_B_C)
  191.         {
  192.             DDRB = ddr_0;
  193.             PORTB = port_0;
  194.             DDRC = ddr_1;
  195.             PORTC = port_1;
  196.             Modulbestimmung(6);
  197.         }
  198.         if((befehl & 0x0F) == Port_C_D)
  199.         {
  200.             DDRC = ddr_0;
  201.             PORTC = port_0;
  202.             DDRD = ddr_1;
  203.             PORTD = port_1;
  204.             Modulbestimmung(7);
  205.         }
  206.        
  207.     }
  208.     return 0;
  209. }
  210.  
  211. //LEDs ansteuern
  212. int LED_MODUL_schreiben(uint8_t bin_wert)
  213. {
  214.     if(posizion_LED_MODUL == 1)
  215.     {
  216.         PORTA = bin_wert;
  217.     }
  218.     if(posizion_LED_MODUL == 2)
  219.     {
  220.         PORTB = bin_wert;
  221.     }
  222.     if(posizion_LED_MODUL == 3)
  223.     {
  224.         PORTC = bin_wert;
  225.     }
  226.     if(posizion_LED_MODUL == 4)
  227.     {
  228.         PORTD = bin_wert;
  229.     }
  230.     return 0;
  231. }
  232.  
  233. int LED_MODUL_An(uint8_t led)
  234. {
  235.     if(posizion_LED_MODUL == 1)
  236.     {
  237.         PORTA |= (1 << led);
  238.     }
  239.    
  240.     if(posizion_LED_MODUL == 2)
  241.     {
  242.         PORTB |= (1 << led);
  243.     }
  244.     if(posizion_LED_MODUL == 3)
  245.     {
  246.         PORTC |= (1 << led);
  247.     }
  248.     if(posizion_LED_MODUL == 4)
  249.     {
  250.         PORTD |= (1 << led);
  251.     }
  252.     return 0;
  253. }
  254.  
  255. int LED_MODUL_Aus(uint8_t led)
  256. {
  257.     if(posizion_LED_MODUL == 1)
  258.     {
  259.         PORTA &= ~(1 << led);
  260.     }
  261.     if(posizion_LED_MODUL == 2)
  262.     {
  263.         PORTB &= ~(1 << led);
  264.     }
  265.     if(posizion_LED_MODUL == 3)
  266.     {
  267.         PORTC &= ~(1 << led);
  268.     }
  269.     if(posizion_LED_MODUL == 4)
  270.     {
  271.         PORTD &= ~(1 << led);
  272.     }
  273.     return 0;
  274. }
  275.  
  276. int LED_MODUL_Wechseln(uint8_t led)
  277. {
  278.     if(posizion_LED_MODUL == 1)
  279.     {
  280.         PORTA ^= (1 << led);
  281.     }
  282.     if(posizion_LED_MODUL == 2)
  283.     {
  284.         PORTB ^= (1 << led);
  285.     }
  286.     if(posizion_LED_MODUL == 3)
  287.     {
  288.         PORTC ^= (1 << led);
  289.     }
  290.     if(posizion_LED_MODUL == 4)
  291.     {
  292.         PORTD ^= (1 << led);
  293.     }
  294.     return 0;
  295. }
  296.  
  297. //Taster auslesen
  298. uint8_t TASTER_MODUL_Port_lesen(void)
  299. {
  300.     if(posizion_TASTER_MODUL == 1)
  301.     {
  302.         return PINA;
  303.     }
  304.     if(posizion_TASTER_MODUL == 2)
  305.     {
  306.         return PINB;
  307.     }
  308.     if(posizion_TASTER_MODUL == 3)
  309.     {
  310.         return PINC;
  311.     }
  312.     if(posizion_TASTER_MODUL == 4)
  313.     {
  314.         return PIND;
  315.     }
  316.     return 0;
  317. }
  318.  
  319. bool TASTER_MODUL_Pin_lesen(uint8_t taster)
  320. {
  321.     if(posizion_TASTER_MODUL == 1)
  322.     {
  323.         return (PINA & (1 << taster));
  324.     }
  325.     if(posizion_TASTER_MODUL == 2)
  326.     {
  327.         return (PINB & (1 << taster));
  328.     }
  329.     if(posizion_TASTER_MODUL == 3)
  330.     {
  331.         return (PINC & (1 << taster));
  332.     }
  333.     if(posizion_TASTER_MODUL == 4)
  334.     {
  335.         return (PIND & (1 << taster));
  336.     }
  337.     return 0;
  338. }
  339.  
  340. //Schalter auslesen
  341. uint8_t SCHALTER_MODUL_Port_lesen(void)
  342. {
  343.     if(posizion_SCHALTER_MODUL == 1)
  344.     {
  345.         return PINA;
  346.     }
  347.     if(posizion_SCHALTER_MODUL == 2)
  348.     {
  349.         return PINB;
  350.     }
  351.     if(posizion_SCHALTER_MODUL == 3)
  352.     {
  353.         return PINC;
  354.     }
  355.     if(posizion_SCHALTER_MODUL == 4)
  356.     {
  357.         return PIND;
  358.     }
  359.     return 0;
  360. }
  361.  
  362. bool SCHALTER_MODUL_Pin_lesen(uint8_t schalter)
  363. {
  364.     /*switch(posizion_SCHALTER_MODUL)
  365.     {
  366.         case 1:
  367.         return (PINA & (1 << schalter));
  368.         break;
  369.         case 2:
  370.         return (PINB & (1 << schalter));
  371.         break;
  372.         case 3:
  373.         return (PINC & (1 << schalter));
  374.         break;
  375.         case 4:
  376.         return (PIND & (1 << schalter));
  377.         break;
  378.         default:
  379.         return 0;
  380.         break;
  381.     }*/
  382.     if(posizion_SCHALTER_MODUL == 1)
  383.     {
  384.         return (PINA & (1 << schalter));
  385.     }
  386.     if(posizion_SCHALTER_MODUL == 2)
  387.     {
  388.         return (PINB & (1 << schalter));
  389.     }
  390.     if(posizion_SCHALTER_MODUL == 3)
  391.     {
  392.         return (PINC & (1 << schalter));
  393.     }
  394.     if(posizion_SCHALTER_MODUL == 4)
  395.     {
  396.         return (PIND & (1 << schalter));
  397.     }
  398.     return 0;
  399. }
  400.  
  401. /***INTERUPT*/
  402. ISR(TIMER2_OVF_vect)
  403. {
  404.     PORTC ^= 0x02;
  405.     TCNT2 = 0;
  406. }
  407.  
  408.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement