From fec34efaa7ab5bc5d3d8bc8bdb73675959d5f526 Mon Sep 17 00:00:00 2001 From: Joshua Matthews Date: Tue, 19 Nov 2024 13:09:03 -0600 Subject: [PATCH] clean up on docs --- README.md | 10 +++++----- src/driver/i2c.c | 13 +++++++------ src/main.c | 3 ++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b4359aa..0b01a47 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/driver/i2c.c b/src/driver/i2c.c index 66acc3e..76e082c 100644 --- a/src/driver/i2c.c +++ b/src/driver/i2c.c @@ -2,6 +2,7 @@ #include // 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) | \ @@ -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; } diff --git a/src/main.c b/src/main.c index 6cd8f26..58eeb8f 100644 --- a/src/main.c +++ b/src/main.c @@ -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);