Ik zou graag de two-wire interface(TWI) van de Atmel AVR serie willen gebruiken om deze met elkaar te laten communiceren. In de datasheet vond ik min of meer het volgende


12      byte START, DATA;
13 
12      void comm(START)
13      {
14	  TWSR=0;
15	  TWBR = (SYSCLK / 100000UL - 16) / 2;
16	  TWAR = 1
17	  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//Send START condition
18	  while (!(TWCR & (1<<TWINT))){}				//Wait for TWINT Flag set. This indicates that the START condition has been transmitted
19	  if ((TWSR & 0xF8) != START)					//Check value of TWI Status Register. Mask prescaler bits. If status different from START go to ERROR
20	  {
21		ERROR();
22	  }
23	  TWDR = SLA_W;								//Load SLA_W into TWDR Register. Clear TWINT bit in TWCR to start transmission of address
24	  TWCR = (1<<TWINT) | (1<<TWEN);
25	  while (!(TWCR & (1<<TWINT))){}				//Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
26	  if ((TWSR & 0xF8) != MT_SLA_ACK)				//Check value of TWI Status Register. Mask prescaler bits. If status different from MT_SLA_ACK go to ERROR
27	  {
28		ERROR();
29	  }
30	  TWDR = DATA;									//Load DATA into TWDR Register. Clear TWINT bit in TWCR to start transmission of data
31	  TWCR = (1<<TWINT) | (1<<TWEN);
32	  while (!(TWCR & (1<<TWINT)))					//Wait for TWINT Flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received.
33	  {
34	  }
35	  if ((TWSR & 0xF8) != MT_DATA_ACK)				//Check value of TWI Status Register. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
36	  {
37		ERROR();
38	  }
39	  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);	//Transmit STOP condition
40      }
41
42      int ERROR()
43      {
44      	TWCR = 0;
45	        TWDR = 0;
46      }
47
48      void main (void)
49      {
50 	  DDRB=255;
51	  PORTB=255;
52	  return(0);
53	  DATA = 10;
54	  comm(1);
55	
56      }

krijg uiteraard veel fouten zo zegt mijn compiler:

testcomm.c:13: warning: function declaration isn't a prototype
testcomm.c: In function `comm':
testcomm.c:15: error: `SYSCLK' undeclared (first use in this function)
testcomm.c:15: error: (Each undeclared identifier is reported only once
testcomm.c:15: error: for each function it appears in.)
testcomm.c:17: error: called object is not a function
testcomm.c:21: warning: implicit declaration of function `ERROR'
testcomm.c:23: error: `SLA_W' undeclared (first use in this function)
testcomm.c:26: error: `MT_SLA_ACK' undeclared (first use in this function)
testcomm.c:35: error: `MT_DATA_ACK' undeclared (first use in this function)
testcomm.c: At top level:
testcomm.c:43: warning: function declaration isn't a prototype
testcomm.c:49: warning: return type of `main' is not `int'
testcomm.c: In function `main':
testcomm.c:52: warning: `return' with a value, in function returning void

wie heeft dit mer gedaan. De bedoeling is dus dat 1 atmel data uit gaat lezen van een andere atmel. kan wel voorbeelden vinden van extern geheuegen, maar das niet het probleem

Ten eerste moet ge alle variabelen en functie declareren voor je ze gebruikt. De functie ERROR moet dus al alleszins voor "comm()", ook zijn er een heleboel variabelen die je nog niet gedeclareert hebt. (SLA_W,...)
Als ik 2 atmels wil laten samenwerken gebruik ik de SPI (zijn 3 wires, maar is misschien makkelijker)

Groeten.

Zijn dat niet systeem variabelen?
net als de TWSR enzo

Er zijn systeemvariabelen, maar die moet je wel in je headerfile hebben zitten (bvb #include <io8515v.h>
Welke AVR's gebruik je eigenlijk?

Heb effe nagekeken, volgens mij zijn `SLA_W',`MT_SLA_ACK',`MT_DATA_ACK' geen standaard systeemvariabelen. Waarop zouden ze moeten slaan volgens jouw?
Kan ook mis zijn. Heb niet zoveel ervaring met AtMega, werk vooral nog met 89.. en 90.., maar veel verschil kan er niet zijn.

geen compile errors meer, maar wel een de source:


int comm(void)
{
	fail = 0;
	sysclk = 3579545;
	TWSR=0;
	TWBR = (sysclk / 100000UL - 16) / 2;
	TWAR = 2;
	PORTB = 4;
	TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);		//Send START condition
	while (!(TWCR & (1<<TWINT))){}				//Wait for TWINT Flag set. This indicates that the START condition has been transmitted
	if ((TWSR & 0xF8) != START)					//Check value of TWI Status Register. Mask prescaler bits. If status different from START go to ERROR
	{
		ERR();
	}
	PORTB = 5;
	if (fail != 0)
	{
		TWDR = slave+read;								//Load slave write into TWDR Register. Clear TWINT bit in TWCR to start transmission of address
		TWCR = (1<<TWINT) | (1<<TWEN);
		PORTB = 6;
		while (!(TWCR & (1<<TWINT))){}				//Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
		PORTB = 7;
		if ((TWSR & 0xF8) != slave)					//Check value of TWI Status Register. Mask prescaler bits. If status different from MT_SLA_ACK go to ERROR
		{
			ERR();
		}
	}
	
	if (fail != 0)
	{	
		if (read == 0)									//if writing to bus
		{
			TWDR = DATA;								//Load DATA into TWDR Register. Clear TWINT bit in TWCR to start transmission of data
		}
		TWCR = (1<<TWINT) | (1<<TWEN);
		while (!(TWCR & (1<<TWINT)))					//Wait for TWINT Flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received.
		{
		}
		if ((TWSR & 0xF8) != DATA)						//Check value of TWI Status Register. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
		{
			ERR();
		}
		if (read  == 1)								//if Reading from bus
		{
			DATA = TWDR;								//Read DATA from TWDR Register
		}
	}
	if (fail != 0)
	{
		TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);	//Transmit STOP condition
	}
	return(0);
}

port b blijft 6 krijg dus geen acknoledge
iemand die het ziet?