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

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.