int main(void) { uint8_t TWIS_ResponseType; init_modi(); init_io_ports(); init_timer(); set_outputs(); /* ** Start TWI Slave with address 15 and bitrate of 100000 Hz */ TWIS_Init(); /* mainloop - Kommunikation mit dem master */ while (1) { if (TWIS_ResponseRequired(&TWIS_ResponseType)) { switch (TWIS_ResponseType) { /* ** Slave is requested to read bytes from the master. */ case TW_SR_SLA_ACK: { outputdata.ports = TWIS_ReadAck(); outputdata.ports += TWIS_ReadAck() << 8; uint8_t i; for (i = 0; i < PWM_CHAN - 1; i++) outputdata.pwmval[i] = TWIS_ReadAck(); outputdata.pwmval[i] = TWIS_ReadNack(); TWIS_Stop(); // I2C stop set_outputs(); } break; /* ** Slave is requested to send bytes from the master. */ case TW_ST_SLA_ACK: TWIS_Write(outputdata.ports); TWIS_Write(outputdata.ports >> 8); for (int read_p = 0; read_p < PWM_CHAN; read_p++) TWIS_Write(outputdata.pwmval[read_p]); TWIS_Stop(); break; default: TWIS_Stop(); break; } } wdt_reset(); } }
/* ------------------------------------------------------------------------- * Main Funktion * * Initialisiert den Timer Interrupt und * behandelt die Laufschrift * -------------------------------------------------------------------------*/ int main(void) { cli(); // Interrupts sperren (damit keiner dazwischenfunkt) _delay_ms(50); // Wait for POR /*--------------------------------------------------- * Ports konfigurieren (Ein-/Ausgänge) *---------------------------------------------------*/ DDRC = 0x0f; // Portc = Output ADC6 und ADC7 gehen nur fur Analog Messung! DDRB = 0xff; // Portb = Output DDRD = 0xff; // Portd = Output /*--------------------------------------------------------------------------- * 8-Bit Timer TCCR0 für das Multiplexing der LEDs initialisieren * Es wird ca. alle 2 Millisekunden ein Overflow0 Interrupt ausgelöst * Berechnung: T = Vorteiler * Wertebereich Zähler / Taktfreuenz * = 64 * 256 / ( 8000000 Hz ) = 2,048 ms *---------------------------------------------------------------------------*/ TCCR0 |= (1<<CS01) | (1<<CS00); // 8-bit Timer mit 1/64 Vorteiler TIFR |= (1<<TOV0); // Clear overflow flag (TOV0) TIMSK |= (1<<TOIE0); // timer0 will create overflow interrupt TWIS_Init (TWI_DISPLAY_ID, 100000); /*--------------------------------------------------- * Hauptschleife (Laufschrift erzeugen) *---------------------------------------------------*/ char buffer[3]; uint8_t TWIS_ResponseType; showText("--"); /* initialize display, cursor off */ lcd_init(LCD_DISP_ON); /* clear display and home cursor */ lcd_clrscr(); lcd_puts("Servu\ns"); sei(); // Interrupts erlauben uint8_t mainScore=0; uint8_t slaveScore=0; uint8_t mainToServe=0; uint8_t mainGames=0; uint8_t slaveGames=0; while(1) // Endlosschleife { if (TWIS_ResonseRequired (&TWIS_ResponseType)) { if (TWIS_ResponseType == TWIS_ReadBytes) { mainScore = TWIS_ReadAck(); slaveScore = TWIS_ReadAck(); mainGames = TWIS_ReadAck(); slaveGames = TWIS_ReadAck(); mainToServe = TWIS_ReadNack(); // The last Byte TWIS_Stop(); // LCD Display lcd_clrscr(); numberToString(mainScore, buffer); lcd_puts(buffer); if (buffer[1] == 0) { lcd_puts(" "); // Formatierung bei nur 1 stellig } lcd_puts(mainToServe?"*":" "); numberToString(mainGames, buffer); lcd_puts(buffer); // Saetze main lcd_puts("<\n>"); numberToString(slaveScore, buffer); lcd_puts(buffer); if (buffer[1] == 0) { lcd_puts(" "); // Formatierung bei nur 1 stellig } lcd_puts(mainToServe?" ":"*"); numberToString(slaveGames, buffer); lcd_puts(buffer); // Saetze slave // Led Display clearScreen(); numberToString(slaveScore, buffer); showText(buffer); led_showGames(slaveGames); if (!mainToServe) { led_showHaveToServ(); } } } } return 0; }