Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Serial interrupt problem with Atmega16

Status
Not open for further replies.

hardik.patel

Member level 5
Joined
Aug 15, 2012
Messages
94
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,008
Xtal-11.0592
compiler - avr gcc

here i had write code in which i used INT2 n serial interrupt for different task.
INT2 is working fine, but as serial interrupt comes...it goes in its routine (it goes to compare())...but it not back from its routine to main().

main.c
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include<compat/deprecated.h>

#include <inttypes.h>

#include<my_uart.c>
#include<fan.c>
#include<my_eeprom.c>
#include<device_define.c>

//#include<compare.c>


#include <rtc.c>
#include <disp.c>
#include <disp1.c>

char data;
char a,b,c,d,e,f=0;

void init_interrupt2()
{
	GICR |= (1<<INT2);                 // Enable external interrupt INT2
    MCUCSR |= (1<<ISC2);               // INT2 is executed on rising edge
   // sei();    	//
}

ISR(INT2_vect)
{
	set_pattern();
	//alarm_set_ok=0;
	alarm_set();
}

ISR (USART_RXC_vect)
{
	compare();
	
}

void compare()
{
		int flag=0;
		int d0,d1,d2,d3,d4,d5,d6,d7,d8=0;
		//int d8,d9,d10,d11,d12=0;
		
		//b4_power();
		//speed_eeprom();

		
	//while(1)
	//{		
		
		
		data=USARTReadChar(); a=data; USARTWriteChar(a);//_delay_ms(5);
		data=USARTReadChar(); b=data; USARTWriteChar(b);//_delay_ms(5);
		data=USARTReadChar(); c=data; USARTWriteChar(c);//_delay_ms(5);
		data=USARTReadChar(); d=data; USARTWriteChar(d);//_delay_ms(5);
		data=USARTReadChar(); e=data; USARTWriteChar(e);//_delay_ms(5);
		
		    if((a=='8') && (flag==0))
				  {
					flag=1; 
				  }
			if((b=='0') && (flag==1))
			{
				flag=2;
			}
			 if((c=='2') && (flag==2))
			{
				flag=3;
			}
			 if((d=='1') && (flag==3))
			{
				flag=4;//PORTC=0x01;
			}
			
				 if((flag==4) && (e=='A'))
						{
							tbi(PORTC,PC2);  //here the toggling takes place
							d0 = bit_is_set(PINC,2); //status of PC0 wheather CLEARED/SET is stored in d0 
								if(d0>0){  EEPROMWrite(1,0); }
							      else{ EEPROMWrite(1,1);  } // That status of d0 will be store at 0th EEPROM Location 
												}
				 if((flag==4) && (e=='B'))
					{
					  tbi(PORTC,PC3);
					  d1 = bit_is_set(PINC,3);
							if(d1>0)
								{  EEPROMWrite(2,0);  }
							  else{ EEPROMWrite(2,1);  }	
					}
				 if((flag==4) && (e=='C'))
					{
						 tbi(PORTC,PC4); 
						 d2 = bit_is_set(PINC,4);
								if(d2>0)
								{  EEPROMWrite(3,0);  }
							  else{ EEPROMWrite(3,1);  }	
					}
				 if((flag==4) && (e=='D'))
					{
						 tbi(PORTC,PC5); 
						 d3 = bit_is_set(PINC,5);
								if(d3>0)
								{  EEPROMWrite(4,0);  }
							  else{ EEPROMWrite(4,1);  }	
					}
				 if((flag==4) && (e=='E'))
					{
						 tbi(PORTC,PC6);
						 d4 = bit_is_set(PINC,6);
								if(d4>0)
								{  EEPROMWrite(5,0);  }
							  else{ EEPROMWrite(5,1);  }	
					}
				 if((flag==4) && (e=='F'))
					{
						 tbi(PORTC,PC7);
						 d5 = bit_is_set(PINC,7);
								if(d5>0)
								{  EEPROMWrite(6,0);  }
							  else{ EEPROMWrite(6,1);  }
					}
	/////////////////////////////////////////////////////////////////////////////
				 if((flag==4) && (e=='G'))
						{
							tbi(PORTD,PD5);  //here the toggling takes place
							d6 = bit_is_set(PIND,5); //status of PC0 wheather CLEARED/SET is stored in d0 
								if(d6>0){  EEPROMWrite(7,0);  }
							      else{ EEPROMWrite(7,1);  } // That status of d0 will be store at 0th EEPROM Location 
						}
				 if((flag==4) && (e=='H'))
					{
					  tbi(PORTD,PD6);
					  d7 = bit_is_set(PIND,6);
							if(d7>0)
								{  EEPROMWrite(8,0);  }
							  else{ EEPROMWrite(8,1);  }	
					}
				 //if((flag==4) && (e=='I'))
					//{
					//	 tbi(PORTD,PD6); 
					//	 d8 = bit_is_set(PIND,6);
					//			if(d8>0)
					//			{  EEPROMWrite(9,0);  }
					//		  else{ EEPROMWrite(9,1);  }	
					//}
				 //if((flag==4) && (e=='J'))
					//{
					//	 tbi(PORTA,PA1); 
					//	 d9 = bit_is_set(PINA,1);
					//			if(d9>0)
					//			{  EEPROMWrite(10,0);  }
					//		  else{ EEPROMWrite(10,1);  }	
					//}
				 //if((flag==4) && (e=='K'))
					//{
					//	 tbi(PORTA,PA2);
					//	 d10 = bit_is_set(PINA,2);
					//			if(d10>0)
					//			{  EEPROMWrite(11,0);  }
					//		  else{ EEPROMWrite(11,1);  }	
					//}
				 //if((flag==4) && (e=='L'))
					//{
					//	 tbi(PORTA,PA3);
					//	 d11 = bit_is_set(PINA,3);
					//			if(d11>0)
					//			{  EEPROMWrite(12,0);  }
					//		  else{ EEPROMWrite(12,1);  }
					//}		  
				 //if((flag==4) && (e=='M'))
					//{
					//	 tbi(PORTA,PA4);
					//	 d12 = bit_is_set(PINA,4);
					//			if(d12>0)
					//			{  EEPROMWrite(13,0);  }
					//		  else{ EEPROMWrite(13,1);  }
					//}		  
//////////////////////////////////////////////////////////////////////////////////////						  
					
				 if((flag==4) && (e=='N'))  //STATUS
					{
						 fan_status();
					}
				 if((flag==4) && (e=='O')) // MINUS
 					{
						 fan_speed_minus();
					}	
				 if((flag==4) && (e=='P')) //MAXIMUM
					{    
						 fan_speed_plus();
					}	 	
				 
				
			
		//goto out_while;		  

	//}
	
}


int main(void)

{ 
	DDRA=0XFF;  //7seg data port
	DDRB=0xF0;	//bit 4-7 as i/p for switch for Alarm setting + bit 0-3 as o/p for 7seg control
	DDRC=0xFC;  // PC0-PC1 i/p for SCL & SDA rest all o/p
	DDRD=0xFE;  //PD0 as RX i/p rest all o/p
	USARTInit(71);    //UBRR = 71,  9600 at 11.0592Mhz
	init_interrupt2();
   // sei();	
  //RTC_write_data();
	//PORTD=0x03;
	b4_power();
	speed_eeprom();
	
	while(1)
	{
       
          RTC_read_data(); //_delay_ms(1000);
		  match(); //_delay_ms(1000);
		//compare();
		//buzzer_off;  _delay_ms(500);
		//buzzer_on;   _delay_ms(500);
 	}    
}

my_uart.c
Code:
#include <avr/io.h>



void USARTInit(uint16_t ubrr_value)
{
   UBRRL = ubrr_value;
   UBRRH = (ubrr_value>>8);
   //UCSRC=(1<<URSEL)|(3<<UCSZ0);
   //UCSRB=(1<<RXEN)|(1<<TXEN);
   
   UCSRB |= (1 << RXEN ) | (1 << TXEN ); // Turn on the transmission and reception circuitry
   UCSRC |= (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 ); // Use 8- bit character sizes
   
   //UCSRB = (1<<TXEN) | (1<<RXEN) | (1<<RXCIE);
   UCSRB |= (1 << RXCIE );
   //UCSRB = (1 << RXCIE );
   //UCSRC = 1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0 ;
   sei ();
}



