Skip to content

Commit

Permalink
clean up on docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatth11 committed Nov 19, 2024
1 parent 3fce63f commit fec34ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# AVR I2C Driver Example

This project is to have a simple example of a primary I2C communication on an ATtiny85 with documentation.
This project is to have a simple example of a controller I2C communication on an ATtiny85 with documentation.

Supports writing and reading from secondary devices.
Supports writing and reading from target devices.

## Project Structure

Expand All @@ -16,11 +16,11 @@ Supports writing and reading from secondary devices.
## Demo

The demo has an ATtiny85 loaded with the code in this project hooked up to an arduino uno using the Wire library to
act as a secondary device to talk to.
act as a target device to talk to.

Actions performed:
- The primary (ATtiny85) sends "Hello, World!" to the secondary (Arduino Uno) device.
- The primary requests a read from the secondary to toggle the LED.
- The controller (ATtiny85) sends "Hello, World!" to the target (Arduino Uno) device.
- The controller requests a read from the target to toggle the LED.

https://github.com/user-attachments/assets/6c168a57-7b3c-4304-adfc-d2d9f486735c

Expand Down
13 changes: 7 additions & 6 deletions src/driver/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <util/delay.h>

// data sheet says between 50 and 300 nanoseconds
// this is 0.2 microseconds which is 200 nanoseconds
#define WAIT 0.2

#define STATUS_CLOCK_8_BITS (_BV(USISIF)|_BV(USIOIF)|_BV(USIPF)|_BV(USIDC) | \
Expand Down Expand Up @@ -156,23 +157,23 @@ unsigned char i2c_write_byte(unsigned char data) {
/**
* Read the next byte.
*
* @param[in] nack True for reading more, false otherwise.
* @param[in] ack True for reading more, false otherwise.
* @return The read byte.
*/
unsigned char i2c_read_byte(bool nack) {
unsigned char i2c_read_byte(bool ack) {
// change data pin to input
i2c_bus &= ~_BV(i2c_sda);
unsigned char data = transfer(STATUS_CLOCK_8_BITS);
// change back to output
i2c_bus |= _BV(i2c_sda);
if (nack) {
// HIGH means read another byte
if (ack) {
// LOW means read another byte
i2c_data = 0x00;
} else {
// LOW means stop sending
// HIGH means stop sending
i2c_data = 0xff;
}
// send nack
// send n/ack
transfer(STATUS_CLOCK_1_BIT);
return data;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ int main (void) {
i2c_stop();
_delay_ms(1000);

if(!i2c_start()) break;
if(!i2c_start()) error_loop();

i2c_write_address(TARGET_ADDR, false);

unsigned char byte = i2c_read_byte(false);
Expand Down

0 comments on commit fec34ef

Please sign in to comment.