You are on page 1of 10

1.

unsigned int Port, Dir;


2. #byte Port = getenv("SFR:PORTC")
3. #bit SDA = Port.4
4. #bit SCL = Port.3
5. #byte Dir = getenv("SFR:TRISC")
6. #bit DirSDA = Dir.4
7. #bit DirSCL = Dir.3
8.
9.
10. #define RTC_SLAVE_ADDRESS 0xD0
11. #define RTC_READ_COMMAND

RTC_SLAVE_ADDRESS | 0x01

12. #define RTC_WRITE_COMMAND RTC_SLAVE_ADDRESS


13.
14.
15. struct Time
16. {
17.

char second;

18.

char minute;

19.

char hour;

20. };
21. struct DateTime
22. {
23.

char second;

24.

char minute;

25.

char hour;

26.

char day;

27.

char date;

28.

char month;

29.

char year;

30. };
31.
32.
33. void i2c_release(void)
34. {
35.

DirSDA = 1;

36.

DirSCL = 1;

37. }
38. void i2c_start(void)
39. {
40.

DirSCL = 0;

41.

SCL = 1;

42.

SCL = 1;

43.

DirSDA = 0;

44.

SDA = 0;

45.

SDA = 0;

46.

SCL = 0;

47.

SCL = 0;

48. }
49. void i2c_stop(void)
50. {

51.
52.

DirSDA = 0;

53.

SDA = 0;

54.

SDA = 0;

55.

DirSCL = 0;

56.

SCL = 1;

57.

SCL = 1;

58.

SDA = 1;

59.

SDA = 1;

60. }
61. unsigned int i2c_read(int1 ack)
62. {
63.

int i, result;

64.
65.

result = 0;

66.

DirSDA = 1; //select input direction

67.

SCL = 0;

68.

SCL = 0;

69.

for(i = 0; i < 8; i++)

70.

71.

SCL = 1;

72.

SCL = 1;

73.

result <<= 1;

74.

if(SDA == 1) result++;

75.

SCL = 0;

76.

SCL = 0;

77.

78.

DirSDA = 0; //select output direction

79.

SDA = (ack == 1)? 0:1;

80.

SCL = 1;

81.

SCL = 1;

82.

SCL = 0;

83.

SCL = 0;

84.

return result;

85. }
86. int1 i2c_write(unsigned int value)
87. {
88.

unsigned int i, temp;

89.

int1 ack;

90.
91.

temp = value;

92.

DirSDA = 0;

93.

SCL = 0;

94.

SCL = 0;

95.

for(i = 0; i < 8; i++)

96.

97.

SDA = (temp & 0x80)? 1:0;

98.

SCL = 1;

99.

SCL = 1;

100.

SCL = 0;

101.

SCL = 0;

102.

temp <<= 1;

103.

104.

DirSDA = 1; // read ACK

105.

SCL = 1;

106.

SCL = 1;

107.

ack = SDA;

108.

SCL = 0;

109.

SCL = 0;

110.

return ack;

111.

112.
113.
114.

void DS1307_init()

115.

116.

char x;

117.
118.

i2c_release();

119.

delay_us(10);

120.

i2c_start();

121.

i2c_write(RTC_WRITE_COMMAND); //Write slave address

122.

i2c_write(0x00);

123.

i2c_start();

124.

i2c_write(RTC_READ_COMMAND);

125.

x = i2c_read(0); // Read second with NACK

126.

i2c_stop();

127.
128.

i2c_start();

129.

i2c_write(RTC_WRITE_COMMAND);

130.

i2c_write(0x00); // Go to Second Reggister

131.

i2c_write(x & 0x7F); //

132.

i2c_stop();

133.
134.

i2c_start();

135.

i2c_write(RTC_WRITE_COMMAND);

136.

i2c_write(0x07); // Go to Control Reggister

137.

i2c_write(0x00); // configure RTC

138.

i2c_stop();

139.

140.
141.
142.

struct Time DS1307_get_time()

143.

144.

struct Time t;

145.
146.

i2c_start();

147.

i2c_write(RTC_WRITE_COMMAND);

148.

i2c_write(0x00);

149.

i2c_start();

150.

i2c_write(RTC_READ_COMMAND);

// Go to top of memory

151.

t.second = i2c_read(1); // Read with ACK

152.

t.minute = i2c_read(1);

153.

t.hour = i2c_read(0);

154.

i2c_stop();

155.

return t;

156.

157.

struct DateTime DS1307_get_date_time()

158.

159.

struct DateTime t;

160.
161.

i2c_start();

162.

i2c_write(RTC_WRITE_COMMAND);

163.

i2c_write(0x00);

164.

i2c_start();

165.

i2c_write(RTC_READ_COMMAND);

166.

t.second = i2c_read(1); // Read with ACK

167.

t.minute = i2c_read(1);

168.

t.hour = i2c_read(1);

169.

t.day = i2c_read(1);

170.

t.date = i2c_read(1);

171.

t.month = i2c_read(1);

172.

t.year = i2c_read(0);

173.

i2c_stop();

174.

return t;

175.

// Go to top of memory

176.

void DS1307_set_time(struct Time t)

177.

178.

i2c_start();

179.

i2c_write(RTC_WRITE_COMMAND);

180.

i2c_write(0x00);

181.

i2c_write(t.second);

182.

i2c_write(t.minute);

183.

i2c_write(t.hour);

184.

i2c_stop();

185.

186.

void DS1307_set_date(struct Time t)

187.

188.

i2c_start();

189.

i2c_write(RTC_WRITE_COMMAND);

190.

i2c_write(0x04);

191.

i2c_write(t.second); // set Date

192.

i2c_write(t.minute); // set Month

193.

i2c_write(t.hour);

194.

i2c_stop();

// set Year

195.

196.

void DS1307_set_date_time(struct DateTime t)

197.

198.

i2c_start();

199.

i2c_write(RTC_WRITE_COMMAND);

200.

i2c_write(0x00);

201.

i2c_write(t.second);

202.

i2c_write(t.minute);

203.

i2c_write(t.hour);

204.

i2c_write(t.day);

205.

i2c_write(t.date);

206.

i2c_write(t.month);

207.

i2c_write(t.year);

208.

i2c_stop();

209.

210.

void DS1307_write(char value, char addr)

211.

212.

i2c_start();

213.

i2c_write(RTC_WRITE_COMMAND);

214.

i2c_write(addr);

215.

i2c_write(value);

216.

i2c_stop();

217.

218.

unsigned int DS1307_read(char addr)

219.

220.

unsigned int value;

221.
222.

i2c_start();

223.

i2c_write(RTC_WRITE_COMMAND);

224.

i2c_write(addr);

225.

i2c_start();

226.

i2c_write(RTC_READ_COMMAND);

227.

value = i2c_read(0);

228.

i2c_stop();

229.

return value;

230.

You might also like