diff --git a/README.md b/README.md index cbf740e..9aa58ec 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +# STOP - IF USING MY CORE, DO NOT USE THIS LIBRARY # +## MY CORE NOW HAS UNIVERSAL Wire.h LIBRARY. Just use Wire.h and unmodified I2C device libraries ## + TinyWireM ========= diff --git a/TinyWireM.cpp b/TinyWireM.cpp index d65a5ed..5ed5a23 100644 --- a/TinyWireM.cpp +++ b/TinyWireM.cpp @@ -57,7 +57,7 @@ size_t USI_TWI::write(uint8_t data){ // buffers up data to send } uint8_t USI_TWI::endTransmission() { - endTransmission(1); + return endTransmission(1); } uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer diff --git a/TinyWireM.h b/TinyWireM.h index f1852c1..effd71f 100644 --- a/TinyWireM.h +++ b/TinyWireM.h @@ -41,7 +41,7 @@ #include "Arduino.h" #define USI_SEND 0 // indicates sending to TWI #define USI_RCVE 1 // indicates receiving from TWI -#define USI_BUF_SIZE 18 // bytes in message buffer +#define USI_BUF_SIZE 19 // bytes in message buffer //class USI_TWI : public Stream class USI_TWI diff --git a/USI_TWI_Master.cpp b/USI_TWI_Master.cpp index a12f223..03802be 100644 --- a/USI_TWI_Master.cpp +++ b/USI_TWI_Master.cpp @@ -46,9 +46,9 @@ union USI_TWI_state void USI_TWI_Master_Initialise( void ) { PORT_USI |= (1<<PIN_USI_SDA); // Enable pullup on SDA, to set high as released state. - PORT_USI |= (1<<PIN_USI_SCL); // Enable pullup on SCL, to set high as released state. + PORT_USI_CL |= (1<<PIN_USI_SCL); // Enable pullup on SCL, to set high as released state. - DDR_USI |= (1<<PIN_USI_SCL); // Enable SCL as output. + DDR_USI_CL |= (1<<PIN_USI_SCL); // Enable SCL as output. DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output. USIDR = 0xFF; // Preload dataregister with "released level" data. @@ -193,7 +193,7 @@ unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned if (USI_TWI_state.addressMode || USI_TWI_state.masterWriteDataMode) { /* Write a byte */ - PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW. + PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW. USIDR = *(msg++); // Setup data. USI_TWI_Master_Transfer( tempUSISR_8bit ); // Send 8 bits on bus. @@ -270,7 +270,7 @@ unsigned char USI_TWI_Master_Transfer( unsigned char temp ) { _delay_us(T2_TWI); USICR = temp; // Generate positve SCL edge. - while( !(PIN_USI & (1<<PIN_USI_SCL)) );// Wait for SCL to go high. + while( !(PIN_USI_CL & (1<<PIN_USI_SCL)) );// Wait for SCL to go high. _delay_us(T4_TWI); USICR = temp; // Generate negative SCL edge. }while( !(USISR & (1<<USIOIF)) ); // Check for transfer complete. @@ -288,14 +288,14 @@ unsigned char USI_TWI_Master_Transfer( unsigned char temp ) unsigned char USI_TWI_Master_Start( void ) { /* Release SCL to ensure that (repeated) Start can be performed */ - PORT_USI |= (1<<PIN_USI_SCL); // Release SCL. - while( !(PORT_USI & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high. + PORT_USI_CL |= (1<<PIN_USI_SCL); // Release SCL. + while( !(PORT_USI_CL & (1<<PIN_USI_SCL)) ); // Verify that SCL becomes high. _delay_us(T2_TWI); /* Generate Start Condition */ PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW. _delay_us(T4_TWI); - PORT_USI &= ~(1<<PIN_USI_SCL); // Pull SCL LOW. + PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW. PORT_USI |= (1<<PIN_USI_SDA); // Release SDA. #ifdef SIGNAL_VERIFY @@ -314,8 +314,8 @@ unsigned char USI_TWI_Master_Start( void ) unsigned char USI_TWI_Master_Stop( void ) { PORT_USI &= ~(1<<PIN_USI_SDA); // Pull SDA low. - PORT_USI |= (1<<PIN_USI_SCL); // Release SCL. - while( !(PIN_USI & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high. + PORT_USI_CL |= (1<<PIN_USI_SCL); // Release SCL. + while( !(PIN_USI_CL & (1<<PIN_USI_SCL)) ); // Wait for SCL to go high. _delay_us(T4_TWI); PORT_USI |= (1<<PIN_USI_SDA); // Release SDA. _delay_us(T2_TWI); diff --git a/USI_TWI_Master.h b/USI_TWI_Master.h index 53c12ad..8ff6366 100644 --- a/USI_TWI_Master.h +++ b/USI_TWI_Master.h @@ -89,6 +89,31 @@ # define PIN_USI_SCL PINA4 #endif +#if defined(__AVR_ATtiny167__) | defined(__AVR_ATtiny87__) + # define DDR_USI DDRB + # define PORT_USI PORTB + # define PIN_USI PINB + # define PORT_USI_SDA PORTB0 + # define PORT_USI_SCL PORTB2 + # define PIN_USI_SDA PINB0 + # define PIN_USI_SCL PINB2 + + +#endif + +#if defined(__AVR_ATtiny1634__) + #define DDR_USI DDRB + #define PORT_USI PORTB + #define PIN_USI PINB + #define DDR_USI_CL DDRC + #define PORT_USI_CL PORTC + #define PIN_USI_CL PINC + #define PORT_USI_SDA PORTB1 + #define PORT_USI_SCL PORTC1 + #define PIN_USI_SDA PINB1 + #define PIN_USI_SCL PINC1 +#endif + #if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__) #define DDR_USI DDRB #define PORT_USI PORTB @@ -115,6 +140,12 @@ #define TRUE 1 #define FALSE 0 +#ifndef DDR_USI_CL + #define DDR_USI_CL DDR_USI + #define PORT_USI_CL PORT_USI +# define PIN_USI_CL PIN_USI +#endif + //********** Prototypes **********// void USI_TWI_Master_Initialise( void ); diff --git a/examples/AT24_EEPROM/AT24_EEPROM.ino b/examples/AT24_EEPROM/AT24_EEPROM.ino new file mode 100644 index 0000000..c1993cc --- /dev/null +++ b/examples/AT24_EEPROM/AT24_EEPROM.ino @@ -0,0 +1,36 @@ +#include <TinyWireM.h> +//Demo of reading AT24 EEPROM, and logging the results to serial. Reads the first 256 bytes. +//#define DoWrite //uncomment to write consecutive integers to first 256 bytes while sketch runs +//comment out Serial statements for boards without hardware serial. + + +byte i=0; + + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); //comment out if you're using a chip w/out serial. + TinyWireM.begin(); // initialize I2C lib + Serial.println("Tinywire begun"); //comment out if you're using a chip w/out serial. +} + +void loop() { + // put your main code here, to run repeatedly: + +#ifdef DoWrite + TinyWireM.beginTransmission(0x50); + TinyWireM.send(0x00); + TinyWireM.send(i); + TinyWireM.send(i); + TinyWireM.endTransmission(); // Send to the slave + delay(500); +#endif + TinyWireM.beginTransmission(0x50); + TinyWireM.send(0x00); + TinyWireM.send(i); + TinyWireM.endTransmission(); + TinyWireM.requestFrom(0x50,1); + Serial.println(TinyWireM.receive()); //comment out if you're using a chip w/out serial. + delay(500); + i++; +}