Difference between revisions of "I2C"
(8 intermediate revisions by one other user not shown) | |||
Line 112: | Line 112: | ||
I2C_Write(C_Ds1307SecondRegAddress_U8); // Select the SEC RAM address | I2C_Write(C_Ds1307SecondRegAddress_U8); // Select the SEC RAM address | ||
− | I2C_Write(var_sec_u8); | + | I2C_Write(var_sec_u8); // Write sec from RAM address 00H |
− | I2C_Write(var_min_u8); | + | I2C_Write(var_min_u8); // Write min from RAM address 01H |
− | I2C_Write(var_hour_u8); | + | I2C_Write(var_hour_u8); // Write hour from RAM address 02H |
− | I2C_Stop(); | + | I2C_Stop(); // Stop I2C communication after Setting the Time |
} | } | ||
Line 122: | Line 122: | ||
void RTC_GetTime(uint8_t *ptr_hour_u8,uint8_t *ptr_min_u8,uint8_t *ptr_sec_u8) | void RTC_GetTime(uint8_t *ptr_hour_u8,uint8_t *ptr_min_u8,uint8_t *ptr_sec_u8) | ||
{ | { | ||
− | I2C_Start(); | + | I2C_Start(); // Start I2C communication |
− | I2C_Write(C_Ds1307WriteMode_U8); | + | I2C_Write(C_Ds1307WriteMode_U8); // connect to DS1307 by sending its ID on I2c Bus |
− | I2C_Write(C_Ds1307SecondRegAddress_U8); // Request Sec RAM address at 00H | + | I2C_Write(C_Ds1307SecondRegAddress_U8); // Request Sec RAM address at 00H |
I2C_Stop(); // Stop I2C communication after selecting Sec Register | I2C_Stop(); // Stop I2C communication after selecting Sec Register | ||
I2C_Start(); // Start I2C communication | I2C_Start(); // Start I2C communication | ||
− | I2C_Write(C_Ds1307ReadMode_U8); // connect to DS1307(Read mode) by sending its ID | + | I2C_Write(C_Ds1307ReadMode_U8); // connect to DS1307(Read mode) by sending its ID |
− | *ptr_sec_u8 = I2C_Read(1); | + | *ptr_sec_u8 = I2C_Read(1); // read second and return Positive ACK |
− | *ptr_min_u8 = I2C_Read(1); | + | *ptr_min_u8 = I2C_Read(1); // read minute and return Positive ACK |
− | *ptr_hour_u8 = I2C_Read(0); | + | *ptr_hour_u8 = I2C_Read(0); // read hour and return Negative/No ACK |
− | I2C_Stop(); | + | I2C_Stop(); // Stop I2C communication after reading the Time |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 10:48, 10 January 2015
I2C
I2C_Init
Defination | void I2C_Init() |
Input Arguments | none |
Return Value | none |
Description | This function is used to initialize the I2C module. |
Usage | I2C_Init(); |
I2C_Start
Defination | void I2C_Start() |
Input Arguments | none |
Return Value | none |
Description | This function is used to generate I2C Start Condition. Start Condition: SDA goes low when SCL is High. |
Usage | I2C_Start(); |
I2C_Stop
Defination | void I2C_Stop() |
Input Arguments | none |
Return Value | none |
Description | This function is used to generate I2C Stop Condition. Stop Condition: SDA goes High when SCL is High. |
Usage | I2C_Stop(); |
I2C_Write
Defination | void I2C_Write(uint8_t var_i2cData_u8) |
Input Arguments | uint8_t : 8-bit data to be transmitted. |
Return Value | none |
Description | This function is used to send a byte on SDA line using I2C protocol. 8bit data is sent bit-by-bit on each clock cycle. MSB(bit) is sent first and LSB(bit) is sent at last. |
Usage | I2C_Write(0x05); //transmits 0x05 on I2C lines I2C_Write(var_i2cData_u8); //Transmits 8bit data stored in var_i2cData_u8 |
I2C_Read
Defination | uint8_t I2C_Read(uint8_t var_ackOption_u8) |
Input Arguments | uint8_t: Acknowledgement to be sent after data reception.
|
Return Value | uint8_t: Data received form I2C lines. |
Description | This fun is used to receive a byte on SDA line using I2C protocol. |
Usage | void mian() { *ptr_sec_u8 = I2C_Read(1); // read second and return Positive ACK *ptr_min_u8 = I2C_Read(1); // read minute and return Positive ACK *ptr_hour_u8 = I2C_Read(0); // read hour and return Negative/No ACK } |
User_Guide
/*Below examples demonstrate the usage of I2C library to read and write the RTC(DS1307) time. */ void RTC_SetTime(uint8_t var_hour_u8, uint8_t var_min_u8, uint8_t var_sec_u8) { I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307WriteMode_U8); // connect to DS1307 by sending its ID on I2c Bus I2C_Write(C_Ds1307SecondRegAddress_U8); // Select the SEC RAM address I2C_Write(var_sec_u8); // Write sec from RAM address 00H I2C_Write(var_min_u8); // Write min from RAM address 01H I2C_Write(var_hour_u8); // Write hour from RAM address 02H I2C_Stop(); // Stop I2C communication after Setting the Time } void RTC_GetTime(uint8_t *ptr_hour_u8,uint8_t *ptr_min_u8,uint8_t *ptr_sec_u8) { I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307WriteMode_U8); // connect to DS1307 by sending its ID on I2c Bus I2C_Write(C_Ds1307SecondRegAddress_U8); // Request Sec RAM address at 00H I2C_Stop(); // Stop I2C communication after selecting Sec Register I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307ReadMode_U8); // connect to DS1307(Read mode) by sending its ID *ptr_sec_u8 = I2C_Read(1); // read second and return Positive ACK *ptr_min_u8 = I2C_Read(1); // read minute and return Positive ACK *ptr_hour_u8 = I2C_Read(0); // read hour and return Negative/No ACK I2C_Stop(); // Stop I2C communication after reading the Time }