Sign In

Trouble Using USART With Timer Interrupts

Manish staring.net@gmail.com India

Hardware: ATmega32 Running At 1Mhz, 9600 BAUD Software: C, Atmel Studio, Proteus

I'm trying to send and recieve data using USART and receiving data in timer interrupt which means i want to recieve data in every something ms, But the problem is UDR reading receving data whithout transmitting and second problem is if somehow i manage to send data accurately then data transmiting is very slow. i really need your help here . please contact me as soon as possible.
Thank you

Images

Replies

R Avinash Ofc PC
2017-09-07 05:07:31
Your approach to the solution of problem is very immature. USART data reception is an UNSOLICITED event that means you can't exactly tell when the other device(i.e. the transmitting device) will send data. So how can you alone decide that you will receive the data every millisecond? What about the transmitter?

If you need to receive data every milli second then you have to program the transmitter to send data every milli second. And in receiver part you dont receive data in TIMER interrupt while you receive it in the USART data interrupt that is exactly when the USART completes the reception of byte from the wire.
R Manish
2017-09-07 10:43:15
Actually my problem is, My main loop is busy with something like endless loop and i don't want to disturb my my main loop, so without disturbing my main loop i want that whenever i send something by pushing button it will received by another microcontroller where another endless loop running in main function without disturbing or interupting it, and the value i transmit will be considerd in main loop variables. hope you get it.
// This is ISR for my timer function
 
ISR (TIMER1_OVF_vect)    // Timer1 ISR
    {
   if(PINA!=0x1f)
        {
	 val=PINA;
	 button(val);
        }
	if(!(UCSRA&(1<<RXC)));
		  received_data=UDR;
		  if(received_data==0)
		  {
		    counta1=1;
			PORTD=1<<7;
		  }
		  if(received_data==1)
		  {
		    countb1=1;
		  }
		  if(received_data==2)
		  {
		    countc1=1;
		  }
		  if(received_data==3)
		  {
		    countd1=1;
		  }
      TCNT1 = 63974;
     }
--------------------------------------
//This is my timer setting 

 TCNT1 = 63974;   // for 1 sec at 16 MHz
   TCCR1B =(1<<CS12); // Timer mode with 256 prescler
   TIMSK = (1<<TOIE1) ;   // Enable timer1 overflow interrupt(TOIE1)
--------------------------------------
// my endless loop
void cycle()
  {
    send_a_command(0xcf);
    send_a_string("A");
    PORTC=1<<2;
   if(PORTC==1<<2)
   {
     if(counta1!=1)   // and this variable got the received value count1a, 
                      //count1b,count1c,count1d
	 {
      PORTD=1<<3;
      PORTC=1<<6|1<<3|1<<2;
      _delay_ms(gtim[0]*1000);
      PORTC=1<<6|1<<3|1<<1;
      _delay_ms(ytim*1000);
      PORTC=1<<5;
      counta=0;
     gtim[0]=6;
	 }
	 else
     PORTC=1<<5;
   }
   send_a_command(0xcf);
   send_a_string("B");
   if(PINC==1<<5)
   {
    if(countb1!=1)
	{
      PORTD=1<<3;
      PORTC=1<<6|1<<0|1<<5;
      _delay_ms(gtim[1]*1000);
      PORTC=1<<6|1<<0|1<<4;
      _delay_ms(ytim*1000); 
	  PORTD=1<<2;
      countb=0;
    gtim[1]=6;
	}
	else
	PORTD=1<<2;
   }
   send_a_command(0xcf);
   send_a_string("C");
   if(PORTD==1<<2)
   {
    if(countc1!=1)
	{
      PORTD=1<<2|1<<3;
      PORTC=1<<0|1<<3;
      _delay_ms(gtim[2]*1000);
      PORTD=1<<3;
      PORTC=1<<0|1<<3|1<<7;
      _delay_ms(ytim*1000); 
      PORTD=1<<5;
      countc=0;
     gtim[2]=6;
	 }
	 else
	 PORTD=1<<5;
   }
    send_a_command(0xcf);
    send_a_string("D");
   
   if(PORTD==1<<5)
   {
    if(countd1!=1)
	 {
      PORTD=1<<5;
      PORTC=1<<0|1<<3|1<<6;
      _delay_ms(gtim[3]*1000);
      PORTD=1<<4;
      PORTC=1<<0|1<<3|1<<6;
      _delay_ms(ytim*1000);
       PORTD=1<<2;
      countd=0;
     gtim[3]=6;
	 }
	 else
	 PORTC=1<<2;
   }
  }


Post a reply to Manish

Think you can help Manish ? Then post your thoughts that might help Manish. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.