Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***INCLUDE********************************************************************************************************************************************/
- #define F_CPU 8000000UL
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <stdint.h>
- #include <stdbool.h>
- /***GLOBALE VARIABELN*/
- uint8_t modul = 0;
- uint8_t posizion_LED_MODUL = 0;
- uint8_t posizion_TASTER_MODUL = 0;
- uint8_t posizion_SCHALTER_MODUL = 0;
- uint8_t posizion_SIEBEN_SEGMENT_MODUL = 0;
- uint8_t posizion_ANALOG_MODUL = 0;
- uint8_t posizion_TASCHENRECHNER_MODUL = 0;
- //Port init auswahl // LSB -> port geschriben // Bit 1 - 3 -> auswahl
- const uint8_t Port_A = 0b00000001;
- const uint8_t Port_B = 0b00000011;
- const uint8_t Port_C = 0b00000101;
- const uint8_t Port_D = 0b00000111;
- const uint8_t Port_A_B = 0b00001001;
- const uint8_t Port_B_C = 0b00001011;
- const uint8_t Port_C_D = 0b00001101;
- //Modul init auswahl // MSB -> port geschriben // Bit 4 -
- const uint8_t LED_MODUL = 0b10000000;
- const uint8_t TASTER_MODUL = 0b10010000;
- const uint8_t SCHALTER_MODUL = 0b10100000;
- const uint8_t SIEBEN_SEGMENT_MODUL = 0b10110000;
- const uint8_t ANALOG_MODUL = 0b11000000;
- const uint8_t TASCHENRECHNER_MODUL = 0b11010000;
- uint8_t ddr_0 = 0;
- uint8_t port_0 = 0;
- uint8_t ddr_1 = 0;
- uint8_t port_1 = 0;
- uint16_t time_counter = 0;
- int Modulbestimmung(uint8_t posizion);
- int Init_Interupt(void);
- int Init(uint8_t befehl);
- int LED_MODUL_schreiben(uint8_t bin_wert);
- int LED_MODUL_An(uint8_t led);
- int LED_MODUL_Aus(uint8_t led);
- int LED_MODUL_Wechseln(uint8_t led);
- uint8_t TASTER_MODUL_Port_lesen(void);
- bool TASTER_MODUL_Pin_lesen(uint8_t taster);
- uint8_t SCHALTER_MODUL_Port_lesen(void);
- bool SCHALTER_MODUL_Pin_lesen(uint8_t schalter);
- /***HAUPTPROGRAMM**************************************************************************************************************************************/
- int main()
- {
- Init_Interupt();
- //
- Init(Port_C + LED_MODUL);
- Init(Port_A + SCHALTER_MODUL);
- //DDRA = 0x00;
- while(1)
- {
- if(SCHALTER_MODUL_Pin_lesen(0) && SCHALTER_MODUL_Pin_lesen(1))
- {
- LED_MODUL_An(0);
- }else
- {
- LED_MODUL_Aus(0);
- }
- }
- }
- /***PROTOTYPEN UNTERFUNKTIONEN DEFINIERET**************************************************************************************************************/
- int Init_Interupt(void)
- {
- TCCR2 |= ((1<<CS22) | (1<<CS20) | (1<<CS21));
- //OCR2 = (125-1);
- TIMSK |= (1<<TOIE2);
- sei();/*
- GICR |= (1<<INT0);
- sei();*/
- return 0;
- }
- int Modulbestimmung(uint8_t posizion)
- {
- if(modul == 1){
- posizion_LED_MODUL = posizion;
- }
- if(modul == 2){
- posizion_TASTER_MODUL = posizion;
- }
- if(modul == 3){
- posizion_SCHALTER_MODUL = posizion;
- }
- if(modul == 4){
- posizion_SIEBEN_SEGMENT_MODUL = posizion;
- }
- if(modul == 5){
- posizion_ANALOG_MODUL = posizion;
- }
- if(modul == 6){
- posizion_TASCHENRECHNER_MODUL = posizion;
- }
- return 0;
- }
- int Init(uint8_t befehl)
- {
- modul = 0;
- if(((befehl & 0b10000000) != 0) && ((befehl & 0b00000001) != 0))
- {
- if((befehl & 0xF0) == LED_MODUL)
- {
- ddr_0 = 0xFF;
- port_0 = 0x00;
- modul = 1;
- }
- if((befehl & 0xF0) == TASTER_MODUL)
- {
- ddr_0 = 0x00;
- modul = 2;
- }
- if((befehl & 0xF0) == SCHALTER_MODUL)
- {
- ddr_0 = 0x00;
- modul = 3;
- }
- if((befehl & 0xF0) == SIEBEN_SEGMENT_MODUL)
- {
- ddr_0 = 0xFF;
- port_0 = 0x00;
- modul = 4;
- }
- if((befehl & 0xF0) == ANALOG_MODUL)
- {
- ddr_0 = 0x00;
- modul = 5;
- }
- if((befehl & 0xF0) == TASCHENRECHNER_MODUL)
- {
- ddr_0 = 0x00;
- modul = 6;
- }
- //Schreiben
- if((befehl & 0x0F) == Port_A)
- {
- DDRA = ddr_0;
- PORTA = port_0;
- Modulbestimmung(1);
- }
- if((befehl & 0x0F) == Port_B)
- {
- DDRB = ddr_0;
- PORTB = port_0;
- Modulbestimmung(2);
- }
- if((befehl & 0x0F) == Port_C)
- {
- DDRC = ddr_0;
- PORTC = port_0;
- Modulbestimmung(3);
- }
- if((befehl & 0x0F) == Port_D)
- {
- DDRD = ddr_0;
- PORTD = port_0;
- Modulbestimmung(4);
- }
- if((befehl & 0x0F) == Port_A_B)
- {
- DDRA = ddr_0;
- PORTA = port_0;
- DDRB = ddr_1;
- PORTB = port_1;
- Modulbestimmung(5);
- }
- if((befehl & 0x0F) == Port_B_C)
- {
- DDRB = ddr_0;
- PORTB = port_0;
- DDRC = ddr_1;
- PORTC = port_1;
- Modulbestimmung(6);
- }
- if((befehl & 0x0F) == Port_C_D)
- {
- DDRC = ddr_0;
- PORTC = port_0;
- DDRD = ddr_1;
- PORTD = port_1;
- Modulbestimmung(7);
- }
- }
- return 0;
- }
- //LEDs ansteuern
- int LED_MODUL_schreiben(uint8_t bin_wert)
- {
- if(posizion_LED_MODUL == 1)
- {
- PORTA = bin_wert;
- }
- if(posizion_LED_MODUL == 2)
- {
- PORTB = bin_wert;
- }
- if(posizion_LED_MODUL == 3)
- {
- PORTC = bin_wert;
- }
- if(posizion_LED_MODUL == 4)
- {
- PORTD = bin_wert;
- }
- return 0;
- }
- int LED_MODUL_An(uint8_t led)
- {
- if(posizion_LED_MODUL == 1)
- {
- PORTA |= (1 << led);
- }
- if(posizion_LED_MODUL == 2)
- {
- PORTB |= (1 << led);
- }
- if(posizion_LED_MODUL == 3)
- {
- PORTC |= (1 << led);
- }
- if(posizion_LED_MODUL == 4)
- {
- PORTD |= (1 << led);
- }
- return 0;
- }
- int LED_MODUL_Aus(uint8_t led)
- {
- if(posizion_LED_MODUL == 1)
- {
- PORTA &= ~(1 << led);
- }
- if(posizion_LED_MODUL == 2)
- {
- PORTB &= ~(1 << led);
- }
- if(posizion_LED_MODUL == 3)
- {
- PORTC &= ~(1 << led);
- }
- if(posizion_LED_MODUL == 4)
- {
- PORTD &= ~(1 << led);
- }
- return 0;
- }
- int LED_MODUL_Wechseln(uint8_t led)
- {
- if(posizion_LED_MODUL == 1)
- {
- PORTA ^= (1 << led);
- }
- if(posizion_LED_MODUL == 2)
- {
- PORTB ^= (1 << led);
- }
- if(posizion_LED_MODUL == 3)
- {
- PORTC ^= (1 << led);
- }
- if(posizion_LED_MODUL == 4)
- {
- PORTD ^= (1 << led);
- }
- return 0;
- }
- //Taster auslesen
- uint8_t TASTER_MODUL_Port_lesen(void)
- {
- if(posizion_TASTER_MODUL == 1)
- {
- return PINA;
- }
- if(posizion_TASTER_MODUL == 2)
- {
- return PINB;
- }
- if(posizion_TASTER_MODUL == 3)
- {
- return PINC;
- }
- if(posizion_TASTER_MODUL == 4)
- {
- return PIND;
- }
- return 0;
- }
- bool TASTER_MODUL_Pin_lesen(uint8_t taster)
- {
- if(posizion_TASTER_MODUL == 1)
- {
- return (PINA & (1 << taster));
- }
- if(posizion_TASTER_MODUL == 2)
- {
- return (PINB & (1 << taster));
- }
- if(posizion_TASTER_MODUL == 3)
- {
- return (PINC & (1 << taster));
- }
- if(posizion_TASTER_MODUL == 4)
- {
- return (PIND & (1 << taster));
- }
- return 0;
- }
- //Schalter auslesen
- uint8_t SCHALTER_MODUL_Port_lesen(void)
- {
- if(posizion_SCHALTER_MODUL == 1)
- {
- return PINA;
- }
- if(posizion_SCHALTER_MODUL == 2)
- {
- return PINB;
- }
- if(posizion_SCHALTER_MODUL == 3)
- {
- return PINC;
- }
- if(posizion_SCHALTER_MODUL == 4)
- {
- return PIND;
- }
- return 0;
- }
- bool SCHALTER_MODUL_Pin_lesen(uint8_t schalter)
- {
- /*switch(posizion_SCHALTER_MODUL)
- {
- case 1:
- return (PINA & (1 << schalter));
- break;
- case 2:
- return (PINB & (1 << schalter));
- break;
- case 3:
- return (PINC & (1 << schalter));
- break;
- case 4:
- return (PIND & (1 << schalter));
- break;
- default:
- return 0;
- break;
- }*/
- if(posizion_SCHALTER_MODUL == 1)
- {
- return (PINA & (1 << schalter));
- }
- if(posizion_SCHALTER_MODUL == 2)
- {
- return (PINB & (1 << schalter));
- }
- if(posizion_SCHALTER_MODUL == 3)
- {
- return (PINC & (1 << schalter));
- }
- if(posizion_SCHALTER_MODUL == 4)
- {
- return (PIND & (1 << schalter));
- }
- return 0;
- }
- /***INTERUPT*/
- ISR(TIMER2_OVF_vect)
- {
- PORTC ^= 0x02;
- TCNT2 = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement