0
\$\begingroup\$

I have a zero crossing detector circuit, whose output I am providing to my Atmega8 as an interrupt, but unfortunately its not working. If I use DC supply 5V (logic 1) as an interrupt, it works. Where am I going wrong?

Attached is the output of zero crossing detector circuit: ZeroCrossingDetector_Output

Here is my complete code:

#include <avr/io.h>
#include <avr/interrupt.h>

#define F_CPU 1000000UL
#include <util/delay.h>

#define DataPort    PORTB   // Using PortB as our Dataport
#define DataDDR     DDRB

//Interrupt Service Routine for INT0
ISR(INT0_vect)
{
    ///* This for loop blink LEDs on Dataport 5 times*/
    for(int i = 0; i<3; i++)
    {
        DataPort = 0x00;
        _delay_ms(50);  // Wait 5 seconds
        DataPort = 0xFF;
        _delay_ms(50);  // Wait 5 seconds
    }
}

int main(void)
{
    DDRD = 1<<PD2;      // Set PD2 as input (Using for interupt INT0)
    PORTD = 0<<PD2;     // Enable PD2 pull-down resistor

    DataDDR = 0xFF;     // Configure Dataport as output
    DataPort = 0xFF;    // Initialise Dataport to 1

    GICR = 1<<INT0;                 // Enable INT0
    MCUCR = 1<<ISC01 | 1<<ISC00;    // Trigger INT0 on rising edge

    sei();              //Enable Global Interrupt

    while(1)
    {
        DataPort = 0xFF;
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It could be a problem with your zero-crossing detector. Can you add a schematic diagram? \$\endgroup\$ – Steve G Mar 5 '16 at 19:05
  • \$\begingroup\$ I have checked the output of zero crossing detector on DSO, and found it satisfactory. \$\endgroup\$ – Ali_Waris Mar 5 '16 at 19:11
  • 2
    \$\begingroup\$ 1. Have you checked the output of your ZCD while it's connected to your MCU? 2. Is the ZCD output wide enough for the MCU to recognize? 3. As onerous as it may be, why don't you post a schematic anyway? Who knows, you might have missed something another pair of eyes might catch. \$\endgroup\$ – EM Fields Mar 5 '16 at 19:23
  • \$\begingroup\$ @EM Fields - thanks for your concern, i'll share the schematic soon. Please give me time till tomorrow morning. Its been 1 am in India and I am already under bed. :) \$\endgroup\$ – Ali_Waris Mar 5 '16 at 19:27
  • \$\begingroup\$ The IO pins do not have pull down resistors, only pull-up resistors. "PORTD = 0<<PD2;" just sets the IO pin to a high impedance state, while "PORTD = 1<<PD2;" would enable the inernal 10k pull-up. If your zero crossing detector relied on the presence of this immaginary "pull down resistor" alone, the input would just have stayed high at all times. \$\endgroup\$ – jms Mar 5 '16 at 22:56
1
\$\begingroup\$

While it may not be your only problem, your ISR takes several times longer to execute than the 50ms interval between pulses.

Long delays, especially repeated, are almost always a bad idea in an ISR. Try pulsing a data pin for a few micro (not milli) seconds or better yet toggling its state whenever the interrupt fires, and monitor with the scope rather than an LED.

Given that your signal has a slow rise and sharp fall you might consider a falling edge interrupt.

Also check if the type of interrupt you are using must be explicitly cleared or reset before it can fire again - some MCUs require this and some do not.

Always use extreme care when working with line-connected circuits.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Can you please provide the code for enabling the interrupt on falling edge? As i think gradual rise but sudden fall might be the issue. \$\endgroup\$ – Ali_Waris Mar 6 '16 at 2:58
1
\$\begingroup\$

Looks like you're seeing no expected interrupt due to pd2 being set for output, not input.

DDRD = 1<<PD2;      // Set PD2 as input (Using for interupt INT0)

There are only pull-up resistors in the ATMEGA8. This may impact your circuit.

PORTD = 0<<PD2;     // Enable PD2 pull-down resistor

From the data sheet,

The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.

Delay in interrupt should be avoided too...

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ (Looks like you're seeing no interrupt...) then how come I am able to blink led if I provide a DC supply as interrupt.. :o \$\endgroup\$ – Ali_Waris Mar 6 '16 at 8:34
  • \$\begingroup\$ An voltage applied to the pin would, when the pin is an output set to 0, let the pin sink current. This could be damaging the pin. The interrupt circuit cares about the voltage more then the pin direction. Does an LED between vcc and pd2 light? (Should have limiting resistor to prevent sinking too much) \$\endgroup\$ – Rob Mar 6 '16 at 8:47
  • \$\begingroup\$ Added 'expected' to answer. \$\endgroup\$ – Rob Mar 6 '16 at 8:51
  • \$\begingroup\$ @Ali_Waris - a "DC supply" should not be triggering more than one interrupt, thus the LED should only change state once. If it is blink"ing" then you have something else not yet understood going on. \$\endgroup\$ – Chris Stratton Mar 8 '16 at 3:53
  • \$\begingroup\$ @Chris Stratton please check my ISR, its clearly mentioned that as soon as any interrupt will occur, LED will blink 3 times; this behaviour is expected. \$\endgroup\$ – Ali_Waris Mar 8 '16 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.