/
i2c.c
59 lines (47 loc) · 1.02 KB
/
i2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*This file establishes i2c communication with 8051 Microcontroller using Bit banging*/
#include<reg51.h>
#include"header.h"
/*Function for I2C Start condition for Slave DS3231*/
/*This function alerts the RTC*/
void Start_I2C(void)
{
SCL_DISABLE;
SDA_ENABLE;
SCL_ENABLE;
SDA_DISABLE;
}
/*Function for I2C Stop condition*/
void Stop_I2C(void)
{
SCL_DISABLE;
SDA_DISABLE;
SCL_ENABLE;
SDA_ENABLE;
}
/*Function for Writing Address to Slave*/
void Slave_Write(unsigned char byte)
{
unsigned char j,k;
k = byte;
for (j= 0; j < 8; j++)
{
SCL_DISABLE;
SDA=(k&(0x80>>j))?1:0; /*Writing bit_by_bit*/
SCL_ENABLE;
}
}
// Function to generate No Acknowledgement signal to slave
void No_Acknowledgement(void)
{
SCL_DISABLE;
SDA_ENABLE;
SCL_ENABLE;
}
/*Function for reading the Acknowledgement from Slave*/
void Read_Ack(void)
{
SCL_DISABLE;
SDA_ENABLE;
SCL_ENABLE;
while(SDA);
}