@@ -44,16 +44,16 @@ static unsigned char twi_sda, twi_scl;
44
44
45
45
void twi_setClock (unsigned int freq ){
46
46
#if F_CPU == FCPU80
47
- if (freq <= 100000 ) twi_dcount = 18 ;//about 100KHz
47
+ if (freq <= 100000 ) twi_dcount = 19 ;//about 100KHz
48
48
else if (freq <= 200000 ) twi_dcount = 8 ;//about 200KHz
49
- else if (freq <= 300000 ) twi_dcount = 4 ;//about 300KHz
50
- else if (freq <= 400000 ) twi_dcount = 2 ;//about 370KHz
49
+ else if (freq <= 300000 ) twi_dcount = 3 ;//about 300KHz
50
+ else if (freq <= 400000 ) twi_dcount = 1 ;//about 370KHz
51
51
else twi_dcount = 1 ;//about 450KHz
52
52
#else
53
53
if (freq <= 100000 ) twi_dcount = 32 ;//about 100KHz
54
- else if (freq <= 200000 ) twi_dcount = 16 ;//about 200KHz
54
+ else if (freq <= 200000 ) twi_dcount = 14 ;//about 200KHz
55
55
else if (freq <= 300000 ) twi_dcount = 8 ;//about 300KHz
56
- else if (freq <= 400000 ) twi_dcount = 4 ;//about 370KHz
56
+ else if (freq <= 400000 ) twi_dcount = 5 ;//about 370KHz
57
57
else twi_dcount = 2 ;//about 450KHz
58
58
#endif
59
59
}
@@ -109,15 +109,15 @@ static bool twi_write_bit(bool bit) {
109
109
twi_delay (twi_dcount + 1 );
110
110
SCL_HIGH ();
111
111
while (SCL_READ () == 0 && (i ++ ) < TWI_CLOCK_STRETCH );// Clock stretching (up to 100us)
112
- twi_delay (twi_dcount + 1 );
112
+ twi_delay (twi_dcount );
113
113
return true;
114
114
}
115
115
116
116
static bool twi_read_bit (void ) {
117
117
unsigned int i = 0 ;
118
118
SCL_LOW ();
119
119
SDA_HIGH ();
120
- twi_delay (twi_dcount + 1 );
120
+ twi_delay (twi_dcount + 2 );
121
121
SCL_HIGH ();
122
122
while (SCL_READ () == 0 && (i ++ ) < TWI_CLOCK_STRETCH );// Clock stretching (up to 100us)
123
123
bool bit = SDA_READ ();
0 commit comments