program Test_Eingaenge_des_MC23017; { Declarations section } // ###################################################### Konstanten #################################################### const IODIRA = 0x00; // Datenrichtungs-Register PortA IODIRB = 0x01; // Datenrichtungs-Register PortB IPOLA = 0x02; // Eingabepolaritäts-Register PortA IPOLB = 0x03; // Eingabepolaritäts-Register PortB GPINTENA = 0x04; // Interrupt-on-change-Register PortA GPINTENB = 0x05; // Interrupt-on-change-Register PortB DEFVALA = 0x06; // Interrupt-Value-Register PortA DEFVALB = 0x07; // Interrupt-Value-Register PortB INTCONA = 0x08; // Interrupt-on-change-Control-Register PortA INTCONB = 0x09; // Interrupt-on-change-Control-Register PortB IOCON = 0x0A; // Konfigurations-Register GPPUa = 0x0C; // GPIO-Pull-up-Widerstand-Register PortA GPPUb = 0x0D; // GPIO-Pull-up-Widerstand-Register PortB INTFA = 0x0E; // Interrupt-Flag-Register PortA INTFB = 0x0F; // Interrupt-Flag-Register PortB INTCAPA = 0x10; // Interrupt-Captured-Value-for-Port-Register PortA INTCAPB = 0x11; // Interrupt-Captured-Value-for-Port-Register PortB GPIOA = 0x12; // PortA GPIOB = 0x13; // PortB OLATA = 0x14; // Latch-Register PortA OLATB = 0x15; // Latch-Register PortB // ###################################################### Variablen ##################################################### // Software I2C connections var Soft_I2C_Scl_Output : sbit at PORTB0_bit; Soft_I2C_Sda_Output : sbit at PORTB1_bit; Soft_I2C_Scl_Input : sbit at PINB0_bit; Soft_I2C_Sda_Input : sbit at PINB1_bit; Soft_I2C_Scl_Direction : sbit at DDB0_bit; Soft_I2C_Sda_Direction : sbit at DDB1_bit; // End Software I2C connections PortF : byte; // Daten vom Eingang (PortB) des MCP23017 // #################################################### Hauptprogramm ################################################### begin // ######################################## Initialisieren der ATMega32 - Port's ######################################## DDRA := 0xFF; // Port A ist Ausgang DDRB := 0xFF; // Port B ist Ausgang DDRC := 0xFF; // Port C ist Ausgang DDRD := 0xF3; // PD2 bis PD3 sind Eingang, PD0, PD1, PD4 bis PD7 sind Ausgang // ######################################### Ports und Variablen initialisieren ######################################### PortA := 0; PortB := 0; PortC := 0; PortD := 0; // ############################################### I2C-Bus initialisieren ############################################### Soft_I2C_Init(); // Initialize Soft I2C communication delay_ms(1000); // Nach der Initialisierung warten // ############################################### MCP23017 initialisieren ############################################## Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0x40); // Address MCP23017 schreiben ==> Soft_I2C_Write(IOCON); // Address MCP23017 schreiben ==> Soft_I2C_Write(0x48); // bank = 0 mirror = 0 seqop = 0 disslw = 0 // haen = 1 odr = 0 intpol = 0 bit0 = 0 Soft_I2C_Stop(); // Issue stop signal delay_ms(500); // warten Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0x40); // Address MCP23017 schreiben ==> Soft_I2C_Write(IODIRA); // Adresse auf IODIRA setzen Soft_I2C_Write(0x00); // PortE ist Ausgang Soft_I2C_Write(0xFF); // PortF ist Eingang Soft_I2C_Stop(); // Issue stop signal delay_ms(500); // warten Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0x40); // Address MCP23017 schreiben ==> Soft_I2C_Write(GPPUB); // Adresse auf GPPUB setzen Soft_I2C_Write(0xff); // Aktivieren der internen PullUp-Widerstände Soft_I2C_Stop(); // Issue stop signal delay_ms(500); // warten Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0x40); // Address MCP23017 schreiben ==> Soft_I2C_Write(IPOLB); // Adresse auf IPOLB setzen Soft_I2C_Write(1); // Invertieren des Eingangsports abschalten Soft_I2C_Stop(); // Issue stop signal delay_ms(500); // warten // #################################################### Hauptschleife ################################################### while true do begin // ############################################################################# // I2C lesen MCP23017 Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0x40); // Adresse MCP23017 schreiben ==> Soft_I2C_Write(GPIOB); // Adresse auf GPIOB setzen Soft_I2C_Write(0x41); // Addrese MCP23017 lesen ==> PortF := Soft_I2C_Read(0); // Lesen der Eingänge des MCP23017 Soft_I2C_Stop(); // Issue stop signal delay_ms(500); Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0x40); // Adresse MCP23017 schreiben ==> Soft_I2C_Write(OLATA); // Adresse auf GPIOB setzen Soft_I2C_Write(PortF); // gelesene Daten von PortB des MCP23017 an PortA des MCP23017 ausgeben Soft_I2C_Stop(); // Issue stop signal delay_ms(500); end; end.