Hello,
I'm trying to work with I2C and I have the following code:

#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <compat/twi.h>
#define I2C_START
#define MAX_TRIES
#define I2C_DATA
#define I2C_STOP
#define i2c_writebyte

unsigned char i2c_transmit(unsigned char type) {

 while (!(TWCR & (1 << TWINT)));
 
  return (TWSR & 0xF8);
}
  {
  switch(type) {
     case I2C_START:   
       TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
       break;
     case I2C_DATA: 
       TWCR = (1 << TWINT) | (1 << TWEN);
       break;
     case I2C_STOP: 
       TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
       return 0;
  }

  }

 int i2c_writebyte (unsigned int i2c_address, unsigned int dev_id,
                  unsigned int dev_addr,char data) {
  unsigned char n = 0;
  unsigned char twi_status;
  char r_val = -1;
i2c_retry:
  if (n++ >= MAX_TRIES) return r_val;
  twi_status=i2c_transmit(I2C_START);

int i2c_readbyte(unsigned int i2c_address, unsigned int dev_id,
                 unsigned int dev_addr, char *data)
{
  unsigned char n = 0;
  unsigned char twi_status;
  char r_val = -1;
i2c_retry:
  if (n++ >= MAX_TRIES) return r_val;
    twi_status=i2c_transmit(I2C_START);

    if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
  if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;
    TWDR = (dev_id & 0xF0) | ((dev_addr << 1) & 0x0E) | TW_WRITE;
    twi_status=i2c_transmit(I2C_DATA);
    if ((twi_status == TW_MT_SLA_NACK) || (twi_status == TW_MT_ARB_LOST)) goto i2c_retry;
  if (twi_status != TW_MT_SLA_ACK) goto i2c_quit;
    TWDR = i2c_address;
    twi_status=i2c_transmit(I2C_DATA);
    if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;
    TWDR = i2c_address >> 8;
    twi_status=i2c_transmit(I2C_DATA);
  
    if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;  

  twi_status=i2c_transmit(I2C_START);
  if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
  if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;
  TWDR = (dev_id & 0xF0) | ((dev_addr << 1) & 0x0E) | TW_READ;
  twi_status=i2c_transmit(I2C_DATA); 


  if ((twi_status == TW_MR_SLA_NACK) || (twi_status == TW_MR_ARB_LOST)) goto i2c_retry;
  if (twi_status != TW_MR_SLA_ACK) goto i2c_quit;
    twi_status=i2c_transmit(I2C_DATA);
  if (twi_status != TW_MR_DATA_NACK) goto i2c_quit;
    *data=TWDR;
  r_val=1;
i2c_quit:
    twi_status=i2c_transmit(I2C_STOP);
  return r_val;

}
int main(void)
{
   char buffer[34]= {0b00001111,0b11110000,
                     0b00000001,
    		     0b00000011,
		     0b00000110,
	  	     0b00001100,
		     0b00011001,
		     0b00110011,
		     0b01100110,
		     0b11001100,
		     0b10011000,
		     0b00110000,
		     0b01100000,
		     0b11000000,
		     0b10000000,
		     0b00000000,
		     0b00000000,
		     0b00000000,
		     0b10000000,
		     0b11000000,
		     0b01100000,
		     0b00110000,
		     0b10011000,
		     0b11001100,
		     0b01100110,
 	             0b00110011,
		     0b00011001,
		     0b00001100,
		     0b00000110,
		     0b00000011,
		     0b00000001,
		     0b00000000,
		     0b00000000,
		     0b00000000,
		     };			
 char data,id1,id2;
  unsigned int dev_address,i,idelay;

  DDRD=0xFF;                   
  PORTD=0x00;                  
    ADMUX=0x00;	             
  
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1);
    ADCSRB = 0x00;
  
  DIDR0 = 0x01;
    TWSR = 0x00;  
    TWBR = 0x30;  

    dev_address=0;              
  i2c_readbyte(dev_address,EEPROM_ID,EEPROM_ADDR,&id1);
  i2c_readbyte(dev_address + 1,EEPROM_ID,EEPROM_ADDR,&id2);
    if (id1 != buffer[0] || id2 != buffer[1]) {
    for(i=0;i < 34;i++) {
       i2c_writebyte(dev_address + i,EEPROM_ID,EEPROM_ADDR,buffer[i]);
      _delay_us(1);
    }
  }   

idelay=100;  

  for(;;) {
    for(i=2;i < 34;i++) {
     
      ADCSRA |= (1<<ADSC);
     
      while (ADCSRA & (1<<ADSC));
      idelay = ADCW;
      i2c_readbyte(dev_address + i,EEPROM_ID,EEPROM_ADDR,&data);
      PORTD=data;
      _delay_ms(idelay);  
    }
  }

  return 0;
}

I get 2 errors and 1 warning.
The errors are:
../1121.c:32: error: expected identifier or '(' before 'unsigned'
../1121.c:17: error: expected identifier or '(' before '{' token


how can I fix them?
best regards,
Floris

Dani AI

Generated

are both on the right track. You have two separate issues:

  1. A macro is clobbering a function name. The line #define i2c_writebyte creates an empty macro, so the compiler sees int (unsigned int ... and throws expected identifier or '(' before 'unsigned'. Delete that #define (or #undef i2c_writebyte before the function).

  2. The switch(type) block belongs inside i2c_transmit, but an early } ends the function first. That stray block at file scope is why you get expected identifier or '(' before '{' token. Do not just remove the {; move the whole switch into the function body and fix the brace layout.

Also, your blank-like macros expand to nothing. Give them values, or you will later hit "expected expression" errors:

  • #define I2C_START 0
  • #define I2C_DATA 1
  • #define I2C_STOP 2
  • #define MAX_TRIES 5

A minimal structure that will compile (showing brace placement, not full logic):

#define I2C_START 0
#define I2C_DATA  1
#define I2C_STOP  2
#define MAX_TRIES 5

unsigned char i2c_transmit(unsigned char type) {
    switch (type) {
        case I2C_START: /* set TWCR for START */ break;
        case I2C_DATA:  /* set TWCR for DATA  */ break;
        case I2C_STOP:  /* set TWCR for STOP  */ return 0;
    }
    /* wait for TWINT, then return masked TWSR */
}

int i2c_writebyte(unsigned int addr, unsigned int dev_id,
                  unsigned int dev_addr, char data) {
    /* ... */
}  /* close this function before starting the next one */

int i2c_readbyte(unsigned int addr, unsigned int dev_id,
                 unsigned int dev_addr, char *data) {
    /* ... */
}

Tip: compile with -Wall -Wextra and, if stuck, inspect the preprocessed output (avr-gcc -E file.c) to spot accidental macro substitutions early.

Recommended Answers

All 4 Replies

It looks like you have some code that's sitting outside of functions.

For example, the switch statement that's between lines 17-30 is not contained within a function. You can't have code just sitting around like that.

You've also got a function inside of another function... i2c_readbyte is inside of i2c_writebyte.

I'm guessing you just need to reorder your {}-braces.

hello,
the error in line 32 is fixed but I can't get the error in line 17 away, how can I do that?
best regards,
floris

I'm not sure how what to do for line 17 since I'm not sure where the switch statement is supposed to go. It looks like you would put it either line 12 or line 14.

What did you do to fix the error on line 32?

I removed the {-token in line 17.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.