TWI interrupt - test programu, master pošle string pro slave

nejde mi rozchodit TWI slave interrupt a neviem ani za nic zistit, kde je chyba. procesor je M8, oscilator RC 8MHz.
master mam spraveny tak, ze po zapnuti napajania pocka sekundu, potom posle slavu string, a potom ho precita. Slave si string po prvom precitani mastrom vymaze. Je to len taky testovaci prikladik. mno a ked to mam sprave len cez volanie funkcie, ktora obsluzi TWI, tak to funguje. tu je funkcny kod:[code]#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/twi.h>
#include <util/delay.h>
#define SET(x,y) (x|=(1<<y))
#define CLR(x,y) (x&=(~(1<<y)))
#define CHK(x,y) (x&(1<<y))
#define TOG(x,y) (x^=(1<<y))

#define buffer_l 80

uint8_t t_index=0;

//test bytes to transmit
uint8_t tran[buffer_l];
uint8_t reset=0;

void handleI2C(void);
void inicializacia_TWI(void);

//---------------MAIN---------------------------------------------
int main()
{
inicializacia_TWI();

while(1)
{
	_delay_ms(10);
	//if(CHK(TWCR,TWINT))
	handleI2C();
}

}

//setup the I2C hardware to ACK the next transmission
//and indicate that we’ve handled the last one.
#define TWACK (TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA))
//setup the I2C hardware to NACK the next transmission
#define TWNACK (TWCR=(1<<TWINT)|(1<<TWEN))
//reset the I2C hardware (used when the bus is in a illegal state)
#define TWRESET (TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO)|(1<<TWEA))

void inicializacia_TWI(void)
{
//load slave address
TWAR = (0x01<<1); //we’re using address 0x01
//enable I2C hardware
TWCR = (1<<TWEN)|(1<<TWEA)|(1<<TWIE)|(1<<TWINT);
//sei();
}

void handleI2C(void)
{
int i;
//check if we need to do any software actions
if(CHK(TWCR,TWINT))
{
switch(TW_STATUS)
{
//--------------Slave receiver------------------------------------
//SLA_W received and acked, prepare for data receiving
case 0x60:
TWACK;
t_index =0;
break;
case 0x80: //a byte was received, store it and
//setup the buffer to recieve another
tran[t_index] = TWDR;
//recv[r_index] = TWDR;
t_index++;
//don’t ack next data if buffer is full
if(t_index >= buffer_l)
{
TWNACK;
}
else
{
TWACK;
}
break;

		case 0x68://adressed as slave while in master mode.
          	//should never happen, better reset;
  			reset=1;
		case 0xA0: //Stop or rep start, reset state machine
  			TWACK;
  			break;

//-------------- error recovery ----------------------------------
case 0x88: //data received but not acked
//should not happen if the master is behaving as expected
//switch to not adressed mode
TWACK;
break;
//---------------Slave Transmitter--------------------------------
case 0xA8: //SLA R received, prep for transmission
//and load first data
t_index=1;
TWDR = tran[0];
TWACK;
break;
case 0xB8: //data transmitted and acked by master, load next
TWDR = tran[t_index];
t_index++;
//designate last byte if we’re at the end of the buffer
if(tran[t_index-1] == 0x00)
{
for(i=0; i<buffer_l; i++)
{
tran*=0;
}
TWNACK;
}
else TWACK;
break;
case 0xC8: //last byte send and acked by master
//last bytes should not be acked, ignore till start/stop
//reset=1;
case 0xC0: //last byte send and nacked by master
//(as should be)
TWACK;
break;
//--------------------- bus error---------------------------------
//illegal start or stop received, reset the I2C hardware
case 0x00:
TWRESET;
break;
}
}
}
[/code]

akonahle sa tuto funkciu snazim prepisat cez preruseni, tak slave nechce citat string. na zaciatku main povolim prerusenie od TWI, zavolam sei(), hlavicku funkcie void handleI2C(void) som prepisal na SIGNAL(SIG_2WIRE_SERIAL). loop v maine ostal prazdny, vsetko by mal obsluzit interrupt, ale nechce mi to fungovat :frowning: snad sa v tom niekto bude vyznat a pomoze mi.

:arrow_right: administrator: přejmenováno z “TWI interrupt”*