/************************************************************ * reads some dis and make them readable over i2c bus. * * Atmega 16 * +-------+ * S26 (XCK/T0) PB0 -|1 O 40|- PA0 (ADC0) S14 * S25 (T1) PB1 -|2 39|- PA1 (ADC1) S13 * S24 (INT2/AIN0) PB2 -|3 38|- PA2 (ADC2) S12 * S23 (OC0/AIN1) PB3 -|4 37|- PA3 (ADC3) S11 * S22 (SS) PB4 -|5 36|- PA4 (ADC4) S10 * (MOSI) PB5 -|6 35|- PA5 (ADC5) S9 * (MISO) PB6 -|7 34|- PA6 (ADC6) S8 * (SCK) PB7 -|8 33|- PA7 (ADC7) * RESET -|9 32|- AREF * VCC -|10 31|- GND * GND -|11 30|- AVCC * XTAL2 -|12 29|- PC7 (TOSC2) S7 * XTAL1 -|13 28|- PC6 (TOSC1) S6 * S21 (RXD) PD0 -|14 27|- PC5 (TDI) S5 * S20 (TXD) PD1 -|15 26|- PC4 (TDO) S4 * S19 (INT0) PD2 -|16 25|- PC3 (TMS) S3 * S18 (INT1) PD3 -|17 24|- PC2 (TCK) S2 * S17 (OC1B) PD4 -|18 23|- PC1 (DSA) I2C * S16 (OC1A) PD5 -|19 22|- PC0 (SCL) I2C * S15 (ICP1) PD6 -|20 21|- PD7 (OC2) S1 * +-------+ */ #include #include #include #include #include #include #include "eprom.h" #define TWCR_ACK TWCR = (1<= I2C_SREGS_OFFSET && data < I2C_SREGS_OFFSET+I2C_SREG_MAX)) { i2c_reg = data; TWCR_ACK; } else TWCR_NACK; } else { if(i2c_reg < I2C_NUM_REGS) { i2c_regs[i2c_reg] = data; i2c_reg++; TWCR_ACK; } else if (i2c_reg >= I2C_SREGS_OFFSET && i2c_reg < I2C_SREGS_OFFSET+I2C_SREG_MAX) { i2c_system[i2c_reg-I2C_SREGS_OFFSET] = data; i2c_reg++; TWCR_ACK; } else TWCR_NACK; } break; case TW_ST_SLA_ACK: //0xA8 Slave wurde im Lesemodus adressiert und hat ein ACK zurückgegeben. case TW_ST_DATA_ACK: //0xB8 Slave Transmitter, Daten wurden angefordert if (i2c_reg == 0xFF) { i2c_reg = 0; } if (i2c_reg < I2C_NUM_REGS) { TWDR = i2c_regs[i2c_reg]; i2c_reg++; TWCR_ACK; } else if (i2c_reg >= I2C_SREGS_OFFSET && i2c_reg < I2C_SREGS_OFFSET+I2C_SREG_MAX) { TWDR = i2c_system[i2c_reg-I2C_SREGS_OFFSET]; i2c_reg++; TWCR_ACK; } else TWCR_NACK; break; case TW_SR_STOP: TWCR_ACK; break; case TW_ST_DATA_NACK: // 0xC0 Keine Daten mehr gefordert case TW_SR_DATA_NACK: // 0x88 case TW_ST_LAST_DATA: // 0xC8 Last data byte in TWDR has been transmitted (TWEA = “0”); ACK has been received default: TWCR_RESET; break; } } ///////////////////////////////////////////////////////////////////////// // int main( void ) { uint32_t cnt[32] = { 0 }; unsigned int i; uint32_t inval = 0; uint32_t temp = 0; int offdelay; // // disable JTAG MCUCSR |= (1< 0x4f || i2c_system[I2C_SREG_ADDR] < 0x20) { i2c_system[I2C_SREG_ADDR] = I2C_DEFAULT_ADDR; } i2c_system[I2C_SREG_DIOFFDELAY] = EEPROM_read(I2C_SREG_DIOFFDELAY); if (i2c_system[I2C_SREG_DIOFFDELAY] == 0 || i2c_system[I2C_SREG_DIOFFDELAY] == 0xFF) { i2c_system[I2C_SREG_DIOFFDELAY] = 0x20; } i2c_slave_init (i2c_system[I2C_SREG_ADDR]); for (i = 0; i < 32; i++) cnt[i] = 0; // // system loop while( 1 ) { // // digital in registers.. offdelay = i2c_system[I2C_SREG_DIOFFDELAY] << 8; // S1 - S14 i = 0; if (!(PIND & (1 << 7))) cnt[i] = offdelay; i++; if (!(PINC & (1 << 2))) cnt[i] = offdelay; i++; if (!(PINC & (1 << 3))) cnt[i] = offdelay; i++; if (!(PINC & (1 << 4))) cnt[i] = offdelay; i++; if (!(PINC & (1 << 5))) cnt[i] = offdelay; i++; if (!(PINC & (1 << 6))) cnt[i] = offdelay; i++; if (!(PINC & (1 << 7))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 6))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 5))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 4))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 3))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 2))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 1))) cnt[i] = offdelay; i++; if (!(PINA & (1 << 0))) cnt[i] = offdelay; // S15 - S26 i++; if (!(PIND & (1 << 6))) cnt[i] = offdelay; i++; if (!(PIND & (1 << 5))) cnt[i] = offdelay; i++; if (!(PIND & (1 << 4))) cnt[i] = offdelay; i++; if (!(PIND & (1 << 3))) cnt[i] = offdelay; i++; if (!(PIND & (1 << 2))) cnt[i] = offdelay; i++; if (!(PIND & (1 << 1))) cnt[i] = offdelay; i++; if (!(PIND & (1 << 0))) cnt[i] = offdelay; i++; if (!(PINB & (1 << 4))) cnt[i] = offdelay; i++; if (!(PINB & (1 << 3))) cnt[i] = offdelay; i++; if (!(PINB & (1 << 2))) cnt[i] = offdelay; i++; if (!(PINB & (1 << 1))) cnt[i] = offdelay; i++; if (!(PINB & (1 << 0))) cnt[i] = offdelay; // all values for (i = 0; i < 16; i++) { if (cnt[i] == 0) inval &= ~(1 << (i)); else { cnt[i]--; inval |= (1 << (i)); } } for (i = 0; i < 16; i++) { if (cnt[16+i] == 0) temp &= ~(1 << (i)); else { cnt[16+i]--; temp |= (1 << (i)); } } i2c_regs[0] = (unsigned char) (inval & 0x000000FF); i2c_regs[1] = (unsigned char) ((inval & 0x0000FF00) >> 8); i2c_regs[2] = (unsigned char) (temp & 0x000000FF); i2c_regs[3] = (unsigned char) ((temp & 0x0000FF00) >> 8); // // system registers if (i2c_system[I2C_SREG_CMD] != 0) { if (i2c_system[I2C_SREG_CMD] == I2C_CMD_EEPROMREAD) { cli(); i2c_system[I2C_SREG_CMD] = 0; for (i = 0; i < I2C_SREG_MAX; i++) { i2c_system[i] = EEPROM_read(i); } sei(); } if (i2c_system[I2C_SREG_CMD] == I2C_CMD_EEPROMWRITE) { cli(); i2c_system[I2C_SREG_CMD] = 0; for (i = 0; i < I2C_SREG_MAX; i++) { EEPROM_write(i, i2c_system[i]); } sei(); } } i2c_system[I2C_SREG_CMD] = 0; } return 0; };