char USARTReadChar()
{
  while(!(UCSRA & (1<<RXC)))
   {
      //Do nothing
   }
   return UDR;
}


void USARTWriteChar(char data)
{
   while(!(UCSRA & (1<<UDRE)))
   {
      //Do nothing
   }
  UDR=data;
}

- - - Updated - - -

complete code is in .rar file
 

Attachments

  • rx combine.rar
    215.4 KB · Views: 74
Last edited:

There may be additional problems, but it's completely wrong to put all the code (multiple UART reads and writes, EEPROM operations) into interrupt.

Instead, you can set a flag in interrupt and perform respective action on main() level.

Rx interrupt must of course clear the interrupt flag, e.g. by reading the UART.
 
I would also add,
when you are in the usart, your usart need to receive data again which makes another interrupt.
The correct thing to do is to redesign your architecture.
Simplest thing to do (if possible with other tasks within your application) is to disable "usart receive interrupt" (if possible global interrupt) when you enter to the USART receive routine and when exiting from the interrupt routine, you need to clear "USART receive interrupt flag" and set "usart receive interrupt" again.
 
@FVM:
I reduce the burden on Serial interrupt as u suggested.Make the changes, and come back to you.

@Duresh : I got your point but not fully, so let me redesign the architecture n then please suggest me changes be specific.

Thank you to both.
 

Adding to FvM's comments, as a general rule, calling other routines from within the ISR should be avoid.

There are several reasons behind this rule, a few of them are as follows:

1. Significant increase in time duration of the ISR

2. As the compiler has no idea what the called routine task is, or entails, it typically saves the entire context of the microcontroller state, thereby dramatically increasing the size of the stack frame.

4. If the default behavior is not saving the entire context and appropriate compiler directives were not issued within the source code directing it to save the relevant contents of variables, shared memory space, etc., then data corruption can occur.

3. If another higher level interrupt should occur while within the routine called by the ISR and appropriate measures were not taken before hand, data corruption can occur.


BigDog
 
I had tried to do change as per your suggestion, but still i cant back from serial ISR.

@FvM: as u said i had only put flag condition in serial ISR, then clear interrupt.
As that flag is set=1, so it must be go in compare() and compare the character.

Even i feeling that i loosing data while UART receiving

main.c
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include<compat/deprecated.h>

//#include <inttypes.h>

#include<my_uart.c>
#include<fan.c>
#include<my_eeprom.c>
#include<device_define.c>

//#include<compare.c>


#include <rtc.c>
#include <disp.c>
#include <disp1.c>

int sr_flag=0;

char data;
char a,b,c,d,e,f;

void init_interrupt2()
{
	GICR |= (1<<INT2);                 // Enable external interrupt INT2
    MCUCSR |= (1<<ISC2);               // INT2 is executed on rising edge
   // sei();    	//
}


ISR(INT2_vect)
{
	set_pattern();
	//alarm_set_ok=0;
	alarm_set();
}

ISR (USART_RXC_vect)
{
	
	   /* data=USARTReadChar(); a=data; USARTWriteChar(a);_delay_ms(5);
		data=USARTReadChar(); b=data; USARTWriteChar(b);_delay_ms(5);
		data=USARTReadChar(); c=data; USARTWriteChar(c);//_delay_ms(5);
		data=USARTReadChar(); d=data; USARTWriteChar(d);//_delay_ms(5);
		data=USARTReadChar(); e=data; USARTWriteChar(e);//_delay_ms(5);
		*/
		
		sr_flag=1;cli();//compare();
	
}







void compare()
{
		int flag=0;
		int d0,d1,d2,d3,d4,d5,d6,d7,d8=0;
		//int d8,d9,d10,d11,d12=0;
		
		//b4_power();
		//speed_eeprom();

		
	//while(1)
	//{		
		
		
		data=USARTReadChar(); a=data; USARTWriteChar(a);//_delay_ms(5);
		data=USARTReadChar(); b=data; USARTWriteChar(b);//_delay_ms(5);
		data=USARTReadChar(); c=data; USARTWriteChar(c);//_delay_ms(5);
		data=USARTReadChar(); d=data; USARTWriteChar(d);//_delay_ms(5);
		data=USARTReadChar(); e=data; USARTWriteChar(e);//_delay_ms(5);
		
		    if((a=='8') && (flag==0))
				  {
					flag=1; 
				  }
			if((b=='0') && (flag==1))
			{
				flag=2;
			}
			 if((c=='2') && (flag==2))
			{
				flag=3;
			}
			 if((d=='1') && (flag==3))
			{
				flag=4;//PORTC=0x01;
			}
			
				 if((flag==4) && (e=='A'))
						{
							tbi(PORTC,PC2);  //here the toggling takes place
							d0 = bit_is_set(PINC,2); //status of PC0 wheather CLEARED/SET is stored in d0 
								if(d0>0){  EEPROMWrite(1,0); }
							      else{ EEPROMWrite(1,1);  } // That status of d0 will be store at 0th EEPROM Location 
												}
				 if((flag==4) && (e=='B'))
					{
					  tbi(PORTC,PC3);
					  d1 = bit_is_set(PINC,3);
							if(d1>0)
								{  EEPROMWrite(2,0);  }
							  else{ EEPROMWrite(2,1);  }	
					}
				 if((flag==4) && (e=='C'))
					{
						 tbi(PORTC,PC4); 
						 d2 = bit_is_set(PINC,4);
								if(d2>0)
								{  EEPROMWrite(3,0);  }
							  else{ EEPROMWrite(3,1);  }	
					}
				 if((flag==4) && (e=='D'))
					{
						 tbi(PORTC,PC5); 
						 d3 = bit_is_set(PINC,5);
								if(d3>0)
								{  EEPROMWrite(4,0);  }
							  else{ EEPROMWrite(4,1);  }	
					}
				 if((flag==4) && (e=='E'))
					{
						 tbi(PORTC,PC6);
						 d4 = bit_is_set(PINC,6);
								if(d4>0)
								{  EEPROMWrite(5,0);  }
							  else{ EEPROMWrite(5,1);  }	
					}
				 if((flag==4) && (e=='F'))
					{
						 tbi(PORTC,PC7);
						 d5 = bit_is_set(PINC,7);
								if(d5>0)
								{  EEPROMWrite(6,0);  }
							  else{ EEPROMWrite(6,1);  }
					}
	/////////////////////////////////////////////////////////////////////////////
				 if((flag==4) && (e=='G'))
						{
							tbi(PORTD,PD5);  //here the toggling takes place
							d6 = bit_is_set(PIND,5); //status of PC0 wheather CLEARED/SET is stored in d0 
								if(d6>0){  EEPROMWrite(7,0);  }
							      else{ EEPROMWrite(7,1);  } // That status of d0 will be store at 0th EEPROM Location 
						}
				 if((flag==4) && (e=='H'))
					{
					  tbi(PORTD,PD6);
					  d7 = bit_is_set(PIND,6);
							if(d7>0)
								{  EEPROMWrite(8,0);  }
							  else{ EEPROMWrite(8,1);  }	
					}
				 //if((flag==4) && (e=='I'))
					//{
					//	 tbi(PORTD,PD6); 
					//	 d8 = bit_is_set(PIND,6);
					//			if(d8>0)
					//			{  EEPROMWrite(9,0);  }
					//		  else{ EEPROMWrite(9,1);  }	
					//}
				 //if((flag==4) && (e=='J'))
					//{
					//	 tbi(PORTA,PA1); 
					//	 d9 = bit_is_set(PINA,1);
					//			if(d9>0)
					//			{  EEPROMWrite(10,0);  }
					//		  else{ EEPROMWrite(10,1);  }	
					//}
				 //if((flag==4) && (e=='K'))
					//{
					//	 tbi(PORTA,PA2);
					//	 d10 = bit_is_set(PINA,2);
					//			if(d10>0)
					//			{  EEPROMWrite(11,0);  }
					//		  else{ EEPROMWrite(11,1);  }	
					//}
				 //if((flag==4) && (e=='L'))
					//{
					//	 tbi(PORTA,PA3);
					//	 d11 = bit_is_set(PINA,3);
					//			if(d11>0)
					//			{  EEPROMWrite(12,0);  }
					//		  else{ EEPROMWrite(12,1);  }
					//}		  
				 //if((flag==4) && (e=='M'))
					//{
					//	 tbi(PORTA,PA4);
					//	 d12 = bit_is_set(PINA,4);
					//			if(d12>0)
					//			{  EEPROMWrite(13,0);  }
					//		  else{ EEPROMWrite(13,1);  }
					//}		  
//////////////////////////////////////////////////////////////////////////////////////						  
					
				 if((flag==4) && (e=='N'))  //STATUS
					{
						 fan_status();
					}
				 if((flag==4) && (e=='O')) // MINUS
 					{
						 fan_speed_minus();
					}	
				 if((flag==4) && (e=='P')) //MAXIMUM
					{    
						 fan_speed_plus();
					}	 	
				 
				
			
		//goto out_while;		  

	//}
}	




