|
|
@ -16,7 +16,15 @@
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t I2C_ErrorCode;
|
|
|
|
uint8_t I2C_ErrorCode;
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
Public Function: i2c_init
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purpose: Initialise TWI/I2C interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Parameter: none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return Value: none
|
|
|
|
|
|
|
|
**********************************************/
|
|
|
|
void i2c_init(void){
|
|
|
|
void i2c_init(void){
|
|
|
|
// set clock
|
|
|
|
// set clock
|
|
|
|
switch (PSC_I2C) {
|
|
|
|
switch (PSC_I2C) {
|
|
|
@ -37,6 +45,16 @@ void i2c_init(void){
|
|
|
|
// enable
|
|
|
|
// enable
|
|
|
|
TWCR = (1 << TWEN);
|
|
|
|
TWCR = (1 << TWEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
Public Function: i2c_start
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purpose: Start TWI/I2C interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Parameter:
|
|
|
|
|
|
|
|
- uint8_t i2c_addr: Adress of reciever
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return Value: none
|
|
|
|
|
|
|
|
**********************************************/
|
|
|
|
void i2c_start(uint8_t i2c_addr){
|
|
|
|
void i2c_start(uint8_t i2c_addr){
|
|
|
|
// i2c start
|
|
|
|
// i2c start
|
|
|
|
TWCR = (1 << TWINT)|(1 << TWSTA)|(1 << TWEN);
|
|
|
|
TWCR = (1 << TWINT)|(1 << TWSTA)|(1 << TWEN);
|
|
|
@ -62,10 +80,29 @@ void i2c_start(uint8_t i2c_addr){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
Public Function: i2c_stop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purpose: Stop TWI/I2C interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Parameter: none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return Value: none
|
|
|
|
|
|
|
|
**********************************************/
|
|
|
|
void i2c_stop(void){
|
|
|
|
void i2c_stop(void){
|
|
|
|
// i2c stop
|
|
|
|
// i2c stop
|
|
|
|
TWCR = (1 << TWINT)|(1 << TWSTO)|(1 << TWEN);
|
|
|
|
TWCR = (1 << TWINT)|(1 << TWSTO)|(1 << TWEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
Public Function: i2c_byte
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purpose: Send byte at TWI/I2C interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Parameter:
|
|
|
|
|
|
|
|
- uint8_t byte: Byte to send to reciever
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return Value: none
|
|
|
|
|
|
|
|
**********************************************/
|
|
|
|
void i2c_byte(uint8_t byte){
|
|
|
|
void i2c_byte(uint8_t byte){
|
|
|
|
TWDR = byte;
|
|
|
|
TWDR = byte;
|
|
|
|
TWCR = (1 << TWINT)|( 1 << TWEN);
|
|
|
|
TWCR = (1 << TWINT)|( 1 << TWEN);
|
|
|
@ -79,6 +116,17 @@ void i2c_byte(uint8_t byte){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
Public Function: i2c_readAck
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purpose: read acknowledge from TWI/I2C Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Parameter: none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return Value: uint8_t
|
|
|
|
|
|
|
|
- TWDR: recieved value at TWI/I2C-Interface, 0 at timeout
|
|
|
|
|
|
|
|
- 0: Error at read
|
|
|
|
|
|
|
|
**********************************************/
|
|
|
|
uint8_t i2c_readAck(void){
|
|
|
|
uint8_t i2c_readAck(void){
|
|
|
|
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
|
|
|
|
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
|
|
|
|
uint16_t timeout = F_CPU/F_I2C*2.0;
|
|
|
|
uint16_t timeout = F_CPU/F_I2C*2.0;
|
|
|
@ -92,6 +140,18 @@ uint8_t i2c_readAck(void){
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return TWDR;
|
|
|
|
return TWDR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
Public Function: i2c_readNAck
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purpose: read non-acknowledge from TWI/I2C Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Parameter: none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return Value: uint8_t
|
|
|
|
|
|
|
|
- TWDR: recieved value at TWI/I2C-Interface
|
|
|
|
|
|
|
|
- 0: Error at read
|
|
|
|
|
|
|
|
**********************************************/
|
|
|
|
uint8_t i2c_readNAck(void){
|
|
|
|
uint8_t i2c_readNAck(void){
|
|
|
|
TWCR = (1<<TWINT)|(1<<TWEN);
|
|
|
|
TWCR = (1<<TWINT)|(1<<TWEN);
|
|
|
|
uint16_t timeout = F_CPU/F_I2C*2.0;
|
|
|
|
uint16_t timeout = F_CPU/F_I2C*2.0;
|
|
|
@ -100,7 +160,7 @@ uint8_t i2c_readNAck(void){
|
|
|
|
timeout--;
|
|
|
|
timeout--;
|
|
|
|
if(timeout == 0){
|
|
|
|
if(timeout == 0){
|
|
|
|
I2C_ErrorCode |= (1 << I2C_READNACK);
|
|
|
|
I2C_ErrorCode |= (1 << I2C_READNACK);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return TWDR;
|
|
|
|
return TWDR;
|
|
|
|