Microcontroller › AVR › TWI doesnt generate interrupt in slave mode, using micro Atmega32
- This topic has 2 replies, 3 voices, and was last updated 11 years, 7 months ago by
Ashutosh Bhatt.
-
AuthorPosts
-
August 4, 2014 at 4:29 pm #3205
Mojtaba
ParticipantHi all, I try to have communicate between two Atmega32. at first master should send zero to the slave, the code is:
start:
senddata=0;
address=10;
master_transmitter(senddata,address);void master_transmitter(unsigned char senddata1,unsigned char address1)
{
TWCR=0b10100100; //start condition
while(!(TWCR &(0b10000000))); //wait for TWINT flag
if((TWSR & 0xF8)==0x08 || (TWSR & 0xF8)==0x10) //check TWSR
{
TWDR=((address1 << 1) & (0xFE)); //send SLA+W
TWCR=0b10000100;
while(!(TWCR & (0b10000000)));
}
if((TWSR & 0xF8)==0x18)
{
TWDR=senddata1;
TWCR=0b10000100;
while(!(TWCR & (0b10000000)));
}
TWCR=0b10010100; //stop condition
}and the slave code is:
start:
TWSR=0x00;
TWCR=0x45;
#asm(“sei”)
while(1)
{
if(receiveddata==0)
{
[do something]
}
}interrupt [TWI] void twi_isr(void)
{
unsigned char Sreg;
Sreg=TWSR;
switch(Sreg)
{
case 0x90:
receiveddata=TWDR;
sprintf(buff,”global:%d”,receiveddata);
lcd_puts(buff);
break;
case 0x98:
TWCR=0b10000000;
break;
case 0x80:
receiveddata=TWDR;
break;
case 0xA8:
TWDR=sendeddata;
TWCR=TWCR|(0b10000000);
break;
}
TWCR|=(0b10000000);
}but I found that there isnt any interrupt in the slave when the master send zero to it! I really confused whit this! please help me.
August 6, 2014 at 4:15 am #11984AJISH ALFRED
ParticipantHi Mojtaba,
In TWI the start and stop bits have some speciality than other bits. They happen when the clock is enabled, while the data bits happen when the clock is disabled. Suggest you to learn the TWI in more detail and check it with your code.
August 19, 2014 at 9:30 am #12037Ashutosh Bhatt
Participantfollow the tutorial on TWI given for AVR micro controller on
EG labs / micro controller / AVR
-
AuthorPosts
- You must be logged in to reply to this topic.