Wednesday, August 4, 2021

Atmega 328P: Dirty Digital LFO--Putting External Interrupts to Work

Hello again! If you've been following this blog, I am turning away from hobby-friendly Arduino audio projects for a more "bare metal" approach using Embedded C. 

A general post about this transition is here

In my studio I am happily patching an Atmel 328 Embedded C  Digital LFO I designed and built a few weeks ago, posts are here and here

Now it's time to add more quirks and features to the LFO because software is never finished. 

How about a waveform external reset? So: when you send the LFO a gate signal, the waveform produced "starts over".  Why not?

Lots of ways to do this. 

An easy way would be to use an external interrupt. The idea: when a gate signal is present at an input jack, the MCU puts whatever it's working on on the stack then runs an interrupt routine--for example, setting a waveform voltage being sent to a DAC back to 0.  


Working proof of concept: Atmel 328 External interrupt, AudiodiWhy style


A good description of using this on an Atmel 328 is here--you can generate the interrupt many ways, but I will code this to do something on a falling edge signal present at the MCU's pin D2. 

This will be used in a modular synthesizer so this trigger signal needs to be buffered. Imagine the outcome of hooking a 20V signal to the D2 pin; you could damage the MCU. 

I was thinking of  modding the DLFO code and its hardware directly to accommodate but had second throughts. I have never experimented with external interrupts on an Amtel 8 bit processor....so let's do a Proof of Concept first, then apply the improvement, once it's working, to the DLFO.  

OK! First up: what hardware to use? 

I already fabbed a "minimal Atmel 328 MCU board", post for that is here. Think of this as an Arduino Uno with every spare pound thrown over the side--for this POC I will use one; it is thru-hole construction and took about 20 minutes to build.

Next I hooked the MCU circuit to an inverter/buffer pcb I designed; I had the PCB fabbed fast by this Blog's new sponsor, PCBWAY. Thanks to PCBWAY for the help! 


Here's the board packaged and ready to go, it arrived in the US 5 days after I ordered it.....go PCBWAY!


  The PCB I had fabbed for this modification is a very simple single NPN transistor inverter:

The output is high (V+ without R1 and R4 as a voltage divider otherwise) until the input 3.5"jack is  about > 1V relative to ground at its input, then the output goes low.  

Good news, the board worked first time--this is a simple circuit, I would have been upset if it didn't:

Layout for the inverter/buffer  





Working gate inverter/buffer


On a scope: output's high usually but we get a transistion to ground for each gate present at its input. 

Super Easy!  Here's a 3 or so second video.....output goes low when input goes high.





Let's Code! I wired the inverter to the dev board like this:

Simple!




And used this code--its a single main.c file.  This code works with the minimalist AVR 328 board posted here.

/*
 * ExtInterrupt_test.c
 *
 * Created: 8/4/2021 11:04:21 AM
 * Author : audiodiwhy
 */ 

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

volatile int led;
ISR (INT0_vect)          /* Ext interrupt_zero ISR */
{
PORTB ^= (1 << PINB0); /* toggle DDRB PB0 */
}



int main(void)
{
        DDRB = 0b00000011;  /* PB0 and PB1 for output */
        DDRD=0x00; /*D ports are inputs; D2 and D3 are INT0 and INT1 */
        EIMSK |= 1<<INT0;  /* enable external interrupt 0 */
        EICRA |= 1<<ISC01; /* interrupt on falling edge */
     
        
        sei();     /* Enable global interrupts */
    while (1) 
    {
    }
}


Viola! Worked. Each gate's rising edge lights or extinguishes the minimal MCU's built in LED.  So I made a somewhat overly complex and fancy flip flop. Good enough.


Now I'm ready to pull the DLFO out of the rack and try to add the interrupt code and inverter PCB/input jack'buffer to it. Note: older post about interrupt for the Arduino Sketch world is here

I figure it will be an easy mod, but who knows? 

Thanks again to PCBWAY for out helping out.  I've been schtooping this blog along for a few years now, and in case you didn't notice, I need all the help I can get.

We'll finish L-FOTUS INTERRUPTUS Dirty Digital LFO next time.  Update: modified DLFO post is here.   Update to the update, 10-31-21: Rev 2 of the LFO is here, which incorporates the interrupt subcircuit just discussed, as well as many other improvements.  



No comments:

Post a Comment

EFM/KORG770 VCF--BUILD: yes, SOUND: no

Ahoy!  This time, I wanted to refine the quick prototyping idea discussed midway through this previous post : Minimal breadboarding--I hate ...