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.

Multi threading and timers in ATmega128

Status
Not open for further replies.

kunal5959

Junior Member level 3
Joined
Jul 26, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,644
This is an example of my Multithreading application program. Since the actual code is too big i have attached here a small part of the code.. Actaully i am controlling three threads through TIMER0 Interrupt such that each thread is called for t=70 that is 10ms approx. everytime,.. But i have to switch on TIMER0 only when a Microocntroller receives something new which is recognized by RECEIVE_Error_FLAG which is cleared when a RECEIVE process is complete(controlled by REceive Interrupt,,not shown in code below)..SO after receiving i have to just once switch from Thread1->Thread2->Thread3...And again in Next Receive Thread1->Thread2->Thread3.

Now these processes are evident in the code below.. THe code isnt working properly,,,,I diagnosed it and found that the code after receiving data for first time enters threads and each thread is called and then wen i send 2nd time(MC Receives data actually) it always jumps to thread3 and ignore thread1 and thread2. I cannot understand the process.Please guide for here something strange is happening.
Code:
/////MAIN FUNCTION//////

int main(void)

{	
      MT_INIT_ALL();int thread=3,t=70;
	  				
      sei();
      Receiving_ON;
    while(1){   // run forever
		Receiving_ON;
		TIMER0_OFF;
		
		
		
		if (REC_ERROR_FLAG==0){
			    Receiving_OFF; 
                TIMER0_ON;
				
                if (thread==1){
					    PORTA|=(1<<PORTA3);	
						Receiving_OFF;
			            ERROR_handler();
						}		 
				if (thread==2){	
						///				
						//						           
						PWM_Update();
						}
				if (thread==3){	
						//PORTA^=(1<<PORTA3);
												
						Report_Log1();
						START_STOP_CMD_INIT();	  
						MT_ERROR_FLAGS_INIT();// REC_ERROR_FLAG flag is set to 1 so that the set pwm functions are executed only once in one thread								           
						
						TIMER0_OFF;
						
						}		
					} 
			     
										
			}  // repeat this forever^
		}
				
				
ISR(TIMER0_COMP_vect)
{

s=s++;
	if(s==t){
	   if(thread==3){
            PORTA&=~(1<<PORTA3); 
			thread=1;t=70;
			}
   	   else if(thread==1)  {
			thread=2;t=70;
		}
	   else if(thread==2)  {
			thread=3;t=70;
		}	  
	s=0;
	}
 }
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top