int main(void)

{ 
	DDRA=0XFF;  //7seg data port
	DDRB=0xF0;	//bit 4-7 as i/p for switch for Alarm setting + bit 0-3 as o/p for 7seg control
	DDRC=0xFC;  // PC0-PC1 i/p for SCL & SDA rest all o/p
	DDRD=0xFE;  //PD0 as RX i/p rest all o/p
	USARTInit(71);    //UBRR = 71,  9600 at 11.0592Mhz
	init_interrupt2();
    //sei();	
  //RTC_write_data();
	//PORTD=0x03;
	b4_power();
	speed_eeprom();
	
	while(1)
	{
       
	   fan_on;
	   _delay_ms(50);
	   fan_off;
	   _delay_ms(50);
	   //compare();
          //RTC_read_data(); //_delay_ms(1000);
		  //match(); //_delay_ms(1000);
		if(sr_flag==1)
		  {  compare();  sr_flag=0;}
		
 	}    
}
uart.c
Code:
#include <avr/io.h>



void USARTInit(uint16_t ubrr_value)
{
   UBRRL = ubrr_value;
   UBRRH = (ubrr_value>>8);
   //UCSRC=(1<<URSEL)|(3<<UCSZ0);
   //UCSRB=(1<<RXEN)|(1<<TXEN);
   
   UCSRB = (1 << RXEN )|(1<<TXEN) ; // Turn on the transmission and reception circuitry
   UCSRC = (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 ); // Use 8- bit character sizes
   
   //UCSRB = (1<<TXEN) | (1<<RXEN) | (1<<RXCIE);
   UCSRB |= (1 << RXCIE );//RXCIE
   //UCSRB = (1 << RXCIE );
   //UCSRC = 1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0 ;
   sei ();
   
  
   
}



char USARTReadChar()
{
  while(!(UCSRA & (1<<RXC)))
   {
      //Do nothing
   }
   return UDR;
}


void USARTWriteChar(char data)
{
   while(!(UCSRA & (1<<UDRE)))
   {
      //Do nothing
   }
  UDR=data;
}
 

Basically from Transmitter i am transmitting "8021A".."8021B".....
So from the 5th data bit(A?..B?..C?..) i am deciding what action to take..

So in main()...i displaying RTC Time...and as serial interrupt comes...i'll compare received data..according to it,i'll turn ON/OFF respective data.

