Update i2c.c

add comments for using functions
pull/7/head
Sylaina 6 years ago committed by GitHub
parent ecc3c11a5a
commit 727bf2b507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

64
i2c.c

@ -16,7 +16,15 @@
#endif
uint8_t I2C_ErrorCode;
/**********************************************
Public Function: i2c_init
Purpose: Initialise TWI/I2C interface
Input Parameter: none
Return Value: none
**********************************************/
void i2c_init(void){
// set clock
switch (PSC_I2C) {
@ -37,6 +45,16 @@ void i2c_init(void){
// enable
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){
// i2c start
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){
// i2c stop
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){
TWDR = byte;
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){
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
uint16_t timeout = F_CPU/F_I2C*2.0;
@ -92,6 +140,18 @@ uint8_t i2c_readAck(void){
};
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){
TWCR = (1<<TWINT)|(1<<TWEN);
uint16_t timeout = F_CPU/F_I2C*2.0;
@ -100,7 +160,7 @@ uint8_t i2c_readNAck(void){
timeout--;
if(timeout == 0){
I2C_ErrorCode |= (1 << I2C_READNACK);
return 0;
return 0;
}
};
return TWDR;

Loading…
Cancel
Save