user1232141234213 user1232141234213 - 8 months ago 37
C Question

c library creation for ATMEL328

I am trying to create a library in C for use in a ATMEL 328pu. I have made the source and header files in C but come unstuck when I try to compile the library. I think I need another AVR library containing the types:

  • TWDR

  • TWCR

Which are the i2c registers in the ATMEGA328. A shortened version of the error message can be seen below followed by a portion of the .cpp file where the error message refers too.

Error message:

Build: Debug in my_i2c (compiler: GNU GCC Compiler)
Code_blocks/my_i2c/my_i2c/my_i2c.cpp|39|error: use of undeclared identifier 'TWCR'|
Build failed: 19 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Extract from.cpp file:

#include "my_i2c.h"

/////////////////////WRITE BIT////////////////////
void my_i2c :: i2cWriteBit (uint8_t i2cAdd, uint8_t i2cReg, uint8_t i2cBit, bool i2cBool) {
uint8_t writeBuff;
writeBuff = i2cRead(i2cAdd, i2cReg); //read uint8_t
i2cBool == true ? writeBuff |= 1 << i2cBit : writeBuff &= ~(1 << i2cBit);
i2cWrite (i2cAdd, i2cReg, writeBuff);

/////////////////////WRITE uint8_t////////////////////
void my_i2c :: i2cWrite (uint8_t i2cAdd, uint8_t i2cReg, uint8_t i2cData) {
TWCR = 0b10100100; //(TWINT)(TWSTA)(TWEN) - Set START condition
while (!(TWCR & 0b10000000)) { //Wait for TWI to set TWINT

Do I need to define the what TWCR and TWDR are for the compiler to understand the functions? and how do I do this, is it like I was thinking by including another library?


You can't refer to an undeclared identifier, that makes it impossible for the compiler to figure out what you mean.

You should probably add

#include <avr/io.h>

to your library's source code.