Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b2f6fc

Browse files
PaoloP74sandeepmistry
authored andcommitted
Fix Wire register
Move TWBR register from Wire.cpp to twi.c file.
1 parent aa710ab commit 7b2f6fc

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

‎libraries/Wire/src/Wire.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ void TwoWire::end(void)
8080
twi_disable();
8181
}
8282

83-
void TwoWire::setClock(uint32_t frequency)
83+
void TwoWire::setClock(uint32_t clock)
8484
{
85-
TWBR = ((F_CPU / frequency) - 16) / 2;
85+
twi_setFrequency(clock);
8686
}
8787

8888
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)

‎libraries/Wire/src/utility/twi.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ void twi_setAddress(uint8_t address)
118118
TWAR = address << 1;
119119
}
120120

121+
/*
122+
* Function twi_setClock
123+
* Desc sets twi bit rate
124+
* Input Clock Frequency
125+
* Output none
126+
*/
127+
void twi_setFrequency(uint32_t frequency)
128+
{
129+
TWBR = ((F_CPU / frequency) - 16) / 2;
130+
131+
/* twi bit rate formula from atmega128 manual pg 204
132+
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
133+
note: TWBR should be 10 or higher for master mode
134+
It is 72 for a 16mhz Wiring board with 100kHz TWI */
135+
}
136+
121137
/*
122138
* Function twi_readFrom
123139
* Desc attempts to become twi bus master and read a

‎libraries/Wire/src/utility/twi.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
void twi_init(void);
4242
void twi_disable(void);
4343
void twi_setAddress(uint8_t);
44+
void twi_setFrequency(uint32_t);
4445
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
4546
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t);
4647
uint8_t twi_transmit(const uint8_t*, uint8_t);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /