I've copied the entire Wire folder into "WinAVR-20071221\avr\include\avr", and I put #include <avr/Wire/wire.h> in my program, during compile, this is the error:
c:/winavr-20071221/bin/../avr/include/avr/Wire/wire.h:27: error: expected '=', ',', ';', 'asm' or 'attribute' before 'TwoWire'
c:/winavr-20071221/bin/../avr/include/avr/Wire/wire.h:64: error: expected '=', ',', ';', 'asm' or 'attribute' before 'Wire'
In file included from ../all_header.h:37,
from ../ACTOR_robot.c:1:
../i2c.h: In function 'accRead':
../i2c.h:21: error: 'Wire' undeclared (first use in this function)
../i2c.h:21: error: (Each undeclared identifier is reported only once
../i2c.h:21: error: for each function it appears in.)
make: *** [ACTOR_robot.o] Error 1
Build failed with 5 errors and 0 warnings...
How can I use this library with WinAVR and AVR Studio?
Also, I gave up on including the library, I found example code on the AVRlib website for TWI, I think I have the basic operation down except checking for status.
void i2cInit()
{
cbi(DDRD, 0);
cbi(DDRD, 1); // input state for now
cbi(PORTD, 0);
cbi(PORTD, 1); // disables pull up
// pull up resistors are external
TWBR = 1; // fast i2c
TWSR = 0b00000011; // prescale 64, fast i2c
TWCR = 0b01000101; // i2c on, interrupt enable, ack enable
TWAR = 0b00111010; // slave address for accelerometer
// apparently this is only used for slave mode, but keep it here
}
void i2cStop()
{
while ((TWCR & _BV(TWINT)) == 0);
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); // stop
}
void i2cStart()
{
while((TWCR & _BV(TWINT)) == 0); // wait
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); // start
}
void i2cSLA_R()
{
TWDR = 0b00111011;
while ((TWCR & _BV(TWINT)) == 0);
TWCR = _BV(TWINT) | _BV(TWEN); // clear int to start transmission
}
void i2cSLA_W()
{
TWDR = 0b00111010;
while ((TWCR & _BV(TWINT)) == 0);
TWCR = _BV(TWINT) | _BV(TWEN); // clear int to start transmission
}
void i2cWrite(uint8_t data)
{
TWDR = data;
while ((TWCR & _BV(TWINT)) == 0);
TWCR = _BV(TWINT) | _BV(TWEN); // clear int to start transmission
}
void i2cReadStart()
{
while ((TWCR & _BV(TWINT)) == 0);
TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWEA); // clear int to start transmission
}
uint8_t i2cRead()
{
while ((TWCR & _BV(TWINT)) == 0);
return TWDR;
}
With only one device on the bus, I think checking for status isn't really needed. (I haven't encountered any malfunctioning devices yet, fingers crossed)
Anything wrong with the code? I have all the waiting loops before the instruction so other functions can be executed during that time if needed (I have some floating point calculations that I plan to insert in a non-linear fashion)
The Arduino IDE compiles C++ and because that is a superset of C it will handle C code also, indeed most of the example sketches stick to C constructs. The Arduino board itself only sees the output of the compiler so doesn't know or care about C or C++.