As you can see i had remove the burden of serial interrupt, just putting flag.

in main()...if flag=1 then i compare the received char.

But it goes in serial ISR.....but i failed to receive complete data.
 

In your code, there are few things, you have set USART receive complete interrupt and when you are reading the usart receive data you again waiting until set receive complete flag to be set !, but after servicing USART receive interrupt routine this flag is going to clear which you miss some data.

After reading your last post, I would suggest you to handle a buffer (which is an array) which store receive data. When the interrupt occur in the interrupt routine you save the receive data in the buffer and set a flag to say that there are new data in the receive buffer. You need to keep a global variables (buff_pos) to memorize the position of the buffer to store next coming data and to memorize next position to read. You need to increase buff_pos value after storing incoming data and decrease after you read data.

Try this code.


Code:
// TODO your other codes 

#define RX_BUFFER_SIZE 30			// Receive buffer size
uint8_t RX_buffer[RX_BUFFER_SIZE];	// Receive buffer
uint8_t buff_pos_store = 0;			// buffer possition to store next coming byte
uint8_t buff_pos_read = 0;			// buffer possition to read
bool buff_overflow = false;

ISR (USART_RXC_vect)
{
	/* check whether the user has read all the data on the buffer before storeing new coming byte. Else set a flag */
	if(buff_pos_store != (buff_pos_read-1))
	{
		RX_buffer[buff_pos_store++] = UDR;
		sr_flag=1;
		
		/* reset the buffer postion variable to store next comming data on to the fist position */
		if(buff_pos_store >= RX_BUFFER_SIZE)
		{
			buff_pos_store = 0;
		}
	}
	else
	{
		/* buffer has not been read so data has been lost */
		buff_overflow = true;
	}
}

void compare()
{
	// TODO your code
}

int main()
{
	// TODO your code
	
	while(1)
	{
		// TODO your code 
		
		if(sr_flag)
		{
			/* read the buffer until you come to last-data-stored position */
			while(buff_pos_read != (buff_pos_store-1))
			{
				// TODO read data
				
				/* increment the read position */
				buff_pos_read++;
				/* if next read-position is outside of the buffer, then reset the positon. */
				if(buff_pos_read >= RX_BUFFER_SIZE)
				{
					buff_pos_read = 0;
				}
			}
			/* clear the flag */
			sr_flag = 0;
		}
	}
}
 
So finally my complete received data is in RX_buffer[] array..am i right??

And i need to compare my inbuilt data with that received data as like.....

if RX_buffer[1]=="8"
if RX_buffer[2]=="0"
if RX_buffer[3]=="2"
if RX_buffer[4]=="1"
if RX_buffer[5]=="A"

Am i getting right??
 

So finally my complete received data is in RX_buffer[] array..am i right??
Yes. Correct.
if RX_buffer[1]=="8"
if RX_buffer[2]=="0"
if RX_buffer[3]=="2"
if RX_buffer[4]=="1"
if RX_buffer[5]=="A"

you need to implement this.
Code:
RX_buffer[4]buff_pos_read

Capture.PNG

- - - Updated - - -

sorry,
RX_buffer[4]buff_pos_read
this should be
RX_buffer[buff_pos_read]
 
@ Duresh:

I had try to follow your suggested concept, but somewhere i doing little mistake.
So i a failed to receive data.

As you can see in Serial ISR, i had put buffer process

so as --data arrive-->ISR executes-> data filled in buffer->Flag set->back to main()--

-->condition true-->compare()-->data retrive from buffer->again display on HyperTerminal

So please go through again in code.Proteous file also attached.
 

Attachments

  • ABC.rar
    71 KB · Views: 57

I had simplify code.
Now its goes in to ISR, even it came back from ISR to main(), but its not considering " if " condition in main() (even flag=1 as ISR executed).

Now problem is only that why "if" condition is not true??

Code:
#include<avr/io.h>
#include<avr/interrupt.h>
#include <util/delay.h>
#include <my_uart.c>



