/********************************************** * I2C library version 1 * Intended for ATmega32U2 processor * * pins: * PORT D 0 - SCL_PULL * PORT D 1 - SDA_PULL * PORT D 2 - SCL_READ * PORT D 3 - SDA_READ * PORT D 6 - LED output * PORT D 7 - Switch output * **********************************************/ #include #include #include #include // bits #define BIT0 0x01 #define BIT1 0x02 #define BIT2 0x04 #define BIT3 0x08 #define BIT4 0x10 #define BIT5 0x20 #define BIT6 0x40 #define BIT7 0x80 // port D stuff #define SCL_PULL BIT0 #define SDA_PULL BIT1 #define SCL_READ BIT2 #define SDA_READ BIT3 #define LED BIT6 #define BUTTON BIT7 // inputs #define SWITCH_ON (PIND & BUTTON) == 0 #define SWITCH_OFF (PIND & BUTTON) != 0 #define SCL_IS_HIGH (PIND & SCL_READ) !=0 #define SDA_IS_HIGH (PIND & SDA_READ) !=0 // outputs #define SCL_SET_LOW PORTD |= SCL_PULL #define SCL_SET_HIGH PORTD &= ~SCL_PULL #define SDA_SET_LOW PORTD |= SDA_PULL #define SDA_SET_HIGH PORTD &= ~SDA_PULL #define LED_ON PORTD |= LED #define LED_OFF PORTD &= ~LED void i2c_usec_delay(void) { __delay_cycles(8); } void delay_10ms(unsigned int val) { unsigned int i; for (i=0; i