Problem with interrupts Please Help

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

Hello :)

I have particular problem with my program.
From this chunk of code which I wrote I reckon that

When I provide rising edege to the let`s say (INT0) I sholud receive OCR2=(OCR2+10) till I recive OCR2=247

When I provide rising edege to the let`s say (INT1) I sholud receive OCR2=(OCR2-10) till I recive OCR2=7

When I first switch on my uC I realized that OCR2 value is changing like mad !!!

I decided to put two resistors 300 Ohm and connect them to the ground.(INT0,INT1) Situation changed and looks closer to desired. But for example ehn I provide rising edge OCR2 value is changing 20 or 30 only sometimes 10 !
Sometimes It can change for 100 without any reason !

Why is hapenning like that ??

Can anybody explain me this ??

What should I do to obtain desired program !!

I am controlling my DC motor

I am enclosing the code below:

#include
#include"HD44780.h" // LCD
#include"HD44780.c" //LCD
#include
int main(void)
{
sei();
LCD_Initalize();
LCD_WriteText("Hello Adam");
LCD_GoTo(0,1);
DDRB= 0x8; // PB3(OC2) for output
PORTB=0;   // Not providing resistor 
TCCR2 = ((0<<WGM21)|(1<<WGM20)|(1<<CS21)|(0<<COM20)|(1<<COM21));
//Timer 2 for OC2 to the device
TCNT2=0x00;
OCR2=127;//  setting OCR2 at the beginning
GICR=((1<<INT0)|(1<<INT1)); //enabling 2 intterupts
MCUCR=((1<<ISC11)|(1<<ISC10)|(1<<ISC01)|(1<<ISC00));
//intterupts should react only for rising edge
LCD_WriteText("OCR2= ");
char buffer[7];
int  num;       
num=OCR2;
itoa( num, buffer, 10); 
LCD_WriteText(buffer); 

for(;;)
 {
  
  asm volatile(
	"WDR"::);
 }
 return(0);
}

ISR(INT0_vect)
{
if(OCR2<=246)
   {
    
	OCR2=(OCR2+10);
	
    LCD_Clear();
	LCD_WriteText("Hello Adam");
    LCD_GoTo(0,1);
	LCD_WriteText("OCR2= ");
    char buffer[7];
    int  num; 
    num=OCR2;
	itoa( num, buffer, 10);
    LCD_WriteText(buffer);
	}
	else{};
}

ISR(INT1_vect)
{
if(OCR2>=10)
	{
	OCR2=(OCR2-10);
	
	LCD_Clear();
	LCD_WriteText("Hello Adam");
    LCD_GoTo(0,1);
	LCD_WriteText("OCR2= ");
    char buffer[7];
    int  num; 
    num=OCR2;
	itoa( num, buffer, 10);
    LCD_WriteText(buffer);
	}
	else{};
}


Thanks in advance !

Adam

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

Where does the rising edge comes to the INT0 and INT1 pins? Is it a pushbutton, isn't it? Switch bounce? Debouncing in hardware or software needed here?

- Jani

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

It should be joystick (up) (down) but I tested it with pushbutton too. ??? Interrupt should react only for rising edge. I should provide 5[V] and ICR2 should increase or decrease for 10.

Still don`t have an idea what is going on

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

try to connect a 100nF in parallel with your switch to decrease debouncing.
The value actually depends on your circuit - the resistors that charge the capacitor.

Klaus
********************************
Look at: www.megausb.de (German)
********************************

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

The fundamental question to the OP is: Are you at all aware of the phenomenon of "switch bounce"?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

And have you read:

http://www.ganssle.com/debouncin...

By the way it is a very bad idea to put long code (like LCD output routines) into an ISR. The ISR should just do the minimum possible, perhaps setting a flag to say that some other work is needed then in the main() code loop, when it gets time it responds to the setting of this (volatile!) flag.

(though as it happens the long delay the LCD stuff will introduce to the ISR may act as a debounce in fact - but it would be bad design to rely on this)

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

Hello :)

Sorry for such a long delay in responding to the post but I recived osciloscope and I could exactly measure what is going on in my circuit :))

I read a pdf from Clawson and I applied it to the circuit. Now everything works fine till motor starts working.

I observed on the oscillosobe high oscillations when motor worked. When It is working with high speed each change from the joystick genarates higher jumps than 10.

I suppose that I need a capacitor in paraller with motor ??

What do you think about these oscillations ??

How can I reduce them ??

I soldered in pallarer zener diode to the motor but I supose capacitor is essential too ?

What value will be the best ??
Motor is for 15[V] and more or less for 20[mA].

Why this motor generates such a big oscillations ???!!

When I solve problems with bouncing I will go further and solve problems with interrupts (flags)
(Clawson suggestion :) )

Thanks for your help friends :)))

Adam

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

Ifryd wrote:
When I solve problems with bouncing I will go further and solve problems with interrupts (flags)
(Clawson suggestion :) )

Err, to be fair, I was actually the FOURTH person to mention bounce ;)

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

Yeah that`s true Jepael was first who found a solution to my problem. :) Thank you Jepeal :) but I meant that PDF from Clawson helped me a lot! By the way it is a good guide to debouncing.

What about motor oscillations ?
Maybe you have another good guide how to get rid of these oscillatins ? ;)

Adam