char received_data[10];


int flag=0;
char a,b,c,d,e;




ISR(USART_RXC_vect)
{
		int i;
		for(i=0;i<7;i++)
		{
			received_data[i] = USARTReadChar();
		}
		
		flag=1; 
		PORTC=0b00000011;
		_delay_ms(500);
		cli();
}


int main (void)
{
   DDRC=0xFF;
   PORTC=0x00;
   
  USARTInit(71); 
   
   
	
   while(1) // Loop forever
   {
        PORTC=0b11000000;
		_delay_ms(50);
		PORTC=0b00110000;
		_delay_ms(50);
		
		
			if(flag==1)
			{
				PORTC=0b00000100;
				_delay_ms(500);
				
				a=received_data[0];
				USARTWriteChar(a);
				
				a=received_data[1];
				USARTWriteChar(a);
		
				a=received_data[2];
				USARTWriteChar(a);
		
				a=received_data[3];
				USARTWriteChar(a);
		
				a=received_data[4];
				USARTWriteChar(a); 
				
				flag=0;
			}
			
			
		
   }   
}

flag is declared globally
 
Last edited:

I debugged your project with proteus. Your transmitter sends more than you said. Use hexa decimal view on hyper terminal to see what is the actual value that are transmitting.

It is actually 0x38(=8), 0x30(=0), 0x32(=2), 0x31(=1), 0x42(=B), 0x0D, 0x0A in hex values.

What is the IDE that you are using. I got some terminal out from Atmega16 by using your main.c file. I used Atmel Studio 6.2. I slightly modified come headers and included some files in order to get it compiled.
But there were some errors. The corrected one is attached.

But I think you need to check compiling process and what are the files that get compiled.

For the "my_usart.c" file you haven't created a header file and also there was no any prototypes for its functions. (its not that critical)

The code I posted is posted to give you the concept. However the attached code is a working one.

View attachment upload.zip

In proteus, you can use .elf file as source file for the processors to enable debugging, breakpoints etc.. (I do not know whether you are aware of this or not.)
debugging.PNG

(sorry for my English)
 
I think you are telling the compiler to optimize the code greatly.
use "volatile" when declaring the "flag".
 
As my last simplified code....if i put all received data in array as --> received_data = USARTReadChar();

then any need to use buffer concept??

(I change the method because till you simulate,analysis n reply....i have to think the solution)

So i just put received data in global array.....then use that data for further comparision.

So, then any need to use buffer concept??

- - - Updated - - -

use "volatile" when declaring the "flag".
I had use "volatile int flag=0;"

Now it goes into "if" but not executing its body (i.e it not transmitting received array data)



Code:
#include<avr/io.h>
#include<avr/interrupt.h>
#include <util/delay.h>
#include <my_uart.c>



char received_data[10];


volatile int flag=0;
volatile char a,b,c,d,e;




ISR(USART_RXC_vect)
{
		int i;
		for(i=0;i<7;i++)
		{
			received_data[i] = USARTReadChar();
		}
		
		flag=1; 
		PORTC=0b00000011;
		_delay_ms(500);
		cli();
}


int main (void)
{
   DDRC=0xFF;
   PORTC=0x00;
   
  USARTInit(71); 
   
   
	
   while(1) // Loop forever
   {
        PORTC=0b11000000;
		_delay_ms(50);
		PORTC=0b00110000;
		_delay_ms(50);
		
		
			if(flag==1)
			{
				PORTC=0b00000100;
				//_delay_ms(500);
				
				a=received_data[0];
				USARTWriteChar(a);
				
				a=received_data[1];
				USARTWriteChar(a);
		
				a=received_data[2];
				USARTWriteChar(a);
		
				a=received_data[3];
				USARTWriteChar(a);
		
				a=received_data[4];
				USARTWriteChar(a); 
				
				flag=0;
			}
			
			
		
   }   
}
 
Last edited:

The array method is working.

Let me go forward...


Heartly Thanks Mr.Duresh.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top