ASURO Library  2.80
i2c.c
gehe zur Dokumentation dieser Datei
1 /****************************************************************************/
18 /*****************************************************************************
19 * *
20 * This program is free software; you can redistribute it and/or modify *
21 * it under the terms of the GNU General Public License as published by *
22 * the Free Software Foundation; either version 2 of the License, or *
23 * any later version. *
24 * *
25 *****************************************************************************/
26 #include "asuro.h"
27 #include "i2c.h"
28 
29 
30 
31 /****************************************************************************/
42 void InitI2C (void)
43 {
44  SDA_DDR |= (1 << SDA);
45  SCL_DDR |= (1 << SCL);
46  SDA_HI;
47  SCL_HI;
48 }
49 
50 
51 
52 /****************************************************************************/
66 unsigned char WriteI2C (unsigned char byte)
67 {
68  unsigned char i;
69 
70  for (i=8; i>0; i--)
71  {
72  if ( byte & (1<<(i-1)) )
73  SDA_HI;
74  else
75  SDA_LO;
76  SCL_TOGGLE;
77  }
78  SDA_HI;
79 
80  SDA_DDR &= ~(1 << SDA);
81  HDEL;
82  SCL_HI;
83  byte = SDA_PIN & (1 << SDA);
84 
85  HDEL;
86  SCL_LO;
87  SDA_DDR |= (1 << SDA);
88  HDEL;
89 
90  return (byte == 0);
91 }
92 
93 
94 
95 /****************************************************************************/
109 unsigned char ReadI2C(unsigned char ack)
110 {
111  unsigned char i, byte = 0;
112 
113  SDA_HI;
114  SDA_DDR &= ~(1 << SDA);
115 
116  for (i=0; i<8; i++)
117  {
118  HDEL;
119  SCL_HI;
120  byte <<= 1;
121  byte |= (SDA_PIN & (1 << SDA)) >> SDA;
122  HDEL;
123  SCL_LO;
124  }
125 
126  SDA_DDR |= (1 << SDA);
127 
128  if (ack)
129  SDA_LO; // ack
130  else
131  SDA_HI; // nak
132 
133  SCL_TOGGLE;
134  SDA_HI;
135 
136  return byte;
137 }
138 
139 
140 
141 /****************************************************************************/
153 unsigned char StartI2C (unsigned char device)
154 {
155  I2C_START;
156  return WriteI2C (device);
157 }
158 
159 
160 
161 /****************************************************************************/
168 void StopI2C (void)
169 {
170  SDA_LO;
171  I2C_STOP;
172 }