TinyI2C.cpp
Kód: Vybrat vše
// TinyI2C.cpp - Minimal Software I2C Library for ATtiny85
#include "TinyI2C.h"
#include <avr/io.h>
#include <util/delay.h>
#define I2C_SCL PB2
#define I2C_SDA PB0
#define I2C_DDR DDRB
#define I2C_PORT PORTB
#define I2C_PIN PINB
void TinyI2C::begin() {
I2C_DDR &= ~((1 << I2C_SCL) | (1 << I2C_SDA)); // Set SCL and SDA as inputs
I2C_PORT |= (1 << I2C_SCL) | (1 << I2C_SDA); // Enable pull-ups
}
void TinyI2C::beginTransmission(uint8_t address) {
_address = (address << 1);
i2c_start();
i2c_write(_address);
}
uint8_t TinyI2C::endTransmission() {
i2c_stop();
return 0; // Success
}
uint8_t TinyI2C::requestFrom(uint8_t address, uint8_t quantity) {
_address = (address << 1) | 1;
_bufferIndex = 0;
_bufferLength = quantity;
i2c_start();
i2c_write(_address);
for (uint8_t i = 0; i < quantity; i++) {
_buffer[i] = i2c_read(i == quantity - 1);
}
i2c_stop();
return quantity;
}
void TinyI2C::write(uint8_t data) {
i2c_write(data);
}
uint8_t TinyI2C::read() {
if (_bufferIndex < _bufferLength) {
return _buffer[_bufferIndex++];
}
return 0;
}
void TinyI2C::i2c_start() {
I2C_DDR |= (1 << I2C_SDA); // Set SDA as output
_delay_us(4);
I2C_DDR |= (1 << I2C_SCL); // Set SCL as output
_delay_us(4);
}
void TinyI2C::i2c_stop() {
I2C_DDR |= (1 << I2C_SDA); // Set SDA as output
_delay_us(4);
I2C_DDR &= ~(1 << I2C_SCL); // Release SCL
_delay_us(4);
I2C_DDR &= ~(1 << I2C_SDA); // Release SDA
_delay_us(4);
}
void TinyI2C::i2c_write(uint8_t data) {
for (uint8_t i = 0; i < 8; i++) {
if (data & 0x80) I2C_DDR &= ~(1 << I2C_SDA); // Release SDA for high
else I2C_DDR |= (1 << I2C_SDA); // Pull SDA low
_delay_us(2);
I2C_DDR &= ~(1 << I2C_SCL); // Release SCL
_delay_us(4);
I2C_DDR |= (1 << I2C_SCL); // Pull SCL low
data <<= 1;
}
I2C_DDR &= ~(1 << I2C_SDA); // Release SDA for ACK
_delay_us(2);
I2C_DDR &= ~(1 << I2C_SCL);
_delay_us(4);
I2C_DDR |= (1 << I2C_SCL);
}
uint8_t TinyI2C::i2c_read(uint8_t ack) {
uint8_t data = 0;
for (uint8_t i = 0; i < 8; i++) {
data <<= 1;
I2C_DDR &= ~(1 << I2C_SCL);
_delay_us(4);
if (I2C_PIN & (1 << I2C_SDA)) data |= 1;
I2C_DDR |= (1 << I2C_SCL);
_delay_us(4);
}
if (ack) I2C_DDR |= (1 << I2C_SDA);
else I2C_DDR &= ~(1 << I2C_SDA);
_delay_us(2);
I2C_DDR &= ~(1 << I2C_SCL);
_delay_us(4);
I2C_DDR |= (1 << I2C_SCL);
return data;
}
Kód: Vybrat vše
// TinyI2C.h - Header file for the minimal Software I2C Library for ATtiny85
#ifndef TINYI2C_H
#define TINYI2C_H
#include <inttypes.h>
class TinyI2C {
public:
void begin(); // Initializes the I2C bus
void beginTransmission(uint8_t address); // Starts an I2C transmission to a device
uint8_t endTransmission(); // Ends the I2C transmission
uint8_t requestFrom(uint8_t address, uint8_t quantity); // Requests data from an I2C device
void write(uint8_t data); // Writes a byte of data to the I2C bus
uint8_t read(); // Reads a byte of data from the I2C bus
private:
void i2c_start(); // Sends a start condition on the I2C bus
void i2c_stop(); // Sends a stop condition on the I2C bus
void i2c_write(uint8_t data); // Writes a byte to the I2C bus
uint8_t i2c_read(uint8_t ack); // Reads a byte from the I2C bus
uint8_t _address; // I2C device address
uint8_t _buffer[32]; // Buffer to store read data
uint8_t _bufferIndex; // Index for buffer
uint8_t _bufferLength; // Length of the buffer
};
#endif
Kód: Vybrat vše
##################################################################
# Syntax Coloring Map
##################################################################
# Datatypes (KEYWORD1)
TinyI2C KEYWORD1
# Methods (KEYWORD2)
TinyI2C KEYWORD2
begin KEYWORD2
beginTransmission KEYWORD2
endTransmission KEYWORD2
requestFrom KEYWORD2
write KEYWORD2
read KEYWORD2