void init(void) // Initialisierung des Systems { cli(); DDR(OPTOINPORT) = 0; // whole Port Input OPTOINPORT = 0b11111111; // Enable internal Pullups DDR(CONTROLPORT) = 0; // whole Port Input DDR(CONTROLPORT) |= _BV(DATAOUT); // ... but .... DDR(CONTROLPORT) |= _BV(SELECT_CURRENT); DDR(CONTROLPORT) |= _BV(SELECT_VOLTAGE); CONTROLPORT = 0b11111111; // Enable internal Pullups State = 0; SysClk = 0; TCCR0 = 0b00000011; // Timer0 Prescaler 64 TCCR1A = 0b00000000; // Timer0 Prescaler 64 MCUCR = 0b00000011; // Int0 rising Edge GIMSK = 0b01000000; // enable INT0 ADCSR = 0; // disable adc RCurrent = eeprom_read_byte(&eeRCurrent); sei(); } SIGNAL (SIG_INTERRUPT0) // Int0 P/S ( LOAD ) { ShiftRegister = TempShiftRegister; // Something to start ;-) if (( ShiftRegister & 0x8000 ) == 0) // Output first Bit of ShiftRegister { CONTROLPORT &= ~_BV(DATAOUT); } else { CONTROLPORT |= _BV(DATAOUT); } State = _S0; // Normally next is rising Edge GIMSK |= _BV(PCIE0); // Enable Pin change Interrupt for "CLK" GIFR |= _BV(PCIF); // Enable Pin change Interrupt for "CLK" } SIGNAL (SIG_PIN_CHANGE) // Pin Change PB[3:0] ... CLK { switch(State) { case _S0: // Rising Edge DataIn = CONTROLPORTPINS & ( 1 << DATAIN ); // Read DATAIN Pin if (( ShiftRegister & 0x8000 ) == 0) { CONTROLPORT &= ~_BV(DATAOUT); // Write 0 to DATAOUT Pin } else { CONTROLPORT |= _BV(DATAOUT); // Write 1 to DATAOUT Pin } ShiftRegister <<= 1; // Rotate left if ( DataIn > 0 ) { ShiftRegister |= 1; // Read 1 at DATAIN } else { ShiftRegister &= 0xFFFE; // Read 0 at DATAIN } State = _S1; break; case _S1: // Falling Edge State = _S0; break; default: break; } DataIn = CONTROLPORTPINS & ( 1 << PB2 ); // Check VCC if ( DataIn == 0 ) { eeprom_write_byte(&eeRCurrent, RCurrent); while(1) { asm volatile ("nop"::); // Nichtstun trotz Compileroptimierung } } switch(RState) { case _R0: CONTROLPORT &= ~_BV(SELECT_CURRENT); // Write 0 to SELECT_CURRENT Pin RState++; break; case _R1: TempCurrent = OPTOINPORTPINS; CONTROLPORT |= _BV(SELECT_CURRENT); // Write 1 to SELECT_CURRENT Pin RState++; break; case _R2: CONTROLPORT &= ~_BV(SELECT_VOLTAGE); // Write 0 to SELECT_VOLTAGE Pin RState++; break; case _R3: TempVoltage = OPTOINPORTPINS; CONTROLPORT |= _BV(SELECT_VOLTAGE); // Write 1 to SELECT_VOLTAGE Pin RState++; break; default: break; } GIFR |= _BV(PCIF); }