NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Strobe Scope with Phase adjustment

June 14, 2020
by scootergarrett
scootergarrett's Avatar

I'm trying to make a strobe scope that takes an input signal from some sensor (hall effect, laser, doesn't really matter at this point) and flashes a light at a user selected phase. For the rest of this example lets say 180deg. Said another way, an incoming 500Hz signal (2ms period) the program would detect the incoming signal pulse and flash 1ms later

Example Plot

I was using the delay functions, but was having issues with high phase angles (300deg and up), I think caused by interrupts overflow, also it seems like the wrong way. Now I'm trying to use the TCNT1 counter to both measure the time between input pulses and control then the output is turned on and off. I think there is a way so when the counter TCNT1 hits OCR1A and OCR1B it will change OC1A (PB1) state on and off respectively.

I have some code that just toggles the state of PB1 when it hist OCR1A.

// for NerdKits with ATmega328 //

#define StrobePin B,1
#define InputTrigger B,5

#define StrobeOn SET(StrobePin)
#define StrobeOff CLEAR(StrobePin)

volatile uint16_t FrequencyCount;

int main()
{
    double Freq;

    // Set up pin conditions //
    OUTPUT(StrobePin);
    INPUT(InputTrigger);
    PULLUP_ON(InputTrigger);

    // Set up UART //
    uart_init();
    FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
    stdin = stdout = &uart_stream;

    // Set up PB5 for pin change interrupts //
    PCICR |= (1<<PCIE0);
    PCMSK0 |= (1<<PCINT5);

    // Working on the timing part //
    TCCR1B = (1<<CS11) | (1<<CS10);

    // WIP //
    TCCR1A |= (1<<COM1A0);
    TCCR1B |= (1<<COM1A0);

    sei();          // Enable interrupts
    delay_ms(10);

    // Will keep sending frequency every 1/2 second //
    while( true )
    {
        Freq = (1843200.0 / FrequencyCount / 8.0);

        OCR1A = FrequencyCount/2; // Just makes the OCR1A event half way between 0 and the FrequencyCount

        printf_P(PSTR("Freq: %.1lf\n\r"), Freq);
        delay_ms(500);
    }

    return 0;
}

/// Interrupt code ///
ISR(PCINT0_vect)
{
    if(READ(InputTrigger))
    {
        FrequencyCount = TCNT1;
        TCNT1 = 0;
    }

    return;
}

Plot

Is anyone here? and really good with the timer settings?

Post a Reply

Please log in to post a reply.

Did you know that you can connect a pushbutton to a microcontroller with only one wire? Learn more...