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.

[AVR] How to use timers in ISR?

Status
Not open for further replies.

Narendra1190

Member level 2
Joined
Jan 23, 2014
Messages
44
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,288
Activity points
1,666
hi everyone!!
I am using ATmega32 MCU running at internal osci. frequency 1 Mhz.I have interfaced stepper motor on PORT A.motor moves when ext int occur when '_delay_ms' function is used. It dsnt move when i use timers..check my code.help me.

Code:
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
unsigned char x[]={0x0A,0x09,0x05,0x06};
unsigned int i = 0,p = 0;
unsigned int j,k,l;
int main()
{
DDRD = 0xFF;		
PORTD = ~(1<<PD2);		
GICR = 1<<6;					// Enable INT0
MCUCR = (1<<ISC01) | (1<<ISC00);	// Trigger INT0 on rising edge

SREG = 1<<7;				//Enable Global Interrupt
DDRA =0xFF;
TCCR1B = (1<<CS10);


	while(1)
	{
		PORTA = 0x00;
	}
}

ISR(INT0_vect)
{		
		for(l=0;l<100;l++)
		{	
			for(j=0;j<50;j++)
			{	
				if(TCNT1>2000)
				{
				    PORTA = x[i];
				    TCNT1 = 0;
			   //	    _delay_us(2200);
				     i++;
				    if(i>3)
				    i=0;
				}
			}
			

			for(k=0;k<50;k++)
			{	
				if(TCNT1>2000)
				{
				     i--;
				     PORTA = x[i];
				    TCNT1 = 0;
			//	    _delay_us(2200);
				     if(i==0)
				     i=4;
				}
		
			}
		
		
	}
}
 

Hi,

******* ... it doesn´t move when you use timers...
maybe it is because when you enter an ISR other interrupts are disabled.

This is because interrupts within an ISR is dangeous fro runtime and data integrity.

If you be aware of these problems an need to use other interrups then you can use "SEI" to enable interrupts.

*************
ISR in general.

As far as i can see you only need the interrupt to trigger an event "run motor".
Interrups should be short in time.

The busy waits should be done in main loop - which is about unused in your application.
Use the ISR just to trigger the event. Because your main loop is ampty i´d poll the "start motor" in main loop and handle in main loop.

**************
If you really like to do it with interrupt then it shoud be like this:

Main:
* init data
* init periferals
* init ISR
* init timer
* endless loop, or handle other tasks

ISR "run motor"
* enable timer
* setup timer interval
* maybe set flags like "motor is running"
* leave ISR

ISR "timer"
* step motor
* If motor not finished:
- calculate new timer interval
- set new timer interval
- maybe do some position calcuations...
Else:
- clear flag "motor is running"
- disable timer
endif
* leave ISR

************

hope this helps
Klaus
 
Hi,

I´m sorry, i dont have the code at hand...

Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top