problem with TXCIE interrupt in atmega 8

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi ,

i am working on usart in atmega 8;

i wanna transmit some byte to another mega . i can do that from other way . but i want to use TXCIE interrupt to check that shift register is emty and then send new byte;

but it seem s dosent work . i dont know why , or i dont know what is my wrong.please guide me;

Atmel Studio

'------------------------------------

int i , t , m;

 

ISR(USART_TXC_vect)
{

m = 1;

}

 

//-----

int main(void)
{
   /* Replace with your application code */
            sei();
            UCSRB = 0x48;
            UBRRL = 51;
            UCSRC = (1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (1<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);

       //-----

        UDR = 30;

        while(1)
        {

          if ( m == 1 )
                {
                   i++;
                   UDR = i;                  
                   m = 0;
                   t = 0;
                 }
    
        
  }
}

------------------------------------

 

but this block of code works:

 

'-------

int i , t , m;

 

//----

ISR(USART_TXC_vect)
{

i++;
UDR = i;

}

 

//------

int main(void)
{
   /* Replace with your application code */
            sei();
            UCSRB = 0x48;
            UBRRL = 51;
            UCSRC = (1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (1<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);

 

 

        UDR = 30;
        while(1)
        {

        
         }
}
 

 

thank you;

This topic has a solution.
Last Edited: Tue. Apr 6, 2021 - 12:32 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Classic mistake, any variable shared between main() and ISR() must be volatile! 

hosseinam wrote:
int i , t , m;

 

make it:

volatile int m;

 

See if that helps.

 

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Classic mistake, any variable shared between main() and ISR() must be volatile! 

hosseinam wrote:
int i , t , m;

 

make it:

volatile int m;

 

See if that helps.

 

 

Jim

 

 

 

realy thanks. that works.

 

what is difference between  volatile  and without  volatile  ?!!
what happen for value with  volatile ?

thanks.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

I wrote a tutorial about volatile...
.
https://www.avrfreaks.net/node/92177

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again ;

 

I have a mistake to understand code ; and it is when I enable RXCIE interrupt And UDRE interrupt in atmel studio , the code of bottom dosnt work , when i disable UDRE interrupt , the code work correctly; 

I think this is Priority  of vectors ;

what is my mistake ?
thank you a lot;

 

volatile  int i , t , m;

ISR(USART_UDRE_vect)
{

}

ISR(USART_RXC_vect)
{
	t = UDR ;
}

ISR(USART_TXC_vect)
{

    m = 1;

}

int main(void)
{
   /* Replace with your application code */
			sei();
			UCSRB = 0xf8;
			UBRRL = 51;
			UCSRC = (1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (1<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);

                UDR = 30;
		while(1)
		{

		  if ( m == 1 && t == 20 )
			{
	 	        	i++;
		 		UDR = i;
		 	     	m = 0;
		 		t = 0;
		 	}

        }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You use UDRE_vect or TXC_vect but not both at the same time.